From f9e2942a80035f044916bd1ce3270381b5a438a3 Mon Sep 17 00:00:00 2001 From: hjk Date: Tue, 14 Nov 2023 14:56:11 +0100 Subject: [PATCH] Bazaar: Use Core::ActionBuilder to create menu entries Change-Id: Ibae72a912dee82ef8a0b7865d69f87fd65ee8f43 Reviewed-by: Christian Stenger --- src/plugins/bazaar/bazaarplugin.cpp | 317 +++++++++--------- .../actionmanager/actionmanager.cpp | 6 + 2 files changed, 171 insertions(+), 152 deletions(-) diff --git a/src/plugins/bazaar/bazaarplugin.cpp b/src/plugins/bazaar/bazaarplugin.cpp index 8a9268748dd..cb3f116e180 100644 --- a/src/plugins/bazaar/bazaarplugin.cpp +++ b/src/plugins/bazaar/bazaarplugin.cpp @@ -209,11 +209,6 @@ public: void uncommit(); void diffFromEditorSelected(const QStringList &files); - // Functions - void createFileActions(const Core::Context &context); - void createDirectoryActions(const Core::Context &context); - void createRepositoryActions(const Core::Context &context); - // Variables BazaarClient m_client; @@ -222,8 +217,7 @@ public: [] { return new CommitEditor; }, this }; - Core::CommandLocator *m_commandLocator = nullptr; - Core::ActionContainer *m_bazaarContainer = nullptr; + CommandLocator *m_commandLocator = nullptr; QList m_repositoryActionList; @@ -354,85 +348,179 @@ BazaarPluginPrivate::BazaarPluginPrivate() m_commandLocator->setDescription(Tr::tr("Triggers a Bazaar version control operation.")); // Create menu item for Bazaar - m_bazaarContainer = ActionManager::createMenu("Bazaar.BazaarMenu"); - QMenu *menu = m_bazaarContainer->menu(); + + const Id bazaarMenuId = "Bazaar.BazaarMenu"; + ActionContainer *bazaarMenu = ActionManager::createMenu(bazaarMenuId); + QMenu *menu = bazaarMenu->menu(); menu->setTitle(Tr::tr("Bazaar")); - createFileActions(context); - m_bazaarContainer->addSeparator(context); - createDirectoryActions(context); - m_bazaarContainer->addSeparator(context); - createRepositoryActions(context); - m_bazaarContainer->addSeparator(context); + // File Actions + + ActionBuilder annotateFile(this, ANNOTATE); + annotateFile.setParameterText(Tr::tr("Annotate \"%1\""), Tr::tr("Annotate Current File")); + annotateFile.setContext(context); + annotateFile.bindContextAction(&m_annotateFile); + annotateFile.setCommandAttribute(Command::CA_UpdateText); + annotateFile.setContainer(bazaarMenuId); + annotateFile.setOnTriggered(this, [this] { annotateCurrentFile(); }); + m_commandLocator->appendCommand(annotateFile.command()); + + ActionBuilder diffFile(this, DIFF); + diffFile.setParameterText(Tr::tr("Diff \"%1\""), Tr::tr("Diff Current File")); + diffFile.setContext(context); + diffFile.bindContextAction(&m_diffFile); + diffFile.setCommandAttribute(Command::CA_UpdateText); + diffFile.setDefaultKeySequence(Tr::tr("Meta+Z,Meta+D"), Tr::tr("Alt+Z,Alt+D")); + diffFile.setContainer(bazaarMenuId); + diffFile.setOnTriggered(this, [this] { diffCurrentFile(); }); + m_commandLocator->appendCommand(diffFile.command()); + + ActionBuilder logFile(this, LOG); + logFile.setParameterText(Tr::tr("Log \"%1\""), Tr::tr("Log Current File")); + logFile.setContext(context); + logFile.bindContextAction(&m_logFile); + logFile.setCommandAttribute(Command::CA_UpdateText); + logFile.setDefaultKeySequence(Tr::tr("Meta+Z,Meta+L"), Tr::tr("Alt+Z,Alt+L")); + logFile.setContainer(bazaarMenuId); + logFile.setOnTriggered(this, [this] { logCurrentFile(); }); + m_commandLocator->appendCommand(logFile.command()); + + ActionBuilder statusFile(this, STATUS); + statusFile.setParameterText(Tr::tr("Status \"%1\""), Tr::tr("Status Current File")); + statusFile.setContext(context); + statusFile.bindContextAction(&m_statusFile); + statusFile.setCommandAttribute(Command::CA_UpdateText); + statusFile.setDefaultKeySequence(Tr::tr("Meta+Z,Meta+S"), Tr::tr("Alt+Z,Alt+S")); + statusFile.setContainer(bazaarMenuId); + statusFile.setOnTriggered(this, [this] { statusCurrentFile(); }); + m_commandLocator->appendCommand(statusFile.command()); + + bazaarMenu->addSeparator(context); + + ActionBuilder addAction(this, ADD); + addAction.bindContextAction(&m_addAction); + addAction.setParameterText(Tr::tr("Add \"%1\""), Tr::tr("Add")); + addAction.setContext(context); + addAction.setCommandAttribute(Command::CA_UpdateText); + addAction.setContainer(bazaarMenuId); + addAction.setOnTriggered(this, [this] { addCurrentFile(); }); + m_commandLocator->appendCommand(addAction.command()); + + ActionBuilder deleteAction(this, DELETE); + deleteAction.setParameterText(Tr::tr("Delete \"%1\"...") , Tr::tr("Delete...")); + deleteAction.setContext(context); + deleteAction.bindContextAction(&m_deleteAction); + deleteAction.setCommandAttribute(Command::CA_UpdateText); + deleteAction.setContainer(bazaarMenuId); + deleteAction.setOnTriggered(this, [this] { promptToDeleteCurrentFile(); }); + m_commandLocator->appendCommand(deleteAction.command()); + + ActionBuilder revertFile(this, REVERT); + revertFile.setParameterText(Tr::tr("Revert \"%1\"..."), Tr::tr("Revert Current File...")); + revertFile.setContext(context); + revertFile.bindContextAction(&m_revertFile); + revertFile.setCommandAttribute(Command::CA_UpdateText); + revertFile.setContainer(bazaarMenuId); + revertFile.setOnTriggered(this, [this] { revertCurrentFile(); }); + m_commandLocator->appendCommand(revertFile.command()); + + bazaarMenu->addSeparator(context); + + // Directory Actions + + ActionBuilder diffMulti(this, DIFFMULTI); + diffMulti.setText(Tr::tr("Diff")); + diffMulti.setContext(context); + diffMulti.setContainer(bazaarMenuId); + diffMulti.setOnTriggered(this, [this] { diffRepository(); }); + m_repositoryActionList.append(diffMulti.contextAction()); + m_commandLocator->appendCommand(diffMulti.command()); + + ActionBuilder logMulti(this, LOGMULTI); + logMulti.setText(Tr::tr("Log")); + logMulti.setContext(context); + logMulti.setContainer(bazaarMenuId); + logMulti.setOnTriggered(this, [this] { logRepository(); }); + m_repositoryActionList.append(logMulti.contextAction()); + m_commandLocator->appendCommand(logMulti.command()); + + ActionBuilder revertMulti(this, REVERTMULTI); + revertMulti.setText(Tr::tr("Revert...")); + revertMulti.setContext(context); + revertMulti.setContainer(bazaarMenuId); + revertMulti.setOnTriggered(this, [this] { revertAll(); }); + m_repositoryActionList.append(revertMulti.contextAction()); + m_commandLocator->appendCommand(revertMulti.command()); + + ActionBuilder statusMulti(this, STATUSMULTI); + statusMulti.setText(Tr::tr("Status")); + statusMulti.setContext(context); + statusMulti.setContainer(bazaarMenuId); + statusMulti.setOnTriggered(this, [this] { this->statusMulti(); }); + m_repositoryActionList.append(statusMulti.contextAction()); + m_commandLocator->appendCommand(statusMulti.command()); + + bazaarMenu->addSeparator(context); + + // Repository Actions + + ActionBuilder pull(this, PULL); + pull.setText(Tr::tr("Pull...")); + pull.setContext(context); + pull.setContainer(bazaarMenuId); + pull.setOnTriggered(this, [this] { this->pull(); }); + m_repositoryActionList.append(pull.contextAction()); + m_commandLocator->appendCommand(pull.command()); + + ActionBuilder push(this, PUSH); + push.setText(Tr::tr("Push...")); + push.setContext(context); + push.setContainer(bazaarMenuId); + push.setOnTriggered(this, [this] { this->push(); }); + m_repositoryActionList.append(push.contextAction()); + m_commandLocator->appendCommand(push.command()); + + ActionBuilder update(this, UPDATE); + update.setText(Tr::tr("Update...")); + update.setContext(context); + update.setContainer(bazaarMenuId); + update.setOnTriggered(this, [this] { this->update(); }); + m_repositoryActionList.append(update.contextAction()); + m_commandLocator->appendCommand(update.command()); + + ActionBuilder commit(this, COMMIT); + commit.setText(Tr::tr("Commit...")); + commit.setContext(context); + commit.setContainer(bazaarMenuId); + commit.setDefaultKeySequence(Tr::tr("Meta+Z,Meta+C"), Tr::tr("Alt+Z,Alt+C")); + commit.setOnTriggered(this, [this] { this->commit(); }); + m_repositoryActionList.append(commit.contextAction()); + m_commandLocator->appendCommand(commit.command()); + + ActionBuilder uncommit(this, UNCOMMIT); + uncommit.setText(Tr::tr("Uncommit...")); + uncommit.setContext(context); + uncommit.setContainer(bazaarMenuId); + uncommit.setOnTriggered(this, [this] { this->uncommit(); }); + m_repositoryActionList.append(uncommit.contextAction()); + m_commandLocator->appendCommand(uncommit.command()); + + ActionBuilder createRepository(this, CREATE_REPOSITORY); + createRepository.setText(Tr::tr("Create Repository...")); + createRepository.setContext(context); + createRepository.setContainer(bazaarMenuId); + createRepository.setOnTriggered(this, [this] { this->createRepository(); }); + + bazaarMenu->addSeparator(context); // Request the Tools menu and add the Bazaar menu to it ActionContainer *toolsMenu = ActionManager::actionContainer(Core::Constants::M_TOOLS); - toolsMenu->addMenu(m_bazaarContainer); - m_menuAction = m_bazaarContainer->menu()->menuAction(); + toolsMenu->addMenu(bazaarMenu); + m_menuAction = bazaarMenu->menu()->menuAction(); connect(&settings(), &AspectContainer::applied, this, &IVersionControl::configurationChanged); } -void BazaarPluginPrivate::createFileActions(const Context &context) -{ - m_annotateFile = new ParameterAction(Tr::tr("Annotate Current File"), Tr::tr("Annotate \"%1\""), ParameterAction::EnabledWithParameter, this); - Command *command = ActionManager::registerAction(m_annotateFile, ANNOTATE, context); - command->setAttribute(Command::CA_UpdateText); - connect(m_annotateFile, &QAction::triggered, this, &BazaarPluginPrivate::annotateCurrentFile); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - m_diffFile = new ParameterAction(Tr::tr("Diff Current File"), Tr::tr("Diff \"%1\""), ParameterAction::EnabledWithParameter, this); - command = ActionManager::registerAction(m_diffFile, DIFF, context); - command->setAttribute(Command::CA_UpdateText); - command->setDefaultKeySequence(QKeySequence(useMacShortcuts ? Tr::tr("Meta+Z,Meta+D") - : Tr::tr("Alt+Z,Alt+D"))); - connect(m_diffFile, &QAction::triggered, this, &BazaarPluginPrivate::diffCurrentFile); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - m_logFile = new ParameterAction(Tr::tr("Log Current File"), Tr::tr("Log \"%1\""), ParameterAction::EnabledWithParameter, this); - command = ActionManager::registerAction(m_logFile, LOG, context); - command->setAttribute(Command::CA_UpdateText); - command->setDefaultKeySequence(QKeySequence(useMacShortcuts ? Tr::tr("Meta+Z,Meta+L") - : Tr::tr("Alt+Z,Alt+L"))); - connect(m_logFile, &QAction::triggered, this, &BazaarPluginPrivate::logCurrentFile); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - m_statusFile = new ParameterAction(Tr::tr("Status Current File"), Tr::tr("Status \"%1\""), ParameterAction::EnabledWithParameter, this); - command = ActionManager::registerAction(m_statusFile, STATUS, context); - command->setAttribute(Command::CA_UpdateText); - command->setDefaultKeySequence(QKeySequence(useMacShortcuts ? Tr::tr("Meta+Z,Meta+S") - : Tr::tr("Alt+Z,Alt+S"))); - connect(m_statusFile, &QAction::triggered, this, &BazaarPluginPrivate::statusCurrentFile); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - m_bazaarContainer->addSeparator(context); - - m_addAction = new ParameterAction(Tr::tr("Add"), Tr::tr("Add \"%1\""), ParameterAction::EnabledWithParameter, this); - command = ActionManager::registerAction(m_addAction, ADD, context); - command->setAttribute(Command::CA_UpdateText); - connect(m_addAction, &QAction::triggered, this, &BazaarPluginPrivate::addCurrentFile); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - m_deleteAction = new ParameterAction(Tr::tr("Delete..."), Tr::tr("Delete \"%1\"..."), ParameterAction::EnabledWithParameter, this); - command = ActionManager::registerAction(m_deleteAction, DELETE, context); - command->setAttribute(Command::CA_UpdateText); - connect(m_deleteAction, &QAction::triggered, this, &BazaarPluginPrivate::promptToDeleteCurrentFile); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - m_revertFile = new ParameterAction(Tr::tr("Revert Current File..."), Tr::tr("Revert \"%1\"..."), ParameterAction::EnabledWithParameter, this); - command = ActionManager::registerAction(m_revertFile, REVERT, context); - command->setAttribute(Command::CA_UpdateText); - connect(m_revertFile, &QAction::triggered, this, &BazaarPluginPrivate::revertCurrentFile); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); -} - void BazaarPluginPrivate::addCurrentFile() { const VcsBasePluginState state = currentState(); @@ -481,36 +569,6 @@ void BazaarPluginPrivate::statusCurrentFile() m_client.status(state.currentFileTopLevel(), state.relativeCurrentFile()); } -void BazaarPluginPrivate::createDirectoryActions(const Context &context) -{ - auto action = new QAction(Tr::tr("Diff"), this); - m_repositoryActionList.append(action); - Command *command = ActionManager::registerAction(action, DIFFMULTI, context); - connect(action, &QAction::triggered, this, &BazaarPluginPrivate::diffRepository); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - action = new QAction(Tr::tr("Log"), this); - m_repositoryActionList.append(action); - command = ActionManager::registerAction(action, LOGMULTI, context); - connect(action, &QAction::triggered, this, &BazaarPluginPrivate::logRepository); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - action = new QAction(Tr::tr("Revert..."), this); - m_repositoryActionList.append(action); - command = ActionManager::registerAction(action, REVERTMULTI, context); - connect(action, &QAction::triggered, this, &BazaarPluginPrivate::revertAll); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - action = new QAction(Tr::tr("Status"), this); - m_repositoryActionList.append(action); - command = ActionManager::registerAction(action, STATUSMULTI, context); - connect(action, &QAction::triggered, this, &BazaarPluginPrivate::statusMulti); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); -} void BazaarPluginPrivate::diffRepository() { @@ -546,51 +604,6 @@ void BazaarPluginPrivate::statusMulti() m_client.status(state.topLevel()); } -void BazaarPluginPrivate::createRepositoryActions(const Context &context) -{ - auto action = new QAction(Tr::tr("Pull..."), this); - m_repositoryActionList.append(action); - Command *command = ActionManager::registerAction(action, PULL, context); - connect(action, &QAction::triggered, this, &BazaarPluginPrivate::pull); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - action = new QAction(Tr::tr("Push..."), this); - m_repositoryActionList.append(action); - command = ActionManager::registerAction(action, PUSH, context); - connect(action, &QAction::triggered, this, &BazaarPluginPrivate::push); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - action = new QAction(Tr::tr("Update..."), this); - m_repositoryActionList.append(action); - command = ActionManager::registerAction(action, UPDATE, context); - connect(action, &QAction::triggered, this, &BazaarPluginPrivate::update); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - action = new QAction(Tr::tr("Commit..."), this); - m_repositoryActionList.append(action); - command = ActionManager::registerAction(action, COMMIT, context); - command->setDefaultKeySequence(QKeySequence(useMacShortcuts ? Tr::tr("Meta+Z,Meta+C") - : Tr::tr("Alt+Z,Alt+C"))); - connect(action, &QAction::triggered, this, &BazaarPluginPrivate::commit); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - action = new QAction(Tr::tr("Uncommit..."), this); - m_repositoryActionList.append(action); - command = ActionManager::registerAction(action, UNCOMMIT, context); - connect(action, &QAction::triggered, this, &BazaarPluginPrivate::uncommit); - m_bazaarContainer->addAction(command); - m_commandLocator->appendCommand(command); - - auto createRepositoryAction = new QAction(Tr::tr("Create Repository..."), this); - command = ActionManager::registerAction(createRepositoryAction, CREATE_REPOSITORY, context); - connect(createRepositoryAction, &QAction::triggered, this, &BazaarPluginPrivate::createRepository); - m_bazaarContainer->addAction(command); -} - void BazaarPluginPrivate::pull() { const VcsBasePluginState state = currentState(); diff --git a/src/plugins/coreplugin/actionmanager/actionmanager.cpp b/src/plugins/coreplugin/actionmanager/actionmanager.cpp index 1ba658837d7..b1b1aedd55a 100644 --- a/src/plugins/coreplugin/actionmanager/actionmanager.cpp +++ b/src/plugins/coreplugin/actionmanager/actionmanager.cpp @@ -255,6 +255,12 @@ void ActionBuilder::bindContextAction(QAction **dest) *dest = d->action; } +void ActionBuilder::bindContextAction(Utils::ParameterAction **dest) +{ + QTC_ASSERT(dest, return); + *dest = d->action; +} + void ActionBuilder::augmentActionWithShortcutToolTip() { d->command->augmentActionWithShortcutToolTip(d->action);