From 4c71c8571ceb10b29e6550cd0d8eb928049e6851 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Tue, 14 Jan 2020 23:34:13 +0200 Subject: Move/rename files for readability - add BookmarkFormat <<|>> BookmarkModel operators --- lib/bookmarks/bookmarkswidget.cpp | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) (limited to 'lib/bookmarks/bookmarkswidget.cpp') diff --git a/lib/bookmarks/bookmarkswidget.cpp b/lib/bookmarks/bookmarkswidget.cpp index aed7e97..29e388c 100644 --- a/lib/bookmarks/bookmarkswidget.cpp +++ b/lib/bookmarks/bookmarkswidget.cpp @@ -8,10 +8,8 @@ #include "bookmarkswidget.h" #include "forms/editbookmarkdialog.h" -#include "model/bookmarkitem.h" -#include "model/bookmarkmodel.h" #include "ui_bookmarksform.h" -#include "formats/format.h" +#include "bookmarkformat.h" #include #include @@ -48,9 +46,7 @@ BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) m_bookmarksPath = path; QFile bookmarksFile(m_bookmarksPath); if(bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { - BookmarksFormat format(&bookmarksFile); - format.read(model->root()); - bookmarksFile.close(); + BookmarkFormat(&bookmarksFile) >> model; } model->resetModified(); @@ -124,11 +120,9 @@ void BookmarksWidget::save() QFile bookmarksFile(m_bookmarksPath); if(bookmarksFile.open(QIODevice::WriteOnly | QIODevice::Text)) { - BookmarksFormat format(&bookmarksFile); - format.write(model->root()); + BookmarkFormat f(&bookmarksFile); + model >> f; bookmarksFile.flush(); - bookmarksFile.close(); - model->resetModified(); } } -- cgit v1.2.1 From 9643e146c35a3a3e9abf0bf332c7c1efd00be9c2 Mon Sep 17 00:00:00 2001 From: Aqua-sama Date: Wed, 15 Jan 2020 13:19:46 +0200 Subject: Move BookmarksWidget out of libbookmarks --- lib/bookmarks/bookmarkswidget.cpp | 138 -------------------------------------- 1 file changed, 138 deletions(-) delete mode 100644 lib/bookmarks/bookmarkswidget.cpp (limited to 'lib/bookmarks/bookmarkswidget.cpp') diff --git a/lib/bookmarks/bookmarkswidget.cpp b/lib/bookmarks/bookmarkswidget.cpp deleted file mode 100644 index 29e388c..0000000 --- a/lib/bookmarks/bookmarkswidget.cpp +++ /dev/null @@ -1,138 +0,0 @@ -/* - * This file is part of smolbote. It's copyrighted by the contributors recorded - * in the version control history of the file, available from its original - * location: https://neueland.iserlohn-fortress.net/gitea/aqua/smolbote - * - * SPDX-License-Identifier: GPL-3.0 - */ - -#include "bookmarkswidget.h" -#include "forms/editbookmarkdialog.h" -#include "ui_bookmarksform.h" -#include "bookmarkformat.h" -#include -#include - -inline void expandChildren(QTreeView *view, BookmarkModel *model, const QModelIndex &rootIndex) -{ - for(int i = 0; i < model->rowCount(rootIndex); ++i) { - QModelIndex idx = model->index(i, 0, rootIndex); - if(model->isItemExpanded(idx)) - view->expand(idx); - - // check if index has children - if(model->rowCount(idx) > 0) - expandChildren(view, model, idx); - } -} - -BookmarksWidget::BookmarksWidget(const QString &path, QWidget *parent) - : QWidget(parent) - , ui(new Ui::BookmarksDialog) -{ - // make sure this dialog does not get deleted on close - setAttribute(Qt::WA_DeleteOnClose, false); - - ui->setupUi(this); - ui->bookmark_groupBox->setVisible(false); - ui->folder_groupBox->setVisible(false); - - ui->addFolder_toolButton->setIcon(style()->standardPixmap(QStyle::SP_DirIcon)); - ui->addBookmark_toolButton->setIcon(style()->standardPixmap(QStyle::SP_FileIcon)); - ui->deleteItem_toolButton->setIcon(style()->standardPixmap(QStyle::SP_TrashIcon)); - ui->deleteItem_toolButton->setShortcut(QKeySequence::Delete); - - model = new BookmarkModel(this); - m_bookmarksPath = path; - QFile bookmarksFile(m_bookmarksPath); - if(bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { - BookmarkFormat(&bookmarksFile) >> model; - } - model->resetModified(); - - ui->treeView->setModel(model); - expandChildren(ui->treeView, model, QModelIndex()); - - // item activated - connect(ui->treeView, &QTreeView::activated, this, [this](const QModelIndex &index) { - if(index.column() == 1) - emit openUrl(index.data(Qt::DisplayRole).toUrl()); - else - editBookmark(index); - }); - - connect(ui->treeView, &QTreeView::expanded, this, [this](const QModelIndex &index) { - model->setItemExpanded(index, true); - }); - connect(ui->treeView, &QTreeView::collapsed, this, [this](const QModelIndex &index) { - model->setItemExpanded(index, false); - }); - - ui->treeView->setContextMenuPolicy(Qt::CustomContextMenu); - connect(ui->treeView, &QTreeView::customContextMenuRequested, this, [this](const QPoint &pos) { - const QModelIndex idx = ui->treeView->indexAt(pos); - if(idx.isValid()) { - const QUrl url = model->data(idx, 1, Qt::DisplayRole).toUrl(); - const QPoint _pos = ui->treeView->viewport()->mapToGlobal(pos); - - emit showContextMenu(url, _pos); - } - }); - - // addBookmark - connect(ui->addBookmark_toolButton, &QToolButton::clicked, this, [this]() { - const QModelIndex idx = model->parentFolder(ui->treeView->currentIndex()); - const QModelIndex childIdx = model->appendBookmark(tr("Title"), QString(), idx); - ui->treeView->setCurrentIndex(childIdx); - editBookmark(childIdx); - }); - - // addFolder - connect(ui->addFolder_toolButton, &QToolButton::clicked, this, [this]() { - const QModelIndex idx = model->parentFolder(ui->treeView->currentIndex()); - const QModelIndex childIdx = model->appendFolder(tr("Title"), idx); - ui->treeView->setCurrentIndex(childIdx); - editBookmark(childIdx); - }); - - // deleteItem - connect(ui->deleteItem_toolButton, &QToolButton::clicked, this, [this]() { - const QModelIndex idx = ui->treeView->currentIndex(); - model->removeRow(idx.row(), idx.parent()); - }); -} - -BookmarksWidget::~BookmarksWidget() -{ - delete ui; -} - -void BookmarksWidget::editBookmark(const QModelIndex &index) -{ - auto *dlg = new EditBookmarkDialog(model, index, this); - dlg->exec(); -} - -void BookmarksWidget::save() -{ - if(!model->isModified()) - return; - - QFile bookmarksFile(m_bookmarksPath); - if(bookmarksFile.open(QIODevice::WriteOnly | QIODevice::Text)) { - BookmarkFormat f(&bookmarksFile); - model >> f; - bookmarksFile.flush(); - } -} - -void BookmarksWidget::addBookmark(const QString &title, const QString &url) -{ - model->appendBookmark(title, url, QModelIndex()); -} - -void BookmarksWidget::search(const QString &term, const std::function &callback) const -{ - QStringList ret = model->search(term); - callback(ret); -} -- cgit v1.2.1