all repos — mgba @ e263467f139a17c5313242711ee2a39c8248cd6d

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