]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Refactor DolphinContextMenu so its actions are retrievable
authorFelix Ernst <fe.a.ernst@gmail.com>
Sat, 2 Apr 2022 17:00:58 +0000 (17:00 +0000)
committerFelix Ernst <fe.a.ernst@gmail.com>
Sat, 2 Apr 2022 17:00:58 +0000 (17:00 +0000)
This mostly red MR should have no visible effect. It is part of my work towards !273.

There are two calls necessary to open the DolphinContextMenu:
One to construct it and one to execute/show it.

Before this commit, the actual populating of the ContextMenu was
done on execute. This meant that the actions of the ContextMenu
couldn't be looked at or changed without first showing the Menu
to the user. It also meant that the construction itself didn't
actually do much constructing/populating at all which might seem
a bit unintuitive.

This commit changes this behaviour so the DolphinContextMenu is
actually populated fully on construction. The executing/showing of
the ContextMenu now does just that and nothing more.

Previously, some actions in the context menu were actually not
wired up to anything and instead the DolphinContextMenu or the
DolphinMainWindow executed some code after the user had clicked
such a dummy action from the ContextMenu. Now all the actions are
properly constructed beforehand and no special handling is
necessary when the ContextMenu hides itself.

This commit removes the pos parameter from the DolphinContextMenu
constructor. This parameter contained the position where the Menu
would be shown later. This information isn't necessary to have on
construction and was already part of the exec(pos) call in the
first place. The variable m_pos that stored the value is removed.

This commit also removes a "customActions" functionality that can
supposedly be used to add further custom actions to the
DolphinContextMenu but this functionality isn't ever used
anywhere so its usefulness is questionable. It also wouldn't be
difficult to re-add this functionality if it was ever required for
something.

This commit also addresses an old TODO in dolphinpart.cpp that
asked for the calls for opening the DolphinContextMenu to actually
contain the information for which items the DolphinContextMenu is
supposed to be constructed. Before this, only the item that was
directly clicked was transmitted and then DolphinContextMenu
retrieved the currently selected set of items by itself.
It makes more sense that DolphinContextMenu would be informed on
construction which items it is supposed to show context actions
for.

Most of this is necessary so we are able to show the contextual
actions anywhere else than in the ContextMenu in the future.

I am targeting 22.08 with this MR because it makes no sense to merge a refactor for the upcoming release already.

src/dolphincontextmenu.cpp
src/dolphincontextmenu.h
src/dolphinmainwindow.cpp
src/dolphinmainwindow.h
src/dolphinpart.cpp
src/dolphinpart.h
src/views/dolphinview.cpp
src/views/dolphinview.h

index 340af6bd0223be8e0c68e81725cda0719cd62c4c..65d84106067b2b7123ac80d09d7e23e65297fc27 100644 (file)
@@ -13,6 +13,7 @@
 #include "dolphinplacesmodelsingleton.h"
 #include "dolphinremoveaction.h"
 #include "dolphinviewcontainer.h"
+#include "global.h"
 #include "trash/dolphintrash.h"
 #include "views/dolphinview.h"
 #include "views/viewmodecontroller.h"
 #include <QMimeDatabase>
 
 DolphinContextMenu::DolphinContextMenu(DolphinMainWindow* parent,
-                                       const QPoint& pos,
                                        const KFileItem& fileInfo,
+                                       const KFileItemList &selectedItems,
                                        const QUrl& baseUrl,
                                        KFileItemActions *fileItemActions) :
     QMenu(parent),
-    m_pos(pos),
     m_mainWindow(parent),
     m_fileInfo(fileInfo),
     m_baseUrl(baseUrl),
     m_baseFileItem(nullptr),
-    m_selectedItems(),
+    m_selectedItems(selectedItems),
     m_selectedItemsProperties(nullptr),
     m_context(NoContext),
     m_copyToMenu(parent),
-    m_customActions(),
-    m_command(None),
     m_removeAction(nullptr),
     m_fileItemActions(fileItemActions)
 {
-    // The context menu either accesses the URLs of the selected items
-    // or the items itself. To increase the performance both lists are cached.
-    const DolphinView* view = m_mainWindow->activeViewContainer()->view();
-    m_selectedItems = view->selectedItems();
-
     QApplication::instance()->installEventFilter(this);
 
-    static_cast<KHamburgerMenu *>(m_mainWindow->actionCollection()->
-                action(QStringLiteral("hamburger_menu")))->addToMenu(this);
+    addAllActions();
 }
 
 DolphinContextMenu::~DolphinContextMenu()
@@ -76,13 +68,11 @@ DolphinContextMenu::~DolphinContextMenu()
     m_selectedItemsProperties = nullptr;
 }
 
-void DolphinContextMenu::setCustomActions(const QList<QAction*>& actions)
+void DolphinContextMenu::addAllActions()
 {
-    m_customActions = actions;
-}
+    static_cast<KHamburgerMenu *>(m_mainWindow->actionCollection()->
+                action(QStringLiteral("hamburger_menu")))->addToMenu(this);
 
-DolphinContextMenu::Command DolphinContextMenu::open()
-{
     // get the context information
     const auto scheme = m_baseUrl.scheme();
     if (scheme == QLatin1String("trash")) {
@@ -101,17 +91,15 @@ DolphinContextMenu::Command DolphinContextMenu::open()
     // open the corresponding popup for the context
     if (m_context & TrashContext) {
         if (m_context & ItemContext) {
-            openTrashItemContextMenu();
+            addTrashItemContextMenu();
         } else {
-            openTrashContextMenu();
+            addTrashContextMenu();
         }
     } else if (m_context & ItemContext) {
-        openItemContextMenu();
+        addItemContextMenu();
     } else {
-        openViewportContextMenu();
+        addViewportContextMenu();
     }
-
-    return m_command;
 }
 
 bool DolphinContextMenu::eventFilter(QObject* object, QEvent* event)
@@ -133,39 +121,25 @@ bool DolphinContextMenu::eventFilter(QObject* object, QEvent* event)
     return false;
 }
 
-void DolphinContextMenu::openTrashContextMenu()
+void DolphinContextMenu::addTrashContextMenu()
 {
     Q_ASSERT(m_context & TrashContext);
 
-    QAction* emptyTrashAction = new QAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), this);
+    QAction *emptyTrashAction = addAction(QIcon::fromTheme(QStringLiteral("trash-empty")), i18nc("@action:inmenu", "Empty Trash"), [this](){
+        Trash::empty(m_mainWindow);
+    });
     emptyTrashAction->setEnabled(!Trash::isEmpty());
-    addAction(emptyTrashAction);
-
-    addCustomActions();
 
     QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
     addAction(propertiesAction);
-
-    if (exec(m_pos) == emptyTrashAction) {
-        Trash::empty(m_mainWindow);
-    }
 }
 
-void DolphinContextMenu::openTrashItemContextMenu()
+void DolphinContextMenu::addTrashItemContextMenu()
 {
     Q_ASSERT(m_context & TrashContext);
     Q_ASSERT(m_context & ItemContext);
 
-    QAction* restoreAction = new QAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), m_mainWindow);
-    addAction(restoreAction);
-
-    QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
-    addAction(deleteAction);
-
-    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
-    addAction(propertiesAction);
-
-    if (exec(m_pos) == restoreAction) {
+    addAction(QIcon::fromTheme("restoration"), i18nc("@action:inmenu", "Restore"), [this](){
         QList<QUrl> selectedUrls;
         selectedUrls.reserve(m_selectedItems.count());
         for (const KFileItem &item : qAsConst(m_selectedItems)) {
@@ -175,7 +149,13 @@ void DolphinContextMenu::openTrashItemContextMenu()
         KIO::RestoreJob *job = KIO::restoreFromTrash(selectedUrls);
         KJobWidgets::setWindow(job, m_mainWindow);
         job->uiDelegate()->setAutoErrorHandlingEnabled(true);
-    }
+    });
+
+    QAction* deleteAction = m_mainWindow->actionCollection()->action(KStandardAction::name(KStandardAction::DeleteFile));
+    addAction(deleteAction);
+
+    QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
+    addAction(propertiesAction);
 }
 
 void DolphinContextMenu::addDirectoryItemContextMenu()
@@ -194,7 +174,6 @@ void DolphinContextMenu::addDirectoryItemContextMenu()
 
     // set up 'Create New' menu
     DolphinNewFileMenu* newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow);
-    const DolphinView* view = m_mainWindow->activeViewContainer()->view();
     newFileMenu->checkUpToDate();
     newFileMenu->setPopupFiles(QList<QUrl>() << m_fileInfo.url());
     newFileMenu->setEnabled(selectedItemsProps.supportsWriting());
@@ -209,16 +188,12 @@ void DolphinContextMenu::addDirectoryItemContextMenu()
     addSeparator();
 }
 
-void DolphinContextMenu::openItemContextMenu()
+void DolphinContextMenu::addItemContextMenu()
 {
     Q_ASSERT(!m_fileInfo.isNull());
 
-    QAction* openParentAction = nullptr;
-    QAction* openParentInNewWindowAction = nullptr;
-    QAction* openParentInNewTabAction = nullptr;
     const KFileItemListProperties& selectedItemsProps = selectedItemsProperties();
 
-
     m_fileItemActions->setItemListProperties(selectedItemsProps);
 
     if (m_selectedItems.count() == 1) {
@@ -228,23 +203,28 @@ void DolphinContextMenu::openItemContextMenu()
         } else if (m_context & TimelineContext || m_context & SearchContext) {
             addOpenWithActions();
 
-            openParentAction = new QAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
+            addAction(QIcon::fromTheme(QStringLiteral("document-open-folder")),
                                            i18nc("@action:inmenu",
                                                  "Open Path"),
-                                           this);
-            addAction(openParentAction);
+                                           [this](){
+                m_mainWindow->changeUrl(KIO::upUrl(m_fileInfo.url()));
+                m_mainWindow->activeViewContainer()->view()->markUrlsAsSelected({m_fileInfo.url()});
+                m_mainWindow->activeViewContainer()->view()->markUrlAsCurrent(m_fileInfo.url());
+            });
 
-            openParentInNewWindowAction = new QAction(QIcon::fromTheme(QStringLiteral("window-new")),
+            addAction(QIcon::fromTheme(QStringLiteral("window-new")),
                                                     i18nc("@action:inmenu",
                                                           "Open Path in New Window"),
-                                                    this);
-            addAction(openParentInNewWindowAction);
+                                                    [this](){
+                Dolphin::openNewWindow({m_fileInfo.url()}, m_mainWindow, Dolphin::OpenNewWindowFlag::Select);
+            });
 
-            openParentInNewTabAction = new QAction(QIcon::fromTheme(QStringLiteral("tab-new")),
+            addAction(QIcon::fromTheme(QStringLiteral("tab-new")),
                                                    i18nc("@action:inmenu",
                                                          "Open Path in New Tab"),
-                                                   this);
-            addAction(openParentInNewTabAction);
+                                                   [this](){
+                m_mainWindow->openNewTab(KIO::upUrl(m_fileInfo.url()));
+            });
 
             addSeparator();
         } else {
@@ -290,23 +270,10 @@ void DolphinContextMenu::openItemContextMenu()
     addSeparator();
     QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
     addAction(propertiesAction);
-
-    QAction* activatedAction = exec(m_pos);
-    if (activatedAction) {
-        if (activatedAction == openParentAction) {
-            m_command = OpenParentFolder;
-        } else if (activatedAction == openParentInNewWindowAction) {
-            m_command = OpenParentFolderInNewWindow;
-        } else if (activatedAction == openParentInNewTabAction) {
-            m_command = OpenParentFolderInNewTab;
-        }
-    }
 }
 
-void DolphinContextMenu::openViewportContextMenu()
+void DolphinContextMenu::addViewportContextMenu()
 {
-    const DolphinView* view = m_mainWindow->activeViewContainer()->view();
-
     const KFileItemListProperties baseUrlProperties(KFileItemList() << baseFileItem());
     m_fileItemActions->setItemListProperties(baseUrlProperties);
 
@@ -344,14 +311,11 @@ void DolphinContextMenu::openViewportContextMenu()
     }
 
     addAdditionalActions(baseUrlProperties);
-    addCustomActions();
 
     addSeparator();
 
     QAction* propertiesAction = m_mainWindow->actionCollection()->action(QStringLiteral("properties"));
     addAction(propertiesAction);
-
-    exec(m_pos);
 }
 
 void DolphinContextMenu::insertDefaultItemActions(const KFileItemListProperties& properties)
@@ -476,11 +440,6 @@ void DolphinContextMenu::addOpenWithActions()
     m_fileItemActions->insertOpenWithActionsTo(nullptr, this, QStringList{qApp->desktopFileName()});
 }
 
-void DolphinContextMenu::addCustomActions()
-{
-    addActions(m_customActions);
-}
-
 void DolphinContextMenu::addAdditionalActions(const KFileItemListProperties &props)
 {
     addSeparator();
index e033fca6e1ac41eddfb01d42678d6e41cf0beb61..627a6e3b84838e87011c66742f6a6847010b0cc6 100644 (file)
@@ -37,60 +37,42 @@ class DolphinContextMenu : public QMenu
     Q_OBJECT
 
 public:
-    enum Command
-    {
-        None,
-        OpenParentFolder,
-        OpenParentFolderInNewWindow,
-        OpenParentFolderInNewTab
-    };
-
     /**
      * @parent        Pointer to the main window the context menu
      *                belongs to.
-     * @pos           Position in screen coordinates.
      * @fileInfo      Pointer to the file item the context menu
      *                is applied. If 0 is passed, the context menu
      *                is above the viewport.
+     * @selectedItems The selected items for which the context menu
+     *                is opened. This list generally includes \a fileInfo.
      * @baseUrl       Base URL of the viewport where the context menu
      *                should be opened.
      */
     DolphinContextMenu(DolphinMainWindow* parent,
-                       const QPoint& pos,
                        const KFileItem& fileInfo,
+                       const KFileItemList &selectedItems,
                        const QUrl& baseUrl,
                        KFileItemActions *fileItemActions);
 
     ~DolphinContextMenu() override;
 
-    void setCustomActions(const QList<QAction*>& actions);
-
-    /**
-     * Opens the context menu model and returns the requested
-     * command, that should be triggered by the caller. If
-     * Command::None has been returned, either the context-menu
-     * had been closed without executing an action or an
-     * already available action from the main-window has been
-     * executed.
-     */
-    Command open();
-
 protected:
     bool eventFilter(QObject* object, QEvent* event) override;
 
 private:
-    void openTrashContextMenu();
-    void openTrashItemContextMenu();
-    void openItemContextMenu();
-    void openViewportContextMenu();
-
-    void insertDefaultItemActions(const KFileItemListProperties&);
-
     /**
-     * Adds the "Show menubar" action to the menu if the
-     * menubar is hidden.
+     * Adds all the actions and menus to this menu based on all given information.
+     * This method calls the other helper methods for adding actions
+     * based on the context given in the constructor.
      */
-    void addShowMenuBarAction();
+    void addAllActions();
+
+    void addTrashContextMenu();
+    void addTrashItemContextMenu();
+    void addItemContextMenu();
+    void addViewportContextMenu();
+
+    void insertDefaultItemActions(const KFileItemListProperties&);
 
     bool placeExists(const QUrl& url) const;
 
@@ -108,18 +90,12 @@ private:
      */
     void addOpenWithActions();
 
-    /**
-     * Adds custom actions e.g. like the "[x] Expandable Folders"-action
-     * provided in the details view.
-     */
-    void addCustomActions();
-
-private:
     /**
      * Add services, custom actions, plugins and version control items to the menu
      */
     void addAdditionalActions(const KFileItemListProperties &props);
 
+private:
     struct Entry
     {
         int type;
@@ -139,7 +115,6 @@ private:
         SearchContext = 8,
     };
 
-    QPoint m_pos;
     DolphinMainWindow* m_mainWindow;
 
     KFileItem m_fileInfo;
@@ -152,9 +127,6 @@ private:
 
     int m_context;
     KFileCopyToMenu m_copyToMenu;
-    QList<QAction*> m_customActions;
-
-    Command m_command;
 
     DolphinRemoveAction* m_removeAction; // Action that represents either 'Move To Trash' or 'Delete'
     void addDirectoryItemContextMenu();
index d244c4a7c1c7d65765ae51afb6f69ac47883fce8..589c2c185b5fc1cbc9fb76eacb8bef3e0749619d 100644 (file)
@@ -1178,32 +1178,11 @@ void DolphinMainWindow::slotWriteStateChanged(bool isFolderWritable)
 
 void DolphinMainWindow::openContextMenu(const QPoint& pos,
                                         const KFileItem& item,
-                                        const QUrl& url,
-                                        const QList<QAction*>& customActions)
+                                        const KFileItemList &selectedItems,
+                                        const QUrl& url)
 {
-    QPointer<DolphinContextMenu> contextMenu = new DolphinContextMenu(this, pos, item, url, &m_fileItemActions);
-    contextMenu.data()->setCustomActions(customActions);
-    const DolphinContextMenu::Command command = contextMenu.data()->open();
-
-    switch (command) {
-    case DolphinContextMenu::OpenParentFolder:
-        changeUrl(KIO::upUrl(item.url()));
-        m_activeViewContainer->view()->markUrlsAsSelected({item.url()});
-        m_activeViewContainer->view()->markUrlAsCurrent(item.url());
-        break;
-
-    case DolphinContextMenu::OpenParentFolderInNewWindow:
-        Dolphin::openNewWindow({item.url()}, this, Dolphin::OpenNewWindowFlag::Select);
-        break;
-
-    case DolphinContextMenu::OpenParentFolderInNewTab:
-        openNewTab(KIO::upUrl(item.url()));
-        break;
-
-    case DolphinContextMenu::None:
-    default:
-        break;
-    }
+    QPointer<DolphinContextMenu> contextMenu = new DolphinContextMenu(this, item, selectedItems, url, &m_fileItemActions);
+    contextMenu.data()->exec(pos);
 
     // Delete the menu, unless it has been deleted in its own nested event loop already.
     if (contextMenu) {
index fbe926cefb479b9f06ac6ceb18cdc0f9750cf4d3..17327f2de9f3bbafd6027b9b89b99f34eef19e24 100644 (file)
@@ -475,14 +475,11 @@ private Q_SLOTS:
      * @pos           Position in screen coordinates.
      * @item          File item context. If item is null, the context menu
      *                should be applied to \a url.
+     * @selectedItems The selected items for which the context menu
+     *                is opened. This list generally includes \a item.
      * @url           URL which contains \a item.
-     * @customActions Actions that should be added to the context menu,
-     *                if the file item is null.
      */
-    void openContextMenu(const QPoint& pos,
-                         const KFileItem& item,
-                         const QUrl& url,
-                         const QList<QAction*>& customActions);
+    void openContextMenu(const QPoint& pos, const KFileItem& item, const KFileItemList &selectedItems, const QUrl& url);
 
     /**
      * Updates the menu that is by default at the right end of the toolbar.
index 0595087784a33ea37b75623688eb34c91567008c..0f10a7769fe2513688f4da5b9ca0869d81bf9dfa 100644 (file)
@@ -385,8 +385,8 @@ void DolphinPart::createNewWindow(const QUrl& url)
 
 void DolphinPart::slotOpenContextMenu(const QPoint& pos,
                                       const KFileItem& _item,
-                                      const QUrl &,
-                                      const QList<QAction*>& customActions)
+                                      const KFileItemList &selectedItems,
+                                      const QUrl &)
 {
     KParts::BrowserExtension::PopupFlags popupFlags = KParts::BrowserExtension::DefaultPopupItems
                                                       | KParts::BrowserExtension::ShowProperties
@@ -402,13 +402,11 @@ void DolphinPart::slotOpenContextMenu(const QPoint& pos,
             item.setUrl(url()); // ensure we use the view url, not the canonical path (#213799)
     }
 
-    // TODO: We should change the signature of the slots (and signals) for being able
-    //       to tell for which items we want a popup.
     KFileItemList items;
-    if (m_view->selectedItems().isEmpty()) {
+    if (selectedItems.isEmpty()) {
         items.append(item);
     } else {
-        items = m_view->selectedItems();
+        items = selectedItems;
     }
 
     KFileItemListProperties capabilities(items);
@@ -416,7 +414,6 @@ void DolphinPart::slotOpenContextMenu(const QPoint& pos,
     KParts::BrowserExtension::ActionGroupMap actionGroups;
     QList<QAction *> editActions;
     editActions += m_view->versionControlActions(m_view->selectedItems());
-    editActions += customActions;
 
     if (!_item.isNull()) { // only for context menu on one or more items
         const bool supportsMoving = capabilities.supportsMoving();
index 25d76950c0c1b50f57decdeeb07ec70dd8100671..a49603ca877fc2c5bd27ab27a1609953d3cf9ca1 100644 (file)
@@ -123,14 +123,11 @@ private Q_SLOTS:
      * @pos           Position in screen coordinates.
      * @item          File item context. If item is null, the context menu
      *                should be applied to \a url.
+     * @selectedItems The selected items for which the context menu
+     *                is opened. This list generally includes \a item.
      * @url           URL which contains \a item.
-     * @customActions Actions that should be added to the context menu,
-     *                if the file item is null.
      */
-    void slotOpenContextMenu(const QPoint& pos,
-                             const KFileItem& item,
-                             const QUrl& url,
-                             const QList<QAction*>& customActions);
+    void slotOpenContextMenu(const QPoint &pos, const KFileItem &_item, const KFileItemList &selectedItems, const QUrl &);
 
     /**
      * Informs the host that we are opening \a url (e.g. after a redirection
index 5646fa9828ccabdcad33ffe746f682a58d2cb8bd..e6aecff80bad89fafc8909d89e9852061cc9997a 100644 (file)
@@ -1058,12 +1058,12 @@ void DolphinView::slotItemContextMenuRequested(int index, const QPointF& pos)
     }
 
     const KFileItem item = m_model->fileItem(index);
-    Q_EMIT requestContextMenu(pos.toPoint(), item, url(), QList<QAction*>());
+    Q_EMIT requestContextMenu(pos.toPoint(), item, selectedItems(), url());
 }
 
 void DolphinView::slotViewContextMenuRequested(const QPointF& pos)
 {
-    Q_EMIT requestContextMenu(pos.toPoint(), KFileItem(), url(), QList<QAction*>());
+    Q_EMIT requestContextMenu(pos.toPoint(), KFileItem(), selectedItems(), url());
 }
 
 void DolphinView::slotHeaderContextMenuRequested(const QPointF& pos)
index e93ca4fa026bebe041ad80791ad55d8d690ffdcf..b40be89367e12a1bc2f145a2ba7a2afc04f02b5f 100644 (file)
@@ -510,13 +510,12 @@ Q_SIGNALS:
     /**
      * Is emitted if a context menu is requested for the item \a item,
      * which is part of \a url. If the item is null, the context menu
-     * for the URL should be shown and the custom actions \a customActions
-     * will be added.
+     * for the URL should be shown.
      */
     void requestContextMenu(const QPoint& pos,
                             const KFileItem& item,
-                            const QUrl& url,
-                            const QList<QAction*>& customActions);
+                            const KFileItemList &selectedItems,
+                            const QUrl& url);
 
     /**
      * Is emitted if an information message with the content \a msg