diff --git a/src/lib/bookmarks/bookmarks.cpp b/src/lib/bookmarks/bookmarks.cpp index 24c14a605..8e885cc3f 100644 --- a/src/lib/bookmarks/bookmarks.cpp +++ b/src/lib/bookmarks/bookmarks.cpp @@ -18,23 +18,16 @@ #include "bookmarks.h" #include "bookmarkitem.h" #include "bookmarksmodel.h" -#include "tabbedwebview.h" -#include "iconprovider.h" #include "mainapplication.h" +#include "webview.h" #include "settings.h" #include "json.h" #include #include -#include #include #include -// SQLite DB -> table bookmarks + folders -// Unique in bookmarks table is id -// However from bookmark icon, it is not possible to add more than one bookmark -// Only from Ctrl+D dialog it is possible - Bookmarks::Bookmarks(QObject* parent) : QObject(parent) { @@ -237,7 +230,7 @@ void Bookmarks::loadSettings() settings.beginGroup("Bookmarks"); m_showMostVisited = settings.value("showMostVisited", true).toBool(); m_showOnlyIconsInToolbar = settings.value("showOnlyIconsInToolbar", false).toBool(); - m_lastFolder = settings.value("LastFolder", "unsorted").toString(); + //m_lastFolder = settings.value("LastFolder", "unsorted").toString(); settings.endGroup(); } @@ -269,355 +262,12 @@ void Bookmarks::setShowingOnlyIconsInToolbar(bool state) m_showOnlyIconsInToolbar = state; } -bool Bookmarks::isFolder(const QString &name) -{ - if (name == QLatin1String("bookmarksToolbar") || name == QLatin1String("bookmarksMenu") - || name == QLatin1String("unsorted") || name == _bookmarksToolbar - || name == _bookmarksMenu || name == _bookmarksUnsorted) { - return true; - } - - QSqlQuery query; - query.prepare("SELECT name FROM folders WHERE name = ?"); - query.bindValue(0, name); - query.exec(); - - return query.next(); -} - -QString Bookmarks::lastFolder() const -{ - return m_lastFolder; -} - void Bookmarks::setLastFolder(const QString &folder) { Settings settings; settings.beginGroup("Bookmarks"); settings.setValue("lastFolder", folder); settings.endGroup(); - m_lastFolder = folder; -} - -bool Bookmarks::isBookmarked(const QUrl &url) -{ - QSqlQuery query; - query.prepare("SELECT count(id) FROM bookmarks WHERE url=?"); - query.bindValue(0, url.toString()); - - if (!query.exec() || !query.next()) { - return false; - } - - return query.value(0).toInt() > 0; -} - -// Bookmark search priority: -// Bookmarks in menu > bookmarks in toolbar -> user folders and unsorted -Bookmarks::Bookmark Bookmarks::getBookmark(const QUrl &url) -{ - QSqlQuery query; - query.prepare("SELECT id FROM bookmarks WHERE url=? AND folder='bookmarksMenu' "); - query.bindValue(0, url.toString()); - query.exec(); - - if (query.next()) { - return getBookmark(query.value(0).toInt()); - } - - query.prepare("SELECT id FROM bookmarks WHERE url=? AND folder='bookmarksToolbar' "); - query.bindValue(0, url.toString()); - query.exec(); - - if (query.next()) { - return getBookmark(query.value(0).toInt()); - } - - query.prepare("SELECT id FROM bookmarks WHERE url=? "); - query.bindValue(0, url.toString()); - query.exec(); - - if (query.next()) { - return getBookmark(query.value(0).toInt()); - } - - return Bookmark(); -} - -Bookmarks::Bookmark Bookmarks::getBookmark(int id) -{ - Bookmark bookmark; - QSqlQuery query; - query.prepare("SELECT url, title, folder, icon FROM bookmarks WHERE id=?"); - query.bindValue(0, id); - query.exec(); - if (query.next()) { - bookmark.id = id; - bookmark.url = query.value(0).toUrl(); - bookmark.title = query.value(1).toString(); - bookmark.folder = query.value(2).toString(); - bookmark.image = QImage::fromData(query.value(3).toByteArray()); - bookmark.inSubfolder = isSubfolder(bookmark.folder); - } - return bookmark; -} - -bool Bookmarks::saveBookmark(const QUrl &url, const QString &title, const QIcon &icon, const QString &folder) -{ - if (url.isEmpty() || title.isEmpty() || folder.isEmpty()) { - return false; - } - - QImage image = icon.pixmap(16, 16).toImage(); - if (image.isNull()) { - image = qIconProvider->emptyWebImage(); - } - - // createFolder() calls isFolder() - createFolder(folder); - - QSqlQuery query; - query.prepare("INSERT INTO bookmarks (url, title, folder, icon) VALUES (?,?,?,?)"); - query.bindValue(0, url.toString()); - query.bindValue(1, title); - query.bindValue(2, folder); - QByteArray ba; - QBuffer buffer(&ba); - buffer.open(QIODevice::WriteOnly); - image.save(&buffer, "PNG"); - query.bindValue(3, buffer.data()); - query.exec(); - - Bookmark bookmark; - bookmark.id = query.lastInsertId().toInt(); - bookmark.url = url; - bookmark.title = title; - bookmark.folder = folder; - bookmark.image = image; - bookmark.inSubfolder = isSubfolder(bookmark.folder); - - setLastFolder(folder); - - emit bookmarkAdded(bookmark); - mApp->sendMessages(Qz::AM_BookmarksChanged, true); - return true; -} - -bool Bookmarks::saveBookmark(WebView* view, QString folder) -{ - if (folder.isEmpty()) { - folder = m_lastFolder; - } - - return saveBookmark(view->url(), view->title(), view->icon(), folder); -} - -void Bookmarks::removeBookmark(int id) -{ - QList list; - list.append(id); - - return removeBookmark(list); -} - -void Bookmarks::removeBookmark(const QList list) -{ - QSqlDatabase db = QSqlDatabase::database(); - db.transaction(); - - foreach (int id, list) { - QSqlQuery query; - query.prepare("SELECT url, title, folder FROM bookmarks WHERE id = ?"); - query.bindValue(0, id); - query.exec(); - if (!query.next()) { - continue; - } - - Bookmark bookmark; - bookmark.id = id; - bookmark.url = query.value(0).toUrl(); - bookmark.title = query.value(1).toString(); - bookmark.folder = query.value(2).toString(); - bookmark.image = QImage::fromData(query.value(3).toByteArray()); - bookmark.inSubfolder = isSubfolder(bookmark.folder); - - query.prepare("DELETE FROM bookmarks WHERE id=?"); - query.addBindValue(id); - query.exec(); - - if (!query.exec()) { - continue; - } - - emit bookmarkDeleted(bookmark); - } - - db.commit(); - mApp->sendMessages(Qz::AM_BookmarksChanged, true); -} - -bool Bookmarks::editBookmark(int id, const QString &title, const QUrl &url, const QString &folder) -{ - if (title.isEmpty() && url.isEmpty() && folder.isEmpty()) { - return false; - } - - QSqlQuery query; - query.prepare("SELECT title, url, folder, icon FROM bookmarks WHERE id=?"); - query.addBindValue(id); - query.exec(); - - if (!query.next()) { - return false; - } - - Bookmark before; - before.id = id; - before.title = query.value(0).toString(); - before.url = query.value(1).toUrl(); - before.folder = query.value(2).toString(); - before.image = QImage::fromData(query.value(3).toByteArray()); - before.inSubfolder = isSubfolder(before.folder); - - Bookmark after; - after.id = id; - after.title = title.isEmpty() ? before.title : title; - after.url = url.isEmpty() ? before.url : url; - after.folder = folder.isEmpty() ? before.folder : folder; - after.image = before.image; - after.inSubfolder = isSubfolder(after.folder); - - query.prepare("UPDATE bookmarks SET title=?, url=?, folder=? WHERE id = ?"); - query.bindValue(0, after.title); - query.bindValue(1, after.url.toString()); - query.bindValue(2, after.folder); - query.bindValue(3, id); - - if (!query.exec()) { - return false; - } - - emit bookmarkEdited(before, after); - mApp->sendMessages(Qz::AM_BookmarksChanged, true); - return true; -} - -bool Bookmarks::changeIcon(int id, const QIcon &icon) -{ - QSqlQuery query; - query.prepare("SELECT title, url, folder, icon FROM bookmarks WHERE id=?"); - query.addBindValue(id); - query.exec(); - - if (!query.next()) { - return false; - } - - Bookmark before; - before.id = id; - before.title = query.value(0).toString(); - before.url = query.value(1).toUrl(); - before.folder = query.value(2).toString(); - before.image = QImage::fromData(query.value(3).toByteArray()); - before.inSubfolder = isSubfolder(before.folder); - - Bookmark after = before; - after.image = icon.pixmap(16).toImage(); - - query.prepare("UPDATE bookmarks SET icon = ? WHERE id = ?"); - QByteArray ba; - QBuffer buffer(&ba); - buffer.open(QIODevice::WriteOnly); - after.image.save(&buffer, "PNG"); - query.bindValue(0, buffer.data()); - query.bindValue(1, id); - - if (!query.exec()) { - return false; - } - - emit bookmarkEdited(before, after); - mApp->sendMessages(Qz::AM_BookmarksChanged, true); - return true; -} - -bool Bookmarks::createFolder(const QString &name) -{ - if (isFolder(name)) { - return false; - } - - QSqlQuery query; - query.prepare("INSERT INTO folders (name, subfolder) VALUES (?, 'no')"); - query.bindValue(0, name); - if (!query.exec()) { - return false; - } - - emit folderAdded(name); - mApp->sendMessages(Qz::AM_BookmarksChanged, true); - return true; -} - -void Bookmarks::removeFolder(const QString &name) -{ - if (name == _bookmarksMenu || name == _bookmarksToolbar) { - return; - } - - QSqlQuery query; - query.prepare("SELECT id FROM bookmarks WHERE folder = ? "); - query.bindValue(0, name); - if (!query.exec()) { - return; - } - - QList list; - while (query.next()) { - list.append(query.value(0).toInt()); - } - removeBookmark(list); - - query.prepare("DELETE FROM folders WHERE name=?"); - query.bindValue(0, name); - query.exec(); - - if (name == m_lastFolder) { - setLastFolder("unsorted"); - } - - emit folderDeleted(name); - - mApp->sendMessages(Qz::AM_BookmarksChanged, true); -} - -bool Bookmarks::renameFolder(const QString &before, const QString &after) -{ - QSqlQuery query; - query.prepare("SELECT name FROM folders WHERE name = ?"); - query.bindValue(0, after); - query.exec(); - if (query.next()) { - return false; - } - - query.prepare("UPDATE folders SET name=? WHERE name=?"); - query.bindValue(0, after); - query.bindValue(1, before); - if (!query.exec()) { - return false; - } - - query.prepare("UPDATE bookmarks SET folder=? WHERE folder=?"); - query.bindValue(0, after); - query.bindValue(1, before); - if (!query.exec()) { - return false; - } - - emit folderRenamed(before, after); - return true; } void Bookmarks::exportToHtml(const QString &fileName) @@ -713,7 +363,7 @@ QVector Bookmarks::getFolderBookmarks(const QString &name) bookmark.title = query.value(2).toString(); bookmark.folder = query.value(3).toString(); bookmark.image = QImage::fromData(query.value(4).toByteArray()); - bookmark.inSubfolder = isSubfolder(bookmark.folder); + bookmark.inSubfolder = true; list.append(bookmark); } @@ -721,37 +371,6 @@ QVector Bookmarks::getFolderBookmarks(const QString &name) return list; } -bool Bookmarks::createSubfolder(const QString &name) -{ - if (isFolder(name)) { - return false; - } - - QSqlQuery query; - query.prepare("INSERT INTO folders (name, subfolder) VALUES (?, 'yes')"); - query.bindValue(0, name); - if (!query.exec()) { - return false; - } - - emit subfolderAdded(name); - mApp->sendMessages(Qz::AM_BookmarksChanged, true); - return true; -} - -bool Bookmarks::isSubfolder(const QString &name) -{ - QSqlQuery query; - query.prepare("SELECT subfolder FROM folders WHERE name = ?"); - query.bindValue(0, name); - query.exec(); - if (!query.next()) { - return false; - } - - return query.value(0).toString() == QLatin1String("yes"); -} - QString Bookmarks::toTranslatedFolder(const QString &name) { QString trFolder; @@ -813,12 +432,17 @@ BookmarkItem* Bookmarks::unsortedFolder() const return m_folderUnsorted; } -BookmarkItem* Bookmarks::getLastFolder() const +BookmarkItem* Bookmarks::lastUsedFolder() const { // TODO: Make it actually return last used folder return unsortedFolder(); } +bool Bookmarks::isBookmarked(const QUrl &url) +{ + return !searchBookmarks(url).isEmpty(); +} + QList Bookmarks::searchBookmarks(const QUrl &url) const { QList items; @@ -873,75 +497,3 @@ void Bookmarks::insertBookmark(BookmarkItem* parent, int row, BookmarkItem* item m_model->addBookmark(parent, row, item); emit bookmarkAdded(item); } - -void Bookmarks::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, Bookmarks::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 Bookmarks::changeFolderParent(const QString &name, bool isSubfolder, bool* ok) -{ - if (name.isEmpty()) { - return; - } - - QSqlQuery query; - query.prepare("UPDATE folders SET subfolder=? WHERE name=?"); - query.bindValue(0, isSubfolder ? "yes" : "no"); - query.bindValue(1, Bookmarks::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 Bookmarks::bookmarkDropedLink(const QUrl &url, const QString &title, const QVariant &imageVariant, const QString &folder, bool* ok) -{ - QIcon icon = qIconProvider->iconFromImage(qvariant_cast(imageVariant)); - bool result = saveBookmark(url, title, icon, Bookmarks::fromTranslatedFolder(folder)); - - if (ok) { - *ok = result; - } -} diff --git a/src/lib/bookmarks/bookmarks.h b/src/lib/bookmarks/bookmarks.h index be8e31d6f..738dbf1d1 100644 --- a/src/lib/bookmarks/bookmarks.h +++ b/src/lib/bookmarks/bookmarks.h @@ -68,32 +68,10 @@ public: bool isShowingOnlyIconsInToolbar() const; void setShowingOnlyIconsInToolbar(bool state); - bool isFolder(const QString &name); - - QString lastFolder() const; void setLastFolder(const QString &folder); - bool isBookmarked(const QUrl &url); - - Bookmark getBookmark(int id); - Bookmark getBookmark(const QUrl &url); QVector getFolderBookmarks(const QString &name); - bool saveBookmark(const QUrl &url, const QString &title, const QIcon &icon, const QString &folder = QLatin1String("unsorted")); - bool saveBookmark(WebView* view, QString folder = QString()); - - void removeBookmark(int id); - void removeBookmark(const QList list); - - bool editBookmark(int id, const QString &title = QString(), const QUrl &url = QUrl(), const QString &folder = QString()); - bool changeIcon(int id, const QIcon &icon); - - bool createFolder(const QString &name); - void removeFolder(const QString &name); - bool renameFolder(const QString &before, const QString &after); - - bool createSubfolder(const QString &name); - bool isSubfolder(const QString &name); void exportToHtml(const QString &fileName); @@ -106,8 +84,9 @@ public: BookmarkItem* toolbarFolder() const; BookmarkItem* menuFolder() const; BookmarkItem* unsortedFolder() const; - BookmarkItem* getLastFolder() const; + BookmarkItem* lastUsedFolder() const; + bool isBookmarked(const QUrl &url); QList searchBookmarks(const QUrl &url) const; bool canBeModified(BookmarkItem* item) const; @@ -119,21 +98,6 @@ public: void notifyBookmarkChanged(BookmarkItem* item); signals: - void bookmarkAdded(const Bookmarks::Bookmark &bookmark); - void bookmarkDeleted(const Bookmarks::Bookmark &bookmark); - void bookmarkEdited(const Bookmarks::Bookmark &before, const Bookmarks::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); - // Item was added to bookmarks void bookmarkAdded(BookmarkItem* item); // Item was removed from bookmarks @@ -141,12 +105,6 @@ signals: // Item data has changed void bookmarkChanged(BookmarkItem* item); -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: void loadBookmarks(); @@ -157,7 +115,6 @@ private: bool m_showMostVisited; bool m_showOnlyIconsInToolbar; - QString m_lastFolder; BookmarkItem* m_root; BookmarkItem* m_folderToolbar; diff --git a/src/lib/bookmarks/bookmarkstoolbar.cpp b/src/lib/bookmarks/bookmarkstoolbar.cpp index 694a72edf..50e48978b 100644 --- a/src/lib/bookmarks/bookmarkstoolbar.cpp +++ b/src/lib/bookmarks/bookmarkstoolbar.cpp @@ -256,7 +256,7 @@ void BookmarksToolbar::editBookmark() return; } - m_bookmarks->editBookmark(b.id, title, url, b.folder); + //m_bookmarks->editBookmark(b.id, title, url, b.folder); } void BookmarksToolbar::removeButton() @@ -272,7 +272,7 @@ void BookmarksToolbar::removeButton() } Bookmark bookmark = button->data().value(); - m_bookmarks->removeBookmark(bookmark.id); + //m_bookmarks->removeBookmark(bookmark.id); } void BookmarksToolbar::hidePanel() diff --git a/src/lib/bookmarks/bookmarkstools.cpp b/src/lib/bookmarks/bookmarkstools.cpp index 5f89426b5..8aeb2d5de 100644 --- a/src/lib/bookmarks/bookmarkstools.cpp +++ b/src/lib/bookmarks/bookmarkstools.cpp @@ -88,7 +88,7 @@ void BookmarksFoldersMenu::createMenu(QMenu* menu, BookmarkItem* parent) BookmarksFoldersButton::BookmarksFoldersButton(QWidget* parent, BookmarkItem* folder) : QPushButton(parent) , m_menu(new BookmarksFoldersMenu(this)) - , m_selectedFolder(folder ? folder : mApp->bookmarks()->getLastFolder()) + , m_selectedFolder(folder ? folder : mApp->bookmarks()->lastUsedFolder()) { init(); diff --git a/src/lib/tools/treewidget.cpp b/src/lib/tools/treewidget.cpp index 5f9785767..bb0ad9e9f 100644 --- a/src/lib/tools/treewidget.cpp +++ b/src/lib/tools/treewidget.cpp @@ -16,16 +16,8 @@ * along with this program. If not, see . * ============================================================ */ #include "treewidget.h" -#include "bookmarks.h" -#include #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) @@ -98,224 +90,6 @@ void TreeWidget::iterateAllItems(QTreeWidgetItem* parent) } } -void TreeWidget::setMimeType(const QString &mimeType) -{ - m_mimeType = mimeType; -} - -Qt::DropActions TreeWidget::supportedDropActions() const -{ - return Qt::CopyAction; -} - -QStringList TreeWidget::mimeTypes() const -{ - QStringList types; - types << m_mimeType; - 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) { - continue; - } - - // Why not just pass pointers ??!! - QTreeWidgetItem* clonedItem = item->clone(); - - // #1097 Clearing icon will properly write this item into stream ... - clonedItem->setIcon(0, QIcon()); - - bool parentIsRoot = !item->parent() || item->parent() == invisibleRootItem(); - - 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(m_mimeType, 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(m_mimeType)) { - return false; - } - - QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); - - QByteArray ba = data->data(m_mimeType); - QDataStream stream(&ba, QIODevice::ReadOnly); - if (stream.atEnd()) { - return false; - } - - QSqlDatabase db = QSqlDatabase::database(); - db.transaction(); - - setUpdatesEnabled(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->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) { - emit folderParentChanged(item->text(0), parent->text(0) == _bookmarksToolbar, &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(m_mimeType)) { - 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(m_mimeType)) { - QTreeWidgetItem* itemUnderMouse = itemAt(event->pos()); - if (!itemUnderMouse) { - return; - } - - 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(m_mimeType); - 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() != 0 - : (!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) { @@ -437,27 +211,3 @@ 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) { -// TODO: It won't probably work in Qt5 -#if QT_VERSION < 0x050000 - model()->setSupportedDragActions(Qt::CopyAction); -#endif - 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 d7e362e76..d3e0c7ac9 100644 --- a/src/lib/tools/treewidget.h +++ b/src/lib/tools/treewidget.h @@ -45,17 +45,10 @@ public: void deleteItem(QTreeWidgetItem* item); void deleteItems(const QList &items); - void setDragDropReceiver(bool enable, QObject* receiver = 0); - void setMimeType(const QString &mimeType); - 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(const QString &string); void clear(); @@ -67,17 +60,9 @@ private: void mousePressEvent(QMouseEvent* event); void iterateAllItems(QTreeWidgetItem* parent); - Qt::DropActions supportedDropActions() const; - 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; - QString m_mimeType; }; #endif // BOOKMARKSTREEWIDGET_H