1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

Fixed crash when dragging bookmark from manager to sidebar

This commit is contained in:
nowrep 2012-09-06 11:28:43 +02:00
parent 3aba5ac7a0
commit 5a3f4e5f4d
8 changed files with 85 additions and 67 deletions

View File

@ -48,8 +48,8 @@ BookmarksManager::BookmarksManager(QupZilla* mainClass, QWidget* parent)
ui->setupUi(this); ui->setupUi(this);
ui->bookmarksTree->setSelectionBehavior(QAbstractItemView::SelectRows); ui->bookmarksTree->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->bookmarksTree->setSelectionMode(QAbstractItemView::ContiguousSelection);
ui->bookmarksTree->setDragDropReceiver(true, m_bookmarksModel); ui->bookmarksTree->setDragDropReceiver(true, m_bookmarksModel);
ui->bookmarksTree->setMimeType(QLatin1String("application/qupzilla.treewidgetitem.bookmarks"));
connect(ui->bookmarksTree, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(itemChanged(QTreeWidgetItem*))); connect(ui->bookmarksTree, SIGNAL(itemChanged(QTreeWidgetItem*, int)), this, SLOT(itemChanged(QTreeWidgetItem*)));
connect(ui->addFolder, SIGNAL(clicked()), this, SLOT(addFolder())); connect(ui->addFolder, SIGNAL(clicked()), this, SLOT(addFolder()));
@ -64,8 +64,8 @@ BookmarksManager::BookmarksManager(QupZilla* mainClass, QWidget* parent)
connect(m_bookmarksModel, SIGNAL(folderAdded(QString)), this, SLOT(addFolder(QString))); connect(m_bookmarksModel, SIGNAL(folderAdded(QString)), this, SLOT(addFolder(QString)));
connect(m_bookmarksModel, SIGNAL(folderDeleted(QString)), this, SLOT(removeFolder(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(folderRenamed(QString, QString)), this, SLOT(renameFolder(QString, QString)));
connect(m_bookmarksModel, SIGNAL(folderParentChanged(QString,bool)), this, SLOT(changeFolderParent(QString,bool))); 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(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->optimizeDb, SIGNAL(clicked(QPoint)), this, SLOT(optimizeDb()));
connect(ui->importBookmarks, SIGNAL(clicked(QPoint)), this, SLOT(importBookmarks())); connect(ui->importBookmarks, SIGNAL(clicked(QPoint)), this, SLOT(importBookmarks()));
@ -328,13 +328,13 @@ void BookmarksManager::refreshTable()
QTreeWidgetItem* newItem = new QTreeWidgetItem(ui->bookmarksTree); QTreeWidgetItem* newItem = new QTreeWidgetItem(ui->bookmarksTree);
newItem->setText(0, _bookmarksMenu); newItem->setText(0, _bookmarksMenu);
newItem->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); newItem->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon));
newItem->setFlags(newItem->flags() & ~Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); newItem->setFlags((newItem->flags() & ~Qt::ItemIsDragEnabled) | Qt::ItemIsDropEnabled);
ui->bookmarksTree->addTopLevelItem(newItem); ui->bookmarksTree->addTopLevelItem(newItem);
QTreeWidgetItem* bookmarksToolbar = new QTreeWidgetItem(ui->bookmarksTree); QTreeWidgetItem* bookmarksToolbar = new QTreeWidgetItem(ui->bookmarksTree);
bookmarksToolbar->setText(0, _bookmarksToolbar); bookmarksToolbar->setText(0, _bookmarksToolbar);
bookmarksToolbar->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); bookmarksToolbar->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon));
bookmarksToolbar->setFlags(bookmarksToolbar->flags() & ~Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); bookmarksToolbar->setFlags((bookmarksToolbar->flags() & ~Qt::ItemIsDragEnabled) | Qt::ItemIsDropEnabled);
ui->bookmarksTree->addTopLevelItem(bookmarksToolbar); ui->bookmarksTree->addTopLevelItem(bookmarksToolbar);
query.exec("SELECT name FROM folders WHERE subfolder!='yes'"); query.exec("SELECT name FROM folders WHERE subfolder!='yes'");
@ -529,12 +529,12 @@ void BookmarksManager::bookmarkEdited(const BookmarksModel::Bookmark &before, co
} }
void BookmarksManager::changeBookmarkParent(const QString &name, const QByteArray &, int id, void BookmarksManager::changeBookmarkParent(const QString &name, const QByteArray &, int id,
const QUrl &, const QString &, const QString &newParent) const QUrl &, const QString &, const QString &newParent)
{ {
QList<QTreeWidgetItem*> list = ui->bookmarksTree->findItems(name, Qt::MatchExactly | Qt::MatchRecursive); QList<QTreeWidgetItem*> list = ui->bookmarksTree->findItems(name, Qt::MatchExactly | Qt::MatchRecursive);
QTreeWidgetItem* item = 0; QTreeWidgetItem* item = 0;
foreach (item, list) { foreach(item, list) {
if (id == item->data(0, Qt::UserRole + 10).toInt()) { if (id == item->data(0, Qt::UserRole + 10).toInt()) {
break; break;
} }
@ -606,7 +606,7 @@ void BookmarksManager::addFolder(const QString &name)
item->setFlags(item->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); item->setFlags(item->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled);
} }
else { else {
item->setFlags(item->flags() & ~Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); item->setFlags((item->flags() & ~Qt::ItemIsDragEnabled) | Qt::ItemIsDropEnabled);
} }
m_isRefreshing = false; m_isRefreshing = false;

View File

@ -22,6 +22,9 @@
<property name="alternatingRowColors"> <property name="alternatingRowColors">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="selectionMode">
<enum>QAbstractItemView::ContiguousSelection</enum>
</property>
<property name="sortingEnabled"> <property name="sortingEnabled">
<bool>true</bool> <bool>true</bool>
</property> </property>

View File

@ -617,7 +617,7 @@ void BookmarksModel::changeFolderParent(const QString &name, bool isSubfolder, b
} }
void BookmarksModel::bookmarkDropedLink(const QUrl &url, const QString &title, const QVariant &imageVariant, const QString &folder, bool *ok) void BookmarksModel::bookmarkDropedLink(const QUrl &url, const QString &title, const QVariant &imageVariant, const QString &folder, bool* ok)
{ {
bool result = false; bool result = false;

View File

@ -60,8 +60,8 @@ BookmarksToolbar::BookmarksToolbar(QupZilla* mainClass, QWidget* parent)
connect(m_bookmarksModel, SIGNAL(subfolderAdded(QString)), this, SLOT(subfolderAdded(QString))); connect(m_bookmarksModel, SIGNAL(subfolderAdded(QString)), this, SLOT(subfolderAdded(QString)));
connect(m_bookmarksModel, SIGNAL(folderDeleted(QString)), this, SLOT(folderDeleted(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(folderRenamed(QString, QString)), this, SLOT(folderRenamed(QString, QString)));
connect(m_bookmarksModel, SIGNAL(folderParentChanged(QString,bool)), this, SLOT(changeFolderParent(QString,bool))); 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(m_bookmarksModel, SIGNAL(bookmarkParentChanged(QString, QByteArray, int, QUrl, QString, QString)), this, SLOT(changeBookmarkParent(QString, QByteArray, int, QUrl, QString, QString)));
setMaximumWidth(p_QupZilla->width()); setMaximumWidth(p_QupZilla->width());
@ -390,7 +390,7 @@ void BookmarksToolbar::folderRenamed(const QString &before, const QString &after
} }
void BookmarksToolbar::changeBookmarkParent(const QString &name, const QByteArray &imageData, int id, void BookmarksToolbar::changeBookmarkParent(const QString &name, const QByteArray &imageData, int id,
const QUrl &url, const QString &oldParent, const QString &newParent) const QUrl &url, const QString &oldParent, const QString &newParent)
{ {
if (oldParent != _bookmarksToolbar && newParent != _bookmarksToolbar) { if (oldParent != _bookmarksToolbar && newParent != _bookmarksToolbar) {
return; return;

View File

@ -42,8 +42,8 @@ BookmarksSideBar::BookmarksSideBar(QupZilla* mainClass, QWidget* parent)
ui->setupUi(this); ui->setupUi(this);
ui->bookmarksTree->setSelectionBehavior(QAbstractItemView::SelectRows); ui->bookmarksTree->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->bookmarksTree->setSelectionMode(QAbstractItemView::ContiguousSelection);
ui->bookmarksTree->setDragDropReceiver(true, m_bookmarksModel); ui->bookmarksTree->setDragDropReceiver(true, m_bookmarksModel);
ui->bookmarksTree->setMimeType(QLatin1String("application/qupzilla.treewidgetitem.bookmarks"));
ui->bookmarksTree->setDefaultItemShowMode(TreeWidget::ItemsExpanded); ui->bookmarksTree->setDefaultItemShowMode(TreeWidget::ItemsExpanded);
connect(ui->bookmarksTree, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenuRequested(const QPoint &))); connect(ui->bookmarksTree, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenuRequested(const QPoint &)));
@ -58,8 +58,8 @@ BookmarksSideBar::BookmarksSideBar(QupZilla* mainClass, QWidget* parent)
connect(m_bookmarksModel, SIGNAL(folderAdded(QString)), this, SLOT(addFolder(QString))); connect(m_bookmarksModel, SIGNAL(folderAdded(QString)), this, SLOT(addFolder(QString)));
connect(m_bookmarksModel, SIGNAL(folderDeleted(QString)), this, SLOT(removeFolder(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(folderRenamed(QString, QString)), this, SLOT(renameFolder(QString, QString)));
connect(m_bookmarksModel, SIGNAL(folderParentChanged(QString,bool)), this, SLOT(changeFolderParent(QString,bool))); 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(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())); QTimer::singleShot(0, this, SLOT(refreshTable()));
} }
@ -218,7 +218,7 @@ void BookmarksSideBar::addFolder(const QString &name)
item->setFlags(item->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); item->setFlags(item->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled);
} }
else { else {
item->setFlags(item->flags() & ~Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); item->setFlags((item->flags() & ~Qt::ItemIsDragEnabled) | Qt::ItemIsDropEnabled);
} }
} }
@ -249,7 +249,7 @@ void BookmarksSideBar::renameFolder(const QString &before, const QString &after)
} }
void BookmarksSideBar::changeBookmarkParent(const QString &name, const QByteArray &imageData, int id, void BookmarksSideBar::changeBookmarkParent(const QString &name, const QByteArray &imageData, int id,
const QUrl &url, const QString &oldParent, const QString &newParent) const QUrl &url, const QString &oldParent, const QString &newParent)
{ {
if (!newParent.isEmpty() && newParent != QLatin1String("unsorted") if (!newParent.isEmpty() && newParent != QLatin1String("unsorted")
&& newParent != _bookmarksUnsorted && !oldParent.isEmpty() && newParent != _bookmarksUnsorted && !oldParent.isEmpty()
@ -267,7 +267,7 @@ void BookmarksSideBar::changeBookmarkParent(const QString &name, const QByteArra
QList<QTreeWidgetItem*> list = ui->bookmarksTree->findItems(name, Qt::MatchExactly | Qt::MatchRecursive); QList<QTreeWidgetItem*> list = ui->bookmarksTree->findItems(name, Qt::MatchExactly | Qt::MatchRecursive);
QTreeWidgetItem* item = 0; QTreeWidgetItem* item = 0;
foreach (item, list) { foreach(item, list) {
if (id == item->data(0, Qt::UserRole + 10).toInt()) { if (id == item->data(0, Qt::UserRole + 10).toInt()) {
break; break;
} }
@ -315,7 +315,7 @@ void BookmarksSideBar::changeFolderParent(const QString &name, bool isSubfolder)
else { else {
addFolder(name); addFolder(name);
QList<Bookmark> bookmarksList = m_bookmarksModel->folderBookmarks(name); QList<Bookmark> bookmarksList = m_bookmarksModel->folderBookmarks(name);
foreach (const Bookmark & b, bookmarksList) { foreach(const Bookmark & b, bookmarksList) {
addBookmark(b); addBookmark(b);
} }
} }
@ -349,7 +349,7 @@ void BookmarksSideBar::refreshTable()
QTreeWidgetItem* newItem = new QTreeWidgetItem(ui->bookmarksTree); QTreeWidgetItem* newItem = new QTreeWidgetItem(ui->bookmarksTree);
newItem->setText(0, _bookmarksMenu); newItem->setText(0, _bookmarksMenu);
newItem->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon)); newItem->setIcon(0, style()->standardIcon(QStyle::SP_DirIcon));
newItem->setFlags(newItem->flags() & ~Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled); newItem->setFlags((newItem->flags() & ~Qt::ItemIsDragEnabled) | Qt::ItemIsDropEnabled);
ui->bookmarksTree->addTopLevelItem(newItem); ui->bookmarksTree->addTopLevelItem(newItem);
query.exec("SELECT name FROM folders WHERE subfolder!='yes'"); query.exec("SELECT name FROM folders WHERE subfolder!='yes'");

View File

@ -41,6 +41,9 @@
<property name="alternatingRowColors"> <property name="alternatingRowColors">
<bool>true</bool> <bool>true</bool>
</property> </property>
<property name="selectionMode">
<enum>QAbstractItemView::ContiguousSelection</enum>
</property>
<property name="headerHidden"> <property name="headerHidden">
<bool>true</bool> <bool>true</bool>
</property> </property>

View File

@ -23,8 +23,8 @@
#include <QApplication> #include <QApplication>
#include <QUrl> #include <QUrl>
const int ITEM_IS_TOPLEVEL = Qt::UserRole+20; const int ITEM_IS_TOPLEVEL = Qt::UserRole + 20;
const int ITEM_PARENT_TITLE = Qt::UserRole+21; const int ITEM_PARENT_TITLE = Qt::UserRole + 21;
TreeWidget::TreeWidget(QWidget* parent) TreeWidget::TreeWidget(QWidget* parent)
: QTreeWidget(parent) : QTreeWidget(parent)
@ -99,6 +99,11 @@ void TreeWidget::iterateAllItems(QTreeWidgetItem* parent)
} }
} }
void TreeWidget::setMimeType(const QString &mimeType)
{
m_mimeType = mimeType;
}
Qt::DropActions TreeWidget::supportedDropActions() Qt::DropActions TreeWidget::supportedDropActions()
{ {
return Qt::CopyAction; return Qt::CopyAction;
@ -107,36 +112,36 @@ Qt::DropActions TreeWidget::supportedDropActions()
QStringList TreeWidget::mimeTypes() const QStringList TreeWidget::mimeTypes() const
{ {
QStringList types; QStringList types;
types << QLatin1String("application/qupzilla.treewidgetitem.list"); types << m_mimeType;
return types; return types;
} }
QMimeData *TreeWidget::mimeData(const QList<QTreeWidgetItem *> items) const QMimeData* TreeWidget::mimeData(const QList<QTreeWidgetItem*> items) const
{ {
QMimeData *data = new QMimeData(); QMimeData* data = new QMimeData();
QByteArray encodedData; QByteArray encodedData;
QDataStream stream(&encodedData, QIODevice::WriteOnly); QDataStream stream(&encodedData, QIODevice::WriteOnly);
foreach (const QTreeWidgetItem* item, items) { foreach(const QTreeWidgetItem * item, items) {
if (item) { if (item) {
QTreeWidgetItem* clonedItem = item->clone(); QTreeWidgetItem* clonedItem = item->clone();
bool parentIsRoot = false; bool parentIsRoot = false;
if (!item->parent() || item->parent() == invisibleRootItem()) { if (!item->parent() || item->parent() == invisibleRootItem()) {
parentIsRoot = true; parentIsRoot = true;
} }
clonedItem->setData(0, ITEM_IS_TOPLEVEL, parentIsRoot ); clonedItem->setData(0, ITEM_IS_TOPLEVEL, parentIsRoot);
clonedItem->setData(0, ITEM_PARENT_TITLE, (parentIsRoot ? QString() : item->parent()->text(0)) ) ; clonedItem->setData(0, ITEM_PARENT_TITLE, (parentIsRoot ? QString() : item->parent()->text(0))) ;
clonedItem->write(stream); clonedItem->write(stream);
delete clonedItem; delete clonedItem;
} }
} }
data->setData(QLatin1String("application/qupzilla.treewidgetitem.list"), encodedData); data->setData(m_mimeType, encodedData);
return data; return data;
} }
bool TreeWidget::dropMimeData(QTreeWidgetItem *parent, int, bool TreeWidget::dropMimeData(QTreeWidgetItem* parent, int,
const QMimeData *data, Qt::DropAction action) const QMimeData* data, Qt::DropAction action)
{ {
if (action == Qt::IgnoreAction) { if (action == Qt::IgnoreAction) {
return true; return true;
@ -154,41 +159,43 @@ bool TreeWidget::dropMimeData(QTreeWidgetItem *parent, int,
if (data->hasUrls()) { if (data->hasUrls()) {
QString folder = (parent == invisibleRootItem()) ? QLatin1String("unsorted") : parent->text(0); QString folder = (parent == invisibleRootItem()) ? QLatin1String("unsorted") : parent->text(0);
QUrl url = data->urls().at(0); QUrl url = data->urls().at(0);
QString title = data->text().isEmpty() ? url.host()+url.path() : data->text(); QString title = data->text().isEmpty() ? url.host() + url.path() : data->text();
emit linkWasDroped(url, title, data->imageData(), folder, &ok); emit linkWasDroped(url, title, data->imageData(), folder, &ok);
return ok; return ok;
} }
if (!data->hasFormat(QLatin1String("application/qupzilla.treewidgetitem.list"))) if (!data->hasFormat(m_mimeType)) {
return false; return false;
}
setUpdatesEnabled(false); setUpdatesEnabled(false);
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor)); QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
QSqlDatabase db = QSqlDatabase::database(); QSqlDatabase db = QSqlDatabase::database();
db.transaction(); db.transaction();
QByteArray ba = data->data(QLatin1String("application/qupzilla.treewidgetitem.list")); QByteArray ba = data->data(m_mimeType);
QDataStream stream(&ba, QIODevice::ReadOnly); QDataStream stream(&ba, QIODevice::ReadOnly);
if (stream.atEnd()) if (stream.atEnd()) {
return false; return false;
}
while (!stream.atEnd()) { while (!stream.atEnd()) {
QTreeWidgetItem *item = new QTreeWidgetItem; QTreeWidgetItem* item = new QTreeWidgetItem;
item->read(stream); item->read(stream);
bool parentIsRoot = item->data(0, ITEM_IS_TOPLEVEL).toBool(); bool parentIsRoot = item->data(0, ITEM_IS_TOPLEVEL).toBool();
QString oldParentTitle = item->data(0, ITEM_PARENT_TITLE).toString(); QString oldParentTitle = item->data(0, ITEM_PARENT_TITLE).toString();
bool isFolder = (item && item->text(1).isEmpty()); bool isFolder = (item && item->text(1).isEmpty());
if (isFolder && (item->text(0) == _bookmarksMenu || if (isFolder && (item->text(0) == _bookmarksMenu ||
item->text(0) == _bookmarksToolbar)) { item->text(0) == _bookmarksToolbar)) {
continue; continue;
} }
bool parentIsOldParent = parentIsRoot ? (parent == invisibleRootItem()) : (oldParentTitle == parent->text(0)); bool parentIsOldParent = parentIsRoot ? (parent == invisibleRootItem()) : (oldParentTitle == parent->text(0));
if ( parentIsOldParent || (isFolder && parent != invisibleRootItem() && if (parentIsOldParent || (isFolder && parent != invisibleRootItem() &&
parent->text(0) != _bookmarksToolbar) ) { parent->text(0) != _bookmarksToolbar)) {
// just 'Bookmarks In ToolBar' folder can have subfolders // just 'Bookmarks In ToolBar' folder can have subfolders
continue; continue;
} }
@ -218,11 +225,11 @@ bool TreeWidget::dropMimeData(QTreeWidgetItem *parent, int,
return true; return true;
} }
void TreeWidget::dragEnterEvent(QDragEnterEvent *event) void TreeWidget::dragEnterEvent(QDragEnterEvent* event)
{ {
const QMimeData *mimeData = event->mimeData(); const QMimeData* mimeData = event->mimeData();
QTreeWidget::dragEnterEvent(event); QTreeWidget::dragEnterEvent(event);
if (mimeData->hasUrls() || mimeData->hasFormat(QLatin1String("application/qupzilla.treewidgetitem.list"))) { if (mimeData->hasUrls() || mimeData->hasFormat(m_mimeType)) {
event->acceptProposedAction(); event->acceptProposedAction();
} }
else { else {
@ -230,26 +237,30 @@ void TreeWidget::dragEnterEvent(QDragEnterEvent *event)
} }
} }
void TreeWidget::dragMoveEvent(QDragMoveEvent *event) void TreeWidget::dragMoveEvent(QDragMoveEvent* event)
{ {
const QMimeData *mimeData = event->mimeData(); const QMimeData* mimeData = event->mimeData();
bool accept = false; bool accept = false;
if (mimeData->hasUrls()) { if (mimeData->hasUrls()) {
accept = true; accept = true;
} }
else if (mimeData->hasFormat(QLatin1String("application/qupzilla.treewidgetitem.list"))) { else if (mimeData->hasFormat(m_mimeType)) {
QTreeWidgetItem *itemUnderMouse = itemAt(event->pos()); QTreeWidgetItem* itemUnderMouse = itemAt(event->pos());
if (!itemUnderMouse) {
return;
}
bool underMouseIsFolder = (itemUnderMouse && itemUnderMouse->text(1).isEmpty()); bool underMouseIsFolder = (itemUnderMouse && itemUnderMouse->text(1).isEmpty());
int top = visualItemRect(itemUnderMouse).top(); int top = visualItemRect(itemUnderMouse).top();
int bottom = visualItemRect(itemUnderMouse).bottom(); int bottom = visualItemRect(itemUnderMouse).bottom();
int y = event->pos().y(); int y = event->pos().y();
bool overEdgeOfItem = (y >= top-1 && y <= top+1) || (y <= bottom+1 && y >= bottom-1); bool overEdgeOfItem = (y >= top - 1 && y <= top + 1) || (y <= bottom + 1 && y >= bottom - 1);
QByteArray ba = mimeData->data(QLatin1String("application/qupzilla.treewidgetitem.list")); QByteArray ba = mimeData->data(m_mimeType);
QDataStream stream(&ba, QIODevice::ReadOnly); QDataStream stream(&ba, QIODevice::ReadOnly);
while (!stream.atEnd()) { while (!stream.atEnd()) {
QTreeWidgetItem *dragItem = new QTreeWidgetItem; QTreeWidgetItem* dragItem = new QTreeWidgetItem;
dragItem->read(stream); dragItem->read(stream);
bool parentIsRoot = dragItem->data(0, ITEM_IS_TOPLEVEL).toBool(); bool parentIsRoot = dragItem->data(0, ITEM_IS_TOPLEVEL).toBool();
QString oldParentTitle = dragItem->data(0, ITEM_PARENT_TITLE).toString(); QString oldParentTitle = dragItem->data(0, ITEM_PARENT_TITLE).toString();
@ -262,11 +273,11 @@ void TreeWidget::dragMoveEvent(QDragMoveEvent *event)
break; break;
} }
bool parentsAreDifferent = parentIsRoot bool parentsAreDifferent = parentIsRoot
? itemUnderMouse->parent() ? itemUnderMouse->parent() != 0
: (!itemUnderMouse->parent() || itemUnderMouse->parent()->text(0) != oldParentTitle); : (!itemUnderMouse->parent() || itemUnderMouse->parent()->text(0) != oldParentTitle);
bool canHasSubFolder = !itemUnderMouse->parent() bool canHasSubFolder = !itemUnderMouse->parent()
|| itemUnderMouse->parent() == invisibleRootItem() || itemUnderMouse->parent() == invisibleRootItem()
|| itemUnderMouse->parent()->text(0) == _bookmarksToolbar; || itemUnderMouse->parent()->text(0) == _bookmarksToolbar;
if (!underMouseIsFolder && parentsAreDifferent) { if (!underMouseIsFolder && parentsAreDifferent) {
if (!itemIsFolder) { if (!itemIsFolder) {
@ -274,8 +285,8 @@ void TreeWidget::dragMoveEvent(QDragMoveEvent *event)
break; break;
} }
else if (!itemUnderMouse->parent() else if (!itemUnderMouse->parent()
|| dragItem->text(0) != itemUnderMouse->parent()->text(0) || (dragItem->text(0) != itemUnderMouse->parent()->text(0)
&& canHasSubFolder) { && canHasSubFolder)) {
accept = true; accept = true;
break; break;
} }
@ -445,13 +456,13 @@ void TreeWidget::setDragDropReceiver(bool enable, QObject* receiver)
setDropIndicatorShown(enable); setDropIndicatorShown(enable);
if (enable) { if (enable) {
model()->setSupportedDragActions(Qt::CopyAction); model()->setSupportedDragActions(Qt::CopyAction);
connect(this, SIGNAL(folderParentChanged(QString,bool,bool*)), receiver, SLOT(changeFolderParent(QString,bool,bool*))); 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(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*))); connect(this, SIGNAL(linkWasDroped(QUrl, QString, QVariant, QString, bool*)), receiver, SLOT(bookmarkDropedLink(QUrl, QString, QVariant, QString, bool*)));
} }
else { else {
disconnect(this, SIGNAL(folderParentChanged(QString,bool,bool*)), receiver, SLOT(changeFolderParent(QString,bool,bool*))); 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(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*))); disconnect(this, SIGNAL(linkWasDroped(QUrl, QString, QVariant, QString, bool*)), receiver, SLOT(bookmarkDropedLink(QUrl, QString, QVariant, QString, bool*)));
} }
} }

View File

@ -44,6 +44,7 @@ public:
void deleteItems(const QList<QTreeWidgetItem*> &items); void deleteItems(const QList<QTreeWidgetItem*> &items);
void setDragDropReceiver(bool enable, QObject* receiver = 0); void setDragDropReceiver(bool enable, QObject* receiver = 0);
void setMimeType(const QString &mimeType);
signals: signals:
void itemControlClicked(QTreeWidgetItem* item); void itemControlClicked(QTreeWidgetItem* item);
@ -66,15 +67,15 @@ private:
Qt::DropActions supportedDropActions(); Qt::DropActions supportedDropActions();
QStringList mimeTypes() const; QStringList mimeTypes() const;
QMimeData *mimeData(const QList<QTreeWidgetItem *> items) const; QMimeData* mimeData(const QList<QTreeWidgetItem*> items) const;
bool dropMimeData(QTreeWidgetItem *parent, int, const QMimeData *data, Qt::DropAction action); bool dropMimeData(QTreeWidgetItem* parent, int, const QMimeData* data, Qt::DropAction action);
void dragEnterEvent(QDragEnterEvent *event); void dragEnterEvent(QDragEnterEvent* event);
void dragMoveEvent(QDragMoveEvent* event); void dragMoveEvent(QDragMoveEvent* event);
bool m_refreshAllItemsNeeded; bool m_refreshAllItemsNeeded;
QList<QTreeWidgetItem*> m_allTreeItems; QList<QTreeWidgetItem*> m_allTreeItems;
ItemShowMode m_showMode; ItemShowMode m_showMode;
QString m_mimeType;
}; };
#endif // BOOKMARKSTREEWIDGET_H #endif // BOOKMARKSTREEWIDGET_H