src/platform/qt/ShortcutView.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 "ShortcutView.h"
7
8#include "GamepadButtonEvent.h"
9#include "ShortcutController.h"
10
11#include <QKeyEvent>
12
13using namespace QGBA;
14
15ShortcutView::ShortcutView(QWidget* parent)
16 : QWidget(parent)
17 , m_controller(nullptr)
18{
19 m_ui.setupUi(this);
20 m_ui.keyEdit->setValueButton(-1);
21 m_ui.keySequenceEdit->installEventFilter(this);
22
23 connect(m_ui.keySequenceEdit, SIGNAL(keySequenceChanged(const QKeySequence&)), this, SLOT(updateKey(const QKeySequence&)));
24 connect(m_ui.keyEdit, SIGNAL(valueChanged(int)), this, SLOT(updateButton(int)));
25 connect(m_ui.keyEdit, SIGNAL(axisChanged(int, int)), this, SLOT(updateAxis(int, int)));
26 connect(m_ui.shortcutTable, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(load(const QModelIndex&)));
27 connect(m_ui.clearButton, SIGNAL(clicked()), this, SLOT(clear()));
28}
29
30void ShortcutView::setController(ShortcutController* controller) {
31 m_controller = controller;
32 m_ui.shortcutTable->setModel(controller);
33}
34
35bool ShortcutView::eventFilter(QObject*, QEvent* event) {
36 if (event->type() == QEvent::KeyPress) {
37 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
38 if (keyEvent->key() != Qt::Key_Tab && keyEvent->key() != Qt::Key_Backtab) {
39 return false;
40 }
41 if (!(keyEvent->modifiers() & ~Qt::ShiftModifier)) {
42 m_ui.keySequenceEdit->setKeySequence(ShortcutController::keyEventToSequence(keyEvent));
43 keyEvent->accept();
44 return true;
45 }
46 }
47 return false;
48}
49
50void ShortcutView::load(const QModelIndex& index) {
51 if (!m_controller) {
52 return;
53 }
54 if (m_controller->isMenuAt(index)) {
55 return;
56 }
57 QKeySequence sequence = m_controller->shortcutAt(index);
58 if (index.column() == 1) {
59 m_ui.keyboardButton->click();
60 } else if (index.column() == 2) {
61 m_ui.gamepadButton->click();
62 }
63 if (m_ui.gamepadButton->isChecked()) {
64 bool blockSignals = m_ui.keyEdit->blockSignals(true);
65 m_ui.keyEdit->setFocus();
66 m_ui.keyEdit->setValueButton(-1); // There are no default bindings
67 m_ui.keyEdit->blockSignals(blockSignals);
68 } else {
69 bool blockSignals = m_ui.keySequenceEdit->blockSignals(true);
70 m_ui.keySequenceEdit->setFocus();
71 m_ui.keySequenceEdit->setKeySequence(sequence);
72 m_ui.keySequenceEdit->blockSignals(blockSignals);
73 }
74}
75
76void ShortcutView::clear() {
77 if (!m_controller) {
78 return;
79 }
80 QModelIndex index = m_ui.shortcutTable->selectionModel()->currentIndex();
81 if (m_controller->isMenuAt(index)) {
82 return;
83 }
84 if (m_ui.gamepadButton->isChecked()) {
85 m_controller->clearButton(index);
86 m_ui.keyEdit->setValueButton(-1);
87 } else {
88 m_controller->clearKey(index);
89 m_ui.keySequenceEdit->setKeySequence(QKeySequence());
90 }
91}
92
93void ShortcutView::updateKey(const QKeySequence& shortcut) {
94 if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
95 return;
96 }
97 m_controller->updateKey(m_ui.shortcutTable->selectionModel()->currentIndex(), shortcut);
98}
99
100void ShortcutView::updateButton(int button) {
101 if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
102 return;
103 }
104 m_controller->updateButton(m_ui.shortcutTable->selectionModel()->currentIndex(), button);
105}
106
107void ShortcutView::updateAxis(int axis, int direction) {
108 if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
109 return;
110 }
111 m_controller->updateAxis(m_ui.shortcutTable->selectionModel()->currentIndex(), axis,
112 static_cast<GamepadAxisEvent::Direction>(direction));
113}