From cb8a1df337d8983ec1adaac13499ea13b09c1195 Mon Sep 17 00:00:00 2001 From: Le Tan Date: Mon, 25 Dec 2017 19:44:12 +0800 Subject: [PATCH] support shortcuts for external programs 1. External editor format: name=program %0, 2. Add "OpenViaDefaultProgram" shortcut to call system's default program to open current note. "F12" by default. --- .../docs/tips_external_program_en.md | 13 +++--- .../docs/tips_external_program_zh.md | 13 +++--- src/resources/vnote.ini | 8 +++- src/vconfigmanager.cpp | 23 ++++++++-- src/vconfigmanager.h | 15 ++++++- src/vfilelist.cpp | 43 ++++++++++++++++--- 6 files changed, 94 insertions(+), 21 deletions(-) diff --git a/src/resources/docs/tips_external_program_en.md b/src/resources/docs/tips_external_program_en.md index 040f6181..05062702 100644 --- a/src/resources/docs/tips_external_program_en.md +++ b/src/resources/docs/tips_external_program_en.md @@ -6,13 +6,16 @@ A sample configuration may look like this: ```ini [external_editors] ; Define external editors which could be called to edit notes -; One program per line with the format name="program %0 arg1 arg2" -; in which %0 will be replaced with the note file path +; One program per line with the format name="program \"%0\" arg1 arg2", +; in which %0 will be replaced with the note file path (so it is better to enclose it +; with double quotes) +; Shortcut could be empty ; Need to escape \ and ", use double quotes to quote paths/arguments with spaces +; SHOULD defined in user config file, not here -GVim=C:\\\"Program Files (x86)\"\\Vim\\vim80\\gvim.exe %0 -Notepad=notepad %0 -Notepad%2B%2B=C:\\\"Program Files (x86)\"\\notepad++\\notepad++.exe %0 +GVim=C:\\\"Program Files (x86)\"\\Vim\\vim80\\gvim.exe \"%0\", F4 +Notepad=notepad \"%0\" +Notepad%2B%2B=C:\\\"Program Files (x86)\"\\notepad++\\notepad++.exe \"%0\" ``` A restart is needed to detect new external programs. diff --git a/src/resources/docs/tips_external_program_zh.md b/src/resources/docs/tips_external_program_zh.md index 32eeff93..ba0b3bdd 100644 --- a/src/resources/docs/tips_external_program_zh.md +++ b/src/resources/docs/tips_external_program_zh.md @@ -6,13 +6,16 @@ VNote支持使用外部程序来打开笔记。VNote将外部程序的配置信 ```ini [external_editors] ; Define external editors which could be called to edit notes -; One program per line with the format name="program %0 arg1 arg2" -; in which %0 will be replaced with the note file path +; One program per line with the format name="program \"%0\" arg1 arg2", +; in which %0 will be replaced with the note file path (so it is better to enclose it +; with double quotes) +; Shortcut could be empty ; Need to escape \ and ", use double quotes to quote paths/arguments with spaces +; SHOULD defined in user config file, not here -GVim=C:\\\"Program Files (x86)\"\\Vim\\vim80\\gvim.exe %0 -Notepad=notepad %0 -Notepad%2B%2B=C:\\\"Program Files (x86)\"\\notepad++\\notepad++.exe %0 +GVim=C:\\\"Program Files (x86)\"\\Vim\\vim80\\gvim.exe \"%0\", F4 +Notepad=notepad \"%0\" +Notepad%2B%2B=C:\\\"Program Files (x86)\"\\notepad++\\notepad++.exe \"%0\" ``` VNote需要重启以检测新的外部程序。 diff --git a/src/resources/vnote.ini b/src/resources/vnote.ini index c577724b..5b54d301 100644 --- a/src/resources/vnote.ini +++ b/src/resources/vnote.ini @@ -222,6 +222,8 @@ ActivateNextTab=Ctrl+Tab ActivatePreviousTab=Ctrl+Shift+Tab ; Activate flash page FlashPage=Ctrl+Alt+L +; Open via system's default program +OpenViaDefaultProgram=F12 [captain_mode_shortcuts] ; Define shortcuts in Captain mode here. @@ -285,7 +287,9 @@ ApplySnippet=S [external_editors] ; Define external editors which could be called to edit notes -; One program per line with the format name="program %0 arg1 arg2" -; in which %0 will be replaced with the note file path +; One program per line with the format name="program \"%0\" arg1 arg2", +; in which %0 will be replaced with the note file path (so it is better to enclose it +; with double quotes) +; Shortcut could be empty ; Need to escape \ and ", use double quotes to quote paths/arguments with spaces ; SHOULD defined in user config file, not here diff --git a/src/vconfigmanager.cpp b/src/vconfigmanager.cpp index 13f388d6..310bde90 100644 --- a/src/vconfigmanager.cpp +++ b/src/vconfigmanager.cpp @@ -1145,9 +1145,9 @@ QVector VConfigManager::getCustomMagicWords() return words; } -QVector> VConfigManager::getExternalEditors() const +QVector VConfigManager::getExternalEditors() const { - QVector> ret; + QVector ret; userSettings->beginGroup("external_editors"); QStringList keys = userSettings->childKeys(); for (auto const & key : keys) { @@ -1155,7 +1155,24 @@ QVector> VConfigManager::getExternalEditors() const continue; } - ret.push_back(QPair(key, userSettings->value(key).toString())); + QStringList val = userSettings->value(key).toStringList(); + if (val.size() > 2 + || val.isEmpty()) { + continue; + } + + VExternalEditor editor; + editor.m_name = key; + editor.m_cmd = val[0].trimmed(); + if (editor.m_cmd.isEmpty()) { + continue; + } + + if (val.size() == 2) { + editor.m_shortcut = val[1].trimmed(); + } + + ret.push_back(editor); } userSettings->endGroup(); diff --git a/src/vconfigmanager.h b/src/vconfigmanager.h index 48b78cb8..679b648d 100644 --- a/src/vconfigmanager.h +++ b/src/vconfigmanager.h @@ -18,6 +18,7 @@ class QJsonObject; class QString; + enum MarkdownConverterType { Hoedown = 0, @@ -26,12 +27,24 @@ enum MarkdownConverterType Showdown }; + struct VColor { QString m_name; QString m_color; // #RGB or color name. }; + +struct VExternalEditor +{ + QString m_name; + + QString m_cmd; + + QString m_shortcut; +}; + + struct MarkdownitOption { MarkdownitOption(bool p_html, bool p_breaks, bool p_linkify) @@ -390,7 +403,7 @@ public: bool getEnableBackupFile() const; // Get defined external editors. - QVector> getExternalEditors() const; + QVector getExternalEditors() const; const QString &getVimExemptionKeys() const; diff --git a/src/vfilelist.cpp b/src/vfilelist.cpp index bb756f73..2886d94d 100644 --- a/src/vfilelist.cpp +++ b/src/vfilelist.cpp @@ -975,18 +975,51 @@ void VFileList::initOpenWithMenu() auto programs = g_config->getExternalEditors(); for (auto const & pa : programs) { - QAction *act = new QAction(pa.first, this); - act->setToolTip(tr("Open current note with %1").arg(pa.first)); - act->setStatusTip(pa.second); - act->setData(pa.second); + QKeySequence seq = QKeySequence(pa.m_shortcut); + QString name = pa.m_name; + if (!seq.isEmpty()) { + name = QString("%1\t%2").arg(pa.m_name) + .arg(VUtils::getShortcutText(pa.m_shortcut)); + } + + QAction *act = new QAction(name, this); + act->setToolTip(tr("Open current note with %1").arg(pa.m_name)); + act->setStatusTip(pa.m_cmd); + act->setData(pa.m_cmd); + + if (!seq.isEmpty()) { + QShortcut *shortcut = new QShortcut(seq, this); + shortcut->setContext(Qt::WidgetWithChildrenShortcut); + connect(shortcut, &QShortcut::activated, + this, [act](){ + act->trigger(); + }); + } + connect(act, &QAction::triggered, this, &VFileList::handleOpenWithActionTriggered); m_openWithMenu->addAction(act); } - QAction *defaultAct = new QAction(tr("System's Default Program"), this); + QKeySequence seq(g_config->getShortcutKeySequence("OpenViaDefaultProgram")); + QString name = tr("System's Default Program"); + if (!seq.isEmpty()) { + name = QString("%1\t%2").arg(name) + .arg(VUtils::getShortcutText(g_config->getShortcutKeySequence("OpenViaDefaultProgram"))); + } + + QAction *defaultAct = new QAction(name, this); defaultAct->setToolTip(tr("Open current note with system's default program")); + if (!seq.isEmpty()) { + QShortcut *shortcut = new QShortcut(seq, this); + shortcut->setContext(Qt::WidgetWithChildrenShortcut); + connect(shortcut, &QShortcut::activated, + this, [defaultAct](){ + defaultAct->trigger(); + }); + } + connect(defaultAct, &QAction::triggered, this, [this]() { QListWidgetItem *item = fileList->currentItem();