src/platform/qt/ShortcutController.cpp (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#include "ShortcutController.h"
7
8#include "ConfigController.h"
9#include "GamepadButtonEvent.h"
10
11#include <QAction>
12#include <QKeyEvent>
13#include <QMenu>
14
15using namespace QGBA;
16
17ShortcutController::ShortcutController(QObject* parent)
18 : QAbstractItemModel(parent)
19 , m_rootMenu(nullptr)
20 , m_config(nullptr)
21{
22}
23
24void ShortcutController::setConfigController(ConfigController* controller) {
25 m_config = controller;
26}
27
28QVariant ShortcutController::data(const QModelIndex& index, int role) const {
29 if (role != Qt::DisplayRole || !index.isValid()) {
30 return QVariant();
31 }
32 int row = index.row();
33 const ShortcutItem* item = static_cast<const ShortcutItem*>(index.internalPointer());
34 switch (index.column()) {
35 case 0:
36 return item->visibleName();
37 case 1:
38 return item->shortcut().toString(QKeySequence::NativeText);
39 case 2:
40 if (item->button() >= 0) {
41 return item->button();
42 }
43 break;
44 }
45 return QVariant();
46}
47
48QVariant ShortcutController::headerData(int section, Qt::Orientation orientation, int role) const {
49 if (role != Qt::DisplayRole) {
50 return QAbstractItemModel::headerData(section, orientation, role);
51 }
52 if (orientation == Qt::Horizontal) {
53 switch (section) {
54 case 0:
55 return tr("Action");
56 case 1:
57 return tr("Keyboard");
58 case 2:
59 return tr("Gamepad");
60 }
61 }
62 return section;
63}
64
65QModelIndex ShortcutController::index(int row, int column, const QModelIndex& parent) const {
66 const ShortcutItem* pmenu = &m_rootMenu;
67 if (parent.isValid()) {
68 pmenu = static_cast<ShortcutItem*>(parent.internalPointer());
69 }
70 return createIndex(row, column, const_cast<ShortcutItem*>(&pmenu->items()[row]));
71}
72
73QModelIndex ShortcutController::parent(const QModelIndex& index) const {
74 if (!index.isValid() || !index.internalPointer()) {
75 return QModelIndex();
76 }
77 ShortcutItem* item = static_cast<ShortcutItem*>(index.internalPointer());
78 if (!item->parent() || !item->parent()->parent()) {
79 return QModelIndex();
80 }
81 return createIndex(item->parent()->parent()->items().indexOf(*item->parent()), 0, item->parent());
82}
83
84int ShortcutController::columnCount(const QModelIndex& index) const {
85 return 3;
86}
87
88int ShortcutController::rowCount(const QModelIndex& index) const {
89 if (!index.isValid()) {
90 return m_rootMenu.items().count();
91 }
92 const ShortcutItem* item = static_cast<const ShortcutItem*>(index.internalPointer());
93 return item->items().count();
94}
95
96void ShortcutController::addAction(QMenu* menu, QAction* action, const QString& name) {
97 ShortcutItem* smenu = m_menuMap[menu];
98 if (!smenu) {
99 return;
100 }
101 ShortcutItem* pmenu = smenu->parent();
102 int row = pmenu->items().indexOf(*smenu);
103 QModelIndex parent = createIndex(row, 0, smenu);
104 beginInsertRows(parent, smenu->items().count(), smenu->items().count());
105 smenu->addAction(action, name);
106 endInsertRows();
107 ShortcutItem* item = &smenu->items().last();
108 if (m_config) {
109 loadShortcuts(item);
110 }
111 emit dataChanged(createIndex(smenu->items().count() - 1, 0, item), createIndex(smenu->items().count() - 1, 2, item));
112}
113
114void ShortcutController::addFunctions(QMenu* menu, std::function<void ()> press, std::function<void ()> release, const QKeySequence& shortcut, const QString& visibleName, const QString& name) {
115 ShortcutItem* smenu = m_menuMap[menu];
116 if (!smenu) {
117 return;
118 }
119 ShortcutItem* pmenu = smenu->parent();
120 int row = pmenu->items().indexOf(*smenu);
121 QModelIndex parent = createIndex(row, 0, smenu);
122 beginInsertRows(parent, smenu->items().count(), smenu->items().count());
123 smenu->addFunctions(qMakePair(press, release), shortcut, visibleName, name);
124 endInsertRows();
125 ShortcutItem* item = &smenu->items().last();
126 if (m_config) {
127 loadShortcuts(item);
128 }
129 m_heldKeys[shortcut] = item;
130 emit dataChanged(createIndex(smenu->items().count() - 1, 0, item), createIndex(smenu->items().count() - 1, 2, item));
131}
132
133void ShortcutController::addMenu(QMenu* menu, QMenu* parentMenu) {
134 ShortcutItem* smenu = m_menuMap[parentMenu];
135 if (!smenu) {
136 smenu = &m_rootMenu;
137 }
138 QModelIndex parent;
139 ShortcutItem* pmenu = smenu->parent();
140 if (pmenu) {
141 int row = pmenu->items().indexOf(*smenu);
142 parent = createIndex(row, 0, smenu);
143 }
144 beginInsertRows(parent, smenu->items().count(), smenu->items().count());
145 smenu->addSubmenu(menu);
146 endInsertRows();
147 ShortcutItem* item = &smenu->items().last();
148 emit dataChanged(createIndex(smenu->items().count() - 1, 0, item), createIndex(smenu->items().count() - 1, 2, item));
149 m_menuMap[menu] = item;
150}
151
152ShortcutController::ShortcutItem* ShortcutController::itemAt(const QModelIndex& index) {
153 if (!index.isValid()) {
154 return nullptr;
155 }
156 return static_cast<ShortcutItem*>(index.internalPointer());
157}
158
159const ShortcutController::ShortcutItem* ShortcutController::itemAt(const QModelIndex& index) const {
160 if (!index.isValid()) {
161 return nullptr;
162 }
163 return static_cast<const ShortcutItem*>(index.internalPointer());
164}
165
166const QAction* ShortcutController::actionAt(const QModelIndex& index) const {
167 const ShortcutItem* item = itemAt(index);
168 if (!item) {
169 return nullptr;
170 }
171 return item->action();
172}
173
174void ShortcutController::updateKey(const QModelIndex& index, const QKeySequence& keySequence) {
175 if (!index.isValid()) {
176 return;
177 }
178 const QModelIndex& parent = index.parent();
179 if (!parent.isValid()) {
180 return;
181 }
182 ShortcutItem* item = itemAt(index);
183 if (item->functions().first) {
184 QKeySequence oldShortcut = item->shortcut();
185 if (!oldShortcut.isEmpty()) {
186 m_heldKeys.take(oldShortcut);
187 }
188 m_heldKeys[keySequence] = item;
189 }
190 item->setShortcut(keySequence);
191 if (m_config) {
192 m_config->setQtOption(item->name(), keySequence.toString(), KEY_SECTION);
193 }
194 emit dataChanged(createIndex(index.row(), 0, index.internalPointer()), createIndex(index.row(), 2, index.internalPointer()));
195}
196
197void ShortcutController::updateButton(const QModelIndex& index, int button) {
198 if (!index.isValid()) {
199 return;
200 }
201 const QModelIndex& parent = index.parent();
202 if (!parent.isValid()) {
203 return;
204 }
205 ShortcutItem* item = itemAt(index);
206 int oldButton = item->button();
207 item->setButton(button);
208 if (oldButton >= 0) {
209 m_buttons.take(oldButton);
210 }
211 m_buttons[button] = item;
212 if (m_config) {
213 m_config->setQtOption(item->name(), button, BUTTON_SECTION);
214 }
215 emit dataChanged(createIndex(index.row(), 0, index.internalPointer()), createIndex(index.row(), 2, index.internalPointer()));
216}
217
218bool ShortcutController::eventFilter(QObject*, QEvent* event) {
219 if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
220 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
221 if (keyEvent->isAutoRepeat()) {
222 return false;
223 }
224 auto item = m_heldKeys.find(keyEventToSequence(keyEvent));
225 if (item == m_heldKeys.end()) {
226 return false;
227 }
228 ShortcutItem::Functions pair = item.value()->functions();
229 if (event->type() == QEvent::KeyPress) {
230 if (pair.first) {
231 pair.first();
232 }
233 } else {
234 if (pair.second) {
235 pair.second();
236 }
237 }
238 event->accept();
239 return true;
240 }
241 if (event->type() == GamepadButtonEvent::Down()) {
242 auto item = m_buttons.find(static_cast<GamepadButtonEvent*>(event)->value());
243 if (item == m_buttons.end()) {
244 return false;
245 }
246 QAction* action = item.value()->action();
247 if (action && action->isEnabled()) {
248 action->trigger();
249 }
250 ShortcutItem::Functions pair = item.value()->functions();
251 if (pair.first) {
252 pair.first();
253 }
254 event->accept();
255 return true;
256 }
257 if (event->type() == GamepadButtonEvent::Up()) {
258 auto item = m_buttons.find(static_cast<GamepadButtonEvent*>(event)->value());
259 if (item == m_buttons.end()) {
260 return false;
261 }
262 ShortcutItem::Functions pair = item.value()->functions();
263 if (pair.second) {
264 pair.second();
265 }
266 event->accept();
267 return true;
268 }
269 return false;
270}
271
272void ShortcutController::loadShortcuts(ShortcutItem* item) {
273 QVariant shortcut = m_config->getQtOption(item->name(), KEY_SECTION);
274 if (!shortcut.isNull()) {
275 QKeySequence keySequence(shortcut.toString());
276 if (item->functions().first) {
277 QKeySequence oldShortcut = item->shortcut();
278 if (!oldShortcut.isEmpty()) {
279 m_heldKeys.take(oldShortcut);
280 }
281 m_heldKeys[keySequence] = item;
282 }
283 item->setShortcut(keySequence);
284 }
285 QVariant button = m_config->getQtOption(item->name(), BUTTON_SECTION);
286 if (!button.isNull()) {
287 int oldButton = item->button();
288 item->setButton(button.toInt());
289 if (oldButton >= 0) {
290 m_buttons.take(oldButton);
291 }
292 m_buttons[button.toInt()] = item;
293 }
294}
295
296QKeySequence ShortcutController::keyEventToSequence(const QKeyEvent* event) {
297 QString modifier = QString::null;
298
299 if (event->modifiers() & Qt::ShiftModifier) {
300 modifier += "Shift+";
301 }
302 if (event->modifiers() & Qt::ControlModifier) {
303 modifier += "Ctrl+";
304 }
305 if (event->modifiers() & Qt::AltModifier) {
306 modifier += "Alt+";
307 }
308 if (event->modifiers() & Qt::MetaModifier) {
309 modifier += "Meta+";
310 }
311
312 QString key = QKeySequence(event->key()).toString();
313 return QKeySequence(modifier + key);
314}
315
316ShortcutController::ShortcutItem::ShortcutItem(QAction* action, const QString& name, ShortcutItem* parent)
317 : m_action(action)
318 , m_shortcut(action->shortcut())
319 , m_menu(nullptr)
320 , m_name(name)
321 , m_button(-1)
322 , m_parent(parent)
323{
324 m_visibleName = action->text()
325 .remove(QRegExp("&(?!&)"))
326 .remove("...");
327}
328
329ShortcutController::ShortcutItem::ShortcutItem(ShortcutController::ShortcutItem::Functions functions, const QKeySequence& shortcut, const QString& visibleName, const QString& name, ShortcutItem* parent)
330 : m_action(nullptr)
331 , m_shortcut(shortcut)
332 , m_functions(functions)
333 , m_menu(nullptr)
334 , m_name(name)
335 , m_visibleName(visibleName)
336 , m_button(-1)
337 , m_parent(parent)
338{
339}
340
341ShortcutController::ShortcutItem::ShortcutItem(QMenu* menu, ShortcutItem* parent)
342 : m_action(nullptr)
343 , m_menu(menu)
344 , m_button(-1)
345 , m_parent(parent)
346{
347 if (menu) {
348 m_visibleName = menu->title()
349 .remove(QRegExp("&(?!&)"))
350 .remove("...");
351 }
352}
353
354void ShortcutController::ShortcutItem::addAction(QAction* action, const QString& name) {
355 m_items.append(ShortcutItem(action, name, this));
356}
357
358void ShortcutController::ShortcutItem::addFunctions(ShortcutController::ShortcutItem::Functions functions, const QKeySequence& shortcut, const QString& visibleName, const QString& name) {
359 m_items.append(ShortcutItem(functions, shortcut, visibleName, name, this));
360}
361
362void ShortcutController::ShortcutItem::addSubmenu(QMenu* menu) {
363 m_items.append(ShortcutItem(menu, this));
364}
365
366void ShortcutController::ShortcutItem::setShortcut(const QKeySequence& shortcut) {
367 m_shortcut = shortcut;
368 if (m_action) {
369 m_action->setShortcut(shortcut);
370 }
371}