From 9a10b210ecadca9669f6c84a4bb4c75ca621eddc Mon Sep 17 00:00:00 2001 From: Le Tan Date: Sun, 9 Oct 2022 12:05:33 +0800 Subject: [PATCH] ViewWindow: add shortcut Ctrl_G,V to alternate view mode --- src/core/editorconfig.h | 1 + src/data/core/vnotex.json | 1 + src/widgets/viewwindow.cpp | 30 +++++++++++++++++++++++++ src/widgets/viewwindowtoolbarhelper.cpp | 2 ++ 4 files changed, 34 insertions(+) diff --git a/src/core/editorconfig.h b/src/core/editorconfig.h index 0eb95fe6..27df0dcd 100644 --- a/src/core/editorconfig.h +++ b/src/core/editorconfig.h @@ -64,6 +64,7 @@ namespace vnotex ClearHighlights, WordCount, Attachment, + AlternateViewMode, MaxShortcut }; Q_ENUM(Shortcut) diff --git a/src/data/core/vnotex.json b/src/data/core/vnotex.json index 799d61cf..e78d2391 100644 --- a/src/data/core/vnotex.json +++ b/src/data/core/vnotex.json @@ -219,6 +219,7 @@ "ApplySnippet" : "Ctrl+G, I", "Tag" : "Ctrl+G, B", "Attachment" : "", + "AlternateViewMode" : "Ctrl+G, V", "WordCount" : "", "Debug" : "F12", "Print" : "", diff --git a/src/widgets/viewwindow.cpp b/src/widgets/viewwindow.cpp index 50b024d9..5169aa93 100644 --- a/src/widgets/viewwindow.cpp +++ b/src/widgets/viewwindow.cpp @@ -358,6 +358,36 @@ QAction *ViewWindow::addAction(QToolBar *p_toolBar, ViewWindowToolBarHelper::Act this, [this, menu]() { updateViewModeMenu(menu); }); + + // Add shortcut to alternate among view modes. + const auto &editorConfig = ConfigMgr::getInst().getEditorConfig(); + auto shortcut = WidgetUtils::createShortcut(editorConfig.getShortcut(EditorConfig::AlternateViewMode), + this, + Qt::WidgetWithChildrenShortcut); + if (shortcut) { + connect(shortcut, &QShortcut::activated, + this, [this, act, menu] { + if (!act->isEnabled()) { + return; + } + updateViewModeMenu(menu); + auto actions = menu->actions(); + int idx = -1; + for (int i = 0; i < actions.size(); ++i) { + if (actions[i]->isChecked()) { + idx = i + 1; + break; + } + } + if (idx == -1) { + return; + } + if (idx >= actions.size()) { + idx = 0; + } + actions[idx]->trigger(); + }); + } break; } diff --git a/src/widgets/viewwindowtoolbarhelper.cpp b/src/widgets/viewwindowtoolbarhelper.cpp index ab4701b9..4dda7c99 100644 --- a/src/widgets/viewwindowtoolbarhelper.cpp +++ b/src/widgets/viewwindowtoolbarhelper.cpp @@ -124,6 +124,8 @@ QAction *ViewWindowToolBarHelper::addAction(QToolBar *p_tb, Action p_action) act = p_tb->addAction(ToolBarHelper::generateIcon("view_mode_editor.svg"), ViewWindow::tr("View Mode")); + WidgetUtils::addActionShortcutText(act, editorConfig.getShortcut(Shortcut::AlternateViewMode)); + auto toolBtn = dynamic_cast(p_tb->widgetForAction(act)); Q_ASSERT(toolBtn); toolBtn->setPopupMode(QToolButton::InstantPopup);