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 <QAbstractItemModel>
10#include <QKeySequence>
11
12#include <functional>
13
14class QAction;
15class QKeyEvent;
16class QMenu;
17class QString;
18
19namespace QGBA {
20
21class ShortcutController : public QAbstractItemModel {
22Q_OBJECT
23
24private:
25 class ShortcutItem {
26 public:
27 typedef QPair<std::function<void ()>, std::function<void ()>> Functions;
28
29 ShortcutItem(QAction* action, const QString& name, ShortcutItem* parent = nullptr);
30 ShortcutItem(Functions functions, const QKeySequence& shortcut, const QString& visibleName, const QString& name, ShortcutItem* parent = nullptr);
31 ShortcutItem(QMenu* action, ShortcutItem* parent = nullptr);
32
33 QAction* action() { return m_action; }
34 const QAction* action() const { return m_action; }
35 const QKeySequence& shortcut() const { return m_shortcut; }
36 Functions functions() const { return m_functions; }
37 QMenu* menu() { return m_menu; }
38 const QMenu* menu() const { return m_menu; }
39 const QString& visibleName() const { return m_visibleName; }
40 const QString& name() const { return m_name; }
41 QList<ShortcutItem>& items() { return m_items; }
42 const QList<ShortcutItem>& items() const { return m_items; }
43 ShortcutItem* parent() { return m_parent; }
44 const ShortcutItem* parent() const { return m_parent; }
45 void addAction(QAction* action, const QString& name);
46 void addFunctions(Functions functions, const QKeySequence& shortcut, const QString& visibleName, const QString& name);
47 void addSubmenu(QMenu* menu);
48 int button() const { return m_button; }
49 void setShortcut(const QKeySequence& sequence);
50 void setButton(int button) { m_button = button; }
51
52 bool operator==(const ShortcutItem& other) const { return m_menu == other.m_menu && m_action == other.m_action; }
53
54 private:
55 QAction* m_action;
56 QKeySequence m_shortcut;
57 QMenu* m_menu;
58 Functions m_functions;
59 QString m_name;
60 QString m_visibleName;
61 int m_button;
62 QList<ShortcutItem> m_items;
63 ShortcutItem* m_parent;
64 };
65
66public:
67 ShortcutController(QObject* parent = nullptr);
68
69 virtual QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const override;
70 virtual QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
71
72 virtual QModelIndex index(int row, int column, const QModelIndex& parent) const override;
73 virtual QModelIndex parent(const QModelIndex& index) const override;
74
75 virtual int columnCount(const QModelIndex& parent = QModelIndex()) const override;
76 virtual int rowCount(const QModelIndex& parent = QModelIndex()) const override;
77
78 void addAction(QMenu* menu, QAction* action, const QString& name);
79 void addFunctions(QMenu* menu, std::function<void ()> press, std::function<void()> release, const QKeySequence& shortcut, const QString& visibleName, const QString& name);
80 void addMenu(QMenu* menu, QMenu* parent = nullptr);
81
82 ShortcutItem* itemAt(const QModelIndex& index);
83 const ShortcutItem* itemAt(const QModelIndex& index) const;
84 const QAction* actionAt(const QModelIndex& index) const;
85
86 void updateKey(const QModelIndex& index, const QKeySequence& keySequence);
87 void updateButton(const QModelIndex& index, int button);
88
89protected:
90 bool eventFilter(QObject*, QEvent*) override;
91
92private:
93 static QKeySequence keyEventToSequence(const QKeyEvent*);
94 ShortcutItem m_rootMenu;
95 QMap<QMenu*, ShortcutItem*> m_menuMap;
96 QMap<int, ShortcutItem*> m_buttons;
97 QMap<QKeySequence, ShortcutItem*> m_heldKeys;
98};
99
100}
101
102#endif