From e04ec8601f3771aa7ce98d9d1de34e332f26c103 Mon Sep 17 00:00:00 2001 From: Alex Miranda Date: Sun, 13 Oct 2019 16:37:00 +0200 Subject: [PATCH] Add actions for switching to a specific tab Summary: Add actions to switch to each of the first 9 tabs and another action to switch to the last tab. This feature makes it much easier to quickly switch between tabs just like you normally would be able to when using a web browser or other applications. Reviewers: #vdg, #dolphin, ngraham, elvisangelaccio Reviewed By: #vdg, #dolphin, ngraham Subscribers: meven, ngraham, elvisangelaccio, kfm-devel Tags: #dolphin Differential Revision: https://phabricator.kde.org/D24353 --- src/dolphinmainwindow.cpp | 24 ++++++++++++++++++++++++ src/dolphintabwidget.cpp | 12 ++++++++++++ src/dolphintabwidget.h | 10 ++++++++++ 3 files changed, 46 insertions(+) diff --git a/src/dolphinmainwindow.cpp b/src/dolphinmainwindow.cpp index ce2013798d..ce1746d2b7 100644 --- a/src/dolphinmainwindow.cpp +++ b/src/dolphinmainwindow.cpp @@ -93,6 +93,8 @@ namespace { const int CurrentDolphinVersion = 200; // The maximum number of entries in the back/forward popup menu const int MaxNumberOfNavigationentries = 12; + // The maximum number of "Activate Tab" shortcuts + const int MaxActivateTabShortcuts = 9; } DolphinMainWindow::DolphinMainWindow() : @@ -1178,6 +1180,10 @@ void DolphinMainWindow::activeViewChanged(DolphinViewContainer* viewContainer) void DolphinMainWindow::tabCountChanged(int count) { const bool enableTabActions = (count > 1); + for (int i = 0; i < MaxActivateTabShortcuts; ++i) { + actionCollection()->action(QStringLiteral("activate_tab_%1").arg(i))->setEnabled(enableTabActions); + } + actionCollection()->action(QStringLiteral("activate_last_tab"))->setEnabled(enableTabActions); actionCollection()->action(QStringLiteral("activate_next_tab"))->setEnabled(enableTabActions); actionCollection()->action(QStringLiteral("activate_prev_tab"))->setEnabled(enableTabActions); } @@ -1503,6 +1509,24 @@ void DolphinMainWindow::setupActions() QList prevTabKeys = KStandardShortcut::tabPrev(); prevTabKeys.append(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_Tab)); + for (int i = 0; i < MaxActivateTabShortcuts; ++i) { + QAction* activateTab = actionCollection()->addAction(QStringLiteral("activate_tab_%1").arg(i)); + activateTab->setText(i18nc("@action:inmenu", "Activate Tab %1", i + 1)); + activateTab->setEnabled(false); + connect(activateTab, &QAction::triggered, this, [this, i]() { m_tabWidget->activateTab(i); }); + + // only add default shortcuts for the first 9 tabs regardless of MaxActivateTabShortcuts + if (i < 9) { + actionCollection()->setDefaultShortcut(activateTab, QStringLiteral("Alt+%1").arg(i + 1)); + } + } + + QAction* activateLastTab = actionCollection()->addAction(QStringLiteral("activate_last_tab")); + activateLastTab->setText(i18nc("@action:inmenu", "Activate Last Tab")); + activateLastTab->setEnabled(false); + connect(activateLastTab, &QAction::triggered, m_tabWidget, &DolphinTabWidget::activateLastTab); + actionCollection()->setDefaultShortcut(activateLastTab, Qt::ALT + Qt::Key_0); + QAction* activateNextTab = actionCollection()->addAction(QStringLiteral("activate_next_tab")); activateNextTab->setIconText(i18nc("@action:inmenu", "Next Tab")); activateNextTab->setText(i18nc("@action:inmenu", "Activate Next Tab")); diff --git a/src/dolphintabwidget.cpp b/src/dolphintabwidget.cpp index ec0c783bc3..0408d7ed47 100644 --- a/src/dolphintabwidget.cpp +++ b/src/dolphintabwidget.cpp @@ -266,6 +266,18 @@ void DolphinTabWidget::closeTab(const int index) tabPage->deleteLater(); } +void DolphinTabWidget::activateTab(const int index) +{ + if (index < count()) { + setCurrentIndex(index); + } +} + +void DolphinTabWidget::activateLastTab() +{ + setCurrentIndex(count() - 1); +} + void DolphinTabWidget::activateNextTab() { const int index = currentIndex() + 1; diff --git a/src/dolphintabwidget.h b/src/dolphintabwidget.h index 4351a40a83..746aec6c64 100644 --- a/src/dolphintabwidget.h +++ b/src/dolphintabwidget.h @@ -154,6 +154,16 @@ public slots: */ void closeTab(const int index); + /** + * Activates the tab with the index \a index. + */ + void activateTab(const int index); + + /** + * Activates the last tab in the tab bar. + */ + void activateLastTab(); + /** * Activates the next tab in the tab bar. * If the current active tab is the last tab, it activates the first tab.