forked from qt-creator/qt-creator
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:
@@ -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)
|
||||
QObject *parent)
|
||||
: QAction(emptyText, parent)
|
||||
, m_emptyText(emptyText)
|
||||
, m_parameterText(parameterText)
|
||||
, m_enablingMode(mode)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
@@ -19,7 +19,8 @@ public:
|
||||
enum EnablingMode { AlwaysEnabled, EnabledWithParameter };
|
||||
Q_ENUM(EnablingMode)
|
||||
|
||||
explicit ParameterAction(const QString &emptyText,
|
||||
explicit ParameterAction(QObject *parent = nullptr);
|
||||
ParameterAction(const QString &emptyText,
|
||||
const QString ¶meterText,
|
||||
EnablingMode em = AlwaysEnabled,
|
||||
QObject *parent = nullptr);
|
||||
|
||||
@@ -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<Id> containerIds, Id groupId
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@@ -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
|
||||
d->contextAction()->setEmptyText(emptyText);
|
||||
d->contextAction()->setParameterText(parameterText);
|
||||
d->contextAction()->setEnablingMode(mode == AlwaysEnabled
|
||||
? ParameterAction::AlwaysEnabled
|
||||
: ParameterAction::EnabledWithParameter);
|
||||
d->contextAction->setText(emptyText);
|
||||
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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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);
|
||||
|
||||
@@ -510,53 +510,72 @@ 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(), [] {
|
||||
|
||||
ActionBuilder(nullptr, EMPHASIS_ACTION)
|
||||
.adopt(&m_emphasisAction)
|
||||
.setText(Tr::tr("Emphasis"))
|
||||
.setContext(textContext)
|
||||
.addOnTriggered(EditorManager::instance(), [] {
|
||||
auto editor = qobject_cast<MarkdownEditor *>(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(), [] {
|
||||
|
||||
ActionBuilder(nullptr, STRONG_ACTION)
|
||||
.adopt(&m_strongAction)
|
||||
.setText("Strong")
|
||||
.setContext(textContext)
|
||||
.addOnTriggered(EditorManager::instance(), [] {
|
||||
auto editor = qobject_cast<MarkdownEditor *>(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(), [] {
|
||||
|
||||
ActionBuilder(nullptr, INLINECODE_ACTION)
|
||||
.adopt(&m_inlineCodeAction)
|
||||
.setText(Tr::tr("Inline Code"))
|
||||
.setContext(textContext)
|
||||
.addOnTriggered(EditorManager::instance(), [] {
|
||||
auto editor = qobject_cast<MarkdownEditor *>(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(), [] {
|
||||
|
||||
ActionBuilder(nullptr, LINK_ACTION)
|
||||
.adopt(&m_linkAction)
|
||||
.setText(Tr::tr("Hyperlink"))
|
||||
.setContext(textContext)
|
||||
.addOnTriggered(EditorManager::instance(), [] {
|
||||
auto editor = qobject_cast<MarkdownEditor *>(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(), [] {
|
||||
ActionBuilder(nullptr, TOGGLEEDITOR_ACTION)
|
||||
.adopt(&m_toggleEditorAction)
|
||||
.setText(Tr::tr("Show Editor"))
|
||||
.setContext(context)
|
||||
.addOnTriggered(EditorManager::instance(), [] {
|
||||
auto editor = qobject_cast<MarkdownEditor *>(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(), [] {
|
||||
|
||||
ActionBuilder(nullptr, TOGGLEPREVIEW_ACTION)
|
||||
.adopt(&m_togglePreviewAction)
|
||||
.setText(Tr::tr("Show Preview"))
|
||||
.setContext(context)
|
||||
.addOnTriggered(EditorManager::instance(), [] {
|
||||
auto editor = qobject_cast<MarkdownEditor *>(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(), [] {
|
||||
|
||||
ActionBuilder(nullptr, SWAPVIEWS_ACTION)
|
||||
.adopt(&m_swapAction)
|
||||
.setText(Tr::tr("Swap Views"))
|
||||
.setContext(context)
|
||||
.addOnTriggered(EditorManager::instance(), [] {
|
||||
auto editor = qobject_cast<MarkdownEditor *>(EditorManager::currentEditor());
|
||||
if (editor)
|
||||
editor->swapViews();
|
||||
|
||||
@@ -7,7 +7,7 @@
|
||||
|
||||
#include <texteditor/texteditoractionhandler.h>
|
||||
|
||||
#include <QAction>
|
||||
#include <utils/parameteraction.h>
|
||||
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user