From 8c0c056c478c7898d7d6c261bd2b9d1b5cc26933 Mon Sep 17 00:00:00 2001 From: chendapao Date: Sun, 10 Jul 2022 10:06:38 +0800 Subject: [PATCH] feature/add_viewarea_shortcut_close_other_right (#2173) * feature/add_viewarea_shortcut_close_other_right * delete default * adj multi * del blank line * add close all and close left * adj * adj * del unless --- src/core/coreconfig.h | 2 + src/data/core/vnotex.json | 2 + src/widgets/viewarea.cpp | 44 ++++++++++++++++++++++ src/widgets/viewsplit.cpp | 77 +++++++++++++++++++++++++++++++-------- src/widgets/viewsplit.h | 11 ++++++ 5 files changed, 121 insertions(+), 15 deletions(-) diff --git a/src/core/coreconfig.h b/src/core/coreconfig.h index 2a997754..ded4f9d2 100644 --- a/src/core/coreconfig.h +++ b/src/core/coreconfig.h @@ -24,7 +24,9 @@ namespace vnotex NewNote, NewFolder, CloseTab, + CloseAllTabs, CloseOtherTabs, + CloseTabsToTheLeft, CloseTabsToTheRight, NavigationDock, OutlineDock, diff --git a/src/data/core/vnotex.json b/src/data/core/vnotex.json index de186587..e2276672 100644 --- a/src/data/core/vnotex.json +++ b/src/data/core/vnotex.json @@ -16,7 +16,9 @@ "NewNote" : "Ctrl+Alt+N", "NewFolder" : "Ctrl+Alt+S", "CloseTab" : "Ctrl+G, X", + "CloseAllTabs": "", "CloseOtherTabs" : "", + "CloseTabsToTheLeft" : "", "CloseTabsToTheRight" : "", "NavigationDock" : "Ctrl+G, A", "OutlineDock" : "Ctrl+G, U", diff --git a/src/widgets/viewarea.cpp b/src/widgets/viewarea.cpp index 92eaf30f..c94f9d3e 100644 --- a/src/widgets/viewarea.cpp +++ b/src/widgets/viewarea.cpp @@ -819,6 +819,50 @@ void ViewArea::setupShortcuts() } } + // CloseAllTabs + { + auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::CloseAllTabs), this); + if (shortcut) { + connect(shortcut, &QShortcut::activated, + this, [this]() { + getCurrentViewSplit()->closeMultipleTabs(ViewSplit::CloseTabMode::CloseAllTabs); + }); + } + } + + // CloseOtherTabs + { + auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::CloseOtherTabs), this); + if (shortcut) { + connect(shortcut, &QShortcut::activated, + this, [this]() { + getCurrentViewSplit()->closeMultipleTabs(ViewSplit::CloseTabMode::CloseOtherTabs); + }); + } + } + + // CloseTabsToTheLeft + { + auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::CloseTabsToTheLeft), this); + if (shortcut) { + connect(shortcut, &QShortcut::activated, + this, [this]() { + getCurrentViewSplit()->closeMultipleTabs(ViewSplit::CloseTabMode::CloseTabsToTheLeft); + }); + } + } + + // CloseTabsToTheRight + { + auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::CloseTabsToTheRight), this); + if (shortcut) { + connect(shortcut, &QShortcut::activated, + this, [this]() { + getCurrentViewSplit()->closeMultipleTabs(ViewSplit::CloseTabMode::CloseTabsToTheRight); + }); + } + } + // LocateNode. { auto shortcut = WidgetUtils::createShortcut(coreConfig.getShortcut(CoreConfig::LocateNode), this); diff --git a/src/widgets/viewsplit.cpp b/src/widgets/viewsplit.cpp index a5fca64f..5e5ef409 100644 --- a/src/widgets/viewsplit.cpp +++ b/src/widgets/viewsplit.cpp @@ -541,34 +541,41 @@ void ViewSplit::createContextMenuOnTabBar(QMenu *p_menu, int p_tabIdx) coreConfig.getShortcut(CoreConfig::Shortcut::CloseTab)); } + // Close All Tabs. + { + auto closeTabAct = p_menu->addAction(tr("Close All Tabs"), + [this, p_tabIdx]() { + closeMultipleTabs(p_tabIdx, CloseTabMode::CloseAllTabs); + }); + WidgetUtils::addActionShortcutText(closeTabAct, + coreConfig.getShortcut(CoreConfig::Shortcut::CloseAllTabs)); + } + // Close Other Tabs. { auto closeTabAct = p_menu->addAction(tr("Close Other Tabs"), [this, p_tabIdx]() { - QVector windowsNeedToClose; - int cnt = getViewWindowCount(); - for (int i = 0; i < cnt; ++i) { - if (i != p_tabIdx) { - windowsNeedToClose.push_back(getViewWindow(i)); - } - } - - for (auto win : windowsNeedToClose) { - emit viewWindowCloseRequested(win); - } + closeMultipleTabs(p_tabIdx, CloseTabMode::CloseOtherTabs); }); WidgetUtils::addActionShortcutText(closeTabAct, coreConfig.getShortcut(CoreConfig::Shortcut::CloseOtherTabs)); } + // Close Tabs To The Left + { + auto closeTabAct = p_menu->addAction(tr("Close Tabs To The Left"), + [this, p_tabIdx]() { + closeMultipleTabs(p_tabIdx, CloseTabMode::CloseTabsToTheLeft); + }); + WidgetUtils::addActionShortcutText(closeTabAct, + coreConfig.getShortcut(CoreConfig::Shortcut::CloseTabsToTheLeft)); + } + // Close Tabs To The Right. { auto closeTabAct = p_menu->addAction(tr("Close Tabs To The Right"), [this, p_tabIdx]() { - int cnt = getViewWindowCount(); - for (int i = cnt - 1; i > p_tabIdx; --i) { - closeTab(i); - } + closeMultipleTabs(p_tabIdx, CloseTabMode::CloseTabsToTheRight); }); WidgetUtils::addActionShortcutText(closeTabAct, coreConfig.getShortcut(CoreConfig::Shortcut::CloseTabsToTheRight)); @@ -700,6 +707,46 @@ void ViewSplit::closeTab(int p_idx) } } +void ViewSplit::closeMultipleTabs(CloseTabMode p_ctm) +{ + closeMultipleTabs(currentIndex(), p_ctm); +} + +void ViewSplit::closeMultipleTabs(int p_idx, CloseTabMode p_ctm) +{ + QVector windowsNeedToClose; + int cnt = getViewWindowCount(); + + switch (p_ctm) { + case CloseTabMode::CloseAllTabs: + for (int i = 0; i < cnt; i++) { + windowsNeedToClose.push_back(getViewWindow(i)); + } + break; + case CloseTabMode::CloseOtherTabs: + for (int i = 0; i < cnt; i++) { + if (i != p_idx) { + windowsNeedToClose.push_back(getViewWindow(i)); + } + } + break; + case CloseTabMode::CloseTabsToTheLeft: + for (int i = 0; i < p_idx; i++) { + windowsNeedToClose.push_back(getViewWindow(i)); + } + break; + case CloseTabMode::CloseTabsToTheRight: + for (int i = cnt - 1; i > p_idx; i--) { + windowsNeedToClose.push_back(getViewWindow(i)); + } + break; + } + + for (auto win : windowsNeedToClose) { + emit viewWindowCloseRequested(win); + } +} + void ViewSplit::mousePressEvent(QMouseEvent *p_event) { QTabWidget::mousePressEvent(p_event); diff --git a/src/widgets/viewsplit.h b/src/widgets/viewsplit.h index 25c6ba3b..1c44f604 100644 --- a/src/widgets/viewsplit.h +++ b/src/widgets/viewsplit.h @@ -70,6 +70,15 @@ namespace vnotex void updateStateToWorkspace() const; + enum class CloseTabMode { + CloseAllTabs, + CloseOtherTabs, + CloseTabsToTheLeft, + CloseTabsToTheRight + }; + + void closeMultipleTabs(CloseTabMode p_ctm); + signals: void viewWindowCloseRequested(ViewWindow *p_win); @@ -136,6 +145,8 @@ namespace vnotex void activateNextTab(bool p_backward); + void closeMultipleTabs(int p_idx, CloseTabMode ctm); + ID m_id = 0; const QVector> &m_allWorkspaces;