]> cloud.milkyroute.net Git - dolphin.git/blobdiff - src/dolphincontextmenu.cpp
Merge branch 'release/20.12'
[dolphin.git] / src / dolphincontextmenu.cpp
index 34d3127dce7d93618ad5cfc0f40e8cd858b7100f..eabd81e225080999c57e7328f9e0a9fc717c8aff 100644 (file)
@@ -61,6 +61,8 @@ DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
     // or the items itself. To increase the performance both lists are cached.
     const DolphinView* view = m_mainWindow->activeViewContainer()->view();
     m_selectedItems = view->selectedItems();
+
+    installEventFilter(this);
 }
 
 DolphinContextMenu::~DolphinContextMenu()
@@ -103,27 +105,34 @@ DolphinContextMenu::Command DolphinContextMenu::open()
     } else if (m_context & ItemContext) {
         openItemContextMenu();
     } else {
-        Q_ASSERT(m_context == NoContext);
         openViewportContextMenu();
     }
 
     return m_command;
 }
 
-void DolphinContextMenu::keyPressEvent(QKeyEvent *ev)
+void DolphinContextMenu::childEvent(QChildEvent* event)
 {
-    if (m_removeAction && ev->key() == Qt::Key_Shift) {
-        m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed);
+    if(event->added()) {
+        event->child()->installEventFilter(this);
     }
-    QMenu::keyPressEvent(ev);
+    QMenu::childEvent(event);
 }
 
-void DolphinContextMenu::keyReleaseEvent(QKeyEvent *ev)
+bool DolphinContextMenu::eventFilter(QObject* dest, QEvent* event)
 {
-    if (m_removeAction && ev->key() == Qt::Key_Shift) {
-        m_removeAction->update(DolphinRemoveAction::ShiftState::Released);
+    if(event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
+        QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
+        if(m_removeAction && keyEvent->key() == Qt::Key_Shift) {
+            if(event->type() == QEvent::KeyPress) {
+                m_removeAction->update(DolphinRemoveAction::ShiftState::Pressed);
+            } else {
+                m_removeAction->update(DolphinRemoveAction::ShiftState::Released);
+            }
+            return true;
+        }
     }
-    QMenu::keyReleaseEvent(ev);
+    return QMenu::eventFilter(dest, event);
 }
 
 void DolphinContextMenu::openTrashContextMenu()
@@ -198,6 +207,7 @@ void DolphinContextMenu::addDirectoryItemContextMenu(KFileItemActions &fileItemA
      QMenu* menu = newFileMenu->menu();
      menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New"));
      menu->setIcon(QIcon::fromTheme(QStringLiteral("document-new")));
+     menu->setParent(this, Qt::Popup);
      addMenu(menu);
 
      addSeparator();
@@ -213,7 +223,7 @@ void DolphinContextMenu::openItemContextMenu()
     const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
 
     KFileItemActions fileItemActions;
-    fileItemActions.setParentWidget(m_mainWindow);
+    fileItemActions.setParentWidget(this);
     fileItemActions.setItemListProperties(selectedItemsProps);
 
     if (m_selectedItems.count() == 1) {
@@ -271,12 +281,7 @@ void DolphinContextMenu::openItemContextMenu()
 
     insertDefaultItemActions(selectedItemsProps);
 
-    addSeparator();
-
-    fileItemActions.addServiceActionsTo(this);
-    fileItemActions.addPluginActionsTo(this);
-
-    addVersionControlPluginActions();
+    addAdditionalActions(fileItemActions, selectedItemsProps);
 
     // insert 'Copy To' and 'Move To' sub menus
     if (GeneralSettings::showCopyMoveMenu()) {
@@ -338,14 +343,7 @@ void DolphinContextMenu::openViewportContextMenu()
     addAction(m_mainWindow->actionCollection()->action(QStringLiteral("sort")));
     addAction(m_mainWindow->actionCollection()->action(QStringLiteral("view_mode")));
 
-    addSeparator();
-
-    // Insert service actions
-    fileItemActions.addServiceActionsTo(this);
-    fileItemActions.addPluginActionsTo(this);
-
-    addVersionControlPluginActions();
-
+    addAdditionalActions(fileItemActions, baseUrlProperties);
     addCustomActions();
 
     addSeparator();
@@ -433,23 +431,23 @@ QAction* DolphinContextMenu::createPasteAction()
 {
     QAction* action = nullptr;
     KFileItem destItem;
-    if (!m_fileInfo.isNull()) {
+    if (!m_fileInfo.isNull() && m_selectedItems.count() <= 1) {
         destItem = m_fileInfo;
     } else {
         destItem = baseFileItem();
     }
 
     if (!destItem.isNull() && destItem.isDir()) {
-        if (m_selectedItems.count() <= 1) {
-            const QMimeData *mimeData = QApplication::clipboard()->mimeData();
-            bool canPaste;
-            const QString text = KIO::pasteActionText(mimeData, &canPaste, destItem);
-            action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
-            if (canPaste) {
+        const QMimeData *mimeData = QApplication::clipboard()->mimeData();
+        bool canPaste;
+        const QString text = KIO::pasteActionText(mimeData, &canPaste, destItem);
+        if (canPaste) {
+            if (destItem == m_fileInfo) {
+                // if paste destination is a selected folder
+                action = new QAction(QIcon::fromTheme(QStringLiteral("edit-paste")), text, this);
                 connect(action, &QAction::triggered, m_mainWindow, &DolphinMainWindow::pasteIntoFolder);
             } else {
-                // don't add the unavailable action
-                action = nullptr;
+                action = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::Paste));
             }
         }
     }
@@ -485,8 +483,21 @@ void DolphinContextMenu::addOpenWithActions(KFileItemActions& fileItemActions)
     fileItemActions.addOpenWithActionsTo(this, QStringLiteral("DesktopEntryName != '%1'").arg(qApp->desktopFileName()));
 }
 
-void DolphinContextMenu::addVersionControlPluginActions()
+void DolphinContextMenu::addCustomActions()
+{
+    addActions(m_customActions);
+}
+
+void DolphinContextMenu::addAdditionalActions(KFileItemActions &fileItemActions, const KFileItemListProperties &props)
 {
+    addSeparator();
+
+    QList<QAction *> additionalActions;
+    if (props.isDirectory() && props.isLocal()) {
+        additionalActions << m_mainWindow->actionCollection()->action(QStringLiteral("open_terminal"));
+    }
+    fileItemActions.addActionsTo(this, KFileItemActions::MenuActionSource::All, additionalActions);
+
     const DolphinView* view = m_mainWindow->activeViewContainer()->view();
     const QList<QAction*> versionControlActions = view->versionControlActions(m_selectedItems);
     if (!versionControlActions.isEmpty()) {
@@ -495,8 +506,3 @@ void DolphinContextMenu::addVersionControlPluginActions()
     }
 }
 
-void DolphinContextMenu::addCustomActions()
-{
-    addActions(m_customActions);
-}
-