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
This commit is contained in:
Alex Miranda 2019-10-13 16:37:00 +02:00 committed by Elvis Angelaccio
parent e7de986202
commit e04ec8601f
3 changed files with 46 additions and 0 deletions

View file

@ -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<QKeySequence> 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"));

View file

@ -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;

View file

@ -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.