diff --git a/src/libs/utils/parameteraction.cpp b/src/libs/utils/parameteraction.cpp index a77154b41f2..eb7c0547c9c 100644 --- a/src/libs/utils/parameteraction.cpp +++ b/src/libs/utils/parameteraction.cpp @@ -24,14 +24,18 @@ namespace Utils { +ParameterAction::ParameterAction(QObject *parent) + : ParameterAction({}, {}, AlwaysEnabled, parent) +{} + ParameterAction::ParameterAction(const QString &emptyText, - const QString ¶meterText, - EnablingMode mode, - QObject* parent) : - QAction(emptyText, parent), - m_emptyText(emptyText), - m_parameterText(parameterText), - m_enablingMode(mode) + const QString ¶meterText, + EnablingMode mode, + QObject *parent) + : QAction(emptyText, parent) + , m_emptyText(emptyText) + , m_parameterText(parameterText) + , m_enablingMode(mode) { } diff --git a/src/libs/utils/parameteraction.h b/src/libs/utils/parameteraction.h index b2d9679b5ff..4ead5c33906 100644 --- a/src/libs/utils/parameteraction.h +++ b/src/libs/utils/parameteraction.h @@ -19,10 +19,11 @@ public: enum EnablingMode { AlwaysEnabled, EnabledWithParameter }; Q_ENUM(EnablingMode) - explicit ParameterAction(const QString &emptyText, - const QString ¶meterText, - EnablingMode em = AlwaysEnabled, - QObject *parent = nullptr); + explicit ParameterAction(QObject *parent = nullptr); + ParameterAction(const QString &emptyText, + const QString ¶meterText, + EnablingMode em = AlwaysEnabled, + QObject *parent = nullptr); QString emptyText() const; void setEmptyText(const QString &); diff --git a/src/plugins/coreplugin/actionmanager/actionmanager.cpp b/src/plugins/coreplugin/actionmanager/actionmanager.cpp index 736a16ed297..f789def20fe 100644 --- a/src/plugins/coreplugin/actionmanager/actionmanager.cpp +++ b/src/plugins/coreplugin/actionmanager/actionmanager.cpp @@ -75,8 +75,8 @@ class ActionBuilderPrivate { public: ActionBuilderPrivate(QObject *contextActionParent, const Id actionId) - : contextAction(new ParameterAction({}, {}, ParameterAction::AlwaysEnabled, contextActionParent)) - , actionId(actionId) + : actionId(actionId) + , m_parent(contextActionParent) { command = ActionManager::createCommand(actionId); } @@ -84,15 +84,40 @@ public: void registerAction() { QTC_ASSERT(actionId.isValid(), return); - ActionManager::registerAction(contextAction, actionId, context, scriptable); + ActionManager::registerAction(contextAction(), actionId, context, scriptable); + } + + ParameterAction *contextAction() + { + if (!m_contextAction) { + QTC_CHECK(m_parent); + m_contextAction = new ParameterAction(m_parent); + } + return m_contextAction; + } + + void adopt(ParameterAction *action) + { + QTC_ASSERT(!m_contextAction, + qWarning() << QLatin1String("Cannot adopt context action for \"%1\"after it " + "already has been created.") + .arg(actionId.toString()); + return); + QTC_ASSERT(action, + qWarning() << QLatin1String("Adopt called with nullptr action for \"%1\".") + .arg(actionId.toString())); + m_contextAction = action; } - ParameterAction *contextAction = nullptr; Command *command = nullptr; Id actionId; Context context{Constants::C_GLOBAL}; bool scriptable = false; + +private: + QObject *m_parent = nullptr; + ParameterAction *m_contextAction = nullptr; }; /*! @@ -111,8 +136,6 @@ public: */ /*! - \fn ActionBuilder::ActionBuilder(QObject *contextActionParent, const Id actionId) - Constructs an action builder for an action with the Id \a actionId. The \a contextActionParent is used to provide a QObject parent for the @@ -122,23 +145,25 @@ public: */ ActionBuilder::ActionBuilder(QObject *contextActionParent, const Id actionId) : d(new ActionBuilderPrivate(contextActionParent, actionId)) -{ -} +{} /*! - \fn ActionBuilder::~ActionBuilder - Registers the created action with the set properties. \sa ActionManager::registerAction() */ - ActionBuilder::~ActionBuilder() { d->registerAction(); delete d; } +ActionBuilder &ActionBuilder::adopt(Utils::ParameterAction *action) +{ + d->adopt(action); + return *this; +} + /*! Sets the \c text property of the action under construction to \a text. @@ -146,7 +171,7 @@ ActionBuilder::~ActionBuilder() */ ActionBuilder &ActionBuilder::setText(const QString &text) { - d->contextAction->setText(text); + d->contextAction()->setText(text); return *this; } @@ -157,13 +182,13 @@ ActionBuilder &ActionBuilder::setText(const QString &text) */ ActionBuilder &ActionBuilder::setIconText(const QString &iconText) { - d->contextAction->setIconText(iconText); + d->contextAction()->setIconText(iconText); return *this; } ActionBuilder &ActionBuilder::setToolTip(const QString &toolTip) { - d->contextAction->setToolTip(toolTip); + d->contextAction()->setToolTip(toolTip); return *this; } @@ -199,7 +224,7 @@ ActionBuilder &ActionBuilder::addToContainers(QList containerIds, Id groupId ActionBuilder &ActionBuilder::addOnTriggered(const std::function &func) { - QObject::connect(d->contextAction, &QAction::triggered, d->contextAction, func); + QObject::connect(d->contextAction(), &QAction::triggered, d->contextAction(), func); return *this; } @@ -223,13 +248,13 @@ ActionBuilder &ActionBuilder::setDefaultKeySequence(const QString &mac, const QS ActionBuilder &ActionBuilder::setIcon(const QIcon &icon) { - d->contextAction->setIcon(icon); + d->contextAction()->setIcon(icon); return *this; } ActionBuilder &ActionBuilder::setIconVisibleInMenu(bool on) { - d->contextAction->setIconVisibleInMenu(on); + d->contextAction()->setIconVisibleInMenu(on); return *this; } @@ -247,31 +272,31 @@ ActionBuilder &ActionBuilder::setTouchBarText(const QString &text) ActionBuilder &ActionBuilder::setEnabled(bool on) { - d->contextAction->setEnabled(on); + d->contextAction()->setEnabled(on); return *this; } ActionBuilder &ActionBuilder::setChecked(bool on) { - d->contextAction->setChecked(on); + d->contextAction()->setChecked(on); return *this; } ActionBuilder &ActionBuilder::setVisible(bool on) { - d->contextAction->setVisible(on); + d->contextAction()->setVisible(on); return *this; } ActionBuilder &ActionBuilder::setCheckable(bool on) { - d->contextAction->setCheckable(on); + d->contextAction()->setCheckable(on); return *this; } ActionBuilder &ActionBuilder::setSeperator(bool on) { - d->contextAction->setSeparator(on); + d->contextAction()->setSeparator(on); return *this; } @@ -283,7 +308,7 @@ ActionBuilder &ActionBuilder::setScriptable(bool on) ActionBuilder &ActionBuilder::setMenuRole(QAction::MenuRole role) { - d->contextAction->setMenuRole(role); + d->contextAction()->setMenuRole(role); return *this; } @@ -294,12 +319,12 @@ ActionBuilder &ActionBuilder::setParameterText(const QString ¶meterText, QTC_CHECK(parameterText.contains("%1")); QTC_CHECK(!emptyText.contains("%1")); - d->contextAction->setEmptyText(emptyText); - d->contextAction->setParameterText(parameterText); - d->contextAction->setEnablingMode(mode == AlwaysEnabled - ? ParameterAction::AlwaysEnabled - : ParameterAction::EnabledWithParameter); - d->contextAction->setText(emptyText); + d->contextAction()->setEmptyText(emptyText); + d->contextAction()->setParameterText(parameterText); + d->contextAction()->setEnablingMode(mode == AlwaysEnabled + ? ParameterAction::AlwaysEnabled + : ParameterAction::EnabledWithParameter); + d->contextAction()->setText(emptyText); return *this; } @@ -320,31 +345,31 @@ QAction *ActionBuilder::commandAction() const QAction *ActionBuilder::contextAction() const { - return d->contextAction; + return d->contextAction(); } ParameterAction *ActionBuilder::contextParameterAction() const { - return d->contextAction; + return d->contextAction(); } ActionBuilder &ActionBuilder::bindContextAction(QAction **dest) { QTC_ASSERT(dest, return *this); - *dest = d->contextAction; + *dest = d->contextAction(); return *this; } ActionBuilder &ActionBuilder::bindContextAction(Utils::ParameterAction **dest) { QTC_ASSERT(dest, return *this); - *dest = d->contextAction; + *dest = d->contextAction(); return *this; } ActionBuilder &ActionBuilder::augmentActionWithShortcutToolTip() { - d->command->augmentActionWithShortcutToolTip(d->contextAction); + d->command->augmentActionWithShortcutToolTip(d->contextAction()); return *this; } diff --git a/src/plugins/coreplugin/actionmanager/actionmanager.h b/src/plugins/coreplugin/actionmanager/actionmanager.h index d7825fbd15d..c7d490bcd56 100644 --- a/src/plugins/coreplugin/actionmanager/actionmanager.h +++ b/src/plugins/coreplugin/actionmanager/actionmanager.h @@ -31,6 +31,8 @@ public: ActionBuilder(QObject *contextActionParent, const Utils::Id actionId); ~ActionBuilder(); + ActionBuilder &adopt(Utils::ParameterAction *action); + ActionBuilder &setContext(const Utils::Id id); ActionBuilder &setContext(const Core::Context &context); ActionBuilder &setText(const QString &text); diff --git a/src/plugins/texteditor/markdowneditor.cpp b/src/plugins/texteditor/markdowneditor.cpp index b574a76074f..6f250d23eb5 100644 --- a/src/plugins/texteditor/markdowneditor.cpp +++ b/src/plugins/texteditor/markdowneditor.cpp @@ -510,57 +510,76 @@ MarkdownEditorFactory::MarkdownEditorFactory() const auto textContext = Context(MARKDOWNVIEWER_TEXT_CONTEXT); const auto context = Context(MARKDOWNVIEWER_ID); - Command *cmd = nullptr; - cmd = ActionManager::registerAction(&m_emphasisAction, EMPHASIS_ACTION, textContext); - cmd->setDescription(Tr::tr("Emphasis")); - QObject::connect(&m_emphasisAction, &QAction::triggered, EditorManager::instance(), [] { - auto editor = qobject_cast(EditorManager::currentEditor()); - if (editor) - editor->triggerEmphasis(); - }); - cmd = ActionManager::registerAction(&m_strongAction, STRONG_ACTION, textContext); - cmd->setDescription(Tr::tr("Strong")); - QObject::connect(&m_strongAction, &QAction::triggered, EditorManager::instance(), [] { - auto editor = qobject_cast(EditorManager::currentEditor()); - if (editor) - editor->triggerStrong(); - }); - cmd = ActionManager::registerAction(&m_inlineCodeAction, INLINECODE_ACTION, textContext); - cmd->setDescription(Tr::tr("Inline Code")); - QObject::connect(&m_inlineCodeAction, &QAction::triggered, EditorManager::instance(), [] { - auto editor = qobject_cast(EditorManager::currentEditor()); - if (editor) - editor->triggerInlineCode(); - }); - cmd = ActionManager::registerAction(&m_linkAction, LINK_ACTION, textContext); - cmd->setDescription(Tr::tr("Hyperlink")); - QObject::connect(&m_linkAction, &QAction::triggered, EditorManager::instance(), [] { - auto editor = qobject_cast(EditorManager::currentEditor()); - if (editor) - editor->triggerLink(); - }); - cmd = ActionManager::registerAction(&m_toggleEditorAction, TOGGLEEDITOR_ACTION, context); - cmd->setDescription(Tr::tr("Show Editor")); - QObject::connect(&m_toggleEditorAction, &QAction::triggered, EditorManager::instance(), [] { - auto editor = qobject_cast(EditorManager::currentEditor()); - if (editor) - editor->toggleEditor(); - }); - cmd = ActionManager::registerAction(&m_togglePreviewAction, TOGGLEPREVIEW_ACTION, context); - cmd->setDescription(Tr::tr("Show Preview")); - QObject::connect(&m_togglePreviewAction, &QAction::triggered, EditorManager::instance(), [] { - auto editor = qobject_cast(EditorManager::currentEditor()); - if (editor) - editor->togglePreview(); - }); - cmd = ActionManager::registerAction(&m_swapAction, SWAPVIEWS_ACTION, context); - cmd->setDescription(Tr::tr("Swap Views")); - QObject::connect(&m_swapAction, &QAction::triggered, EditorManager::instance(), [] { - auto editor = qobject_cast(EditorManager::currentEditor()); - if (editor) - editor->swapViews(); - }); + ActionBuilder(nullptr, EMPHASIS_ACTION) + .adopt(&m_emphasisAction) + .setText(Tr::tr("Emphasis")) + .setContext(textContext) + .addOnTriggered(EditorManager::instance(), [] { + auto editor = qobject_cast(EditorManager::currentEditor()); + if (editor) + editor->triggerEmphasis(); + }); + + ActionBuilder(nullptr, STRONG_ACTION) + .adopt(&m_strongAction) + .setText("Strong") + .setContext(textContext) + .addOnTriggered(EditorManager::instance(), [] { + auto editor = qobject_cast(EditorManager::currentEditor()); + if (editor) + editor->triggerStrong(); + }); + + ActionBuilder(nullptr, INLINECODE_ACTION) + .adopt(&m_inlineCodeAction) + .setText(Tr::tr("Inline Code")) + .setContext(textContext) + .addOnTriggered(EditorManager::instance(), [] { + auto editor = qobject_cast(EditorManager::currentEditor()); + if (editor) + editor->triggerInlineCode(); + }); + + ActionBuilder(nullptr, LINK_ACTION) + .adopt(&m_linkAction) + .setText(Tr::tr("Hyperlink")) + .setContext(textContext) + .addOnTriggered(EditorManager::instance(), [] { + auto editor = qobject_cast(EditorManager::currentEditor()); + if (editor) + editor->triggerLink(); + }); + + ActionBuilder(nullptr, TOGGLEEDITOR_ACTION) + .adopt(&m_toggleEditorAction) + .setText(Tr::tr("Show Editor")) + .setContext(context) + .addOnTriggered(EditorManager::instance(), [] { + auto editor = qobject_cast(EditorManager::currentEditor()); + if (editor) + editor->toggleEditor(); + }); + + ActionBuilder(nullptr, TOGGLEPREVIEW_ACTION) + .adopt(&m_togglePreviewAction) + .setText(Tr::tr("Show Preview")) + .setContext(context) + .addOnTriggered(EditorManager::instance(), [] { + auto editor = qobject_cast(EditorManager::currentEditor()); + if (editor) + editor->togglePreview(); + }); + + ActionBuilder(nullptr, SWAPVIEWS_ACTION) + .adopt(&m_swapAction) + .setText(Tr::tr("Swap Views")) + .setContext(context) + .addOnTriggered(EditorManager::instance(), [] { + auto editor = qobject_cast(EditorManager::currentEditor()); + if (editor) + editor->swapViews(); + }); } void MarkdownEditorWidget::findLinkAt(const QTextCursor &cursor, diff --git a/src/plugins/texteditor/markdowneditor.h b/src/plugins/texteditor/markdowneditor.h index 92fc18a8b88..54194342606 100644 --- a/src/plugins/texteditor/markdowneditor.h +++ b/src/plugins/texteditor/markdowneditor.h @@ -7,7 +7,7 @@ #include -#include +#include namespace TextEditor::Internal { @@ -18,13 +18,13 @@ public: private: TextEditor::TextEditorActionHandler m_actionHandler; - QAction m_emphasisAction; - QAction m_strongAction; - QAction m_inlineCodeAction; - QAction m_linkAction; - QAction m_toggleEditorAction; - QAction m_togglePreviewAction; - QAction m_swapAction; + Utils::ParameterAction m_emphasisAction; + Utils::ParameterAction m_strongAction; + Utils::ParameterAction m_inlineCodeAction; + Utils::ParameterAction m_linkAction; + Utils::ParameterAction m_toggleEditorAction; + Utils::ParameterAction m_togglePreviewAction; + Utils::ParameterAction m_swapAction; }; } // TextEditor::Internal