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 , m_inputController(nullptr)
19{
20 m_ui.setupUi(this);
21 m_ui.keyEdit->setValueButton(-1);
22 m_ui.keySequenceEdit->installEventFilter(this);
23
24 connect(m_ui.keySequenceEdit, SIGNAL(keySequenceChanged(const QKeySequence&)), this, SLOT(updateKey(const QKeySequence&)));
25 connect(m_ui.keyEdit, SIGNAL(valueChanged(int)), this, SLOT(updateButton(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
35void ShortcutView::setInputController(InputController* controller) {
36 m_inputController = controller;
37 connect(controller, SIGNAL(axisChanged(int, int32_t)), m_ui.keyEdit, SLOT(setValueAxis(int, int32_t)));
38}
39
40bool ShortcutView::event(QEvent* event) {
41 if (event->type() == GamepadButtonEvent::Down()) {
42 updateButton(static_cast<GamepadButtonEvent*>(event)->value());
43 event->accept();
44 return true;
45 }
46 return QWidget::event(event);
47}
48
49bool ShortcutView::eventFilter(QObject*, QEvent* event) {
50 if (event->type() == QEvent::KeyPress) {
51 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
52 if (keyEvent->key() != Qt::Key_Tab && keyEvent->key() != Qt::Key_Backtab) {
53 return false;
54 }
55 if (!(keyEvent->modifiers() & ~Qt::ShiftModifier)) {
56 m_ui.keySequenceEdit->setKeySequence(ShortcutController::keyEventToSequence(keyEvent));
57 keyEvent->accept();
58 return true;
59 }
60 }
61 return false;
62}
63
64void ShortcutView::load(const QModelIndex& index) {
65 if (!m_controller) {
66 return;
67 }
68 if (m_controller->isMenuAt(index)) {
69 return;
70 }
71 QKeySequence sequence = m_controller->shortcutAt(index);
72 if (index.column() == 1) {
73 m_ui.keyboardButton->click();
74 } else if (index.column() == 2) {
75 m_ui.gamepadButton->click();
76 }
77 if (m_ui.gamepadButton->isChecked()) {
78 m_ui.keyEdit->setFocus();
79 m_ui.keyEdit->setValueButton(-1); // There are no default bindings
80 } else {
81 m_ui.keySequenceEdit->setFocus();
82 m_ui.keySequenceEdit->setKeySequence(sequence);
83 }
84}
85
86void ShortcutView::clear() {
87 if (!m_controller) {
88 return;
89 }
90 QModelIndex index = m_ui.shortcutTable->selectionModel()->currentIndex();
91 if (m_controller->isMenuAt(index)) {
92 return;
93 }
94 if (m_ui.gamepadButton->isChecked()) {
95 m_controller->clearButton(index);
96 m_ui.keyEdit->setValueButton(-1);
97 } else {
98 m_controller->clearKey(index);
99 m_ui.keySequenceEdit->setKeySequence(QKeySequence());
100 }
101}
102
103void ShortcutView::updateKey(const QKeySequence& shortcut) {
104 if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
105 return;
106 }
107 m_controller->updateKey(m_ui.shortcutTable->selectionModel()->currentIndex(), shortcut);
108}
109
110void ShortcutView::updateButton(int button) {
111 if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
112 return;
113 }
114 m_controller->updateButton(m_ui.shortcutTable->selectionModel()->currentIndex(), button);
115
116}