From 23908b283e429a65d937231d59375c72ac1b62f3 Mon Sep 17 00:00:00 2001 From: Xavier BESSON Date: Thu, 28 Sep 2023 08:54:34 +0200 Subject: [PATCH] Bookmarks: goto on double click + drag & drop on view Change-Id: Iec21843fb6679d156e9839f84e56005b8eef7f32 Reviewed-by: David Schulz --- src/plugins/texteditor/bookmarkmanager.cpp | 78 +++++++++++++++++++++- src/plugins/texteditor/bookmarkmanager.h | 4 ++ 2 files changed, 79 insertions(+), 3 deletions(-) diff --git a/src/plugins/texteditor/bookmarkmanager.cpp b/src/plugins/texteditor/bookmarkmanager.cpp index c6cc58d7627..ae21a75be88 100644 --- a/src/plugins/texteditor/bookmarkmanager.cpp +++ b/src/plugins/texteditor/bookmarkmanager.cpp @@ -215,9 +215,9 @@ BookmarkView::BookmarkView(BookmarkManager *manager) : setSelectionMode(QAbstractItemView::SingleSelection); setSelectionBehavior(QAbstractItemView::SelectRows); setDragEnabled(true); - setDragDropMode(QAbstractItemView::DragOnly); + setDragDropMode(QAbstractItemView::DragDrop); - connect(this, &QAbstractItemView::clicked, this, &BookmarkView::gotoBookmark); + connect(this, &QAbstractItemView::doubleClicked, this, &BookmarkView::gotoBookmark); connect(this, &QAbstractItemView::activated, this, &BookmarkView::gotoBookmark); } @@ -395,7 +395,7 @@ Qt::ItemFlags BookmarkManager::flags(const QModelIndex &index) const { if (!index.isValid() || index.column() !=0 || index.row() < 0 || index.row() >= m_bookmarksList.count()) return Qt::NoItemFlags; - return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled; + return Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsDragEnabled | Qt::ItemIsDropEnabled; } Qt::DropActions BookmarkManager::supportedDragActions() const @@ -416,10 +416,62 @@ QMimeData *BookmarkManager::mimeData(const QModelIndexList &indexes) const continue; Bookmark *bookMark = m_bookmarksList.at(index.row()); data->addFile(bookMark->filePath(), bookMark->lineNumber()); + data->addValue(QVariant::fromValue(bookMark)); } return data; } +Qt::DropActions BookmarkManager::supportedDropActions() const +{ + return Qt::MoveAction; +} + +bool BookmarkManager::canDropMimeData(const QMimeData *data, Qt::DropAction action, + int row, int column, + const QModelIndex &parent) const +{ + Q_UNUSED(row); + Q_UNUSED(column); + Q_UNUSED(parent); + + if (!(action & supportedDropActions())) + return false; + + const DropMimeData* customData = qobject_cast(data); + if (!customData) + return false; + + return true; +} + +bool BookmarkManager::dropMimeData(const QMimeData *data, Qt::DropAction action, + int row, int column, const QModelIndex &parent) +{ + Q_UNUSED(column); + + if (!(action & supportedDropActions())) + return false; + + const DropMimeData* customData = qobject_cast(data); + if (!customData) + return false; + + row = parent.row(); + if (row >= m_bookmarksList.size()) + row = m_bookmarksList.size() - 1; + if (row == -1) + row = m_bookmarksList.size() - 1; + + const QList values = customData->values(); + for (const QVariant &value : values) { + auto mark = value.value(); + if (mark) + move(mark, row); + } + + return true; +} + void BookmarkManager::toggleBookmark(const FilePath &fileName, int lineNumber) { if (lineNumber <= 0 || fileName.isEmpty()) @@ -628,6 +680,25 @@ void BookmarkManager::updateActionStatus() emit updateActions(enableToggle, state()); } +void BookmarkManager::move(Bookmark* mark, int newRow) +{ + int currentRow = m_bookmarksList.indexOf(mark); + if (newRow <= currentRow) + ++currentRow; + else + ++newRow; + m_bookmarksList.insert(newRow, mark); + m_bookmarksList.removeAt(currentRow); + + QModelIndex current = selectionModel()->currentIndex(); + QModelIndex topLeft = current.sibling(std::min(newRow, currentRow), 0); + QModelIndex bottomRight = current.sibling(std::max(newRow, currentRow), 2); + emit dataChanged(topLeft, bottomRight); + selectionModel()->setCurrentIndex(topLeft, QItemSelectionModel::Select | QItemSelectionModel::Clear); + + saveBookmarks(); +} + void BookmarkManager::moveUp() { QModelIndex current = selectionModel()->currentIndex(); @@ -824,6 +895,7 @@ BookmarkViewFactory::BookmarkViewFactory(BookmarkManager *bm) NavigationView BookmarkViewFactory::createWidget() { auto view = new BookmarkView(m_manager); + view->setActivationMode(Utils::DoubleClickActivation); // QUESTION: is this useful ? return {view, view->createToolBarWidgets()}; } diff --git a/src/plugins/texteditor/bookmarkmanager.h b/src/plugins/texteditor/bookmarkmanager.h index 4df91094e3d..c83763d870f 100644 --- a/src/plugins/texteditor/bookmarkmanager.h +++ b/src/plugins/texteditor/bookmarkmanager.h @@ -50,6 +50,9 @@ public: Qt::DropActions supportedDragActions() const final; QStringList mimeTypes() const final; QMimeData *mimeData(const QModelIndexList &indexes) const final; + Qt::DropActions supportedDropActions() const final; + bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const final; + bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) final; // this QItemSelectionModel is shared by all views QItemSelectionModel *selectionModel() const; @@ -69,6 +72,7 @@ public: void prevInDocument(); void next(); void prev(); + void move(Bookmark* mark, int newRow); void moveUp(); void moveDown(); void edit();