diff options
author | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-01-15 13:19:46 +0200 |
---|---|---|
committer | Aqua-sama <aqua@iserlohn-fortress.net> | 2020-01-15 13:19:46 +0200 |
commit | 9643e146c35a3a3e9abf0bf332c7c1efd00be9c2 (patch) | |
tree | e69c2d5ce92786a566c5e2c997f4ce516c35d238 /src/bookmarks/bookmarkswidget.cpp | |
parent | Move/rename files for readability (diff) | |
download | smolbote-9643e146c35a3a3e9abf0bf332c7c1efd00be9c2.tar.xz |
Move BookmarksWidget out of libbookmarks
Diffstat (limited to 'src/bookmarks/bookmarkswidget.cpp')
-rw-r--r-- | src/bookmarks/bookmarkswidget.cpp | 138 |
1 files changed, 138 insertions, 0 deletions
diff --git a/src/bookmarks/bookmarkswidget.cpp b/src/bookmarks/bookmarkswidget.cpp new file mode 100644 index 0000000..26fdddf --- /dev/null +++ b/src/bookmarks/bookmarkswidget.cpp @@ -0,0 +1,138 @@ +/* + * 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 "editbookmarkdialog.h" +#include "ui_bookmarksform.h" +#include "bookmarkformat.h" +#include <QTreeView> +#include <QUrl> + +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<XbelFormat>(&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<XbelFormat> 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<void(QStringList &)> &callback) const +{ + QStringList ret = model->search(term); + callback(ret); +} |