Qt: Use MenuRole with QActions

This commit is contained in:
Vicki Pfau 2022-01-20 18:34:50 -08:00
parent 24a41d8453
commit e3ea64ad65
3 changed files with 27 additions and 7 deletions

View File

@ -17,6 +17,12 @@ Q_OBJECT
public:
typedef std::function<void ()> Function;
typedef std::function<void (bool)> BooleanFunction;
enum class Role {
NO_ROLE = 0,
ABOUT,
SETTINGS,
QUIT,
};
Action(Function, const QString& name, const QString& visibleName, QObject* parent = nullptr);
Action(BooleanFunction, const QString& name, const QString& visibleName, QObject* parent = nullptr);
@ -44,8 +50,10 @@ public:
bool isEnabled() const { return m_enabled; }
bool isActive() const { return m_active; }
bool isExclusive() const { return m_exclusive; }
Role role() const { return m_role; }
void setExclusive(bool exclusive = true) { m_exclusive = exclusive; }
void setRole(Role role) { m_role = role; }
Action& operator=(const Action&);
@ -62,6 +70,7 @@ private:
bool m_enabled = true;
bool m_active = false;
bool m_exclusive = false;
Role m_role = Role::NO_ROLE;
Function m_function;
BooleanFunction m_booleanFunction;

View File

@ -79,6 +79,20 @@ void ActionMapper::rebuildMenu(const QString& menu, QMenu* qmenu, QWidget* conte
} else if (!m_defaultShortcuts[actionName].isEmpty()) {
qaction->setShortcut(m_defaultShortcuts[actionName][0]);
}
switch (action->role()) {
case Action::Role::NO_ROLE:
qaction->setMenuRole(QAction::NoRole);
break;
case Action::Role::SETTINGS:
qaction->setMenuRole(QAction::PreferencesRole);
break;
case Action::Role::ABOUT:
qaction->setMenuRole(QAction::AboutRole);
break;
case Action::Role::QUIT:
qaction->setMenuRole(QAction::QuitRole);
break;
}
QObject::connect(qaction, &QAction::triggered, [qaction, action](bool enabled) {
if (qaction->isCheckable()) {
action->trigger(enabled);
@ -172,4 +186,4 @@ Action* ActionMapper::getAction(const QString& itemName) {
QKeySequence ActionMapper::defaultShortcut(const QString& itemName) {
return m_defaultShortcuts[itemName];
}
}

View File

@ -1311,11 +1311,8 @@ void Window::setupMenu(QMenuBar* menubar) {
m_actions.addSeparator("file");
#endif
m_actions.addAction(tr("About..."), "about", openTView<AboutScreen>(), "file");
#ifndef Q_OS_MAC
m_actions.addAction(tr("E&xit"), "quit", static_cast<QWidget*>(this), &QWidget::close, "file", QKeySequence::Quit);
#endif
m_actions.addAction(tr("About..."), "about", openTView<AboutScreen>(), "file")->setRole(Action::Role::ABOUT);
m_actions.addAction(tr("E&xit"), "quit", static_cast<QWidget*>(this), &QWidget::close, "file", QKeySequence::Quit)->setRole(Action::Role::SETTINGS);
m_actions.addMenu(tr("&Emulation"), "emu");
addGameAction(tr("&Reset"), "reset", &CoreController::reset, "emu", QKeySequence("Ctrl+R"));
@ -1579,7 +1576,7 @@ void Window::setupMenu(QMenuBar* menubar) {
addGameAction(tr("&Cheats..."), "cheatsWindow", openControllerTView<CheatsView>(), "tools");
m_actions.addSeparator("tools");
m_actions.addAction(tr("Settings..."), "settings", this, &Window::openSettingsWindow, "tools");
m_actions.addAction(tr("Settings..."), "settings", this, &Window::openSettingsWindow, "tools")->setRole(Action::Role::SETTINGS);
m_actions.addAction(tr("Make portable"), "makePortable", this, &Window::tryMakePortable, "tools");
#ifdef USE_DEBUGGERS