From d9ae633d1a870cb9cdb7849234e4eeb5a9167269 Mon Sep 17 00:00:00 2001 From: "S. Razi Alavizadeh" Date: Thu, 6 Sep 2012 02:22:40 +0430 Subject: [PATCH] Added Drag-n-Drop functionality to bookmarkmanager and bookmarksidebar. --- src/lib/bookmarks/bookmarksmanager.cpp | 90 +++++++++- src/lib/bookmarks/bookmarksmanager.h | 4 + src/lib/bookmarks/bookmarksmodel.cpp | 73 +++++++- src/lib/bookmarks/bookmarksmodel.h | 9 + src/lib/bookmarks/bookmarkstoolbar.cpp | 37 ++++ src/lib/bookmarks/bookmarkstoolbar.h | 4 + src/lib/sidebar/bookmarkssidebar.cpp | 90 ++++++++++ src/lib/sidebar/bookmarkssidebar.h | 4 + src/lib/tools/treewidget.cpp | 233 +++++++++++++++++++++++++ src/lib/tools/treewidget.h | 13 ++ 10 files changed, 553 insertions(+), 4 deletions(-) diff --git a/src/lib/bookmarks/bookmarksmanager.cpp b/src/lib/bookmarks/bookmarksmanager.cpp index da1b4412a..d18518b62 100644 --- a/src/lib/bookmarks/bookmarksmanager.cpp +++ b/src/lib/bookmarks/bookmarksmanager.cpp @@ -47,6 +47,10 @@ BookmarksManager::BookmarksManager(QupZilla* mainClass, QWidget* parent) { ui->setupUi(this); + ui->bookmarksTree->setSelectionBehavior(QAbstractItemView::SelectRows); + ui->bookmarksTree->setSelectionMode(QAbstractItemView::ContiguousSelection); + ui->bookmarksTree->setDragDropReceiver(true, m_bookmarksModel); + connect(ui->bookmarksTree, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(itemChanged(QTreeWidgetItem*))); connect(ui->addFolder, SIGNAL(clicked()), this, SLOT(addFolder())); connect(ui->bookmarksTree, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenuRequested(const QPoint &))); @@ -60,6 +64,8 @@ BookmarksManager::BookmarksManager(QupZilla* mainClass, QWidget* parent) connect(m_bookmarksModel, SIGNAL(folderAdded(QString)), this, SLOT(addFolder(QString))); connect(m_bookmarksModel, SIGNAL(folderDeleted(QString)), this, SLOT(removeFolder(QString))); connect(m_bookmarksModel, SIGNAL(folderRenamed(QString, QString)), this, SLOT(renameFolder(QString, QString))); + connect(m_bookmarksModel, SIGNAL(folderParentChanged(QString,bool)), this, SLOT(changeFolderParent(QString,bool))); + connect(m_bookmarksModel, SIGNAL(bookmarkParentChanged(QString,QByteArray,int,QUrl,QString,QString)), this, SLOT(changeBookmarkParent(QString,QByteArray,int,QUrl,QString,QString))); connect(ui->optimizeDb, SIGNAL(clicked(QPoint)), this, SLOT(optimizeDb())); connect(ui->importBookmarks, SIGNAL(clicked(QPoint)), this, SLOT(importBookmarks())); @@ -322,11 +328,13 @@ void BookmarksManager::refreshTable() QTreeWidgetItem* newItem = new QTreeWidgetItem(ui->bookmarksTree); newItem->setText(0, _bookmarksMenu); newItem->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); + newItem->setFlags(newItem->flags() & ~Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); ui->bookmarksTree->addTopLevelItem(newItem); QTreeWidgetItem* bookmarksToolbar = new QTreeWidgetItem(ui->bookmarksTree); bookmarksToolbar->setText(0, _bookmarksToolbar); bookmarksToolbar->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); + bookmarksToolbar->setFlags(bookmarksToolbar->flags() & ~Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); ui->bookmarksTree->addTopLevelItem(bookmarksToolbar); query.exec("SELECT name FROM folders WHERE subfolder!='yes'"); @@ -334,6 +342,7 @@ void BookmarksManager::refreshTable() newItem = new QTreeWidgetItem(ui->bookmarksTree); newItem->setText(0, query.value(0).toString()); newItem->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); + newItem->setFlags(newItem->flags() | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled); ui->bookmarksTree->addTopLevelItem(newItem); } @@ -368,7 +377,7 @@ void BookmarksManager::refreshTable() item->setData(0, Qt::UserRole + 10, id); item->setData(0, Qt::UserRole + 11, url); item->setIcon(0, icon); - item->setFlags(item->flags() | Qt::ItemIsEditable); + item->setFlags(item->flags() | Qt::ItemIsEditable | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled); ui->bookmarksTree->addTopLevelItem(item); } @@ -377,6 +386,7 @@ void BookmarksManager::refreshTable() newItem = new QTreeWidgetItem(bookmarksToolbar); newItem->setText(0, query.value(0).toString()); newItem->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); + newItem->setFlags(newItem->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); QSqlQuery query2; query2.prepare("SELECT title, url, id, icon FROM bookmarks WHERE folder=?"); @@ -397,7 +407,7 @@ void BookmarksManager::refreshTable() item->setData(0, Qt::UserRole + 10, id); item->setData(0, Qt::UserRole + 11, url); item->setIcon(0, icon); - item->setFlags(item->flags() | Qt::ItemIsEditable); + item->setFlags(item->flags() | Qt::ItemIsEditable | Qt::ItemIsDropEnabled | Qt::ItemIsDragEnabled); } } @@ -419,7 +429,7 @@ void BookmarksManager::addBookmark(const BookmarksModel::Bookmark &bookmark) item->setIcon(0, qIconProvider->iconFromImage(bookmark.image)); item->setToolTip(0, bookmark.title); item->setToolTip(1, bookmark.url.toEncoded()); - item->setFlags(item->flags() | Qt::ItemIsEditable); + item->setFlags(item->flags() | Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); if (bookmark.inSubfolder) { QList list = ui->bookmarksTree->findItems(bookmark.folder, Qt::MatchExactly | Qt::MatchRecursive); @@ -518,6 +528,72 @@ void BookmarksManager::bookmarkEdited(const BookmarksModel::Bookmark &before, co addBookmark(after); } +void BookmarksManager::changeBookmarkParent(const QString &name, const QByteArray &, int id, + const QUrl &, const QString &, const QString &newParent) +{ + QList list = ui->bookmarksTree->findItems(name, Qt::MatchExactly | Qt::MatchRecursive); + + QTreeWidgetItem* item = 0; + foreach (item, list) { + if (id == item->data(0, Qt::UserRole + 10).toInt()) { + break; + } + } + if (!item || id != item->data(0, Qt::UserRole + 10).toInt()) { + return; + } + + item->parent() ? item->parent()->removeChild(item) : ui->bookmarksTree->invisibleRootItem()->removeChild(item); + + QTreeWidgetItem* parent = 0; + if (newParent.isEmpty() || newParent == QLatin1String("unsorted")) { + parent = ui->bookmarksTree->invisibleRootItem(); + } + + if (!parent) { + list = ui->bookmarksTree->findItems(newParent, Qt::MatchExactly | Qt::MatchRecursive); + if (list.count() == 0) { + return; + } + parent = list.at(0); + if (!parent) { + return; + } + } + parent->addChild(item); +} + +void BookmarksManager::changeFolderParent(const QString &name, bool isSubfolder) +{ + QList list = ui->bookmarksTree->findItems(name, Qt::MatchExactly | Qt::MatchRecursive); + + if (list.count() == 0) { + return; + } + + QTreeWidgetItem* item = list.at(0); + if (!item) { + return; + } + + item->parent() ? item->parent()->removeChild(item) : ui->bookmarksTree->invisibleRootItem()->removeChild(item); + + QTreeWidgetItem* parent = 0; + if (isSubfolder) { + list = ui->bookmarksTree->findItems(_bookmarksToolbar, Qt::MatchExactly); + if (!list.isEmpty() && list.at(0)) { + parent = list.at(0); + } + } + else { + parent = ui->bookmarksTree->invisibleRootItem(); + } + if (!parent) { + return; + } + parent->addChild(item); +} + void BookmarksManager::addFolder(const QString &name) { m_isRefreshing = true; @@ -526,6 +602,13 @@ void BookmarksManager::addFolder(const QString &name) item->setText(0, name); item->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); + if (name != _bookmarksToolbar && name != _bookmarksMenu) { + item->setFlags(item->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); + } + else { + item->setFlags(item->flags() & ~Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); + } + m_isRefreshing = false; } @@ -538,6 +621,7 @@ void BookmarksManager::addSubfolder(const QString &name) QTreeWidgetItem* item = new QTreeWidgetItem(list.at(0)); item->setText(0, name); item->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); + item->setFlags(item->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); } m_isRefreshing = false; diff --git a/src/lib/bookmarks/bookmarksmanager.h b/src/lib/bookmarks/bookmarksmanager.h index 1f52217c2..272814cda 100644 --- a/src/lib/bookmarks/bookmarksmanager.h +++ b/src/lib/bookmarks/bookmarksmanager.h @@ -75,6 +75,10 @@ private slots: void removeBookmark(const BookmarksModel::Bookmark &bookmark); void bookmarkEdited(const BookmarksModel::Bookmark &before, const BookmarksModel::Bookmark &after); + void changeBookmarkParent(const QString &name, const QByteArray &, int id, + const QUrl &, const QString &, const QString &newParent); + void changeFolderParent(const QString &name, bool isSubfolder); + private: QupZilla* getQupZilla(); diff --git a/src/lib/bookmarks/bookmarksmodel.cpp b/src/lib/bookmarks/bookmarksmodel.cpp index da900c624..a32505058 100644 --- a/src/lib/bookmarks/bookmarksmodel.cpp +++ b/src/lib/bookmarks/bookmarksmodel.cpp @@ -333,7 +333,7 @@ bool BookmarksModel::editBookmark(int id, const QString &title, const QUrl &url, bool BookmarksModel::changeIcon(int id, const QIcon &icon) { QSqlQuery query; - query.prepare("SELECT title, url, folder, icon FROM bookmarks WWHERE id=?"); + query.prepare("SELECT title, url, folder, icon FROM bookmarks WHERE id=?"); query.addBindValue(id); query.exec(); @@ -557,3 +557,74 @@ QString BookmarksModel::fromTranslatedFolder(const QString &name) } return folder; } + +void BookmarksModel::changeBookmarkParent(int id, const QString &newParent, const QString &oldParent, bool* ok) +{ + QSqlQuery query; + query.prepare("SELECT title, url, icon FROM bookmarks WHERE id=?"); + query.addBindValue(id); + query.exec(); + + if (!query.next()) { + if (ok) { + *ok = false; + } + return; + } + + QString title = query.value(0).toString(); + QUrl url = query.value(1).toUrl(); + QByteArray imageData = query.value(2).toByteArray(); + + query.prepare("UPDATE bookmarks SET folder = ? WHERE id = ?"); + query.bindValue(0, BookmarksModel::fromTranslatedFolder(newParent)); + query.bindValue(1, id); + + if (!query.exec()) { + if (ok) { + *ok = false; + } + return; + } + + emit bookmarkParentChanged(title, imageData, id, url, oldParent, newParent); + mApp->sendMessages(Qz::AM_BookmarksChanged, true); + + if (ok) { + *ok = true; + } +} + +void BookmarksModel::changeFolderParent(const QString &name, bool isSubfolder, bool* ok) +{ + QSqlQuery query; + query.prepare("UPDATE folders SET subfolder=? WHERE name=?"); + query.bindValue(0, isSubfolder ? "yes" : "no"); + query.bindValue(1, BookmarksModel::fromTranslatedFolder(name)); + if (!query.exec()) { + if (ok) { + *ok = false; + } + return; + } + + emit folderParentChanged(name, isSubfolder); + mApp->sendMessages(Qz::AM_BookmarksChanged, true); + + if (ok) { + *ok = true; + } +} + + +void BookmarksModel::bookmarkDropedLink(const QUrl &url, const QString &title, const QVariant &imageVariant, const QString &folder, bool *ok) +{ + bool result = false; + + QIcon icon = qIconProvider->iconFromImage(qvariant_cast(imageVariant)); + result = saveBookmark(url, title, icon, BookmarksModel::fromTranslatedFolder(folder)); + + if (ok) { + *ok = result; + } +} diff --git a/src/lib/bookmarks/bookmarksmodel.h b/src/lib/bookmarks/bookmarksmodel.h index ff7f0b8f6..3e6da40c1 100644 --- a/src/lib/bookmarks/bookmarksmodel.h +++ b/src/lib/bookmarks/bookmarksmodel.h @@ -109,13 +109,22 @@ signals: void bookmarkDeleted(const BookmarksModel::Bookmark &bookmark); void bookmarkEdited(const BookmarksModel::Bookmark &before, const BookmarksModel::Bookmark &after); + void bookmarkParentChanged(const QString &name, const QByteArray &imageData, int id, + const QUrl &url, const QString &oldParent, const QString &newParent); + void folderAdded(const QString &title); void folderDeleted(const QString &title); void subfolderAdded(const QString &title); void folderRenamed(const QString &before, const QString &after); + void folderParentChanged(const QString &name, bool isSubfolder); + public slots: + void bookmarkDropedLink(const QUrl &url, const QString &title, const QVariant &imageVariant, + const QString &folder = QLatin1String("unsorted"), bool* ok = 0); + void changeBookmarkParent(int id, const QString &newParent, const QString &oldParent, bool* ok = 0); + void changeFolderParent(const QString &name, bool isSubfolder, bool* ok = 0); private: bool m_showMostVisited; diff --git a/src/lib/bookmarks/bookmarkstoolbar.cpp b/src/lib/bookmarks/bookmarkstoolbar.cpp index fe75830eb..c564ea8d0 100644 --- a/src/lib/bookmarks/bookmarkstoolbar.cpp +++ b/src/lib/bookmarks/bookmarkstoolbar.cpp @@ -60,6 +60,8 @@ BookmarksToolbar::BookmarksToolbar(QupZilla* mainClass, QWidget* parent) connect(m_bookmarksModel, SIGNAL(subfolderAdded(QString)), this, SLOT(subfolderAdded(QString))); connect(m_bookmarksModel, SIGNAL(folderDeleted(QString)), this, SLOT(folderDeleted(QString))); connect(m_bookmarksModel, SIGNAL(folderRenamed(QString, QString)), this, SLOT(folderRenamed(QString, QString))); + connect(m_bookmarksModel, SIGNAL(folderParentChanged(QString,bool)), this, SLOT(changeFolderParent(QString,bool))); + connect(m_bookmarksModel, SIGNAL(bookmarkParentChanged(QString,QByteArray,int,QUrl,QString,QString)), this, SLOT(changeBookmarkParent(QString,QByteArray,int,QUrl,QString,QString))); setMaximumWidth(p_QupZilla->width()); @@ -387,6 +389,41 @@ void BookmarksToolbar::folderRenamed(const QString &before, const QString &after } } +void BookmarksToolbar::changeBookmarkParent(const QString &name, const QByteArray &imageData, int id, + const QUrl &url, const QString &oldParent, const QString &newParent) +{ + if (oldParent != _bookmarksToolbar && newParent != _bookmarksToolbar) { + return; + } + + bool itemIsAboutToRemove = (newParent != _bookmarksToolbar); + + Bookmark bookmark; + bookmark.id = id; + bookmark.url = url; + bookmark.title = name; + bookmark.folder = QLatin1String("bookmarksToolbar"); + bookmark.image = QImage::fromData(imageData); + bookmark.inSubfolder = false; + + if (itemIsAboutToRemove) { + removeBookmark(bookmark); + } + else { + addBookmark(bookmark); + } +} + +void BookmarksToolbar::changeFolderParent(const QString &name, bool isSubfolder) +{ + if (!isSubfolder) { + folderDeleted(name); + } + else { + subfolderAdded(name); + } +} + void BookmarksToolbar::addBookmark(const BookmarksModel::Bookmark &bookmark) { if (bookmark.folder != QLatin1String("bookmarksToolbar")) { diff --git a/src/lib/bookmarks/bookmarkstoolbar.h b/src/lib/bookmarks/bookmarkstoolbar.h index 221841e03..455d7da0e 100644 --- a/src/lib/bookmarks/bookmarkstoolbar.h +++ b/src/lib/bookmarks/bookmarkstoolbar.h @@ -68,6 +68,10 @@ private slots: void folderDeleted(const QString &name); void folderRenamed(const QString &before, const QString &after); + void changeBookmarkParent(const QString &name, const QByteArray &imageData, int id, + const QUrl &url, const QString &oldParent, const QString &newParent); + void changeFolderParent(const QString &name, bool isSubfolder); + private: void dropEvent(QDropEvent* e); void dragEnterEvent(QDragEnterEvent* e); diff --git a/src/lib/sidebar/bookmarkssidebar.cpp b/src/lib/sidebar/bookmarkssidebar.cpp index bab9552ce..e7099563f 100644 --- a/src/lib/sidebar/bookmarkssidebar.cpp +++ b/src/lib/sidebar/bookmarkssidebar.cpp @@ -41,6 +41,10 @@ BookmarksSideBar::BookmarksSideBar(QupZilla* mainClass, QWidget* parent) { ui->setupUi(this); + ui->bookmarksTree->setSelectionBehavior(QAbstractItemView::SelectRows); + ui->bookmarksTree->setSelectionMode(QAbstractItemView::ContiguousSelection); + ui->bookmarksTree->setDragDropReceiver(true, m_bookmarksModel); + ui->bookmarksTree->setDefaultItemShowMode(TreeWidget::ItemsExpanded); connect(ui->bookmarksTree, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenuRequested(const QPoint &))); connect(ui->bookmarksTree, SIGNAL(itemControlClicked(QTreeWidgetItem*)), this, SLOT(itemControlClicked(QTreeWidgetItem*))); @@ -54,6 +58,8 @@ BookmarksSideBar::BookmarksSideBar(QupZilla* mainClass, QWidget* parent) connect(m_bookmarksModel, SIGNAL(folderAdded(QString)), this, SLOT(addFolder(QString))); connect(m_bookmarksModel, SIGNAL(folderDeleted(QString)), this, SLOT(removeFolder(QString))); connect(m_bookmarksModel, SIGNAL(folderRenamed(QString, QString)), this, SLOT(renameFolder(QString, QString))); + connect(m_bookmarksModel, SIGNAL(folderParentChanged(QString,bool)), this, SLOT(changeFolderParent(QString,bool))); + connect(m_bookmarksModel, SIGNAL(bookmarkParentChanged(QString,QByteArray,int,QUrl,QString,QString)), this, SLOT(changeBookmarkParent(QString,QByteArray,int,QUrl,QString,QString))); QTimer::singleShot(0, this, SLOT(refreshTable())); } @@ -149,6 +155,7 @@ void BookmarksSideBar::addBookmark(const BookmarksModel::Bookmark &bookmark) item->setData(0, Qt::UserRole + 10, bookmark.id); item->setIcon(0, qIconProvider->iconFromImage(bookmark.image)); item->setToolTip(0, bookmark.url.toEncoded()); + item->setFlags(item->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); if (bookmark.folder != QLatin1String("unsorted")) { ui->bookmarksTree->appendToParentItem(translatedFolder, item); @@ -206,6 +213,13 @@ void BookmarksSideBar::addFolder(const QString &name) QTreeWidgetItem* item = new QTreeWidgetItem(ui->bookmarksTree); item->setText(0, name); item->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); + + if (name != _bookmarksToolbar && name != _bookmarksMenu) { + item->setFlags(item->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); + } + else { + item->setFlags(item->flags() & ~Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); + } } void BookmarksSideBar::removeFolder(const QString &name) @@ -234,6 +248,79 @@ void BookmarksSideBar::renameFolder(const QString &before, const QString &after) item->setText(0, after); } +void BookmarksSideBar::changeBookmarkParent(const QString &name, const QByteArray &imageData, int id, + const QUrl &url, const QString &oldParent, const QString &newParent) +{ + if (!newParent.isEmpty() && newParent != QLatin1String("unsorted") + && newParent != _bookmarksUnsorted && !oldParent.isEmpty() + && oldParent != QLatin1String("unsorted") + && oldParent != _bookmarksUnsorted) { // old and new parent are not invisibleRootItem() + QList list = ui->bookmarksTree->findItems(newParent, Qt::MatchExactly); + if (list.isEmpty()) { // newParent is a subfolder + list = ui->bookmarksTree->findItems(oldParent, Qt::MatchExactly); + if (list.isEmpty()) { // oldParent is a subfolder + return; // item was reparented from bookmarkToolbar(subfolder) to subfolder(bookmarkToolbar) + } + } + } + + QList list = ui->bookmarksTree->findItems(name, Qt::MatchExactly | Qt::MatchRecursive); + + QTreeWidgetItem* item = 0; + foreach (item, list) { + if (id == item->data(0, Qt::UserRole + 10).toInt()) { + break; + } + } + + if (!item || id != item->data(0, Qt::UserRole + 10).toInt()) { // list is empty or item with 'id' was not found + // bookmark's oldParent was bookmarkToolbar(subfolder) + Bookmark bookmark; + bookmark.id = id; + bookmark.url = url; + bookmark.title = name; + bookmark.folder = BookmarksModel::fromTranslatedFolder(newParent.isEmpty() ? QLatin1String("unsorted") : newParent); + bookmark.image = QImage::fromData(imageData); + bookmark.inSubfolder = false; + addBookmark(bookmark); + return; + } + + QTreeWidgetItem* parent = 0; + if (newParent.isEmpty() || newParent == QLatin1String("unsorted") || newParent == _bookmarksUnsorted) { + parent = ui->bookmarksTree->invisibleRootItem(); + } + item->parent() ? item->parent()->removeChild(item) : ui->bookmarksTree->invisibleRootItem()->removeChild(item); + + if (!parent) { + list = ui->bookmarksTree->findItems(newParent, Qt::MatchExactly); + if (list.count() == 0) { + return; + } + + parent = list.at(0); + if (!parent) { + return; + } + } + + parent->addChild(item); +} + +void BookmarksSideBar::changeFolderParent(const QString &name, bool isSubfolder) +{ + if (isSubfolder) { // folder moved to _bookmarksToolbar as a subfolder + removeFolder(name); + } + else { + addFolder(name); + QList bookmarksList = m_bookmarksModel->folderBookmarks(name); + foreach (const Bookmark & b, bookmarksList) { + addBookmark(b); + } + } +} + void BookmarksSideBar::keyPressEvent(QKeyEvent* event) { if (event->key() == Qt::Key_Return || event->key() == Qt::Key_Enter) { @@ -262,6 +349,7 @@ void BookmarksSideBar::refreshTable() QTreeWidgetItem* newItem = new QTreeWidgetItem(ui->bookmarksTree); newItem->setText(0, _bookmarksMenu); newItem->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); + newItem->setFlags(newItem->flags() & ~Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); ui->bookmarksTree->addTopLevelItem(newItem); query.exec("SELECT name FROM folders WHERE subfolder!='yes'"); @@ -269,6 +357,7 @@ void BookmarksSideBar::refreshTable() newItem = new QTreeWidgetItem(ui->bookmarksTree); newItem->setText(0, query.value(0).toString()); newItem->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); + newItem->setFlags(newItem->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); ui->bookmarksTree->addTopLevelItem(newItem); } @@ -306,6 +395,7 @@ void BookmarksSideBar::refreshTable() item->setData(0, Qt::UserRole + 10, id); item->setIcon(0, icon); + item->setFlags(item->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); ui->bookmarksTree->addTopLevelItem(item); } ui->bookmarksTree->expandAll(); diff --git a/src/lib/sidebar/bookmarkssidebar.h b/src/lib/sidebar/bookmarkssidebar.h index 82abd2dce..2dc4e0ccd 100644 --- a/src/lib/sidebar/bookmarkssidebar.h +++ b/src/lib/sidebar/bookmarkssidebar.h @@ -61,6 +61,10 @@ private slots: void removeFolder(const QString &name); void renameFolder(const QString &before, const QString &after); + void changeBookmarkParent(const QString &name, const QByteArray &imageData, int id, + const QUrl &url, const QString &oldParent, const QString &newParent); + void changeFolderParent(const QString &name, bool isSubfolder); + private: void keyPressEvent(QKeyEvent* event); diff --git a/src/lib/tools/treewidget.cpp b/src/lib/tools/treewidget.cpp index a0ea9ca7d..026113e7a 100644 --- a/src/lib/tools/treewidget.cpp +++ b/src/lib/tools/treewidget.cpp @@ -16,8 +16,15 @@ * along with this program. If not, see . * ============================================================ */ #include "treewidget.h" +#include "bookmarksmodel.h" #include +#include +#include +#include + +const int ITEM_IS_TOPLEVEL = Qt::UserRole+20; +const int ITEM_PARENT_TITLE = Qt::UserRole+21; TreeWidget::TreeWidget(QWidget* parent) : QTreeWidget(parent) @@ -92,6 +99,211 @@ void TreeWidget::iterateAllItems(QTreeWidgetItem* parent) } } +Qt::DropActions TreeWidget::supportedDropActions() +{ + return Qt::CopyAction; +} + +QStringList TreeWidget::mimeTypes() const +{ + QStringList types; + types << QLatin1String("application/qupzilla.treewidgetitem.list"); + return types; +} + +QMimeData *TreeWidget::mimeData(const QList items) const +{ + QMimeData *data = new QMimeData(); + QByteArray encodedData; + + QDataStream stream(&encodedData, QIODevice::WriteOnly); + foreach (const QTreeWidgetItem* item, items) { + if (item) { + QTreeWidgetItem* clonedItem = item->clone(); + bool parentIsRoot = false; + if (!item->parent() || item->parent() == invisibleRootItem()) { + parentIsRoot = true; + } + clonedItem->setData(0, ITEM_IS_TOPLEVEL, parentIsRoot ); + clonedItem->setData(0, ITEM_PARENT_TITLE, (parentIsRoot ? QString() : item->parent()->text(0)) ) ; + clonedItem->write(stream); + delete clonedItem; + } + } + + data->setData(QLatin1String("application/qupzilla.treewidgetitem.list"), encodedData); + return data; +} + +bool TreeWidget::dropMimeData(QTreeWidgetItem *parent, int, + const QMimeData *data, Qt::DropAction action) +{ + if (action == Qt::IgnoreAction) { + return true; + } + + if (parent && !parent->text(1).isEmpty()) { // parent is a bookmark, go one level up! + parent = parent->parent(); + } + + if (!parent) { + parent = invisibleRootItem(); + } + + bool ok = false; + if (data->hasUrls()) { + QString folder = (parent == invisibleRootItem()) ? QLatin1String("unsorted") : parent->text(0); + QUrl url = data->urls().at(0); + QString title = data->text().isEmpty() ? url.host()+url.path() : data->text(); + emit linkWasDroped(url, title, data->imageData(), folder, &ok); + + return ok; + } + + if (!data->hasFormat(QLatin1String("application/qupzilla.treewidgetitem.list"))) + return false; + + setUpdatesEnabled(false); + QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); + QSqlDatabase db = QSqlDatabase::database(); + db.transaction(); + + QByteArray ba = data->data(QLatin1String("application/qupzilla.treewidgetitem.list")); + QDataStream stream(&ba, QIODevice::ReadOnly); + if (stream.atEnd()) + return false; + + while (!stream.atEnd()) { + + QTreeWidgetItem *item = new QTreeWidgetItem; + item->read(stream); + bool parentIsRoot = item->data(0, ITEM_IS_TOPLEVEL).toBool(); + QString oldParentTitle = item->data(0, ITEM_PARENT_TITLE).toString(); + + bool isFolder = (item && item->text(1).isEmpty()); + if (isFolder && (item->text(0) == _bookmarksMenu || + item->text(0) == _bookmarksToolbar)) { + continue; + } + + bool parentIsOldParent = parentIsRoot ? (parent == invisibleRootItem()) : (oldParentTitle == parent->text(0)); + if ( parentIsOldParent || (isFolder && parent != invisibleRootItem() && + parent->text(0) != _bookmarksToolbar) ) { + // just 'Bookmarks In ToolBar' folder can have subfolders + continue; + } + + if (isFolder) { + if (parent->text(0) == _bookmarksToolbar) { + emit folderParentChanged(item->text(0), true, &ok); + } + else { + emit folderParentChanged(item->text(0), false, &ok); + } + } + else { + emit bookmarkParentChanged(item->data(0, Qt::UserRole + 10).toInt(), + parent->text(0), oldParentTitle, &ok); + } + + if (!ok) { + continue; + } + } + + db.commit(); + clearSelection(); + setUpdatesEnabled(true); + QApplication::restoreOverrideCursor(); + return true; +} + +void TreeWidget::dragEnterEvent(QDragEnterEvent *event) +{ + const QMimeData *mimeData = event->mimeData(); + QTreeWidget::dragEnterEvent(event); + if (mimeData->hasUrls() || mimeData->hasFormat(QLatin1String("application/qupzilla.treewidgetitem.list"))) { + event->acceptProposedAction(); + } + else { + event->ignore(); + } +} + +void TreeWidget::dragMoveEvent(QDragMoveEvent *event) +{ + const QMimeData *mimeData = event->mimeData(); + bool accept = false; + if (mimeData->hasUrls()) { + accept = true; + } + else if (mimeData->hasFormat(QLatin1String("application/qupzilla.treewidgetitem.list"))) { + QTreeWidgetItem *itemUnderMouse = itemAt(event->pos()); + bool underMouseIsFolder = (itemUnderMouse && itemUnderMouse->text(1).isEmpty()); + int top = visualItemRect(itemUnderMouse).top(); + int bottom = visualItemRect(itemUnderMouse).bottom(); + int y = event->pos().y(); + bool overEdgeOfItem = (y >= top-1 && y <= top+1) || (y <= bottom+1 && y >= bottom-1); + + QByteArray ba = mimeData->data(QLatin1String("application/qupzilla.treewidgetitem.list")); + QDataStream stream(&ba, QIODevice::ReadOnly); + + while (!stream.atEnd()) { + QTreeWidgetItem *dragItem = new QTreeWidgetItem; + dragItem->read(stream); + bool parentIsRoot = dragItem->data(0, ITEM_IS_TOPLEVEL).toBool(); + QString oldParentTitle = dragItem->data(0, ITEM_PARENT_TITLE).toString(); + + bool itemIsFolder = dragItem->text(1).isEmpty(); + if (dragItem->text(0) != _bookmarksMenu + && dragItem->text(0) != _bookmarksToolbar) { + if (!itemUnderMouse->parent() && !parentIsRoot && overEdgeOfItem) { + accept = true; + break; + } + bool parentsAreDifferent = parentIsRoot + ? itemUnderMouse->parent() + : (!itemUnderMouse->parent() || itemUnderMouse->parent()->text(0) != oldParentTitle); + bool canHasSubFolder = !itemUnderMouse->parent() + || itemUnderMouse->parent() == invisibleRootItem() + || itemUnderMouse->parent()->text(0) == _bookmarksToolbar; + + if (!underMouseIsFolder && parentsAreDifferent) { + if (!itemIsFolder) { + accept = true; + break; + } + else if (!itemUnderMouse->parent() + || dragItem->text(0) != itemUnderMouse->parent()->text(0) + && canHasSubFolder) { + accept = true; + break; + } + } + else if (underMouseIsFolder) { + if (itemIsFolder && itemUnderMouse->text(0) == _bookmarksToolbar + && (parentIsRoot || oldParentTitle != _bookmarksToolbar)) { + accept = true; + break; + } + else if (!itemIsFolder && oldParentTitle != itemUnderMouse->text(0)) { + accept = true; + break; + } + } + } + } + } + + QTreeWidget::dragMoveEvent(event); + if (accept) { + event->acceptProposedAction(); + } + else { + event->ignore(); + } +} + QList TreeWidget::allItems() { if (m_refreshAllItemsNeeded) { @@ -222,3 +434,24 @@ void TreeWidget::deleteItems(const QList &items) qDeleteAll(items); } + +void TreeWidget::setDragDropReceiver(bool enable, QObject* receiver) +{ + if (!receiver) { + enable = false; + } + setDragEnabled(enable); + viewport()->setAcceptDrops(enable); + setDropIndicatorShown(enable); + if (enable) { + model()->setSupportedDragActions(Qt::CopyAction); + connect(this, SIGNAL(folderParentChanged(QString,bool,bool*)), receiver, SLOT(changeFolderParent(QString,bool,bool*))); + connect(this, SIGNAL(bookmarkParentChanged(int,QString,QString,bool*)), receiver, SLOT(changeBookmarkParent(int,QString,QString,bool*))); + connect(this, SIGNAL(linkWasDroped(QUrl,QString,QVariant,QString,bool*)), receiver, SLOT(bookmarkDropedLink(QUrl,QString,QVariant,QString,bool*))); + } + else { + disconnect(this, SIGNAL(folderParentChanged(QString,bool,bool*)), receiver, SLOT(changeFolderParent(QString,bool,bool*))); + disconnect(this, SIGNAL(bookmarkParentChanged(int,QString,QString,bool*)), receiver, SLOT(changeBookmarkParent(int,QString,QString,bool*))); + disconnect(this, SIGNAL(linkWasDroped(QUrl,QString,QVariant,QString,bool*)), receiver, SLOT(bookmarkDropedLink(QUrl,QString,QVariant,QString,bool*))); + } +} diff --git a/src/lib/tools/treewidget.h b/src/lib/tools/treewidget.h index 6e1d39db0..7d63869b8 100644 --- a/src/lib/tools/treewidget.h +++ b/src/lib/tools/treewidget.h @@ -43,10 +43,16 @@ public: void deleteItem(QTreeWidgetItem* item); void deleteItems(const QList &items); + void setDragDropReceiver(bool enable, QObject* receiver = 0); + signals: void itemControlClicked(QTreeWidgetItem* item); void itemMiddleButtonClicked(QTreeWidgetItem* item); + void linkWasDroped(const QUrl &url, const QString &title, const QVariant &imageVariant, const QString &folder, bool* ok); + void bookmarkParentChanged(int id, const QString &newParent, const QString &oldParent, bool* ok); + void folderParentChanged(const QString &name, bool isSubfolder, bool* ok); + public slots: void filterString(QString string); void clear(); @@ -58,6 +64,13 @@ private: void mousePressEvent(QMouseEvent* event); void iterateAllItems(QTreeWidgetItem* parent); + Qt::DropActions supportedDropActions(); + QStringList mimeTypes() const; + QMimeData *mimeData(const QList items) const; + bool dropMimeData(QTreeWidgetItem *parent, int, const QMimeData *data, Qt::DropAction action); + void dragEnterEvent(QDragEnterEvent *event); + void dragMoveEvent(QDragMoveEvent* event); + bool m_refreshAllItemsNeeded; QList m_allTreeItems; ItemShowMode m_showMode;