/* * 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/smolbote.hg * * SPDX-License-Identifier: GPL-3.0 */ #include "bookmarkswidget.h" #include "ui_bookmarksform.h" #include "xbel.h" #include #ifdef QT_DEBUG #include #include "bookmarkmodel.h" #include "bookmarkitem.h" #include "forms/editbookmarkdialog.h" 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); } } #endif 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); // Testing model/view #ifdef QT_DEBUG { model = new BookmarkModel(this); QFile bookmarksFile(path); if(bookmarksFile.open(QIODevice::ReadOnly | QIODevice::Text)) { XbelReader xbel(&bookmarksFile); xbel.read(model->root()); bookmarksFile.close(); } ui->treeView->setModel(model); expandChildren(ui->treeView, model, QModelIndex()); connect(ui->treeView, &QTreeView::activated, this, [this](const QModelIndex &index) { if(index.column() == 1) emit openUrl(index.data(Qt::DisplayRole).toUrl()); else { auto *dlg = new EditBookmarkDialog(model, index, this); dlg->exec(); } }); } #endif // m_bookmarksPath = path; } BookmarksWidget::~BookmarksWidget() { delete ui; } void BookmarksWidget::save() { if(!m_isChanged) { return; } // QFile bookmarksFile(m_bookmarksPath); // if(bookmarksFile.open(QIODevice::WriteOnly | QIODevice::Text)) { // XbelWriter xbel(&bookmarksFile); // xbel.write(ui->treeWidget); // bookmarksFile.flush(); // bookmarksFile.close(); // // m_isChanged = false; // } } void BookmarksWidget::addBookmark(const QString &title, const QString &url) { model->appendBookmark(title, url, QModelIndex()); m_isChanged = true; } void BookmarksWidget::search(const QString &term, std::function callback) const { QStringList ret = model->search(term); // const QList res = ui->treeWidget->findItems(term, Qt::MatchContains | Qt::MatchRecursive, 1); // for(const QTreeWidgetItem *i : res) { // if(!i->text(1).isEmpty()) // ret.append(i->text(1)); // } callback(ret); }