From 71d5091533f34e3b4ec998f53cf3a070c082aec7 Mon Sep 17 00:00:00 2001 From: nowrep Date: Sun, 17 Apr 2011 13:03:29 +0200 Subject: [PATCH] BookmarksManager rewritten to support bookmarks model signals --- src/bookmarks/bookmarksmanager.cpp | 87 +++++++++++++++++++++++++++--- src/bookmarks/bookmarksmanager.h | 8 +++ src/sidebar/bookmarkssidebar.cpp | 10 +++- src/tools/treewidget.cpp | 5 +- 4 files changed, 101 insertions(+), 9 deletions(-) diff --git a/src/bookmarks/bookmarksmanager.cpp b/src/bookmarks/bookmarksmanager.cpp index 8f5366b84..e1bc3f8a5 100644 --- a/src/bookmarks/bookmarksmanager.cpp +++ b/src/bookmarks/bookmarksmanager.cpp @@ -54,6 +54,13 @@ BookmarksManager::BookmarksManager(QupZilla* mainClass, QWidget* parent) : connect(ui->addFolder, SIGNAL(clicked()), this, SLOT(addFolder())); connect(ui->bookmarksTree, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenuRequested(const QPoint &))); connect(ui->bookmarksTree, SIGNAL(itemControlClicked(QTreeWidgetItem*)), this, SLOT(itemControlClicked(QTreeWidgetItem*))); + + connect(m_bookmarksModel, SIGNAL(bookmarkAdded(BookmarksModel::Bookmark)), this, SLOT(addBookmark(BookmarksModel::Bookmark))); + connect(m_bookmarksModel, SIGNAL(bookmarkDeleted(BookmarksModel::Bookmark)), this, SLOT(removeBookmark(BookmarksModel::Bookmark))); + connect(m_bookmarksModel, SIGNAL(folderAdded(QString)), this, SLOT(addFolder(QString))); + connect(m_bookmarksModel, SIGNAL(folderDeleted(QString)), this, SLOT(removeFolder(QString))); + connect(m_bookmarksModel, SIGNAL(bookmarkEdited(BookmarksModel::Bookmark,BookmarksModel::Bookmark)), this, SLOT(bookmarkEdited(BookmarksModel::Bookmark,BookmarksModel::Bookmark))); + //QTimer::singleShot(0, this, SLOT(refreshTable())); } @@ -77,7 +84,6 @@ void BookmarksManager::addFolder() return; m_bookmarksModel->createFolder(text); - refreshTable(); } void BookmarksManager::itemChanged(QTreeWidgetItem* item) @@ -89,6 +95,7 @@ void BookmarksManager::itemChanged(QTreeWidgetItem* item) QUrl url = QUrl(item->text(1)); int id = item->whatsThis(1).toInt(); + delete item; m_bookmarksModel->editBookmark(id, name, url, ""); } @@ -113,15 +120,12 @@ void BookmarksManager::deleteItem() if (item->text(1).isEmpty()) { // Delete folder QString folder = item->text(0); - if (m_bookmarksModel->removeFolder(folder)) - delete item; + m_bookmarksModel->removeFolder(folder); return; } int id = item->whatsThis(1).toInt(); - m_bookmarksModel->removeBookmark(id); - delete item; } void BookmarksManager::addBookmark(WebView* view) @@ -137,7 +141,6 @@ void BookmarksManager::moveBookmark() if (QAction* action = qobject_cast(sender())) { m_bookmarksModel->editBookmark(item->whatsThis(1).toInt(), item->text(0), QUrl(), action->data().toString()); } - refreshTable(); } void BookmarksManager::contextMenuRequested(const QPoint &position) @@ -240,6 +243,78 @@ void BookmarksManager::refreshTable() m_isRefreshing = false; } +void BookmarksManager::addBookmark(const BookmarksModel::Bookmark &bookmark) +{ + m_isRefreshing = true; + QString translatedFolder = BookmarksModel::toTranslatedFolder(bookmark.folder); + QTreeWidgetItem* item = new QTreeWidgetItem(); + item->setText(0, bookmark.title); + item->setText(1, bookmark.url.toEncoded()); + item->setWhatsThis(1, QString::number(bookmark.id)); + item->setIcon(0, LocationBar::icon(bookmark.url)); + item->setToolTip(0, bookmark.title); + item->setToolTip(1, bookmark.url.toEncoded()); + item->setFlags(item->flags() | Qt::ItemIsEditable); + + if (bookmark.folder != "unsorted") + ui->bookmarksTree->addToParentItem(translatedFolder, item); + else + ui->bookmarksTree->addTopLevelItem(item); + m_isRefreshing = false; +} + +void BookmarksManager::removeBookmark(const BookmarksModel::Bookmark &bookmark) +{ + m_isRefreshing = true; + if (bookmark.folder == "unsorted") { + QList list = ui->bookmarksTree->findItems(bookmark.title, Qt::MatchExactly); + if (list.count() == 0) + return; + QTreeWidgetItem* item = list.at(0); + if (item && item->whatsThis(1) == QString::number(bookmark.id)) + delete item; + } else { + QList list = ui->bookmarksTree->findItems(BookmarksModel::toTranslatedFolder(bookmark.folder), Qt::MatchExactly); + if (list.count() == 0) + return; + QTreeWidgetItem* parentItem = list.at(0); + if (!parentItem) + return; + for (int i = 0; i < parentItem->childCount(); i++) { + QTreeWidgetItem* item = parentItem->child(i); + if (!item) + continue; + if (item->text(0) == bookmark.title && item->whatsThis(1) == QString::number(bookmark.id)) { + delete item; + return; + } + } + } + m_isRefreshing = false; +} + +void BookmarksManager::bookmarkEdited(const BookmarksModel::Bookmark &before, const BookmarksModel::Bookmark &after) +{ + removeBookmark(before); + addBookmark(after); +} + +void BookmarksManager::addFolder(const QString &name) +{ + m_isRefreshing = true; + QTreeWidgetItem* item = new QTreeWidgetItem(ui->bookmarksTree); + item->setText(0, name); + item->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); + m_isRefreshing = false; +} + +void BookmarksManager::removeFolder(const QString &name) +{ + QTreeWidgetItem* item = ui->bookmarksTree->findItems(name, Qt::MatchExactly).at(0); + if (item) + delete item; +} + void BookmarksManager::insertBookmark(const QUrl &url, const QString &title) { if (url.isEmpty() || title.isEmpty()) diff --git a/src/bookmarks/bookmarksmanager.h b/src/bookmarks/bookmarksmanager.h index e4d0e235f..41f205bea 100644 --- a/src/bookmarks/bookmarksmanager.h +++ b/src/bookmarks/bookmarksmanager.h @@ -23,6 +23,8 @@ #include #include +#include "bookmarksmodel.h" + namespace Ui { class BookmarksManager; } @@ -54,6 +56,12 @@ private slots: void itemControlClicked(QTreeWidgetItem* item); void moveBookmark(); + void addBookmark(const BookmarksModel::Bookmark &bookmark); + void removeBookmark(const BookmarksModel::Bookmark &bookmark); + void bookmarkEdited(const BookmarksModel::Bookmark &before, const BookmarksModel::Bookmark &after); + void addFolder(const QString &name); + void removeFolder(const QString &name); + private: QupZilla* getQupZilla(); diff --git a/src/sidebar/bookmarkssidebar.cpp b/src/sidebar/bookmarkssidebar.cpp index eb82948e3..e65f4415d 100644 --- a/src/sidebar/bookmarkssidebar.cpp +++ b/src/sidebar/bookmarkssidebar.cpp @@ -140,11 +140,17 @@ void BookmarksSideBar::addBookmark(const BookmarksModel::Bookmark &bookmark) void BookmarksSideBar::removeBookmark(const BookmarksModel::Bookmark &bookmark) { if (bookmark.folder == "unsorted") { - QTreeWidgetItem* item = ui->bookmarksTree->findItems(bookmark.title, Qt::MatchExactly).at(0); + QList list = ui->bookmarksTree->findItems(bookmark.title, Qt::MatchExactly); + if (list.count() == 0) + return; + QTreeWidgetItem* item = list.at(0); if (item && item->whatsThis(0) == QString::number(bookmark.id)) delete item; } else { - QTreeWidgetItem* parentItem = ui->bookmarksTree->findItems(BookmarksModel::toTranslatedFolder(bookmark.folder), Qt::MatchExactly).at(0); + QList list = ui->bookmarksTree->findItems(BookmarksModel::toTranslatedFolder(bookmark.folder), Qt::MatchExactly); + if (list.count() == 0) + return; + QTreeWidgetItem* parentItem = list.at(0); if (!parentItem) return; for (int i = 0; i < parentItem->childCount(); i++) { diff --git a/src/tools/treewidget.cpp b/src/tools/treewidget.cpp index 37236ea00..1bca7ca9a 100644 --- a/src/tools/treewidget.cpp +++ b/src/tools/treewidget.cpp @@ -84,7 +84,10 @@ void TreeWidget::filterStringWithTopItems(QString string) bool TreeWidget::addToParentItem(const QString &text, QTreeWidgetItem* item) { - QTreeWidgetItem* parentItem = findItems(text, Qt::MatchExactly).at(0); + QList list = findItems(text, Qt::MatchExactly); + if (list.count() == 0) + return false; + QTreeWidgetItem* parentItem = list.at(0); if (!parentItem) return false;