From e3ea64ad65dbdc786861156b26e9a7b205411f13 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Thu, 20 Jan 2022 18:34:50 -0800 Subject: [PATCH] Qt: Use MenuRole with QActions --- src/platform/qt/Action.h | 9 +++++++++ src/platform/qt/ActionMapper.cpp | 16 +++++++++++++++- src/platform/qt/Window.cpp | 9 +++------ 3 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/platform/qt/Action.h b/src/platform/qt/Action.h index 3d1e24c5f..9fe2f6aa2 100644 --- a/src/platform/qt/Action.h +++ b/src/platform/qt/Action.h @@ -17,6 +17,12 @@ Q_OBJECT public: typedef std::function Function; typedef std::function 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; diff --git a/src/platform/qt/ActionMapper.cpp b/src/platform/qt/ActionMapper.cpp index a0d80a0d1..e0166d5aa 100644 --- a/src/platform/qt/ActionMapper.cpp +++ b/src/platform/qt/ActionMapper.cpp @@ -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]; -} \ No newline at end of file +} diff --git a/src/platform/qt/Window.cpp b/src/platform/qt/Window.cpp index be78a8fb9..ece85c851 100644 --- a/src/platform/qt/Window.cpp +++ b/src/platform/qt/Window.cpp @@ -1311,11 +1311,8 @@ void Window::setupMenu(QMenuBar* menubar) { m_actions.addSeparator("file"); #endif - m_actions.addAction(tr("About..."), "about", openTView(), "file"); - -#ifndef Q_OS_MAC - m_actions.addAction(tr("E&xit"), "quit", static_cast(this), &QWidget::close, "file", QKeySequence::Quit); -#endif + m_actions.addAction(tr("About..."), "about", openTView(), "file")->setRole(Action::Role::ABOUT); + m_actions.addAction(tr("E&xit"), "quit", static_cast(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(), "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