diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index c69567e268..5f7a8e7280 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -65,6 +65,7 @@ #include #include #include +#include #include #include #include @@ -90,6 +91,8 @@ namespace { // Used for GeneralSettings::version() to determine whether // an updated version of Dolphin is running. const int CurrentDolphinVersion = 200; + // The maximum number of entries in the back/forward popup menu + const int MaxNumberOfNavigationentries = 12; } DolphinMainWindow::DolphinMainWindow() : @@ -107,7 +110,9 @@ DolphinMainWindow::DolphinMainWindow() : m_lastHandleUrlStatJob(nullptr), m_terminalPanel(nullptr), m_placesPanel(nullptr), - m_tearDownFromPlacesRequested(false) + m_tearDownFromPlacesRequested(false), + m_backAction(nullptr), + m_forwardAction(nullptr) { Q_INIT_RESOURCE(dolphin); setComponentName(QStringLiteral("dolphin"), QGuiApplication::applicationDisplayName()); @@ -676,6 +681,56 @@ void DolphinMainWindow::slotToolBarActionMiddleClicked(QAction *action) } } +void DolphinMainWindow::slotAboutToShowBackPopupMenu() +{ + KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator(); + int entries = 0; + m_backAction->menu()->clear(); + for (int i = urlNavigator->historyIndex() + 1; i < urlNavigator->historySize() && entries < MaxNumberOfNavigationentries; ++i, ++entries) { + QAction* action = new QAction(urlNavigator->locationUrl(i).toString(QUrl::PreferLocalFile), m_backAction->menu()); + action->setData(i); + m_backAction->menu()->addAction(action); + } +} + +void DolphinMainWindow::slotGoBack(QAction* action) +{ + int gotoIndex = action->data().value(); + KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator(); + for (int i = gotoIndex - urlNavigator->historyIndex(); i > 0; --i) { + goBack(); + } +} + +void DolphinMainWindow::slotBackForwardActionMiddleClicked(QAction* action) +{ + if (action) { + KUrlNavigator* urlNavigator = activeViewContainer()->urlNavigator(); + openNewTabAfterCurrentTab(urlNavigator->locationUrl(action->data().value())); + } +} + +void DolphinMainWindow::slotAboutToShowForwardPopupMenu() +{ + KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator(); + int entries = 0; + m_forwardAction->menu()->clear(); + for (int i = urlNavigator->historyIndex() - 1; i >= 0 && entries < MaxNumberOfNavigationentries; --i, ++entries) { + QAction* action = new QAction(urlNavigator->locationUrl(i).toString(QUrl::PreferLocalFile), m_forwardAction->menu()); + action->setData(i); + m_forwardAction->menu()->addAction(action); + } +} + +void DolphinMainWindow::slotGoForward(QAction* action) +{ + int gotoIndex = action->data().value(); + KUrlNavigator* urlNavigator = m_activeViewContainer->urlNavigator(); + for (int i = urlNavigator->historyIndex() - gotoIndex; i > 0; --i) { + goForward(); + } +} + void DolphinMainWindow::selectAll() { clearStatusBar(); @@ -1313,10 +1368,22 @@ void DolphinMainWindow::setupActions() connect(replaceLocation, &QAction::triggered, this, &DolphinMainWindow::replaceLocation); // setup 'Go' menu - QAction* backAction = KStandardAction::back(this, &DolphinMainWindow::goBack, actionCollection()); - auto backShortcuts = backAction->shortcuts(); + { + QScopedPointer backAction(KStandardAction::back(nullptr, nullptr, nullptr)); + m_backAction = new KToolBarPopupAction(backAction->icon(), backAction->text(), actionCollection()); + m_backAction->setObjectName(backAction->objectName()); + m_backAction->setShortcuts(backAction->shortcuts()); + } + m_backAction->setDelayed(true); + m_backAction->setStickyMenu(false); + connect(m_backAction, &QAction::triggered, this, &DolphinMainWindow::goBack); + connect(m_backAction->menu(), &QMenu::aboutToShow, this, &DolphinMainWindow::slotAboutToShowBackPopupMenu); + connect(m_backAction->menu(), &QMenu::triggered, this, &DolphinMainWindow::slotGoBack); + actionCollection()->addAction(m_backAction->objectName(), m_backAction); + + auto backShortcuts = m_backAction->shortcuts(); backShortcuts.append(QKeySequence(Qt::Key_Backspace)); - actionCollection()->setDefaultShortcuts(backAction, backShortcuts); + actionCollection()->setDefaultShortcuts(m_backAction, backShortcuts); DolphinRecentTabsMenu* recentTabsMenu = new DolphinRecentTabsMenu(this); actionCollection()->addAction(QStringLiteral("closed_tabs"), recentTabsMenu); @@ -1345,7 +1412,23 @@ void DolphinMainWindow::setupActions() "be undone will ask for your confirmation.")); undoAction->setEnabled(false); // undo should be disabled by default - KStandardAction::forward(this, &DolphinMainWindow::goForward, actionCollection()); + { + QScopedPointer forwardAction(KStandardAction::forward(nullptr, nullptr, nullptr)); + m_forwardAction = new KToolBarPopupAction(forwardAction->icon(), forwardAction->text(), actionCollection()); + m_forwardAction->setObjectName(forwardAction->objectName()); + m_forwardAction->setShortcuts(forwardAction->shortcuts()); + } + m_forwardAction->setDelayed(true); + m_forwardAction->setStickyMenu(false); + connect(m_forwardAction, &QAction::triggered, this, &DolphinMainWindow::goForward); + connect(m_forwardAction->menu(), &QMenu::aboutToShow, this, &DolphinMainWindow::slotAboutToShowForwardPopupMenu); + connect(m_forwardAction->menu(), &QMenu::triggered, this, &DolphinMainWindow::slotGoForward); + actionCollection()->addAction(m_forwardAction->objectName(), m_forwardAction); + // enable middle-click to open in a new tab + auto *middleClickEventFilter = new MiddleClickActionEventFilter(this); + connect(middleClickEventFilter, &MiddleClickActionEventFilter::actionMiddleClicked, this, &DolphinMainWindow::slotBackForwardActionMiddleClicked); + m_backAction->menu()->installEventFilter(middleClickEventFilter); + m_forwardAction->menu()->installEventFilter(middleClickEventFilter); KStandardAction::up(this, &DolphinMainWindow::goUp, actionCollection()); QAction* homeAction = KStandardAction::home(this, &DolphinMainWindow::goHome, actionCollection()); homeAction->setWhatsThis(xi18nc("@info:whatsthis", "Go to your " diff --git a/src/dolphinmainwindow.h b/src/dolphinmainwindow.h index 253fc74d4e..3d86340d6a 100644 --- a/src/dolphinmainwindow.h +++ b/src/dolphinmainwindow.h @@ -47,6 +47,7 @@ class KFileItemList; class KJob; class KNewFileMenu; class KHelpMenu; +class KToolBarPopupAction; class QToolButton; class QIcon; class PlacesPanel; @@ -509,6 +510,36 @@ private slots: */ void slotToolBarActionMiddleClicked(QAction *action); + /** + * Is called before the Back popup menu is shown. This slot will populate + * the menu with history data + */ + void slotAboutToShowBackPopupMenu(); + + /** + * This slot is used by the Back Popup Menu to go back to a specific + * history index. The QAction::data will carry an int with the index + * to go to. + */ + void slotGoBack(QAction* action); + + /** + * Middle clicking Back/Forward will open the resulting folder in a new tab. + */ + void slotBackForwardActionMiddleClicked(QAction *action); + + /** + * Is called before the Forward popup menu is shown. This slot will populate + * the menu with history data + */ + void slotAboutToShowForwardPopupMenu(); + + /** + * This slot is used by the Forward Popup Menu to go forward to a specific + * history index. The QAction::data will carry an int with the index + * to go to. + */ + void slotGoForward(QAction* action); private: /** * Sets up the various menus and actions and connects them. @@ -599,6 +630,9 @@ private: TerminalPanel* m_terminalPanel; PlacesPanel* m_placesPanel; bool m_tearDownFromPlacesRequested; + + KToolBarPopupAction* m_backAction; + KToolBarPopupAction* m_forwardAction; }; inline DolphinViewContainer* DolphinMainWindow::activeViewContainer() const diff --git a/src/middleclickactioneventfilter.cpp b/src/middleclickactioneventfilter.cpp index e0917850a0..293e16e0ca 100644 --- a/src/middleclickactioneventfilter.cpp +++ b/src/middleclickactioneventfilter.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include @@ -39,16 +40,32 @@ bool MiddleClickActionEventFilter::eventFilter(QObject *watched, QEvent *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); + if (toolBar) { + 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; + } + } + } + QMenu *menu = qobject_cast(watched); + if (menu) { + QAction *action = menu->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); + return true; + } + m_lastMiddlePressedAction = nullptr; } - m_lastMiddlePressedAction = nullptr; } } }