Skip to content
This repository has been archived by the owner on May 10, 2018. It is now read-only.

Commit

Permalink
Added Drag-n-Drop functionality to bookmarkmanager and bookmarksidebar.
Browse files Browse the repository at this point in the history
  • Loading branch information
srazi committed Sep 5, 2012
1 parent a046ec4 commit d9ae633
Show file tree
Hide file tree
Showing 10 changed files with 553 additions and 4 deletions.
90 changes: 87 additions & 3 deletions src/lib/bookmarks/bookmarksmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 &)));
Expand All @@ -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()));
Expand Down Expand Up @@ -322,18 +328,21 @@ 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'");
while (query.next()) {
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);
}

Expand Down Expand Up @@ -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);
}

Expand All @@ -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=?");
Expand All @@ -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);
}
}

Expand All @@ -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<QTreeWidgetItem*> list = ui->bookmarksTree->findItems(bookmark.folder, Qt::MatchExactly | Qt::MatchRecursive);
Expand Down Expand Up @@ -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<QTreeWidgetItem*> 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<QTreeWidgetItem*> 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;
Expand All @@ -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;
}

Expand All @@ -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;
Expand Down
4 changes: 4 additions & 0 deletions src/lib/bookmarks/bookmarksmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down
73 changes: 72 additions & 1 deletion src/lib/bookmarks/bookmarksmodel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand Down Expand Up @@ -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<QImage>(imageVariant));
result = saveBookmark(url, title, icon, BookmarksModel::fromTranslatedFolder(folder));

if (ok) {
*ok = result;
}
}
9 changes: 9 additions & 0 deletions src/lib/bookmarks/bookmarksmodel.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,13 +109,22 @@ class QT_QUPZILLA_EXPORT BookmarksModel : public QObject
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;
Expand Down
37 changes: 37 additions & 0 deletions src/lib/bookmarks/bookmarkstoolbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down Expand Up @@ -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")) {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/bookmarks/bookmarkstoolbar.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading

0 comments on commit d9ae633

Please sign in to comment.