From c9c3531c0b6da07de8f90761a3d799ace62f8e89 Mon Sep 17 00:00:00 2001 From: Jin Liu Date: Sat, 17 Feb 2024 11:14:46 +0000 Subject: [PATCH] Improve DnD handling in read-only dirs 1. Places panel and tabbar update drag status in read-only dir 2. Don't create drop job in readonly directories --- src/dolphintabbar.cpp | 1 + src/dolphintabbar.h | 1 + src/dolphintabwidget.cpp | 10 +++++++++ src/dolphintabwidget.h | 6 ++++++ src/kitemviews/kfileitemmodel.cpp | 3 ++- src/panels/places/placespanel.cpp | 10 ++++++--- src/views/draganddrophelper.cpp | 36 +++++++++++++++++++++++++++---- src/views/draganddrophelper.h | 30 ++++++++++++++++++++++++++ 8 files changed, 89 insertions(+), 8 deletions(-) diff --git a/src/dolphintabbar.cpp b/src/dolphintabbar.cpp index 4c918e6114..f6af9932da 100644 --- a/src/dolphintabbar.cpp +++ b/src/dolphintabbar.cpp @@ -80,6 +80,7 @@ void DolphinTabBar::dragMoveEvent(QDragMoveEvent *event) const int index = tabAt(event->position().toPoint()); if (mimeData->hasUrls()) { + Q_EMIT tabDragMoveEvent(index, event); updateAutoActivationTimer(index); } diff --git a/src/dolphintabbar.h b/src/dolphintabbar.h index 1177cda934..4a59199bee 100644 --- a/src/dolphintabbar.h +++ b/src/dolphintabbar.h @@ -18,6 +18,7 @@ public: Q_SIGNALS: void openNewActivatedTab(int index); + void tabDragMoveEvent(int index, QDragMoveEvent *event); void tabDropEvent(int index, QDropEvent *event); void tabDetachRequested(int index); diff --git a/src/dolphintabwidget.cpp b/src/dolphintabwidget.cpp index f274ffa2ab..f80b94ea7c 100644 --- a/src/dolphintabwidget.cpp +++ b/src/dolphintabwidget.cpp @@ -9,6 +9,7 @@ #include "dolphin_generalsettings.h" #include "dolphintabbar.h" #include "dolphinviewcontainer.h" +#include "views/draganddrophelper.h" #include #include @@ -33,6 +34,7 @@ DolphinTabWidget::DolphinTabWidget(DolphinNavigatorsWidgetAction *navigatorsWidg DolphinTabBar *tabBar = new DolphinTabBar(this); connect(tabBar, &DolphinTabBar::openNewActivatedTab, this, QOverload::of(&DolphinTabWidget::openNewActivatedTab)); + connect(tabBar, &DolphinTabBar::tabDragMoveEvent, this, &DolphinTabWidget::tabDragMoveEvent); connect(tabBar, &DolphinTabBar::tabDropEvent, this, &DolphinTabWidget::tabDropEvent); connect(tabBar, &DolphinTabBar::tabDetachRequested, this, &DolphinTabWidget::detachTab); @@ -388,6 +390,14 @@ void DolphinTabWidget::openNewActivatedTab(int index) openNewActivatedTab(tabPage->activeViewContainer()->url()); } +void DolphinTabWidget::tabDragMoveEvent(int index, QDragMoveEvent *event) +{ + if (index >= 0) { + DolphinView *view = tabPageAt(index)->activeViewContainer()->view(); + DragAndDropHelper::updateDropAction(event, view->url()); + } +} + void DolphinTabWidget::tabDropEvent(int index, QDropEvent *event) { if (index >= 0) { diff --git a/src/dolphintabwidget.h b/src/dolphintabwidget.h index 22f65b634c..a28a6bea1f 100644 --- a/src/dolphintabwidget.h +++ b/src/dolphintabwidget.h @@ -208,6 +208,12 @@ private Q_SLOTS: */ void openNewActivatedTab(int index); + /** + * Is connected to the KTabBar signal receivedDragMoveEvent. + * Allows dragging and dropping files onto tabs. + */ + void tabDragMoveEvent(int tab, QDragMoveEvent *event); + /** * Is connected to the KTabBar signal receivedDropEvent. * Allows dragging and dropping files onto tabs. diff --git a/src/kitemviews/kfileitemmodel.cpp b/src/kitemviews/kfileitemmodel.cpp index f9bab74cd6..99a3d163fe 100644 --- a/src/kitemviews/kfileitemmodel.cpp +++ b/src/kitemviews/kfileitemmodel.cpp @@ -12,6 +12,7 @@ #include "dolphin_generalsettings.h" #include "dolphindebug.h" #include "private/kfileitemmodelsortalgorithm.h" +#include "views/draganddrophelper.h" #include #include @@ -348,7 +349,7 @@ bool KFileItemModel::supportsDropping(int index) const } else { item = fileItem(index); } - return !item.isNull() && ((item.isDir() && item.isWritable()) || item.isDesktopFile()); + return !item.isNull() && DragAndDropHelper::supportsDropping(item); } QString KFileItemModel::roleDescription(const QByteArray &role) const diff --git a/src/panels/places/placespanel.cpp b/src/panels/places/placespanel.cpp index 2c19d8f294..ba3451bd5e 100644 --- a/src/panels/places/placespanel.cpp +++ b/src/panels/places/placespanel.cpp @@ -156,9 +156,13 @@ void PlacesPanel::dragMoveEvent(QDragMoveEvent *event) // Reject drag ontop of a non-writable protocol // We don't know whether we're dropping inbetween or ontop of a place // so still allow internal drag events so that re-arranging still works. - const QUrl url = placesModel->url(index); - if (url.isValid() && !isInternalDrag(event->mimeData()) && !KProtocolManager::supportsWriting(url)) { - event->setDropAction(Qt::IgnoreAction); + if (!isInternalDrag(event->mimeData())) { + const QUrl url = placesModel->url(index); + if (!url.isValid() || !KProtocolManager::supportsWriting(url)) { + event->setDropAction(Qt::IgnoreAction); + } else { + DragAndDropHelper::updateDropAction(event, url); + } } } diff --git a/src/views/draganddrophelper.cpp b/src/views/draganddrophelper.cpp index 2953233d0d..efdec5b92c 100644 --- a/src/views/draganddrophelper.cpp +++ b/src/views/draganddrophelper.cpp @@ -52,15 +52,43 @@ KIO::DropJob *DragAndDropHelper::dropUrls(const QUrl &destUrl, QDropEvent *event return nullptr; } - // Drop into a directory or a desktop-file - KIO::DropJob *job = KIO::drop(event, destUrl); - KJobWidgets::setWindow(job, window); - return job; + if (supportsDropping(destUrl)) { + // Drop into a directory or a desktop-file + KIO::DropJob *job = KIO::drop(event, destUrl); + KJobWidgets::setWindow(job, window); + return job; + } } return nullptr; } +bool DragAndDropHelper::supportsDropping(const QUrl &destUrl) +{ + KFileItem item(destUrl); + return supportsDropping(item); +} + +bool DragAndDropHelper::supportsDropping(const KFileItem &destItem) +{ + return (destItem.isDir() && destItem.isWritable()) || destItem.isDesktopFile(); +} + +void DragAndDropHelper::updateDropAction(QDropEvent *event, const QUrl &destUrl) +{ + if (urlListMatchesUrl(event->mimeData()->urls(), destUrl)) { + event->setDropAction(Qt::IgnoreAction); + event->ignore(); + } + if (supportsDropping(destUrl)) { + event->setDropAction(event->proposedAction()); + event->accept(); + } else { + event->setDropAction(Qt::IgnoreAction); + event->ignore(); + } +} + void DragAndDropHelper::clearUrlListMatchesUrlCache() { DragAndDropHelper::m_urlListMatchesUrlCache.clear(); diff --git a/src/views/draganddrophelper.h b/src/views/draganddrophelper.h index 656cefe1be..0eee34a3d8 100644 --- a/src/views/draganddrophelper.h +++ b/src/views/draganddrophelper.h @@ -10,6 +10,8 @@ #include "dolphin_export.h" +#include + #include #include #include @@ -40,6 +42,34 @@ public: */ static KIO::DropJob *dropUrls(const QUrl &destUrl, QDropEvent *event, QWidget *window); + /** + * Checks if the destination supports dropping. + * + * @param destUrl URL of the item destination. + * @return True if the destination is a directory and is writable, or it's a desktop file. + * False otherwise. + */ + static bool supportsDropping(const QUrl &destUrl); + + /** + * Checks if the destination supports dropping. + * + * @param destItem The item destination. + * @return True if the destination is a directory and is writable, or it's a desktop file. + * False otherwise. + */ + static bool supportsDropping(const KFileItem &destItem); + + /** + * Updates the drop action according to whether the destination supports dropping. + * If supportsDropping(destUrl), set dropAction = proposedAction. Otherwise, set + * dropAction = Qt::IgnoreAction. + * + * @param event Drop event. + * @param destUrl Destination URL. + */ + static void updateDropAction(QDropEvent *event, const QUrl &destUrl); + /** * @return True if destUrl is contained in the urls parameter. */