diff --git a/plugins/projectmanagerview/projectmanagerviewplugin.h b/plugins/projectmanagerview/projectmanagerviewplugin.h --- a/plugins/projectmanagerview/projectmanagerviewplugin.h +++ b/plugins/projectmanagerview/projectmanagerviewplugin.h @@ -40,6 +40,7 @@ class Context; } +class QMimeData; class ProjectManagerView; class ProjectManagerViewPlugin: public KDevelop::IPlugin @@ -81,6 +82,7 @@ void createFolderFromContextMenu(); void createFileFromContextMenu(); void removeFromContextMenu(); + void cutFromContextMenu(); void removeTargetFilesFromContextMenu(); void renameItemFromContextMenu(); void updateActionState( KDevelop::Context* ctx ); @@ -91,6 +93,10 @@ QList collectItems(); QList collectAllProjects(); void runBuilderJob( KDevelop::BuilderJob::BuildType type, QList items ); + + // Returns nullptr iff the list of URLs to copy/cut was empty + QMimeData* createClipboardMimeData(bool cut); + class ProjectManagerViewPluginPrivate* const d; }; diff --git a/plugins/projectmanagerview/projectmanagerviewplugin.cpp b/plugins/projectmanagerview/projectmanagerviewplugin.cpp --- a/plugins/projectmanagerview/projectmanagerviewplugin.cpp +++ b/plugins/projectmanagerview/projectmanagerviewplugin.cpp @@ -32,6 +32,9 @@ #include #include #include +#include +#include +#include #include #include @@ -58,6 +61,13 @@ Q_LOGGING_CATEGORY(PLUGIN_PROJECTMANAGERVIEW, "kdevplatform.plugins.projectmanagerview") K_PLUGIN_FACTORY_WITH_JSON(ProjectManagerFactory, "kdevprojectmanagerview.json", registerPlugin();) +static QAction* createSeparatorAction() +{ + QAction* separator = new QAction(nullptr); + separator->setSeparator(true); + return separator; +} + class KDevProjectManagerViewFactory: public KDevelop::IToolViewFactory { public: @@ -212,7 +222,7 @@ bool needsCloseProjects = true; bool needsBuildItems = true; bool needsFolderItems = true; - bool needsRemoveAndRename = true; + bool needsCutRenameRemove = true; bool needsRemoveTargetFiles = true; bool needsPaste = true; @@ -234,7 +244,7 @@ needsFolderItems &= (bool)item->folder(); //needsRemove if items are limited to non-top-level folders or files that don't belong to targets - needsRemoveAndRename &= (item->folder() && item->parent()) || (item->file() && !item->parent()->target()); + needsCutRenameRemove &= (item->folder() && item->parent()) || (item->file() && !item->parent()->target()); //needsRemoveTargets if items are limited to file items with target parents needsRemoveTargetFiles &= (item->file() && item->parent()->target()); @@ -284,11 +294,37 @@ connect( action, &QAction::triggered, this, &ProjectManagerViewPlugin::reloadFromContextMenu ); menuExt.addAction( ContextMenuExtension::FileGroup, action ); } - if ( needsRemoveAndRename ) { + + // Populating cut/copy/paste group + if (!menuExt.actions(ContextMenuExtension::FileGroup).isEmpty()) { + menuExt.addAction(ContextMenuExtension::FileGroup, createSeparatorAction()); + } + if (needsCutRenameRemove) { + QAction* cut = KStandardAction::cut(this, SLOT(cutFromContextMenu()), this); + cut->setShortcutContext(Qt::WidgetShortcut); + menuExt.addAction(ContextMenuExtension::FileGroup, cut); + } + { + QAction* copy = KStandardAction::copy(this, SLOT(copyFromContextMenu()), this); + copy->setShortcutContext(Qt::WidgetShortcut); + menuExt.addAction( ContextMenuExtension::FileGroup, copy ); + } + if (needsPaste) { + QAction* paste = KStandardAction::paste(this, SLOT(pasteFromContextMenu()), this); + paste->setShortcutContext(Qt::WidgetShortcut); + menuExt.addAction( ContextMenuExtension::FileGroup, paste ); + } + + // Populating rename/remove group + { + menuExt.addAction(ContextMenuExtension::FileGroup, createSeparatorAction()); + } + if (needsCutRenameRemove) { QAction* remove = new QAction( i18n( "Remove" ), this ); remove->setIcon(QIcon::fromTheme(QStringLiteral("user-trash"))); connect( remove, &QAction::triggered, this, &ProjectManagerViewPlugin::removeFromContextMenu ); menuExt.addAction( ContextMenuExtension::FileGroup, remove ); + QAction* rename = new QAction( i18n( "Rename..." ), this ); rename->setIcon(QIcon::fromTheme(QStringLiteral("edit-rename"))); connect( rename, &QAction::triggered, this, &ProjectManagerViewPlugin::renameItemFromContextMenu ); @@ -301,15 +337,8 @@ menuExt.addAction( ContextMenuExtension::FileGroup, remove ); } - { - QAction* copy = KStandardAction::copy(this, SLOT(copyFromContextMenu()), this); - copy->setShortcutContext(Qt::WidgetShortcut); - menuExt.addAction( ContextMenuExtension::FileGroup, copy ); - } - if (needsPaste) { - QAction* paste = KStandardAction::paste(this, SLOT(pasteFromContextMenu()), this); - paste->setShortcutContext(Qt::WidgetShortcut); - menuExt.addAction( ContextMenuExtension::FileGroup, paste ); + if (needsCutRenameRemove || needsRemoveTargetFiles) { + menuExt.addAction(ContextMenuExtension::FileGroup, createSeparatorAction()); } return menuExt; @@ -656,21 +685,39 @@ } } -void ProjectManagerViewPlugin::copyFromContextMenu() +QMimeData* ProjectManagerViewPlugin::createClipboardMimeData(bool cut) { - KDevelop::ProjectItemContext* ctx = dynamic_cast(ICore::self()->selectionController()->currentSelection()); + auto* ctx = dynamic_cast( + ICore::self()->selectionController()->currentSelection()); QList urls; + QList mostLocalUrls; foreach (ProjectBaseItem* item, ctx->items()) { if (item->folder() || item->file()) { - urls << item->path().toUrl(); + const QUrl& url = item->path().toUrl(); + urls << url; + mostLocalUrls << KFileItem(url).mostLocalUrl(); } } qCDebug(PLUGIN_PROJECTMANAGERVIEW) << urls; - if (!urls.isEmpty()) { - QMimeData* data = new QMimeData; - data->setUrls(urls); - qApp->clipboard()->setMimeData(data); + + if (urls.isEmpty()) { + return nullptr; } + + QMimeData* mimeData = new QMimeData; + KIO::setClipboardDataCut(mimeData, cut); + KUrlMimeData::setUrls(urls, mostLocalUrls, mimeData); + return mimeData; +} + +void ProjectManagerViewPlugin::copyFromContextMenu() +{ + qApp->clipboard()->setMimeData(createClipboardMimeData(false)); +} + +void ProjectManagerViewPlugin::cutFromContextMenu() +{ + qApp->clipboard()->setMimeData(createClipboardMimeData(true)); } void ProjectManagerViewPlugin::pasteFromContextMenu() @@ -710,6 +757,5 @@ } } - #include "projectmanagerviewplugin.moc"