[dolphin] /: [Dolphin] Open Preferred Search Tool action

Elvis Angelaccio null at kde.org
Sun Nov 17 17:23:29 GMT 2019


Git commit 537dc7864ae31e7c1c9a0a0ecf559b7cace23a82 by Elvis Angelaccio, on behalf of Piotr Henryk Dabrowski.
Committed on 17/11/2019 at 17:15.
Pushed by elvisangelaccio into branch 'master'.

[Dolphin] Open Preferred Search Tool action

Summary:
Added "Open Preferred Search Tool" action to Tools menu.

It runs preferred (topmost) external search tool as configured in the "More Search Tools" menu.

By default Ctrl+Shift+F shortcut is assigned to this action.

FEATURE: 384798
FIXED-IN: 20.03.80

{F7134238}
{F7134240}
{F7134242}

Reviewers: #dolphin, ngraham, elvisangelaccio

Reviewed By: #dolphin, ngraham

Subscribers: pkloc, kfm-devel, kde-doc-english

Tags: #dolphin, #documentation

Differential Revision: https://phabricator.kde.org/D22594

M  +11   -0    doc/index.docbook
M  +90   -9    src/dolphinmainwindow.cpp
M  +22   -0    src/dolphinmainwindow.h
M  +11   -1    src/dolphinpart.cpp
M  +1    -1    src/dolphinpart.h
M  +2    -1    src/dolphinui.rc

https://commits.kde.org/dolphin/537dc7864ae31e7c1c9a0a0ecf559b7cace23a82

diff --git a/doc/index.docbook b/doc/index.docbook
index d2c06c6a02..71ac3cfc20 100644
--- a/doc/index.docbook
+++ b/doc/index.docbook
@@ -2018,6 +2018,17 @@ for this action.</para></listitem>
 <listitem><para><action>Opens &konsole; within the current folder.</action></para></listitem>
 </varlistentry>
 
+<varlistentry>
+<term><menuchoice>
+<shortcut>
+<keycombo action="simul">&Ctrl;&Shift;<keycap>F</keycap></keycombo>
+</shortcut>
+<guimenu>Tools</guimenu>
+<guimenuitem>Open Preferred Search Tool</guimenuitem>
+</menuchoice></term>
+<listitem><para><action>Opens preferred search tool in the current folder.</action></para></listitem>
+</varlistentry>
+
 <varlistentry>
 <term><menuchoice>
 <guimenu>Tools</guimenu>
diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp
index e28b18cd3d..e615fbab8a 100644
--- a/src/dolphinmainwindow.cpp
+++ b/src/dolphinmainwindow.cpp
@@ -57,6 +57,7 @@
 #include <KJobWidgets>
 #include <KLocalizedString>
 #include <KMessageBox>
+#include <KNS3/KMoreToolsMenuFactory>
 #include <KProtocolInfo>
 #include <KProtocolManager>
 #include <KRun>
@@ -197,6 +198,8 @@ DolphinMainWindow::DolphinMainWindow() :
     toolBar()->installEventFilter(middleClickEventFilter);
 
     setupWhatsThis();
+
+    QTimer::singleShot(0, this, &DolphinMainWindow::setupUpdateOpenPreferredSearchToolAction);
 }
 
 DolphinMainWindow::~DolphinMainWindow()
@@ -933,23 +936,88 @@ void DolphinMainWindow::toggleShowMenuBar()
     }
 }
 
-void DolphinMainWindow::openTerminal()
+QString DolphinMainWindow::activeContainerLocalPath()
 {
-    QString dir(QDir::homePath());
-
-    // If the given directory is not local, it can still be the URL of an
-    // ioslave using UDS_LOCAL_PATH which to be converted first.
     KIO::StatJob* statJob = KIO::mostLocalUrl(m_activeViewContainer->url());
     KJobWidgets::setWindow(statJob, this);
     statJob->exec();
     QUrl url = statJob->mostLocalUrl();
-
-    //If the URL is local after the above conversion, set the directory.
     if (url.isLocalFile()) {
-        dir = url.toLocalFile();
+        return url.toLocalFile();
+    }
+    return QDir::homePath();
+}
+
+QPointer<QAction> DolphinMainWindow::preferredSearchTool()
+{
+    m_searchTools.clear();
+    KMoreToolsMenuFactory("dolphin/search-tools").fillMenuFromGroupingNames(
+        &m_searchTools, { "files-find" }, QUrl::fromLocalFile(activeContainerLocalPath())
+    );
+    QList<QAction*> actions = m_searchTools.actions();
+    if (actions.isEmpty()) {
+        return nullptr;
+    }
+    QAction* action = actions.first();
+    if (action->isSeparator()) {
+        return nullptr;
+    }
+    return action;
+}
+
+void DolphinMainWindow::setupUpdateOpenPreferredSearchToolAction()
+{
+    QAction* openPreferredSearchTool = actionCollection()->action(QStringLiteral("open_preferred_search_tool"));
+    const QList<QWidget*> widgets = openPreferredSearchTool->associatedWidgets();
+    for (QWidget* widget : widgets) {
+        QMenu* menu = qobject_cast<QMenu*>(widget);
+        if (menu) {
+            connect(menu, &QMenu::aboutToShow, this, &DolphinMainWindow::updateOpenPreferredSearchToolAction);
+        }
     }
 
-    KToolInvocation::invokeTerminal(QString(), dir);
+    // Update the open_preferred_search_tool action *before* the Configure Shortcuts window is shown,
+    // since this action is then listed in that window and it should be up-to-date when it is displayed.
+    // This update is instantaneous if user made no changes to the search tools in the meantime.
+    // Maybe all KStandardActions should defer calls to their slots, so that we could simply connect() to trigger()?
+    connect(
+        actionCollection()->action(KStandardAction::name(KStandardAction::KeyBindings)), &QAction::hovered,
+        this, &DolphinMainWindow::updateOpenPreferredSearchToolAction
+    );
+
+    updateOpenPreferredSearchToolAction();
+}
+
+void DolphinMainWindow::updateOpenPreferredSearchToolAction()
+{
+    QAction* openPreferredSearchTool = actionCollection()->action(QStringLiteral("open_preferred_search_tool"));
+    if (!openPreferredSearchTool) {
+        return;
+    }
+    QPointer<QAction> tool = preferredSearchTool();
+    if (tool) {
+        openPreferredSearchTool->setVisible(true);
+        openPreferredSearchTool->setText(i18nc("@action:inmenu Tools", "Open %1", tool->text()));
+        openPreferredSearchTool->setIcon(tool->icon());
+    } else {
+        openPreferredSearchTool->setVisible(false);
+        // still visible in Shortcuts configuration window
+        openPreferredSearchTool->setText(i18nc("@action:inmenu Tools", "Open Preferred Search Tool"));
+        openPreferredSearchTool->setIcon(QIcon::fromTheme(QStringLiteral("search")));
+    }
+}
+
+void DolphinMainWindow::openPreferredSearchTool()
+{
+    QPointer<QAction> tool = preferredSearchTool();
+    if (tool) {
+        tool->trigger();
+    }
+}
+
+void DolphinMainWindow::openTerminal()
+{
+    KToolInvocation::invokeTerminal(QString(), activeContainerLocalPath());
 }
 
 void DolphinMainWindow::editSettings()
@@ -1091,7 +1159,9 @@ void DolphinMainWindow::updateControlMenu()
 
     // Add a curated assortment of items from the "Tools" menu
     addActionToMenu(ac->action(QStringLiteral("show_filter_bar")), menu);
+    addActionToMenu(ac->action(QStringLiteral("open_preferred_search_tool")), menu);
     addActionToMenu(ac->action(QStringLiteral("open_terminal")), menu);
+    connect(menu, &QMenu::aboutToShow, this, &DolphinMainWindow::updateOpenPreferredSearchToolAction);
 
     menu->addSeparator();
 
@@ -1465,6 +1535,15 @@ void DolphinMainWindow::setupActions()
     compareFiles->setEnabled(false);
     connect(compareFiles, &QAction::triggered, this, &DolphinMainWindow::compareFiles);
 
+    QAction* openPreferredSearchTool = actionCollection()->addAction(QStringLiteral("open_preferred_search_tool"));
+    openPreferredSearchTool->setText(i18nc("@action:inmenu Tools", "Open Preferred Search Tool"));
+    openPreferredSearchTool->setWhatsThis(xi18nc("@info:whatsthis",
+        "<para>This opens a preferred search tool for the viewed location.</para>"
+        "<para>Use <emphasis>More Search Tools</emphasis> menu to configure it.</para>"));
+    openPreferredSearchTool->setIcon(QIcon::fromTheme(QStringLiteral("search")));
+    actionCollection()->setDefaultShortcut(openPreferredSearchTool, Qt::CTRL + Qt::SHIFT + Qt::Key_F);
+    connect(openPreferredSearchTool, &QAction::triggered, this, &DolphinMainWindow::openPreferredSearchTool);
+
 #ifdef HAVE_TERMINAL
     if (KAuthorized::authorize(QStringLiteral("shell_access"))) {
         QAction* openTerminal = actionCollection()->addAction(QStringLiteral("open_terminal"));
@@ -2207,6 +2286,8 @@ bool DolphinMainWindow::event(QEvent *event)
         QWhatsThisClickedEvent* whatsThisEvent = dynamic_cast<QWhatsThisClickedEvent*>(event);
         QDesktopServices::openUrl(QUrl(whatsThisEvent->href()));
         return true;
+    } else if (event->type() == QEvent::WindowActivate) {
+        updateOpenPreferredSearchToolAction();
     }
     return KXmlGuiWindow::event(event);
 }
diff --git a/src/dolphinmainwindow.h b/src/dolphinmainwindow.h
index 3d86340d6a..0520e1091e 100644
--- a/src/dolphinmainwindow.h
+++ b/src/dolphinmainwindow.h
@@ -30,6 +30,7 @@
 
 #include <QIcon>
 #include <QList>
+#include <QMenu>
 #include <QPointer>
 #include <QUrl>
 #include <QVector>
@@ -352,6 +353,15 @@ private slots:
      */
     void toggleShowMenuBar();
 
+    /** Sets up updates for "Open Preferred Search Tool" action. */
+    void setupUpdateOpenPreferredSearchToolAction();
+
+    /** Updates "Open Preferred Search Tool" action. */
+    void updateOpenPreferredSearchToolAction();
+
+    /** Opens preferred search tool for the current location. */
+    void openPreferredSearchTool();
+
     /** Opens a terminal window for the current location. */
     void openTerminal();
 
@@ -597,6 +607,15 @@ private:
     /** Adds "What's This?" texts to many widgets and StandardActions. */
     void setupWhatsThis();
 
+    /**
+     * Returns the KIO::StatJob::mostLocalUrl() for the active container URL
+     * if it's a local file. Otherwise returns the user's home path.
+     */
+    QString activeContainerLocalPath();
+
+    /** Returns preferred search tool as configured in "More Search Tools" menu. */
+    QPointer<QAction> preferredSearchTool();
+
 private:
     /**
      * Implements a custom error handling for the undo manager. This
@@ -633,6 +652,9 @@ private:
 
     KToolBarPopupAction* m_backAction;
     KToolBarPopupAction* m_forwardAction;
+
+    QMenu m_searchTools;
+
 };
 
 inline DolphinViewContainer* DolphinMainWindow::activeViewContainer() const
diff --git a/src/dolphinpart.cpp b/src/dolphinpart.cpp
index 607917f9a1..dc083f1a55 100644
--- a/src/dolphinpart.cpp
+++ b/src/dolphinpart.cpp
@@ -40,6 +40,7 @@
 #include <KLocalizedString>
 #include <KMessageBox>
 #include <KMimeTypeEditor>
+#include <KNS3/KMoreToolsMenuFactory>
 #include <KPluginFactory>
 #include <KRun>
 #include <KSharedConfig>
@@ -553,7 +554,16 @@ void DolphinPart::slotOpenTerminal()
 
 void DolphinPart::slotFindFile()
 {
-    KRun::run(QStringLiteral("kfind"), {url()}, widget());
+    QMenu searchTools;
+    KMoreToolsMenuFactory("dolphin/search-tools").fillMenuFromGroupingNames(
+        &searchTools, { "files-find" }, QUrl::fromLocalFile(KParts::ReadOnlyPart::localFilePath())
+    );
+    QList<QAction*> actions = searchTools.actions();
+    if (!(actions.isEmpty())) {
+        actions.first()->trigger();
+    } else {
+        KRun::run(QStringLiteral("kfind"), {url()}, widget());
+    }
 }
 
 void DolphinPart::updateNewMenu()
diff --git a/src/dolphinpart.h b/src/dolphinpart.h
index 864c08344e..fe8f2d14ea 100644
--- a/src/dolphinpart.h
+++ b/src/dolphinpart.h
@@ -194,7 +194,7 @@ private Q_SLOTS:
     void slotOpenTerminal();
 
     /**
-     * Open KFind with the current path.
+     * Open preferred search tool in the current directory to find files.
      */
     void slotFindFile();
 
diff --git a/src/dolphinui.rc b/src/dolphinui.rc
index dcacc56c49..4de1c609c7 100644
--- a/src/dolphinui.rc
+++ b/src/dolphinui.rc
@@ -1,5 +1,5 @@
 <!DOCTYPE kpartgui SYSTEM "kpartgui.dtd">
-<kpartgui name="dolphin" version="27">
+<kpartgui name="dolphin" version="28">
     <MenuBar>
         <Menu name="file">
             <Action name="new_menu" />
@@ -54,6 +54,7 @@
         </Menu>
         <Menu name="tools">
             <Action name="show_filter_bar" />
+            <Action name="open_preferred_search_tool" />
             <Action name="open_terminal" />
             <Action name="compare_files" />
             <Action name="change_remote_encoding" />


More information about the kde-doc-english mailing list