all repos — mgba @ f6755a6e1b7b0cf2b944cd8ca842746f11d6bf82

mGBA Game Boy Advance Emulator

src/platform/qt/ActionMapper.cpp (view raw)

  1/* Copyright (c) 2013-2018 Jeffrey Pfau
  2 *
  3 * This Source Code Form is subject to the terms of the Mozilla Public
  4 * License, v. 2.0. If a copy of the MPL was not distributed with this
  5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6#include "ActionMapper.h"
  7
  8#include "ConfigController.h"
  9#include "ShortcutController.h"
 10
 11#include <QMenu>
 12#include <QMenuBar>
 13
 14using namespace QGBA;
 15
 16void ActionMapper::addMenu(const QString& visibleName, const QString& name, const QString& parent) {
 17	QString mname(QString(".%1").arg(name));
 18	m_menus[parent].append(mname);
 19	m_reverseMenus[mname] = parent;
 20	m_menuNames[name] = visibleName;
 21}
 22
 23void ActionMapper::addHiddenMenu(const QString& visibleName, const QString& name, const QString& parent) {
 24	m_hiddenActions.insert(QString(".%1").arg(name));
 25	addMenu(visibleName, name, parent);
 26}
 27
 28void ActionMapper::clearMenu(const QString& name) {
 29	m_menus[name].clear();
 30	emit menuCleared(name);
 31}
 32
 33void ActionMapper::rebuildMenu(QMenuBar* menubar, QWidget* context, const ShortcutController& shortcuts) {
 34	menubar->clear();
 35	for (QAction* action : context->actions()) {
 36		context->removeAction(action);
 37	}
 38	for (const QString& m : m_menus[{}]) {
 39		if (m_hiddenActions.contains(m)) {
 40			continue;
 41		}
 42		QString menu = m.mid(1);
 43		QMenu* qmenu = menubar->addMenu(m_menuNames[menu]);
 44
 45		rebuildMenu(menu, qmenu, context, shortcuts);
 46	}
 47}
 48
 49void ActionMapper::rebuildMenu(const QString& menu, QMenu* qmenu, QWidget* context, const ShortcutController& shortcuts) {
 50	for (const QString& actionName : m_menus[menu]) {
 51		if (actionName.isNull()) {
 52			qmenu->addSeparator();
 53			continue;
 54		}
 55		if (m_hiddenActions.contains(actionName)) {
 56			continue;
 57		}
 58		if (actionName[0] == '.') {
 59			QString name = actionName.mid(1);
 60			QMenu* newMenu = qmenu->addMenu(m_menuNames[name]);
 61			rebuildMenu(name, newMenu, context, shortcuts);
 62			continue;
 63		}
 64		Action* action = &m_actions[actionName];
 65		QAction* qaction = qmenu->addAction(action->visibleName());
 66		qaction->setEnabled(action->isEnabled());
 67		qaction->setShortcutContext(Qt::WidgetShortcut);
 68		if (action->isExclusive() || action->booleanAction()) {
 69			qaction->setCheckable(true);
 70		}
 71		if (action->isActive()) {
 72			qaction->setChecked(true);
 73		}
 74		const Shortcut* shortcut = shortcuts.shortcut(actionName);
 75		if (shortcut && shortcut->shortcut() > 0) {
 76			qaction->setShortcut(QKeySequence(shortcut->shortcut()));
 77		} else if (!m_defaultShortcuts[actionName].isEmpty()) {
 78			qaction->setShortcut(m_defaultShortcuts[actionName][0]);
 79		}
 80		QObject::connect(qaction, &QAction::triggered, [qaction, action](bool enabled) {
 81			if (qaction->isCheckable()) {
 82				action->trigger(enabled);
 83			} else {
 84				action->trigger();
 85			}
 86		});
 87		QObject::connect(action, &Action::enabled, qaction, &QAction::setEnabled);
 88		QObject::connect(action, &Action::activated, qaction, &QAction::setChecked);
 89		QObject::connect(action, &Action::destroyed, qaction, &QAction::deleteLater);
 90		if (shortcut) {
 91			QObject::connect(shortcut, &Shortcut::shortcutChanged, qaction, [qaction](int shortcut) {
 92				qaction->setShortcut(QKeySequence(shortcut));
 93			});
 94		}
 95		context->addAction(qaction);
 96	}
 97}
 98
 99void ActionMapper::addSeparator(const QString& menu) {
100	m_menus[menu].append(QString{});
101}
102
103Action* ActionMapper::addAction(const Action& act, const QString& name, const QString& menu, const QKeySequence& shortcut) {
104	m_actions.insert(name, act);
105	m_reverseMenus[name] = menu;
106	m_menus[menu].append(name);
107	if (!shortcut.isEmpty()) {
108		m_defaultShortcuts[name] = shortcut;
109	}
110	emit actionAdded(name);
111
112	return &m_actions[name];
113}
114
115Action* ActionMapper::addAction(const QString& visibleName, const QString& name, Action::Function action, const QString& menu, const QKeySequence& shortcut) {
116	return addAction(Action(action, name, visibleName), name, menu, shortcut);
117}
118
119Action* ActionMapper::addAction(const QString& visibleName, ConfigOption* option, const QVariant& variant, const QString& menu) {
120	return addAction(Action([option, variant]() {
121		option->setValue(variant);
122	}, option->name(), visibleName), QString("%1.%2").arg(option->name()).arg(variant.toString()), menu, {});
123}
124
125Action* ActionMapper::addBooleanAction(const QString& visibleName, const QString& name, Action::BooleanFunction action, const QString& menu, const QKeySequence& shortcut) {
126	return addAction(Action(action, name, visibleName), name, menu, shortcut);
127}
128
129Action* ActionMapper::addBooleanAction(const QString& visibleName, ConfigOption* option, const QString& menu) {
130	return addAction(Action([option](bool value) {
131		option->setValue(value);
132	}, option->name(), visibleName), option->name(), menu, {});
133}
134
135Action* ActionMapper::addHeldAction(const QString& visibleName, const QString& name, Action::BooleanFunction action, const QString& menu, const QKeySequence& shortcut) {
136	m_hiddenActions.insert(name);
137	m_heldActions.insert(name);
138	return addBooleanAction(visibleName, name, action, menu, shortcut);
139}
140
141Action* ActionMapper::addHiddenAction(const QString& visibleName, const QString& name, Action::Function action, const QString& menu, const QKeySequence& shortcut) {
142	m_hiddenActions.insert(name);
143	return addAction(visibleName, name, action, menu, shortcut);
144}
145
146QStringList ActionMapper::menuItems(const QString& menu) const {
147	return m_menus[menu];
148}
149
150QString ActionMapper::menuFor(const QString& menu) const {
151	return m_reverseMenus[menu];
152}
153
154QString ActionMapper::menuName(const QString& menu) const {
155	if (!menu.isNull() && menu[0] == '.') {
156		return m_menuNames[menu.mid(1)];
157	}
158	return m_menuNames[menu];
159}
160
161Action* ActionMapper::getAction(const QString& itemName) {
162	return &m_actions[itemName];
163}
164
165QKeySequence ActionMapper::defaultShortcut(const QString& itemName) {
166	return m_defaultShortcuts[itemName];
167}