1
mirror of https://invent.kde.org/network/falkon.git synced 2024-11-11 01:22:10 +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->bookmarksTree->setSelectionBehavior(QAbstractItemView::SelectRows);
ui->bookmarksTree->setSelectionMode(QAbstractItemView::ContiguousSelection);
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->addFolder, SIGNAL(clicked()), this, SLOT(addFolder()));
@ -328,13 +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);
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);
bookmarksToolbar->setFlags((bookmarksToolbar->flags() & ~Qt::ItemIsDragEnabled) | Qt::ItemIsDropEnabled);
ui->bookmarksTree->addTopLevelItem(bookmarksToolbar);
query.exec("SELECT name FROM folders WHERE subfolder!='yes'");
@ -606,7 +606,7 @@ void BookmarksManager::addFolder(const QString &name)
item->setFlags(item->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled);
}
else {
item->setFlags(item->flags() & ~Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled);
item->setFlags((item->flags() & ~Qt::ItemIsDragEnabled) | Qt::ItemIsDropEnabled);
}
m_isRefreshing = false;

View File

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

View File

@ -42,8 +42,8 @@ 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->setMimeType(QLatin1String("application/qupzilla.treewidgetitem.bookmarks"));
ui->bookmarksTree->setDefaultItemShowMode(TreeWidget::ItemsExpanded);
connect(ui->bookmarksTree, SIGNAL(customContextMenuRequested(const QPoint &)), this, SLOT(contextMenuRequested(const QPoint &)));
@ -218,7 +218,7 @@ void BookmarksSideBar::addFolder(const QString &name)
item->setFlags(item->flags() | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled);
}
else {
item->setFlags(item->flags() & ~Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled);
item->setFlags((item->flags() & ~Qt::ItemIsDragEnabled) | Qt::ItemIsDropEnabled);
}
}
@ -349,7 +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);
newItem->setFlags((newItem->flags() & ~Qt::ItemIsDragEnabled) | Qt::ItemIsDropEnabled);
ui->bookmarksTree->addTopLevelItem(newItem);
query.exec("SELECT name FROM folders WHERE subfolder!='yes'");

View File

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

View File

@ -99,6 +99,11 @@ void TreeWidget::iterateAllItems(QTreeWidgetItem* parent)
}
}
void TreeWidget::setMimeType(const QString &mimeType)
{
m_mimeType = mimeType;
}
Qt::DropActions TreeWidget::supportedDropActions()
{
return Qt::CopyAction;
@ -107,7 +112,7 @@ Qt::DropActions TreeWidget::supportedDropActions()
QStringList TreeWidget::mimeTypes() const
{
QStringList types;
types << QLatin1String("application/qupzilla.treewidgetitem.list");
types << m_mimeType;
return types;
}
@ -131,7 +136,7 @@ QMimeData *TreeWidget::mimeData(const QList<QTreeWidgetItem *> items) const
}
}
data->setData(QLatin1String("application/qupzilla.treewidgetitem.list"), encodedData);
data->setData(m_mimeType, encodedData);
return data;
}
@ -160,18 +165,20 @@ bool TreeWidget::dropMimeData(QTreeWidgetItem *parent, int,
return ok;
}
if (!data->hasFormat(QLatin1String("application/qupzilla.treewidgetitem.list")))
if (!data->hasFormat(m_mimeType)) {
return false;
}
setUpdatesEnabled(false);
QApplication::setOverrideCursor(QCursor(Qt::WaitCursor));
QSqlDatabase db = QSqlDatabase::database();
db.transaction();
QByteArray ba = data->data(QLatin1String("application/qupzilla.treewidgetitem.list"));
QByteArray ba = data->data(m_mimeType);
QDataStream stream(&ba, QIODevice::ReadOnly);
if (stream.atEnd())
if (stream.atEnd()) {
return false;
}
while (!stream.atEnd()) {
@ -222,7 +229,7 @@ void TreeWidget::dragEnterEvent(QDragEnterEvent *event)
{
const QMimeData* mimeData = event->mimeData();
QTreeWidget::dragEnterEvent(event);
if (mimeData->hasUrls() || mimeData->hasFormat(QLatin1String("application/qupzilla.treewidgetitem.list"))) {
if (mimeData->hasUrls() || mimeData->hasFormat(m_mimeType)) {
event->acceptProposedAction();
}
else {
@ -237,15 +244,19 @@ void TreeWidget::dragMoveEvent(QDragMoveEvent *event)
if (mimeData->hasUrls()) {
accept = true;
}
else if (mimeData->hasFormat(QLatin1String("application/qupzilla.treewidgetitem.list"))) {
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(QLatin1String("application/qupzilla.treewidgetitem.list"));
QByteArray ba = mimeData->data(m_mimeType);
QDataStream stream(&ba, QIODevice::ReadOnly);
while (!stream.atEnd()) {
@ -262,7 +273,7 @@ void TreeWidget::dragMoveEvent(QDragMoveEvent *event)
break;
}
bool parentsAreDifferent = parentIsRoot
? itemUnderMouse->parent()
? itemUnderMouse->parent() != 0
: (!itemUnderMouse->parent() || itemUnderMouse->parent()->text(0) != oldParentTitle);
bool canHasSubFolder = !itemUnderMouse->parent()
|| itemUnderMouse->parent() == invisibleRootItem()
@ -274,8 +285,8 @@ void TreeWidget::dragMoveEvent(QDragMoveEvent *event)
break;
}
else if (!itemUnderMouse->parent()
|| dragItem->text(0) != itemUnderMouse->parent()->text(0)
&& canHasSubFolder) {
|| (dragItem->text(0) != itemUnderMouse->parent()->text(0)
&& canHasSubFolder)) {
accept = true;
break;
}

View File

@ -44,6 +44,7 @@ public:
void deleteItems(const QList<QTreeWidgetItem*> &items);
void setDragDropReceiver(bool enable, QObject* receiver = 0);
void setMimeType(const QString &mimeType);
signals:
void itemControlClicked(QTreeWidgetItem* item);
@ -74,7 +75,7 @@ private:
bool m_refreshAllItemsNeeded;
QList<QTreeWidgetItem*> m_allTreeItems;
ItemShowMode m_showMode;
QString m_mimeType;
};
#endif // BOOKMARKSTREEWIDGET_H