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
174bool ShortcutController::isMenuAt(const QModelIndex& index) const {
175 const ShortcutItem* item = itemAt(index);
176 if (!item) {
177 return false;
178 }
179 return item->menu();
180}
181
182void ShortcutController::updateKey(const QModelIndex& index, const QKeySequence& keySequence) {
183 if (!index.isValid()) {
184 return;
185 }
186 const QModelIndex& parent = index.parent();
187 if (!parent.isValid()) {
188 return;
189 }
190 ShortcutItem* item = itemAt(index);
191 if (item->functions().first) {
192 QKeySequence oldShortcut = item->shortcut();
193 if (!oldShortcut.isEmpty()) {
194 m_heldKeys.take(oldShortcut);
195 }
196 m_heldKeys[keySequence] = item;
197 }
198 item->setShortcut(keySequence);
199 if (m_config) {
200 m_config->setQtOption(item->name(), keySequence.toString(), KEY_SECTION);
201 }
202 emit dataChanged(createIndex(index.row(), 0, index.internalPointer()), createIndex(index.row(), 2, index.internalPointer()));
203}
204
205void ShortcutController::updateButton(const QModelIndex& index, int button) {
206 if (!index.isValid()) {
207 return;
208 }
209 const QModelIndex& parent = index.parent();
210 if (!parent.isValid()) {
211 return;
212 }
213 ShortcutItem* item = itemAt(index);
214 int oldButton = item->button();
215 item->setButton(button);
216 if (oldButton >= 0) {
217 m_buttons.take(oldButton);
218 }
219 m_buttons[button] = item;
220 if (m_config) {
221 m_config->setQtOption(item->name(), button, BUTTON_SECTION);
222 }
223 emit dataChanged(createIndex(index.row(), 0, index.internalPointer()), createIndex(index.row(), 2, index.internalPointer()));
224}
225
226bool ShortcutController::eventFilter(QObject*, QEvent* event) {
227 if (event->type() == QEvent::KeyPress || event->type() == QEvent::KeyRelease) {
228 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
229 if (keyEvent->isAutoRepeat()) {
230 return false;
231 }
232 auto item = m_heldKeys.find(keyEventToSequence(keyEvent));
233 if (item == m_heldKeys.end()) {
234 return false;
235 }
236 ShortcutItem::Functions pair = item.value()->functions();
237 if (event->type() == QEvent::KeyPress) {
238 if (pair.first) {
239 pair.first();
240 }
241 } else {
242 if (pair.second) {
243 pair.second();
244 }
245 }
246 event->accept();
247 return true;
248 }
249 if (event->type() == GamepadButtonEvent::Down()) {
250 auto item = m_buttons.find(static_cast<GamepadButtonEvent*>(event)->value());
251 if (item == m_buttons.end()) {
252 return false;
253 }
254 QAction* action = item.value()->action();
255 if (action && action->isEnabled()) {
256 action->trigger();
257 }
258 ShortcutItem::Functions pair = item.value()->functions();
259 if (pair.first) {
260 pair.first();
261 }
262 event->accept();
263 return true;
264 }
265 if (event->type() == GamepadButtonEvent::Up()) {
266 auto item = m_buttons.find(static_cast<GamepadButtonEvent*>(event)->value());
267 if (item == m_buttons.end()) {
268 return false;
269 }
270 ShortcutItem::Functions pair = item.value()->functions();
271 if (pair.second) {
272 pair.second();
273 }
274 event->accept();
275 return true;
276 }
277 return false;
278}
279
280void ShortcutController::loadShortcuts(ShortcutItem* item) {
281 QVariant shortcut = m_config->getQtOption(item->name(), KEY_SECTION);
282 if (!shortcut.isNull()) {
283 QKeySequence keySequence(shortcut.toString());
284 if (item->functions().first) {
285 QKeySequence oldShortcut = item->shortcut();
286 if (!oldShortcut.isEmpty()) {
287 m_heldKeys.take(oldShortcut);
288 }
289 m_heldKeys[keySequence] = item;
290 }
291 item->setShortcut(keySequence);
292 }
293 QVariant button = m_config->getQtOption(item->name(), BUTTON_SECTION);
294 if (!button.isNull()) {
295 int oldButton = item->button();
296 item->setButton(button.toInt());
297 if (oldButton >= 0) {
298 m_buttons.take(oldButton);
299 }
300 m_buttons[button.toInt()] = item;
301 }
302}
303
304QKeySequence ShortcutController::keyEventToSequence(const QKeyEvent* event) {
305 QString modifier = QString::null;
306
307 if (event->modifiers() & Qt::ShiftModifier) {
308 modifier += "Shift+";
309 }
310 if (event->modifiers() & Qt::ControlModifier) {
311 modifier += "Ctrl+";
312 }
313 if (event->modifiers() & Qt::AltModifier) {
314 modifier += "Alt+";
315 }
316 if (event->modifiers() & Qt::MetaModifier) {
317 modifier += "Meta+";
318 }
319
320 QString key = QKeySequence(event->key()).toString();
321 return QKeySequence(modifier + key);
322}
323
324ShortcutController::ShortcutItem::ShortcutItem(QAction* action, const QString& name, ShortcutItem* parent)
325 : m_action(action)
326 , m_shortcut(action->shortcut())
327 , m_menu(nullptr)
328 , m_name(name)
329 , m_button(-1)
330 , m_parent(parent)
331{
332 m_visibleName = action->text()
333 .remove(QRegExp("&(?!&)"))
334 .remove("...");
335}
336
337ShortcutController::ShortcutItem::ShortcutItem(ShortcutController::ShortcutItem::Functions functions, const QKeySequence& shortcut, const QString& visibleName, const QString& name, ShortcutItem* parent)
338 : m_action(nullptr)
339 , m_shortcut(shortcut)
340 , m_functions(functions)
341 , m_menu(nullptr)
342 , m_name(name)
343 , m_visibleName(visibleName)
344 , m_button(-1)
345 , m_parent(parent)
346{
347}
348
349ShortcutController::ShortcutItem::ShortcutItem(QMenu* menu, ShortcutItem* parent)
350 : m_action(nullptr)
351 , m_menu(menu)
352 , m_button(-1)
353 , m_parent(parent)
354{
355 if (menu) {
356 m_visibleName = menu->title()
357 .remove(QRegExp("&(?!&)"))
358 .remove("...");
359 }
360}
361
362void ShortcutController::ShortcutItem::addAction(QAction* action, const QString& name) {
363 m_items.append(ShortcutItem(action, name, this));
364}
365
366void ShortcutController::ShortcutItem::addFunctions(ShortcutController::ShortcutItem::Functions functions, const QKeySequence& shortcut, const QString& visibleName, const QString& name) {
367 m_items.append(ShortcutItem(functions, shortcut, visibleName, name, this));
368}
369
370void ShortcutController::ShortcutItem::addSubmenu(QMenu* menu) {
371 m_items.append(ShortcutItem(menu, this));
372}
373
374void ShortcutController::ShortcutItem::setShortcut(const QKeySequence& shortcut) {
375 m_shortcut = shortcut;
376 if (m_action) {
377 m_action->setShortcut(shortcut);
378 }
379}