Core: Add a MenuBuilder

Use it in the AutoTest as example.

Change-Id: I357faba71f685ea05206a7e1ca93069a88787823
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
hjk
2024-01-29 11:13:06 +01:00
parent 6e7b83bb4a
commit 17055f2e18
3 changed files with 64 additions and 14 deletions

View File

@@ -161,16 +161,19 @@ TestProjectSettings *projectSettings(ProjectExplorer::Project *project)
void AutotestPluginPrivate::initializeMenuEntries() void AutotestPluginPrivate::initializeMenuEntries()
{ {
ActionContainer *menu = ActionManager::createMenu(Constants::MENU_ID); const Id menuId = Constants::MENU_ID;
menu->menu()->setTitle(Tr::tr("&Tests"));
menu->setOnAllDisabledBehavior(ActionContainer::Show); MenuBuilder(menuId)
.setTitle(Tr::tr("&Tests"))
.setOnAllDisabledBehavior(ActionContainer::Show)
.addToContainer(Core::Constants::M_TOOLS);
ActionBuilder(this, Constants::ACTION_RUN_ALL_ID) ActionBuilder(this, Constants::ACTION_RUN_ALL_ID)
.setText(Tr::tr("Run &All Tests")) .setText(Tr::tr("Run &All Tests"))
.setIcon(Utils::Icons::RUN_SMALL.icon()) .setIcon(Utils::Icons::RUN_SMALL.icon())
.setToolTip(Tr::tr("Run All Tests")) .setToolTip(Tr::tr("Run All Tests"))
.setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+A"), Tr::tr("Alt+Shift+T,Alt+A")) .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+A"), Tr::tr("Alt+Shift+T,Alt+A"))
.addToContainer(Constants::MENU_ID) .addToContainer(menuId)
.setEnabled(false) .setEnabled(false)
.addOnTriggered(this, [this] { onRunAllTriggered(TestRunMode::Run); }); .addOnTriggered(this, [this] { onRunAllTriggered(TestRunMode::Run); });
@@ -179,7 +182,7 @@ void AutotestPluginPrivate::initializeMenuEntries()
.setIcon(Utils::Icons::RUN_SMALL.icon()) .setIcon(Utils::Icons::RUN_SMALL.icon())
.setToolTip(Tr::tr("Run All Tests Without Deployment")) .setToolTip(Tr::tr("Run All Tests Without Deployment"))
.setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+E"), Tr::tr("Alt+Shift+T,Alt+E")) .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+E"), Tr::tr("Alt+Shift+T,Alt+E"))
.addToContainer(Constants::MENU_ID) .addToContainer(menuId)
.setEnabled(false) .setEnabled(false)
.addOnTriggered(this, [this] { onRunAllTriggered(TestRunMode::RunWithoutDeploy); }); .addOnTriggered(this, [this] { onRunAllTriggered(TestRunMode::RunWithoutDeploy); });
@@ -188,7 +191,7 @@ void AutotestPluginPrivate::initializeMenuEntries()
.setIcon(Utils::Icons::RUN_SELECTED.icon()) .setIcon(Utils::Icons::RUN_SELECTED.icon())
.setToolTip(Tr::tr("Run Selected Tests")) .setToolTip(Tr::tr("Run Selected Tests"))
.setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+R"), Tr::tr("Alt+Shift+T,Alt+R")) .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+R"), Tr::tr("Alt+Shift+T,Alt+R"))
.addToContainer(Constants::MENU_ID) .addToContainer(menuId)
.setEnabled(false) .setEnabled(false)
.addOnTriggered(this, [this] { onRunSelectedTriggered(TestRunMode::Run); }); .addOnTriggered(this, [this] { onRunSelectedTriggered(TestRunMode::Run); });
@@ -197,7 +200,7 @@ void AutotestPluginPrivate::initializeMenuEntries()
.setIcon(Utils::Icons::RUN_SELECTED.icon()) .setIcon(Utils::Icons::RUN_SELECTED.icon())
.setToolTip(Tr::tr("Run Selected Tests Without Deployment")) .setToolTip(Tr::tr("Run Selected Tests Without Deployment"))
.setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+W"), Tr::tr("Alt+Shift+T,Alt+W")) .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+W"), Tr::tr("Alt+Shift+T,Alt+W"))
.addToContainer(Constants::MENU_ID) .addToContainer(menuId)
.setEnabled(false) .setEnabled(false)
.addOnTriggered(this, [this] { onRunSelectedTriggered(TestRunMode::RunWithoutDeploy); }); .addOnTriggered(this, [this] { onRunSelectedTriggered(TestRunMode::RunWithoutDeploy); });
@@ -206,7 +209,7 @@ void AutotestPluginPrivate::initializeMenuEntries()
.setIcon(Icons::RUN_FAILED.icon()) .setIcon(Icons::RUN_FAILED.icon())
.setToolTip(Tr::tr("Run Failed Tests")) .setToolTip(Tr::tr("Run Failed Tests"))
.setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+F"), Tr::tr("Alt+Shift+T,Alt+F")) .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+F"), Tr::tr("Alt+Shift+T,Alt+F"))
.addToContainer(Constants::MENU_ID) .addToContainer(menuId)
.setEnabled(false) .setEnabled(false)
.addOnTriggered(this, [this] { onRunFailedTriggered(); }); .addOnTriggered(this, [this] { onRunFailedTriggered(); });
@@ -215,7 +218,7 @@ void AutotestPluginPrivate::initializeMenuEntries()
.setIcon(Utils::Icons::RUN_FILE.icon()) .setIcon(Utils::Icons::RUN_FILE.icon())
.setToolTip(Tr::tr("Run Tests for Current File")) .setToolTip(Tr::tr("Run Tests for Current File"))
.setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+C"), Tr::tr("Alt+Shift+T,Alt+C")) .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+C"), Tr::tr("Alt+Shift+T,Alt+C"))
.addToContainer(Constants::MENU_ID) .addToContainer(menuId)
.setEnabled(false) .setEnabled(false)
.addOnTriggered(this, [this] { onRunFileTriggered(); }); .addOnTriggered(this, [this] { onRunFileTriggered(); });
@@ -224,13 +227,13 @@ void AutotestPluginPrivate::initializeMenuEntries()
.setToolTip(Tr::tr("Disable scanning and other actions until explicitly rescanning, " .setToolTip(Tr::tr("Disable scanning and other actions until explicitly rescanning, "
"re-enabling, or restarting Qt Creator.")) "re-enabling, or restarting Qt Creator."))
.setCheckable(true) .setCheckable(true)
.addToContainer(Constants::MENU_ID) .addToContainer(menuId)
.addOnTriggered(this, [this](bool on) { onDisableTemporarily(on); }); .addOnTriggered(this, [this](bool on) { onDisableTemporarily(on); });
ActionBuilder(this, Constants::ACTION_SCAN_ID) ActionBuilder(this, Constants::ACTION_SCAN_ID)
.setText(Tr::tr("Re&scan Tests")) .setText(Tr::tr("Re&scan Tests"))
.setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+S"), Tr::tr("Alt+Shift+T,Alt+S")) .setDefaultKeySequence(Tr::tr("Ctrl+Meta+T, Ctrl+Meta+S"), Tr::tr("Alt+Shift+T,Alt+S"))
.addToContainer(Constants::MENU_ID) .addToContainer(menuId)
.addOnTriggered(this, [] { .addOnTriggered(this, [] {
if (dd->m_testCodeParser.state() == TestCodeParser::DisabledTemporarily) if (dd->m_testCodeParser.state() == TestCodeParser::DisabledTemporarily)
dd->onDisableTemporarily(false); // Rescan Test should explicitly re-enable dd->onDisableTemporarily(false); // Rescan Test should explicitly re-enable
@@ -238,8 +241,6 @@ void AutotestPluginPrivate::initializeMenuEntries()
dd->m_testCodeParser.updateTestTree(); dd->m_testCodeParser.updateTestTree();
}); });
ActionContainer *toolsMenu = ActionManager::actionContainer(Core::Constants::M_TOOLS);
toolsMenu->addMenu(menu);
using namespace ProjectExplorer; using namespace ProjectExplorer;
connect(BuildManager::instance(), &BuildManager::buildStateChanged, connect(BuildManager::instance(), &BuildManager::buildStateChanged,
this, &updateMenuItemsEnabledState); this, &updateMenuItemsEnabledState);

View File

@@ -432,6 +432,40 @@ void Menu::addSeparator()
m_menu->addSeparator(); m_menu->addSeparator();
} }
// MenuBuilder
MenuBuilder::MenuBuilder(Id id)
{
m_menu = ActionManager::createMenu(id);
}
MenuBuilder::~MenuBuilder() = default;
MenuBuilder &MenuBuilder::setTitle(const QString &title)
{
m_menu->menu()->setTitle(title);
return *this;
}
MenuBuilder &MenuBuilder::setOnAllDisabledBehavior(ActionContainer::OnAllDisabledBehavior behavior)
{
m_menu->setOnAllDisabledBehavior(behavior);
return *this;
}
MenuBuilder &MenuBuilder::addToContainer(Id containerId, Id groupId)
{
ActionContainer *container = ActionManager::actionContainer(containerId);
if (QTC_GUARD(container))
container->addMenu(m_menu, groupId);
return *this;
}
MenuBuilder &MenuBuilder::addSeparator()
{
m_menu->addSeparator();
return *this;
}
/*! /*!
\class Core::ActionManager \class Core::ActionManager
\inheaderfile coreplugin/actionmanager/actionmanager.h \inheaderfile coreplugin/actionmanager/actionmanager.h

View File

@@ -6,6 +6,7 @@
#include "../core_global.h" #include "../core_global.h"
#include "../coreconstants.h" #include "../coreconstants.h"
#include "../icontext.h" #include "../icontext.h"
#include "actioncontainer.h" // For enum ActionContainer::OnAllDisabledBehavior
#include "command.h" #include "command.h"
#include <utils/action.h> #include <utils/action.h>
@@ -14,7 +15,6 @@
namespace Core { namespace Core {
class ActionContainer;
class ICore; class ICore;
namespace Internal { namespace Internal {
@@ -115,6 +115,21 @@ private:
ActionContainer *m_menu = nullptr; ActionContainer *m_menu = nullptr;
}; };
class CORE_EXPORT MenuBuilder
{
public:
MenuBuilder(Utils::Id menuId);
~MenuBuilder();
MenuBuilder &setTitle(const QString &title);
MenuBuilder &setOnAllDisabledBehavior(ActionContainer::OnAllDisabledBehavior behavior);
MenuBuilder &addToContainer(Utils::Id containerId, Utils::Id groupId = {});
MenuBuilder &addSeparator();
private:
ActionContainer *m_menu = nullptr;
};
class CORE_EXPORT ActionSeparator class CORE_EXPORT ActionSeparator
{ {
public: public: