From 8f8c1052a2ccec0d1bbcdcf94a0346ae3609a3e0 Mon Sep 17 00:00:00 2001 From: Tobias Hunger Date: Fri, 20 Nov 2015 12:09:47 +0100 Subject: [PATCH] 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 --- src/plugins/git/gitplugin.cpp | 60 +++++++++++++++++------------------ src/plugins/git/gitplugin.h | 4 +++ 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/src/plugins/git/gitplugin.cpp b/src/plugins/git/gitplugin.cpp index 261e4c91f93..b52e02d767c 100644 --- a/src/plugins/git/gitplugin.cpp +++ b/src/plugins/git/gitplugin.cpp @@ -178,20 +178,31 @@ const VcsBaseSubmitEditorParameters submitParameters = { VcsBaseSubmitEditorParameters::DiffRows }; +Command *GitPlugin::createCommand(QAction *action, ActionContainer *ac, Id id, + const Context &context, bool addToLocator, + const std::function &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 ParameterAction *GitPlugin::createParameterAction(ActionContainer *ac, const QString &defaultText, const QString ¶meterText, Id id, const Context &context, - bool addToLocator, const QKeySequence &keys) + bool addToLocator, const std::function &callback, + const QKeySequence &keys) { auto action = new ParameterAction(defaultText, parameterText, ParameterAction::EnabledWithParameter, this); - Command *command = ActionManager::registerAction(action, id, context); - if (!keys.isEmpty()) - command->setDefaultKeySequence(keys); + Command *command = createCommand(action, ac, id, context, addToLocator, callback, keys); command->setAttribute(Command::CA_UpdateText); - ac->addAction(command); - if (addToLocator) - m_commandLocator->appendCommand(command); return action; } @@ -202,9 +213,9 @@ QAction *GitPlugin::createFileAction(ActionContainer *ac, const std::function &callback, 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); - connect(action, &QAction::triggered, this, callback); return action; } @@ -223,29 +234,22 @@ QAction *GitPlugin::createProjectAction(ActionContainer *ac, const QString &defa const QKeySequence &keys) { ParameterAction *action = createParameterAction(ac, defaultText, parameterText, id, context, - addToLocator, keys); + addToLocator, + [this, func]() { return (this->*func)(); }, + keys); m_projectActions.push_back(action); - connect(action, &QAction::triggered, this, [this, func]() { return (this->*func)(); }); return action; } // Create an action to act on the repository with slot -QAction *GitPlugin::createRepositoryAction(ActionContainer *ac, - const QString &text, Id id, +QAction *GitPlugin::createRepositoryAction(ActionContainer *ac, const QString &text, Id id, const Context &context, bool addToLocator, const std::function &callback, const QKeySequence &keys) { auto action = new QAction(text, this); - Command *command = ActionManager::registerAction(action, id, context); - if (!keys.isEmpty()) - command->setDefaultKeySequence(keys); - if (ac) - ac->addAction(command); + createCommand(action, ac, id, context, addToLocator, callback, keys); m_repositoryActions.push_back(action); - if (addToLocator) - m_commandLocator->appendCommand(command); - connect(action, &QAction::triggered, this, callback); return action; } @@ -459,15 +463,11 @@ bool GitPlugin::initialize(const QStringList &arguments, QString *errorMessage) localRepositoryMenu->addMenu(patchMenu); // Apply current file as patch is handled specially. - m_applyCurrentFilePatchAction = - createParameterAction(patchMenu, - tr("Apply from Editor"), tr("Apply \"%1\""), - "Git.ApplyCurrentFilePatch", - context, true); - - connect(m_applyCurrentFilePatchAction, &QAction::triggered, - this, &GitPlugin::applyCurrentFilePatch); - + m_applyCurrentFilePatchAction + = createParameterAction(patchMenu, + tr("Apply from Editor"), tr("Apply \"%1\""), + "Git.ApplyCurrentFilePatch", + context, true, [this] { applyCurrentFilePatch(); }); createRepositoryAction(patchMenu, tr("Apply from File..."), "Git.ApplyPatch", context, true, [this] { promptApplyPatch(); }); diff --git a/src/plugins/git/gitplugin.h b/src/plugins/git/gitplugin.h index 35ab6346e22..ea575e60fd5 100644 --- a/src/plugins/git/gitplugin.h +++ b/src/plugins/git/gitplugin.h @@ -150,9 +150,13 @@ protected: bool submitEditorAboutToClose() override; private: + Core::Command *createCommand(QAction *action, Core::ActionContainer *ac, Core::Id id, + const Core::Context &context, bool addToLocator, + const std::function &callback, const QKeySequence &keys); Utils::ParameterAction *createParameterAction(Core::ActionContainer *ac, const QString &defaultText, const QString ¶meterText, Core::Id id, const Core::Context &context, bool addToLocator, + const std::function &callback, const QKeySequence &keys = QKeySequence()); QAction *createFileAction(Core::ActionContainer *ac,