src/platform/qt/ShortcutController.h (view raw)
1/* Copyright (c) 2013-2015 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#ifndef QGBA_SHORTCUT_MODEL
7#define QGBA_SHORTCUT_MODEL
8
9#include "GamepadAxisEvent.h"
10
11#include <QAbstractItemModel>
12
13#include <functional>
14
15class QAction;
16class QKeyEvent;
17class QMenu;
18class QString;
19
20namespace QGBA {
21
22class ConfigController;
23class InputProfile;
24
25class ShortcutController : public QAbstractItemModel {
26Q_OBJECT
27
28private:
29 constexpr static const char* const KEY_SECTION = "shortcutKey";
30 constexpr static const char* const BUTTON_SECTION = "shortcutButton";
31 constexpr static const char* const AXIS_SECTION = "shortcutAxis";
32 constexpr static const char* const BUTTON_PROFILE_SECTION = "shortcutProfileButton.";
33 constexpr static const char* const AXIS_PROFILE_SECTION = "shortcutProfileAxis.";
34
35 class ShortcutItem {
36 public:
37 typedef QPair<std::function<void ()>, std::function<void ()>> Functions;
38
39 ShortcutItem(QAction* action, const QString& name, ShortcutItem* parent = nullptr);
40 ShortcutItem(Functions functions, int shortcut, const QString& visibleName, const QString& name,
41 ShortcutItem* parent = nullptr);
42 ShortcutItem(QMenu* action, ShortcutItem* parent = nullptr);
43
44 QAction* action() { return m_action; }
45 const QAction* action() const { return m_action; }
46 const int shortcut() const { return m_shortcut; }
47 Functions functions() const { return m_functions; }
48 QMenu* menu() { return m_menu; }
49 const QMenu* menu() const { return m_menu; }
50 const QString& visibleName() const { return m_visibleName; }
51 const QString& name() const { return m_name; }
52 QList<ShortcutItem>& items() { return m_items; }
53 const QList<ShortcutItem>& items() const { return m_items; }
54 ShortcutItem* parent() { return m_parent; }
55 const ShortcutItem* parent() const { return m_parent; }
56 void addAction(QAction* action, const QString& name);
57 void addFunctions(Functions functions, int shortcut, const QString& visibleName,
58 const QString& name);
59 void addSubmenu(QMenu* menu);
60 int button() const { return m_button; }
61 void setShortcut(int sequence);
62 void setButton(int button) { m_button = button; }
63 int axis() const { return m_axis; }
64 GamepadAxisEvent::Direction direction() const { return m_direction; }
65 void setAxis(int axis, GamepadAxisEvent::Direction direction);
66
67 bool operator==(const ShortcutItem& other) const {
68 return m_menu == other.m_menu && m_action == other.m_action;
69 }
70
71 private:
72 QAction* m_action;
73 int m_shortcut;
74 QMenu* m_menu;
75 Functions m_functions;
76 QString m_name;
77 QString m_visibleName;
78 int m_button;
79 int m_axis;
80 GamepadAxisEvent::Direction m_direction;
81 QList<ShortcutItem> m_items;
82 ShortcutItem* m_parent;
83 };
84
85public:
86 ShortcutController(QObject* parent = nullptr);
87
88 void setConfigController(ConfigController* controller);
89 void setProfile(const QString& profile);
90
91 virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
92 virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
93
94 virtual QModelIndex index(int row, int column, const QModelIndex& parent) const override;
95 virtual QModelIndex parent(const QModelIndex& index) const override;
96
97 virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
98 virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
99
100 void addAction(QMenu* menu, QAction* action, const QString& name);
101 void addFunctions(QMenu* menu, std::function<void()> press, std::function<void()> release,
102 int shortcut, const QString& visibleName, const QString& name);
103 void addFunctions(QMenu* menu, std::function<void()> press, std::function<void()> release,
104 const QKeySequence& shortcut, const QString& visibleName, const QString& name);
105 void addMenu(QMenu* menu, QMenu* parent = nullptr);
106
107 QAction* getAction(const QString& name);
108 int shortcutAt(const QModelIndex& index) const;
109 bool isMenuAt(const QModelIndex& index) const;
110
111 void updateKey(const QModelIndex& index, int keySequence);
112 void updateButton(const QModelIndex& index, int button);
113 void updateAxis(const QModelIndex& index, int axis, GamepadAxisEvent::Direction direction);
114
115 void clearKey(const QModelIndex& index);
116 void clearButton(const QModelIndex& index);
117
118 static int toModifierShortcut(const QString& shortcut);
119 static bool isModifierKey(int key);
120 static int toModifierKey(int key);
121
122public slots:
123 void loadProfile(const QString& profile);
124
125protected:
126 bool eventFilter(QObject*, QEvent*) override;
127
128private:
129 ShortcutItem* itemAt(const QModelIndex& index);
130 const ShortcutItem* itemAt(const QModelIndex& index) const;
131 void loadShortcuts(ShortcutItem*);
132 void loadGamepadShortcuts(ShortcutItem*);
133 void onSubitems(ShortcutItem*, std::function<void(ShortcutItem*)> func);
134 void updateKey(ShortcutItem* item, int keySequence);
135
136 ShortcutItem m_rootMenu;
137 QMap<QMenu*, ShortcutItem*> m_menuMap;
138 QMap<int, ShortcutItem*> m_buttons;
139 QMap<QPair<int, GamepadAxisEvent::Direction>, ShortcutItem*> m_axes;
140 QMap<int, ShortcutItem*> m_heldKeys;
141 ConfigController* m_config;
142 QString m_profileName;
143 const InputProfile* m_profile;
144};
145
146}
147
148#endif