all repos — mgba @ 73518ceda5abab61009b1c5e49d32eb534b5c9ae

mGBA Game Boy Advance Emulator

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