]> cloud.milkyroute.net Git - dolphin.git/commitdiff
Support middle clicking of Back/Forward/Up/Home toolbar buttons
authorKai Uwe Broulik <kde@privat.broulik.de>
Thu, 24 Aug 2017 13:45:28 +0000 (15:45 +0200)
committerKai Uwe Broulik <kde@privat.broulik.de>
Thu, 24 Aug 2017 13:45:28 +0000 (15:45 +0200)
This opens the resulting page in a new tab.

BUG: 358649

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

src/CMakeLists.txt
src/dolphinmainwindow.cpp
src/dolphinmainwindow.h
src/middleclickactioneventfilter.cpp [new file with mode: 0644]
src/middleclickactioneventfilter.h [new file with mode: 0644]

index 13b94f73b4131000e29f2718af217acbf72a6dea..e1b63358dcf024d08150a2a617514c65aaa984eb 100644 (file)
@@ -110,6 +110,7 @@ set(dolphinprivate_LIB_SRCS
     views/viewproperties.cpp
     views/zoomlevelinfo.cpp
     dolphinremoveaction.cpp
+    middleclickactioneventfilter.cpp
     dolphinnewfilemenu.cpp
     dolphindebug.cpp
 )
index 6701fbcfa7e97315c988b5ff8df4bf2400654a07..9369b3c7749ae63071445883b277b4045316e383 100644 (file)
@@ -29,6 +29,7 @@
 #include "dolphintabwidget.h"
 #include "dolphinviewcontainer.h"
 #include "dolphintabpage.h"
+#include "middleclickactioneventfilter.h"
 #include "panels/folders/folderspanel.h"
 #include "panels/places/placespanel.h"
 #include "panels/information/informationpanel.h"
@@ -170,6 +171,11 @@ DolphinMainWindow::DolphinMainWindow() :
     if (!showMenu) {
         createControlButton();
     }
+
+    // enable middle-click on back/forward/up to open in a new tab
+    auto *middleClickEventFilter = new MiddleClickActionEventFilter(this);
+    connect(middleClickEventFilter, &MiddleClickActionEventFilter::actionMiddleClicked, this, &DolphinMainWindow::slotToolBarActionMiddleClicked);
+    toolBar()->installEventFilter(middleClickEventFilter);
 }
 
 DolphinMainWindow::~DolphinMainWindow()
@@ -501,6 +507,19 @@ void DolphinMainWindow::slotDirectoryLoadingCompleted()
     updatePasteAction();
 }
 
+void DolphinMainWindow::slotToolBarActionMiddleClicked(QAction *action)
+{
+    if (action == actionCollection()->action(QStringLiteral("go_back"))) {
+        goBackInNewTab();
+    } else if (action == actionCollection()->action(QStringLiteral("go_forward"))) {
+        goForwardInNewTab();
+    } else if (action == actionCollection()->action(QStringLiteral("go_up"))) {
+        goUpInNewTab();
+    } else if (action == actionCollection()->action(QStringLiteral("go_home"))) {
+        goHomeInNewTab();
+    }
+}
+
 void DolphinMainWindow::selectAll()
 {
     clearStatusBar();
@@ -626,40 +645,29 @@ void DolphinMainWindow::goHome()
     m_activeViewContainer->urlNavigator()->goHome();
 }
 
-void DolphinMainWindow::goBack(Qt::MouseButtons buttons)
+void DolphinMainWindow::goBackInNewTab()
 {
-    // The default case (left button pressed) is handled in goBack().
-    if (buttons == Qt::MiddleButton) {
-        KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
-        const int index = urlNavigator->historyIndex() + 1;
-        openNewTab(urlNavigator->locationUrl(index));
-    }
+    KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
+    const int index = urlNavigator->historyIndex() + 1;
+    openNewTab(urlNavigator->locationUrl(index));
 }
 
-void DolphinMainWindow::goForward(Qt::MouseButtons buttons)
+void DolphinMainWindow::goForwardInNewTab()
 {
-    // The default case (left button pressed) is handled in goForward().
-    if (buttons == Qt::MiddleButton) {
-        KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
-        const int index = urlNavigator->historyIndex() - 1;
-        openNewTab(urlNavigator->locationUrl(index));
-    }
+    KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator();
+    const int index = urlNavigator->historyIndex() - 1;
+    openNewTab(urlNavigator->locationUrl(index));
 }
 
-void DolphinMainWindow::goUp(Qt::MouseButtons buttons)
+void DolphinMainWindow::goUpInNewTab()
 {
-    // The default case (left button pressed) is handled in goUp().
-    if (buttons == Qt::MiddleButton) {
-        openNewTab(KIO::upUrl(activeViewContainer()->url()));
-    }
+    const QUrl currentUrl = activeViewContainer()->urlNavigator()->locationUrl();
+    openNewTab(KIO::upUrl(currentUrl));
 }
 
-void DolphinMainWindow::goHome(Qt::MouseButtons buttons)
+void DolphinMainWindow::goHomeInNewTab()
 {
-    // The default case (left button pressed) is handled in goHome().
-    if (buttons == Qt::MiddleButton) {
-        openNewTab(Dolphin::homeUrl());
-    }
+    openNewTab(Dolphin::homeUrl());
 }
 
 void DolphinMainWindow::compareFiles()
index d741eb21ed455aa84e1f85c05e3347c4fdc5b355..06eb5a7a50ab3de51aca6b02cc38cd0478f0ab45 100644 (file)
@@ -267,28 +267,17 @@ private slots:
     /** Changes the location to the home URL. */
     void goHome();
 
-    /**
-     * Open the previous URL in the URL history in a new tab
-     * if the middle mouse button is clicked.
-     */
-    void goBack(Qt::MouseButtons buttons);
+    /** Open the previous URL in the URL history in a new tab. */
+    void goBackInNewTab();
 
-    /**
-     * Open the next URL in the URL history in a new tab
-     * if the middle mouse button is clicked.
-     */
-    void goForward(Qt::MouseButtons buttons);
+    /** Open the next URL in the URL history in a new tab. */
+    void goForwardInNewTab();
 
-    /**
-     * Open the URL one hierarchy above the current URL in a new tab
-     * if the middle mouse button is clicked.
-     */
-    void goUp(Qt::MouseButtons buttons);
+    /** Open the URL one hierarchy above the current URL in a new tab. */
+    void goUpInNewTab();
 
-    /**
-     * Open the home URL in a new tab
-     */
-    void goHome(Qt::MouseButtons buttons);
+    /** * Open the home URL in a new tab. */
+    void goHomeInNewTab();
 
     /** Opens Kompare for 2 selected files. */
     void compareFiles();
@@ -425,6 +414,14 @@ private slots:
      */
     void slotDirectoryLoadingCompleted();
 
+    /**
+     * Is called when the user middle clicks a toolbar button.
+     *
+     * Here middle clicking Back/Forward/Up/Home will open the resulting
+     * folder in a new tab.
+     */
+    void slotToolBarActionMiddleClicked(QAction *action);
+
 private:
     void setupActions();
     void setupDockWidgets();
diff --git a/src/middleclickactioneventfilter.cpp b/src/middleclickactioneventfilter.cpp
new file mode 100644 (file)
index 0000000..e091785
--- /dev/null
@@ -0,0 +1,58 @@
+/***************************************************************************
+ *   Copyright (C) 2017 Kai Uwe Broulik <kde@privat.broulik.de>            *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#include "middleclickactioneventfilter.h"
+
+#include <QAction>
+#include <QEvent>
+#include <QMouseEvent>
+#include <QToolBar>
+
+MiddleClickActionEventFilter::MiddleClickActionEventFilter(QObject *parent) : QObject(parent)
+{
+
+}
+
+MiddleClickActionEventFilter::~MiddleClickActionEventFilter() = default;
+
+bool MiddleClickActionEventFilter::eventFilter(QObject *watched, QEvent *event)
+{
+    if (event->type() == QEvent::MouseButtonPress
+            || event->type() == QEvent::MouseButtonRelease) {
+        QMouseEvent *me = static_cast<QMouseEvent *>(event);
+
+        if (me->button() == Qt::MiddleButton) {
+            QToolBar *toolBar = qobject_cast<QToolBar *>(watched);
+
+            QAction *action = toolBar->actionAt(me->pos());
+            if (action) {
+                if (event->type() == QEvent::MouseButtonPress) {
+                    m_lastMiddlePressedAction = action;
+                } else if (event->type() == QEvent::MouseButtonRelease) {
+                    if (m_lastMiddlePressedAction == action) {
+                        emit actionMiddleClicked(action);
+                    }
+                    m_lastMiddlePressedAction = nullptr;
+                }
+            }
+        }
+    }
+
+    return QObject::eventFilter(watched, event);
+}
diff --git a/src/middleclickactioneventfilter.h b/src/middleclickactioneventfilter.h
new file mode 100644 (file)
index 0000000..6974f46
--- /dev/null
@@ -0,0 +1,50 @@
+/***************************************************************************
+ *   Copyright (C) 2017 Kai Uwe Broulik <kde@privat.broulik.de>            *
+ *                                                                         *
+ *   This program is free software; you can redistribute it and/or modify  *
+ *   it under the terms of the GNU General Public License as published by  *
+ *   the Free Software Foundation; either version 2 of the License, or     *
+ *   (at your option) any later version.                                   *
+ *                                                                         *
+ *   This program is distributed in the hope that it will be useful,       *
+ *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
+ *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
+ *   GNU General Public License for more details.                          *
+ *                                                                         *
+ *   You should have received a copy of the GNU General Public License     *
+ *   along with this program; if not, write to the                         *
+ *   Free Software Foundation, Inc.,                                       *
+ *   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA            *
+ ***************************************************************************/
+
+#pragma once
+
+#include "dolphin_export.h"
+
+#include <QObject>
+#include <QPointer>
+
+class QAction;
+
+/**
+ * An event filter that allows to detect a middle click
+ * to trigger e.g. opening something in a new tab.
+ */
+class DOLPHIN_EXPORT MiddleClickActionEventFilter : public QObject
+{
+    Q_OBJECT
+
+public:
+    MiddleClickActionEventFilter(QObject *parent);
+    ~MiddleClickActionEventFilter() override;
+
+signals:
+    void actionMiddleClicked(QAction *action);
+
+protected:
+    bool eventFilter(QObject *watched, QEvent *event) override;
+
+private:
+    QPointer<QAction> m_lastMiddlePressedAction;
+
+};