From 454e9ee5ca9d8266ab8e5a654a1d4e7a04b40833 Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Thu, 22 Mar 2018 09:52:49 +0100 Subject: [PATCH] Add Context Help to text editor context menu For this make the default context menu for the text editor extensible and add the context help item from the help plugin, which now has an optional dependency on the text editor to ensure correct loading order if both are present. Task-number: QTCREATORBUG-55 Change-Id: I378a491ba3700e65fc262bdb10c8ead5ad62cb33 Reviewed-by: David Schulz --- src/plugins/help/help_dependencies.pri | 2 ++ src/plugins/help/helpplugin.cpp | 8 +++++ src/plugins/texteditor/texteditor.cpp | 31 ++++++-------------- src/plugins/texteditor/texteditorconstants.h | 5 ++++ src/plugins/texteditor/texteditorplugin.cpp | 31 ++++++++++++++++++++ 5 files changed, 55 insertions(+), 22 deletions(-) diff --git a/src/plugins/help/help_dependencies.pri b/src/plugins/help/help_dependencies.pri index 2094a72dd37..cf2e37d3c3b 100644 --- a/src/plugins/help/help_dependencies.pri +++ b/src/plugins/help/help_dependencies.pri @@ -6,3 +6,5 @@ QTC_LIB_DEPENDS += \ QTC_PLUGIN_DEPENDS += \ coreplugin \ projectexplorer +QTC_PLUGIN_RECOMMENDS += \ + texteditor diff --git a/src/plugins/help/helpplugin.cpp b/src/plugins/help/helpplugin.cpp index 263f0aac151..091a8456863 100644 --- a/src/plugins/help/helpplugin.cpp +++ b/src/plugins/help/helpplugin.cpp @@ -254,6 +254,14 @@ HelpPluginPrivate::HelpPluginPrivate() ActionManager::actionContainer(Core::Constants::M_HELP)->addAction(cmd, Core::Constants::G_HELP_HELP); cmd->setDefaultKeySequence(QKeySequence(Qt::Key_F1)); connect(action, &QAction::triggered, this, &HelpPluginPrivate::requestContextHelp); + ActionContainer *textEditorContextMenu = ActionManager::actionContainer( + TextEditor::Constants::M_STANDARDCONTEXTMENU); + if (textEditorContextMenu) { + textEditorContextMenu->insertGroup(TextEditor::Constants::G_BOM, + Core::Constants::G_HELP); + textEditorContextMenu->addSeparator(Core::Constants::G_HELP); + textEditorContextMenu->addAction(cmd, Core::Constants::G_HELP); + } action = new QAction(HelpPlugin::tr("Technical Support"), this); cmd = ActionManager::registerAction(action, "Help.TechSupport"); diff --git a/src/plugins/texteditor/texteditor.cpp b/src/plugins/texteditor/texteditor.cpp index f93d8109e67..a01d8a6e6b3 100644 --- a/src/plugins/texteditor/texteditor.cpp +++ b/src/plugins/texteditor/texteditor.cpp @@ -7762,30 +7762,17 @@ void TextEditorWidget::setupFallBackEditor(Id id) void TextEditorWidget::appendStandardContextMenuActions(QMenu *menu) { menu->addSeparator(); - const auto add = [menu](const Id &id) { - QAction *a = ActionManager::command(id)->action(); - if (a) - menu->addAction(a); - }; - - add(Core::Constants::UNDO); - add(Core::Constants::REDO); - menu->addSeparator(); - add(Core::Constants::CUT); - add(Core::Constants::COPY); - add(Core::Constants::PASTE); - add(Constants::CIRCULAR_PASTE); - menu->addSeparator(); - add(Core::Constants::SELECTALL); - - TextDocument *doc = textDocument(); - if (doc->codec()->name() == QByteArray("UTF-8") && doc->supportsUtf8Bom()) { - QAction *a = ActionManager::command(Constants::SWITCH_UTF8BOM)->action(); - if (a) { + appendMenuActionsFromContext(menu, Constants::M_STANDARDCONTEXTMENU); + Command *bomCmd = ActionManager::command(Constants::SWITCH_UTF8BOM); + if (bomCmd) { + QAction *a = bomCmd->action(); + TextDocument *doc = textDocument(); + if (doc->codec()->name() == QByteArray("UTF-8") && doc->supportsUtf8Bom()) { + a->setVisible(true); a->setText(doc->format().hasUtf8Bom ? tr("Delete UTF-8 BOM on Save") : tr("Add UTF-8 BOM on Save")); - menu->addSeparator(); - menu->addAction(a); + } else { + a->setVisible(false); } } } diff --git a/src/plugins/texteditor/texteditorconstants.h b/src/plugins/texteditor/texteditorconstants.h index e7024614e58..8007c76617f 100644 --- a/src/plugins/texteditor/texteditorconstants.h +++ b/src/plugins/texteditor/texteditorconstants.h @@ -109,6 +109,11 @@ enum TextStyle : quint8 { namespace Constants { const char C_TEXTEDITOR[] = "Text Editor"; +const char M_STANDARDCONTEXTMENU[] = "TextEditor.StandardContextMenu"; +const char G_UNDOREDO[] = "TextEditor.UndoRedoGroup"; +const char G_COPYPASTE[] = "TextEditor.CopyPasteGroup"; +const char G_SELECT[] = "TextEditor.SelectGroup"; +const char G_BOM[] = "TextEditor.BomGroup"; const char COMPLETE_THIS[] = "TextEditor.CompleteThis"; const char QUICKFIX_THIS[] = "TextEditor.QuickFix"; const char SHOWCONTEXTMENU[] = "TextEditor.ShowContextMenu"; diff --git a/src/plugins/texteditor/texteditorplugin.cpp b/src/plugins/texteditor/texteditorplugin.cpp index b0809ad094e..5cacd941c47 100644 --- a/src/plugins/texteditor/texteditorplugin.cpp +++ b/src/plugins/texteditor/texteditorplugin.cpp @@ -40,6 +40,7 @@ #include #include +#include #include #include #include @@ -74,6 +75,8 @@ public: void updateSearchResultsTabWidth(const TextEditor::TabSettings &tabSettings); void updateCurrentSelection(const QString &text); + void createStandardContextMenu(); + TextEditorSettings settings; LineNumberFilter lineNumberFilter; // Goto line functionality for quick open OutlineFactory outlineFactory; @@ -148,6 +151,7 @@ bool TextEditorPlugin::initialize(const QStringList &arguments, QString *errorMe SnippetProvider::registerGroup(Constants::TEXT_SNIPPET_GROUP_ID, tr("Text", "SnippetProvider")); + d->createStandardContextMenu(); return true; } @@ -262,5 +266,32 @@ void TextEditorPluginPrivate::updateCurrentSelection(const QString &text) } } +void TextEditorPluginPrivate::createStandardContextMenu() +{ + ActionContainer *contextMenu = ActionManager::createMenu(Constants::M_STANDARDCONTEXTMENU); + contextMenu->appendGroup(Constants::G_UNDOREDO); + contextMenu->appendGroup(Constants::G_COPYPASTE); + contextMenu->appendGroup(Constants::G_SELECT); + contextMenu->appendGroup(Constants::G_BOM); + + const auto add = [contextMenu](const Id &id, const Id &group) { + Command *cmd = ActionManager::command(id); + if (cmd) + contextMenu->addAction(cmd, group); + }; + + add(Core::Constants::UNDO, Constants::G_UNDOREDO); + add(Core::Constants::REDO, Constants::G_UNDOREDO); + contextMenu->addSeparator(Constants::G_COPYPASTE); + add(Core::Constants::CUT, Constants::G_COPYPASTE); + add(Core::Constants::COPY, Constants::G_COPYPASTE); + add(Core::Constants::PASTE, Constants::G_COPYPASTE); + add(Constants::CIRCULAR_PASTE, Constants::G_COPYPASTE); + contextMenu->addSeparator(Constants::G_SELECT); + add(Core::Constants::SELECTALL, Constants::G_SELECT); + contextMenu->addSeparator(Constants::G_BOM); + add(Constants::SWITCH_UTF8BOM, Constants::G_BOM); +} + } // namespace Internal } // namespace TextEditor