From b5b6762b15564919b32b33cde9961a75a2e3de22 Mon Sep 17 00:00:00 2001 From: Jakob Petsovits Date: Fri, 12 May 2023 18:13:39 -0400 Subject: [PATCH] Bring back the "Create New" menu in the menu bar MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit This commit is the result of a three-way diff that combines my own initial patch with related changes from Felix Ernst's MR !545 and further suggestions by Méven Car. Fixes DolphinMainWindowTest::testNewFileMenuEnabled(). Tests are now passing again for my build. "Create New" was broken by commit c64059bd which switched to the new, non-deprecated KNewFileMenu constructor (kio commit 89bc6bad) that doesn't add itself to the passed KActionCollection parameter. After the switch, hamburger menu and context menu was still working as intended but the menu bar was missing the "Create New" menu. This commit adds the addAction("new_menu") call to the File menu setup that would have previously been called by the deprecated KNewFileMenu constructor. The corresponding test can now find the QObject for the menu's named action again, verifying its existence and enabled-ness like it did before. The DolphinNewFileMenu constructor's unused actionCollection parameter serves no use anymore except to confuse people. We replace it with a single QAction* parameter, createDirAction, which gets passed to setNewFolderShortcutAction(). 2 out of 3 constructor call sites have access to this action, while the remaining call site in dolphinmainwindow.cpp must wait until after it has been initialized by DolphinViewActionHandler. In this case, setNewFolderShortcutAction() is still called manually at a later time. --- src/dolphincontextmenu.cpp | 3 +-- src/dolphinmainwindow.cpp | 6 ++++-- src/dolphinnewfilemenu.cpp | 7 ++++--- src/dolphinnewfilemenu.h | 2 +- src/dolphinpart.cpp | 3 +-- 5 files changed, 11 insertions(+), 10 deletions(-) diff --git a/src/dolphincontextmenu.cpp b/src/dolphincontextmenu.cpp index 3519d416a..62990cab2 100644 --- a/src/dolphincontextmenu.cpp +++ b/src/dolphincontextmenu.cpp @@ -168,8 +168,7 @@ void DolphinContextMenu::addDirectoryItemContextMenu() addOpenWithActions(); // set up 'Create New' menu - DolphinNewFileMenu *newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection(), m_mainWindow); - newFileMenu->setNewFolderShortcutAction(m_mainWindow->actionCollection()->action("create_dir")); + DolphinNewFileMenu *newFileMenu = new DolphinNewFileMenu(m_mainWindow->actionCollection()->action(QStringLiteral("create_dir")), m_mainWindow); newFileMenu->checkUpToDate(); newFileMenu->setWorkingDirectory(m_fileInfo.url()); newFileMenu->setEnabled(selectedItemsProps.supportsWriting()); diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index 80dcff1c3..ef8f9770f 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -159,7 +159,8 @@ DolphinMainWindow::DolphinMainWindow() connect(m_actionHandler, &DolphinViewActionHandler::createDirectoryTriggered, this, &DolphinMainWindow::createDirectory); connect(m_actionHandler, &DolphinViewActionHandler::selectionModeChangeTriggered, this, &DolphinMainWindow::slotSetSelectionMode); - m_newFileMenu->setNewFolderShortcutAction(actionCollection()->action("create_dir")); + Q_CHECK_PTR(actionCollection()->action(QStringLiteral("create_dir"))); + m_newFileMenu->setNewFolderShortcutAction(actionCollection()->action(QStringLiteral("create_dir"))); m_remoteEncoding = new DolphinRemoteEncoding(this, m_actionHandler); connect(this, &DolphinMainWindow::urlChanged, m_remoteEncoding, &DolphinRemoteEncoding::slotAboutToOpenUrl); @@ -1551,7 +1552,8 @@ void DolphinMainWindow::setupActions() auto hamburgerMenuAction = KStandardAction::hamburgerMenu(nullptr, nullptr, actionCollection()); // setup 'File' menu - m_newFileMenu = new DolphinNewFileMenu(actionCollection(), this); + m_newFileMenu = new DolphinNewFileMenu(nullptr, this); + actionCollection()->addAction(QStringLiteral("new_menu"), m_newFileMenu); QMenu *menu = m_newFileMenu->menu(); menu->setTitle(i18nc("@title:menu Create new folder, file, link, etc.", "Create New")); menu->setIcon(QIcon::fromTheme(QStringLiteral("list-add"))); diff --git a/src/dolphinnewfilemenu.cpp b/src/dolphinnewfilemenu.cpp index 55c11bc2f..d499ae42c 100644 --- a/src/dolphinnewfilemenu.cpp +++ b/src/dolphinnewfilemenu.cpp @@ -8,13 +8,14 @@ #include "views/dolphinnewfilemenuobserver.h" -#include #include -DolphinNewFileMenu::DolphinNewFileMenu(KActionCollection *collection, QObject *parent) +#include + +DolphinNewFileMenu::DolphinNewFileMenu(QAction *createDirAction, QObject *parent) : KNewFileMenu(parent) { - Q_UNUSED(collection) + setNewFolderShortcutAction(createDirAction); DolphinNewFileMenuObserver::instance().attach(this); } diff --git a/src/dolphinnewfilemenu.h b/src/dolphinnewfilemenu.h index 4fa173ef9..5538c9265 100644 --- a/src/dolphinnewfilemenu.h +++ b/src/dolphinnewfilemenu.h @@ -25,7 +25,7 @@ class DOLPHIN_EXPORT DolphinNewFileMenu : public KNewFileMenu Q_OBJECT public: - DolphinNewFileMenu(KActionCollection *collection, QObject *parent); + DolphinNewFileMenu(QAction *createDirAction, QObject *parent); ~DolphinNewFileMenu() override; Q_SIGNALS: diff --git a/src/dolphinpart.cpp b/src/dolphinpart.cpp index c3447ad6e..46b5e7e94 100644 --- a/src/dolphinpart.cpp +++ b/src/dolphinpart.cpp @@ -141,9 +141,8 @@ void DolphinPart::createActions() { // Edit menu - m_newFileMenu = new DolphinNewFileMenu(actionCollection(), this); + m_newFileMenu = new DolphinNewFileMenu(actionCollection()->action(QStringLiteral("create_dir")), this); m_newFileMenu->setParentWidget(widget()); - m_newFileMenu->setNewFolderShortcutAction(actionCollection()->action("create_dir")); connect(m_newFileMenu->menu(), &QMenu::aboutToShow, this, &DolphinPart::updateNewMenu); QAction *editMimeTypeAction = actionCollection()->addAction(QStringLiteral("editMimeType")); -- 2.47.3