src/platform/qt/ActionMapper.h (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#pragma once
7
8#include "Action.h"
9
10#include <QHash>
11#include <QKeySequence>
12#include <QObject>
13#include <QSet>
14
15#include <functional>
16
17class QMenu;
18class QMenuBar;
19
20namespace QGBA {
21
22class ConfigOption;
23class ShortcutController;
24
25class ActionMapper : public QObject {
26Q_OBJECT
27
28public:
29 void addMenu(const QString& visibleName, const QString& name, const QString& parent = {});
30 void addHiddenMenu(const QString& visibleName, const QString& name, const QString& parent = {});
31 void clearMenu(const QString& name);
32 void rebuildMenu(QMenuBar*, QWidget* context, const ShortcutController&);
33
34 void addSeparator(const QString& menu);
35
36 Action* addAction(const QString& visibleName, const QString& name, Action::Function action, const QString& menu = {}, const QKeySequence& = {});
37 template<typename T, typename V> Action* addAction(const QString& visibleName, const QString& name, T* obj, V (T::*method)(), const QString& menu = {}, const QKeySequence& = {});
38 Action* addAction(const QString& visibleName, ConfigOption* option, const QVariant& variant, const QString& menu = {});
39
40 Action* addBooleanAction(const QString& visibleName, const QString& name, Action::BooleanFunction action, const QString& menu = {}, const QKeySequence& = {});
41 Action* addBooleanAction(const QString& visibleName, ConfigOption* option, const QString& menu = {});
42
43 Action* addHeldAction(const QString& visibleName, const QString& name, Action::BooleanFunction action, const QString& menu = {}, const QKeySequence& = {});
44
45 Action* addHiddenAction(const QString& visibleName, const QString& name, Action::Function action, const QString& menu = {}, const QKeySequence& = {});
46 template<typename T, typename V> Action* addHiddenAction(const QString& visibleName, const QString& name, T* obj, V (T::*method)(), const QString& menu = {}, const QKeySequence& = {});
47
48 bool isHeld(const QString& name) const { return m_heldActions.contains(name); }
49
50 QStringList menuItems(const QString& menu = QString()) const;
51 QString menuFor(const QString& action) const;
52 QString menuName(const QString& menu) const;
53
54 Action* getAction(const QString& action);
55 QKeySequence defaultShortcut(const QString& action);
56
57signals:
58 void actionAdded(const QString& name);
59 void menuCleared(const QString& name);
60
61private:
62 void rebuildMenu(const QString& menu, QMenu* qmenu, QWidget* context, const ShortcutController&);
63 Action* addAction(const Action& act, const QString& name, const QString& menu, const QKeySequence& shortcut);
64
65 QHash<QString, Action> m_actions;
66 QHash<QString, QStringList> m_menus;
67 QHash<QString, QString> m_reverseMenus;
68 QHash<QString, QString> m_menuNames;
69 QHash<QString, QKeySequence> m_defaultShortcuts;
70 QSet<QString> m_hiddenActions;
71 QSet<QString> m_heldActions;
72};
73
74template<typename T, typename V>
75Action* ActionMapper::addAction(const QString& visibleName, const QString& name, T* obj, V (T::*method)(), const QString& menu, const QKeySequence& shortcut) {
76 return addAction(visibleName, name, std::bind(method, obj), menu, shortcut);
77}
78
79template<typename T, typename V>
80Action* ActionMapper::addHiddenAction(const QString& visibleName, const QString& name, T* obj, V (T::*method)(), const QString& menu, const QKeySequence& shortcut) {
81 m_hiddenActions.insert(name);
82 return addAction(visibleName, name, obj, method, menu, shortcut);
83}
84
85}