Use ActionBuilder for Markdown editor

Also add method that sets the ParameterAction for the context action.
That is an alternative to "binding" the context action, and is useful in this
case, since we do not have a QObject that we can use as a parent for the
constructor that creates the ParameterAction from scratch.

Change-Id: I6ba05208d33460cfa2df9ce8247f7ca30624c22f
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Eike Ziller
2024-01-24 15:07:07 +01:00
parent d9404208da
commit f22fa5275e
6 changed files with 154 additions and 103 deletions

View File

@@ -24,14 +24,18 @@
namespace Utils { namespace Utils {
ParameterAction::ParameterAction(QObject *parent)
: ParameterAction({}, {}, AlwaysEnabled, parent)
{}
ParameterAction::ParameterAction(const QString &emptyText, ParameterAction::ParameterAction(const QString &emptyText,
const QString &parameterText, const QString &parameterText,
EnablingMode mode, EnablingMode mode,
QObject* parent) : QObject *parent)
QAction(emptyText, parent), : QAction(emptyText, parent)
m_emptyText(emptyText), , m_emptyText(emptyText)
m_parameterText(parameterText), , m_parameterText(parameterText)
m_enablingMode(mode) , m_enablingMode(mode)
{ {
} }

View File

@@ -19,7 +19,8 @@ public:
enum EnablingMode { AlwaysEnabled, EnabledWithParameter }; enum EnablingMode { AlwaysEnabled, EnabledWithParameter };
Q_ENUM(EnablingMode) Q_ENUM(EnablingMode)
explicit ParameterAction(const QString &emptyText, explicit ParameterAction(QObject *parent = nullptr);
ParameterAction(const QString &emptyText,
const QString &parameterText, const QString &parameterText,
EnablingMode em = AlwaysEnabled, EnablingMode em = AlwaysEnabled,
QObject *parent = nullptr); QObject *parent = nullptr);

View File

@@ -75,8 +75,8 @@ class ActionBuilderPrivate
{ {
public: public:
ActionBuilderPrivate(QObject *contextActionParent, const Id actionId) ActionBuilderPrivate(QObject *contextActionParent, const Id actionId)
: contextAction(new ParameterAction({}, {}, ParameterAction::AlwaysEnabled, contextActionParent)) : actionId(actionId)
, actionId(actionId) , m_parent(contextActionParent)
{ {
command = ActionManager::createCommand(actionId); command = ActionManager::createCommand(actionId);
} }
@@ -84,15 +84,40 @@ public:
void registerAction() void registerAction()
{ {
QTC_ASSERT(actionId.isValid(), return); 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; Command *command = nullptr;
Id actionId; Id actionId;
Context context{Constants::C_GLOBAL}; Context context{Constants::C_GLOBAL};
bool scriptable = false; 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. Constructs an action builder for an action with the Id \a actionId.
The \a contextActionParent is used to provide a QObject parent for the The \a contextActionParent is used to provide a QObject parent for the
@@ -122,23 +145,25 @@ public:
*/ */
ActionBuilder::ActionBuilder(QObject *contextActionParent, const Id actionId) ActionBuilder::ActionBuilder(QObject *contextActionParent, const Id actionId)
: d(new ActionBuilderPrivate(contextActionParent, actionId)) : d(new ActionBuilderPrivate(contextActionParent, actionId))
{ {}
}
/*! /*!
\fn ActionBuilder::~ActionBuilder
Registers the created action with the set properties. Registers the created action with the set properties.
\sa ActionManager::registerAction() \sa ActionManager::registerAction()
*/ */
ActionBuilder::~ActionBuilder() ActionBuilder::~ActionBuilder()
{ {
d->registerAction(); d->registerAction();
delete d; 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. 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) ActionBuilder &ActionBuilder::setText(const QString &text)
{ {
d->contextAction->setText(text); d->contextAction()->setText(text);
return *this; return *this;
} }
@@ -157,13 +182,13 @@ ActionBuilder &ActionBuilder::setText(const QString &text)
*/ */
ActionBuilder &ActionBuilder::setIconText(const QString &iconText) ActionBuilder &ActionBuilder::setIconText(const QString &iconText)
{ {
d->contextAction->setIconText(iconText); d->contextAction()->setIconText(iconText);
return *this; return *this;
} }
ActionBuilder &ActionBuilder::setToolTip(const QString &toolTip) ActionBuilder &ActionBuilder::setToolTip(const QString &toolTip)
{ {
d->contextAction->setToolTip(toolTip); d->contextAction()->setToolTip(toolTip);
return *this; return *this;
} }
@@ -199,7 +224,7 @@ ActionBuilder &ActionBuilder::addToContainers(QList<Id> containerIds, Id groupId
ActionBuilder &ActionBuilder::addOnTriggered(const std::function<void()> &func) ActionBuilder &ActionBuilder::addOnTriggered(const std::function<void()> &func)
{ {
QObject::connect(d->contextAction, &QAction::triggered, d->contextAction, func); QObject::connect(d->contextAction(), &QAction::triggered, d->contextAction(), func);
return *this; return *this;
} }
@@ -223,13 +248,13 @@ ActionBuilder &ActionBuilder::setDefaultKeySequence(const QString &mac, const QS
ActionBuilder &ActionBuilder::setIcon(const QIcon &icon) ActionBuilder &ActionBuilder::setIcon(const QIcon &icon)
{ {
d->contextAction->setIcon(icon); d->contextAction()->setIcon(icon);
return *this; return *this;
} }
ActionBuilder &ActionBuilder::setIconVisibleInMenu(bool on) ActionBuilder &ActionBuilder::setIconVisibleInMenu(bool on)
{ {
d->contextAction->setIconVisibleInMenu(on); d->contextAction()->setIconVisibleInMenu(on);
return *this; return *this;
} }
@@ -247,31 +272,31 @@ ActionBuilder &ActionBuilder::setTouchBarText(const QString &text)
ActionBuilder &ActionBuilder::setEnabled(bool on) ActionBuilder &ActionBuilder::setEnabled(bool on)
{ {
d->contextAction->setEnabled(on); d->contextAction()->setEnabled(on);
return *this; return *this;
} }
ActionBuilder &ActionBuilder::setChecked(bool on) ActionBuilder &ActionBuilder::setChecked(bool on)
{ {
d->contextAction->setChecked(on); d->contextAction()->setChecked(on);
return *this; return *this;
} }
ActionBuilder &ActionBuilder::setVisible(bool on) ActionBuilder &ActionBuilder::setVisible(bool on)
{ {
d->contextAction->setVisible(on); d->contextAction()->setVisible(on);
return *this; return *this;
} }
ActionBuilder &ActionBuilder::setCheckable(bool on) ActionBuilder &ActionBuilder::setCheckable(bool on)
{ {
d->contextAction->setCheckable(on); d->contextAction()->setCheckable(on);
return *this; return *this;
} }
ActionBuilder &ActionBuilder::setSeperator(bool on) ActionBuilder &ActionBuilder::setSeperator(bool on)
{ {
d->contextAction->setSeparator(on); d->contextAction()->setSeparator(on);
return *this; return *this;
} }
@@ -283,7 +308,7 @@ ActionBuilder &ActionBuilder::setScriptable(bool on)
ActionBuilder &ActionBuilder::setMenuRole(QAction::MenuRole role) ActionBuilder &ActionBuilder::setMenuRole(QAction::MenuRole role)
{ {
d->contextAction->setMenuRole(role); d->contextAction()->setMenuRole(role);
return *this; return *this;
} }
@@ -294,12 +319,12 @@ ActionBuilder &ActionBuilder::setParameterText(const QString &parameterText,
QTC_CHECK(parameterText.contains("%1")); QTC_CHECK(parameterText.contains("%1"));
QTC_CHECK(!emptyText.contains("%1")); QTC_CHECK(!emptyText.contains("%1"));
d->contextAction->setEmptyText(emptyText); d->contextAction()->setEmptyText(emptyText);
d->contextAction->setParameterText(parameterText); d->contextAction()->setParameterText(parameterText);
d->contextAction->setEnablingMode(mode == AlwaysEnabled d->contextAction()->setEnablingMode(mode == AlwaysEnabled
? ParameterAction::AlwaysEnabled ? ParameterAction::AlwaysEnabled
: ParameterAction::EnabledWithParameter); : ParameterAction::EnabledWithParameter);
d->contextAction->setText(emptyText); d->contextAction()->setText(emptyText);
return *this; return *this;
} }
@@ -320,31 +345,31 @@ QAction *ActionBuilder::commandAction() const
QAction *ActionBuilder::contextAction() const QAction *ActionBuilder::contextAction() const
{ {
return d->contextAction; return d->contextAction();
} }
ParameterAction *ActionBuilder::contextParameterAction() const ParameterAction *ActionBuilder::contextParameterAction() const
{ {
return d->contextAction; return d->contextAction();
} }
ActionBuilder &ActionBuilder::bindContextAction(QAction **dest) ActionBuilder &ActionBuilder::bindContextAction(QAction **dest)
{ {
QTC_ASSERT(dest, return *this); QTC_ASSERT(dest, return *this);
*dest = d->contextAction; *dest = d->contextAction();
return *this; return *this;
} }
ActionBuilder &ActionBuilder::bindContextAction(Utils::ParameterAction **dest) ActionBuilder &ActionBuilder::bindContextAction(Utils::ParameterAction **dest)
{ {
QTC_ASSERT(dest, return *this); QTC_ASSERT(dest, return *this);
*dest = d->contextAction; *dest = d->contextAction();
return *this; return *this;
} }
ActionBuilder &ActionBuilder::augmentActionWithShortcutToolTip() ActionBuilder &ActionBuilder::augmentActionWithShortcutToolTip()
{ {
d->command->augmentActionWithShortcutToolTip(d->contextAction); d->command->augmentActionWithShortcutToolTip(d->contextAction());
return *this; return *this;
} }

View File

@@ -31,6 +31,8 @@ public:
ActionBuilder(QObject *contextActionParent, const Utils::Id actionId); ActionBuilder(QObject *contextActionParent, const Utils::Id actionId);
~ActionBuilder(); ~ActionBuilder();
ActionBuilder &adopt(Utils::ParameterAction *action);
ActionBuilder &setContext(const Utils::Id id); ActionBuilder &setContext(const Utils::Id id);
ActionBuilder &setContext(const Core::Context &context); ActionBuilder &setContext(const Core::Context &context);
ActionBuilder &setText(const QString &text); ActionBuilder &setText(const QString &text);

View File

@@ -510,53 +510,72 @@ MarkdownEditorFactory::MarkdownEditorFactory()
const auto textContext = Context(MARKDOWNVIEWER_TEXT_CONTEXT); const auto textContext = Context(MARKDOWNVIEWER_TEXT_CONTEXT);
const auto context = Context(MARKDOWNVIEWER_ID); const auto context = Context(MARKDOWNVIEWER_ID);
Command *cmd = nullptr;
cmd = ActionManager::registerAction(&m_emphasisAction, EMPHASIS_ACTION, textContext); ActionBuilder(nullptr, EMPHASIS_ACTION)
cmd->setDescription(Tr::tr("Emphasis")); .adopt(&m_emphasisAction)
QObject::connect(&m_emphasisAction, &QAction::triggered, EditorManager::instance(), [] { .setText(Tr::tr("Emphasis"))
.setContext(textContext)
.addOnTriggered(EditorManager::instance(), [] {
auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor()); auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor());
if (editor) if (editor)
editor->triggerEmphasis(); editor->triggerEmphasis();
}); });
cmd = ActionManager::registerAction(&m_strongAction, STRONG_ACTION, textContext);
cmd->setDescription(Tr::tr("Strong")); ActionBuilder(nullptr, STRONG_ACTION)
QObject::connect(&m_strongAction, &QAction::triggered, EditorManager::instance(), [] { .adopt(&m_strongAction)
.setText("Strong")
.setContext(textContext)
.addOnTriggered(EditorManager::instance(), [] {
auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor()); auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor());
if (editor) if (editor)
editor->triggerStrong(); editor->triggerStrong();
}); });
cmd = ActionManager::registerAction(&m_inlineCodeAction, INLINECODE_ACTION, textContext);
cmd->setDescription(Tr::tr("Inline Code")); ActionBuilder(nullptr, INLINECODE_ACTION)
QObject::connect(&m_inlineCodeAction, &QAction::triggered, EditorManager::instance(), [] { .adopt(&m_inlineCodeAction)
.setText(Tr::tr("Inline Code"))
.setContext(textContext)
.addOnTriggered(EditorManager::instance(), [] {
auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor()); auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor());
if (editor) if (editor)
editor->triggerInlineCode(); editor->triggerInlineCode();
}); });
cmd = ActionManager::registerAction(&m_linkAction, LINK_ACTION, textContext);
cmd->setDescription(Tr::tr("Hyperlink")); ActionBuilder(nullptr, LINK_ACTION)
QObject::connect(&m_linkAction, &QAction::triggered, EditorManager::instance(), [] { .adopt(&m_linkAction)
.setText(Tr::tr("Hyperlink"))
.setContext(textContext)
.addOnTriggered(EditorManager::instance(), [] {
auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor()); auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor());
if (editor) if (editor)
editor->triggerLink(); editor->triggerLink();
}); });
cmd = ActionManager::registerAction(&m_toggleEditorAction, TOGGLEEDITOR_ACTION, context); ActionBuilder(nullptr, TOGGLEEDITOR_ACTION)
cmd->setDescription(Tr::tr("Show Editor")); .adopt(&m_toggleEditorAction)
QObject::connect(&m_toggleEditorAction, &QAction::triggered, EditorManager::instance(), [] { .setText(Tr::tr("Show Editor"))
.setContext(context)
.addOnTriggered(EditorManager::instance(), [] {
auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor()); auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor());
if (editor) if (editor)
editor->toggleEditor(); editor->toggleEditor();
}); });
cmd = ActionManager::registerAction(&m_togglePreviewAction, TOGGLEPREVIEW_ACTION, context);
cmd->setDescription(Tr::tr("Show Preview")); ActionBuilder(nullptr, TOGGLEPREVIEW_ACTION)
QObject::connect(&m_togglePreviewAction, &QAction::triggered, EditorManager::instance(), [] { .adopt(&m_togglePreviewAction)
.setText(Tr::tr("Show Preview"))
.setContext(context)
.addOnTriggered(EditorManager::instance(), [] {
auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor()); auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor());
if (editor) if (editor)
editor->togglePreview(); editor->togglePreview();
}); });
cmd = ActionManager::registerAction(&m_swapAction, SWAPVIEWS_ACTION, context);
cmd->setDescription(Tr::tr("Swap Views")); ActionBuilder(nullptr, SWAPVIEWS_ACTION)
QObject::connect(&m_swapAction, &QAction::triggered, EditorManager::instance(), [] { .adopt(&m_swapAction)
.setText(Tr::tr("Swap Views"))
.setContext(context)
.addOnTriggered(EditorManager::instance(), [] {
auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor()); auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor());
if (editor) if (editor)
editor->swapViews(); editor->swapViews();

View File

@@ -7,7 +7,7 @@
#include <texteditor/texteditoractionhandler.h> #include <texteditor/texteditoractionhandler.h>
#include <QAction> #include <utils/parameteraction.h>
namespace TextEditor::Internal { namespace TextEditor::Internal {
@@ -18,13 +18,13 @@ public:
private: private:
TextEditor::TextEditorActionHandler m_actionHandler; TextEditor::TextEditorActionHandler m_actionHandler;
QAction m_emphasisAction; Utils::ParameterAction m_emphasisAction;
QAction m_strongAction; Utils::ParameterAction m_strongAction;
QAction m_inlineCodeAction; Utils::ParameterAction m_inlineCodeAction;
QAction m_linkAction; Utils::ParameterAction m_linkAction;
QAction m_toggleEditorAction; Utils::ParameterAction m_toggleEditorAction;
QAction m_togglePreviewAction; Utils::ParameterAction m_togglePreviewAction;
QAction m_swapAction; Utils::ParameterAction m_swapAction;
}; };
} // TextEditor::Internal } // TextEditor::Internal