diff options
Diffstat (limited to 'lib/bookmarks/bookmarksmodel.cpp')
-rw-r--r-- | lib/bookmarks/bookmarksmodel.cpp | 75 |
1 files changed, 62 insertions, 13 deletions
diff --git a/lib/bookmarks/bookmarksmodel.cpp b/lib/bookmarks/bookmarksmodel.cpp index 418d456..ba537ba 100644 --- a/lib/bookmarks/bookmarksmodel.cpp +++ b/lib/bookmarks/bookmarksmodel.cpp @@ -7,6 +7,8 @@ */ #include "bookmarksmodel.h" +#include <QMimeData> +#include <QBuffer> BookmarksModel::BookmarksModel(QStyle *style, QObject *parent) : QAbstractItemModel(parent) @@ -166,10 +168,10 @@ Qt::ItemFlags BookmarksModel::flags(const QModelIndex &index) const case BookmarkItem::Root: break; case BookmarkItem::Folder: - flags |= Qt::ItemIsEditable; + flags |= Qt::ItemIsEditable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; break; case BookmarkItem::Bookmark: - flags |= Qt::ItemIsEnabled | Qt::ItemNeverHasChildren; + flags |= Qt::ItemIsEnabled | Qt::ItemNeverHasChildren | Qt::ItemIsDragEnabled; break; } return flags; @@ -253,19 +255,18 @@ bool BookmarksModel::setData(const QModelIndex &index, const QVariant &value, in case BookmarkItem::Bookmark: if(role == TitleRole && (node->title != value.toString())) { - node->title = value.toString(); - modified = true; - emit dataChanged(this->index(node, 0), this->index(node, 1)); - //emit dataChanged(index, index); - return true; + node->title = value.toString(); + modified = true; + emit dataChanged(this->index(node, 0), this->index(node, 1)); + //emit dataChanged(index, index); + return true; } else if(role == OpenUrlRole && (node->href != value.toString())) { - node->href = value.toString(); - modified = true; - emit dataChanged(this->index(node, 0), this->index(node, 1)); - //emit dataChanged(index, index); - return true; - + node->href = value.toString(); + modified = true; + emit dataChanged(this->index(node, 0), this->index(node, 1)); + //emit dataChanged(index, index); + return true; } return false; } @@ -327,3 +328,51 @@ void BookmarksModel::expandItems(QTreeView *view, BookmarkItem *root) } } } +QMimeData *BookmarksModel::mimeData(const QModelIndexList &indexes) const +{ + auto *qMimeData = new QMimeData(); + QByteArray data; + + for(const QModelIndex &idx : indexes) { + if(idx.column() != 0 || !idx.isValid()) + continue; + + QByteArray encodedData; + QBuffer buffer(&encodedData); + buffer.open(QBuffer::ReadWrite); + Xbel writer(&buffer); + writer.write(static_cast<BookmarkItem *>(idx.internalPointer())); + + data.append(encodedData); + } + + qMimeData->setData(mimeTypes().first(), data); + + return qMimeData; +} + +bool BookmarksModel::dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) +{ + if(action == Qt::IgnoreAction) + return true; + + // invalid format or column + if(!data->hasFormat(mimeTypes().first()) || column > 0) + return false; + + QByteArray bytes = data->data(mimeTypes().first()); + + QBuffer buffer(&bytes); + buffer.open(QIODevice::ReadOnly); + + Xbel reader(&buffer); + auto *parentItem = static_cast<BookmarkItem *>(parent.internalPointer()); + Q_CHECK_PTR(parentItem); + + beginInsertRows(parent, parentItem->row(), parentItem->row()); + reader.read(parentItem); + endInsertRows(); + + modified = true; + return true; +} |