diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -110,6 +110,7 @@ views/viewproperties.cpp views/zoomlevelinfo.cpp dolphinremoveaction.cpp + middleclickactioneventfilter.cpp dolphinnewfilemenu.cpp dolphindebug.cpp ) diff --git a/src/dolphinmainwindow.h b/src/dolphinmainwindow.h --- a/src/dolphinmainwindow.h +++ b/src/dolphinmainwindow.h @@ -267,28 +267,17 @@ /** 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 @@ */ 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/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -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 @@ 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 @@ 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 @@ 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() diff --git a/src/middleclickactioneventfilter.h b/src/middleclickactioneventfilter.h new file mode 100644 --- /dev/null +++ b/src/middleclickactioneventfilter.h @@ -0,0 +1,50 @@ +/*************************************************************************** + * Copyright (C) 2017 Kai Uwe Broulik * + * * + * 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 +#include + +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 m_lastMiddlePressedAction; + +}; diff --git a/src/middleclickactioneventfilter.cpp b/src/middleclickactioneventfilter.cpp new file mode 100644 --- /dev/null +++ b/src/middleclickactioneventfilter.cpp @@ -0,0 +1,58 @@ +/*************************************************************************** + * Copyright (C) 2017 Kai Uwe Broulik * + * * + * 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 +#include +#include +#include + +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(event); + + if (me->button() == Qt::MiddleButton) { + QToolBar *toolBar = qobject_cast(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); +}