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#include <QKeySequence>
13
14#include <functional>
15
16class QAction;
17class QKeyEvent;
18class QMenu;
19class QString;
20
21namespace QGBA {
22
23class ConfigController;
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
33 class ShortcutItem {
34 public:
35 typedef QPair<std::function<void ()>, std::function<void ()>> Functions;
36
37 ShortcutItem(QAction* action, const QString& name, ShortcutItem* parent = nullptr);
38 ShortcutItem(Functions functions, const QKeySequence& shortcut, const QString& visibleName, const QString& name, ShortcutItem* parent = nullptr);
39 ShortcutItem(QMenu* action, ShortcutItem* parent = nullptr);
40
41 QAction* action() { return m_action; }
42 const QAction* action() const { return m_action; }
43 const QKeySequence& shortcut() const { return m_shortcut; }
44 Functions functions() const { return m_functions; }
45 QMenu* menu() { return m_menu; }
46 const QMenu* menu() const { return m_menu; }
47 const QString& visibleName() const { return m_visibleName; }
48 const QString& name() const { return m_name; }
49 QList<ShortcutItem>& items() { return m_items; }
50 const QList<ShortcutItem>& items() const { return m_items; }
51 ShortcutItem* parent() { return m_parent; }
52 const ShortcutItem* parent() const { return m_parent; }
53 void addAction(QAction* action, const QString& name);
54 void addFunctions(Functions functions, const QKeySequence& shortcut, const QString& visibleName, const QString& name);
55 void addSubmenu(QMenu* menu);
56 int button() const { return m_button; }
57 void setShortcut(const QKeySequence& sequence);
58 void setButton(int button) { m_button = button; }
59 int axis() const { return m_axis; }
60 GamepadAxisEvent::Direction direction() const { return m_direction; }
61 void setAxis(int axis, GamepadAxisEvent::Direction direction);
62
63 bool operator==(const ShortcutItem& other) const { return m_menu == other.m_menu && m_action == other.m_action; }
64
65 private:
66 QAction* m_action;
67 QKeySequence m_shortcut;
68 QMenu* m_menu;
69 Functions m_functions;
70 QString m_name;
71 QString m_visibleName;
72 int m_button;
73 int m_axis;
74 GamepadAxisEvent::Direction m_direction;
75 QList<ShortcutItem> m_items;
76 ShortcutItem* m_parent;
77 };
78
79public:
80 ShortcutController(QObject* parent = nullptr);
81
82 void setConfigController(ConfigController* controller);
83
84 virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
85 virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
86
87 virtual QModelIndex index(int row, int column, const QModelIndex& parent) const override;
88 virtual QModelIndex parent(const QModelIndex& index) const override;
89
90 virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
91 virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
92
93 void addAction(QMenu* menu, QAction* action, const QString& name);
94 void addFunctions(QMenu* menu, std::function<void ()> press, std::function<void()> release, const QKeySequence& shortcut, const QString& visibleName, const QString& name);
95 void addMenu(QMenu* menu, QMenu* parent = nullptr);
96
97 QKeySequence shortcutAt(const QModelIndex& index) const;
98 bool isMenuAt(const QModelIndex& index) const;
99
100 void updateKey(const QModelIndex& index, const QKeySequence& keySequence);
101 void updateButton(const QModelIndex& index, int button);
102 void updateAxis(const QModelIndex& index, int axis, GamepadAxisEvent::Direction direction);
103
104 void clearKey(const QModelIndex& index);
105 void clearButton(const QModelIndex& index);
106
107 static QKeySequence keyEventToSequence(const QKeyEvent*);
108
109protected:
110 bool eventFilter(QObject*, QEvent*) override;
111
112private:
113 ShortcutItem* itemAt(const QModelIndex& index);
114 const ShortcutItem* itemAt(const QModelIndex& index) const;
115 void loadShortcuts(ShortcutItem*);
116
117 ShortcutItem m_rootMenu;
118 QMap<QMenu*, ShortcutItem*> m_menuMap;
119 QMap<int, ShortcutItem*> m_buttons;
120 QMap<QPair<int, GamepadAxisEvent::Direction>, ShortcutItem*> m_axes;
121 QMap<QKeySequence, ShortcutItem*> m_heldKeys;
122 ConfigController* m_config;
123};
124
125}
126
127#endif