Git: Simplify action creation methods

* Use a common method to do the basic setup for all the different
  actions.

Change-Id: I92e1d959c45e70c8145b7665dde8827edaf2331d
Reviewed-by: Orgad Shaneh <orgads@gmail.com>
This commit is contained in:
Tobias Hunger
2015-11-20 12:09:47 +01:00
parent a01a8e7452
commit 8f8c1052a2
2 changed files with 34 additions and 30 deletions

View File

@@ -178,20 +178,31 @@ const VcsBaseSubmitEditorParameters submitParameters = {
VcsBaseSubmitEditorParameters::DiffRows VcsBaseSubmitEditorParameters::DiffRows
}; };
Command *GitPlugin::createCommand(QAction *action, ActionContainer *ac, Id id,
const Context &context, bool addToLocator,
const std::function<void()> &callback, const QKeySequence &keys)
{
Command *command = ActionManager::registerAction(action, id, context);
if (!keys.isEmpty())
command->setDefaultKeySequence(keys);
if (ac)
ac->addAction(command);
if (addToLocator)
m_commandLocator->appendCommand(command);
connect(action, &QAction::triggered, this, callback);
return command;
}
// Create a parameter action // Create a parameter action
ParameterAction *GitPlugin::createParameterAction(ActionContainer *ac, ParameterAction *GitPlugin::createParameterAction(ActionContainer *ac,
const QString &defaultText, const QString &parameterText, const QString &defaultText, const QString &parameterText,
Id id, const Context &context, Id id, const Context &context,
bool addToLocator, const QKeySequence &keys) bool addToLocator, const std::function<void()> &callback,
const QKeySequence &keys)
{ {
auto action = new ParameterAction(defaultText, parameterText, ParameterAction::EnabledWithParameter, this); auto action = new ParameterAction(defaultText, parameterText, ParameterAction::EnabledWithParameter, this);
Command *command = ActionManager::registerAction(action, id, context); Command *command = createCommand(action, ac, id, context, addToLocator, callback, keys);
if (!keys.isEmpty())
command->setDefaultKeySequence(keys);
command->setAttribute(Command::CA_UpdateText); command->setAttribute(Command::CA_UpdateText);
ac->addAction(command);
if (addToLocator)
m_commandLocator->appendCommand(command);
return action; return action;
} }
@@ -202,9 +213,9 @@ QAction *GitPlugin::createFileAction(ActionContainer *ac,
const std::function<void()> &callback, const std::function<void()> &callback,
const QKeySequence &keys) const QKeySequence &keys)
{ {
ParameterAction *action = createParameterAction(ac, defaultText, parameterText, id, context, addToLocator, keys); ParameterAction *action = createParameterAction(ac, defaultText, parameterText, id, context,
addToLocator, callback, keys);
m_fileActions.push_back(action); m_fileActions.push_back(action);
connect(action, &QAction::triggered, this, callback);
return action; return action;
} }
@@ -223,29 +234,22 @@ QAction *GitPlugin::createProjectAction(ActionContainer *ac, const QString &defa
const QKeySequence &keys) const QKeySequence &keys)
{ {
ParameterAction *action = createParameterAction(ac, defaultText, parameterText, id, context, ParameterAction *action = createParameterAction(ac, defaultText, parameterText, id, context,
addToLocator, keys); addToLocator,
[this, func]() { return (this->*func)(); },
keys);
m_projectActions.push_back(action); m_projectActions.push_back(action);
connect(action, &QAction::triggered, this, [this, func]() { return (this->*func)(); });
return action; return action;
} }
// Create an action to act on the repository with slot // Create an action to act on the repository with slot
QAction *GitPlugin::createRepositoryAction(ActionContainer *ac, QAction *GitPlugin::createRepositoryAction(ActionContainer *ac, const QString &text, Id id,
const QString &text, Id id,
const Context &context, bool addToLocator, const Context &context, bool addToLocator,
const std::function<void()> &callback, const std::function<void()> &callback,
const QKeySequence &keys) const QKeySequence &keys)
{ {
auto action = new QAction(text, this); auto action = new QAction(text, this);
Command *command = ActionManager::registerAction(action, id, context); createCommand(action, ac, id, context, addToLocator, callback, keys);
if (!keys.isEmpty())
command->setDefaultKeySequence(keys);
if (ac)
ac->addAction(command);
m_repositoryActions.push_back(action); m_repositoryActions.push_back(action);
if (addToLocator)
m_commandLocator->appendCommand(command);
connect(action, &QAction::triggered, this, callback);
return action; return action;
} }
@@ -459,15 +463,11 @@ bool GitPlugin::initialize(const QStringList &arguments, QString *errorMessage)
localRepositoryMenu->addMenu(patchMenu); localRepositoryMenu->addMenu(patchMenu);
// Apply current file as patch is handled specially. // Apply current file as patch is handled specially.
m_applyCurrentFilePatchAction = m_applyCurrentFilePatchAction
createParameterAction(patchMenu, = createParameterAction(patchMenu,
tr("Apply from Editor"), tr("Apply \"%1\""), tr("Apply from Editor"), tr("Apply \"%1\""),
"Git.ApplyCurrentFilePatch", "Git.ApplyCurrentFilePatch",
context, true); context, true, [this] { applyCurrentFilePatch(); });
connect(m_applyCurrentFilePatchAction, &QAction::triggered,
this, &GitPlugin::applyCurrentFilePatch);
createRepositoryAction(patchMenu, tr("Apply from File..."), "Git.ApplyPatch", createRepositoryAction(patchMenu, tr("Apply from File..."), "Git.ApplyPatch",
context, true, [this] { promptApplyPatch(); }); context, true, [this] { promptApplyPatch(); });

View File

@@ -150,9 +150,13 @@ protected:
bool submitEditorAboutToClose() override; bool submitEditorAboutToClose() override;
private: private:
Core::Command *createCommand(QAction *action, Core::ActionContainer *ac, Core::Id id,
const Core::Context &context, bool addToLocator,
const std::function<void()> &callback, const QKeySequence &keys);
Utils::ParameterAction *createParameterAction(Core::ActionContainer *ac, Utils::ParameterAction *createParameterAction(Core::ActionContainer *ac,
const QString &defaultText, const QString &parameterText, const QString &defaultText, const QString &parameterText,
Core::Id id, const Core::Context &context, bool addToLocator, Core::Id id, const Core::Context &context, bool addToLocator,
const std::function<void()> &callback,
const QKeySequence &keys = QKeySequence()); const QKeySequence &keys = QKeySequence());
QAction *createFileAction(Core::ActionContainer *ac, QAction *createFileAction(Core::ActionContainer *ac,