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 "InputController.h"
10#include "ShortcutController.h"
11
12#include <QKeyEvent>
13
14using namespace QGBA;
15
16ShortcutView::ShortcutView(QWidget* parent)
17 : QWidget(parent)
18 , m_controller(nullptr)
19 , m_input(nullptr)
20{
21 m_ui.setupUi(this);
22 m_ui.keyEdit->setValueKey(0);
23
24 connect(m_ui.gamepadButton, &QAbstractButton::pressed, [this]() {
25 bool signalsBlocked = m_ui.keyEdit->blockSignals(true);
26 m_ui.keyEdit->setValueButton(-1);
27 m_ui.keyEdit->blockSignals(signalsBlocked);
28 });
29 connect(m_ui.keyboardButton, &QAbstractButton::pressed, [this]() {
30 bool signalsBlocked = m_ui.keyEdit->blockSignals(true);
31 m_ui.keyEdit->setValueKey(0);
32 m_ui.keyEdit->blockSignals(signalsBlocked);
33 });
34 connect(m_ui.keyEdit, SIGNAL(valueChanged(int)), this, SLOT(updateButton(int)));
35 connect(m_ui.keyEdit, SIGNAL(axisChanged(int, int)), this, SLOT(updateAxis(int, int)));
36 connect(m_ui.shortcutTable, SIGNAL(doubleClicked(const QModelIndex&)), this, SLOT(load(const QModelIndex&)));
37 connect(m_ui.clearButton, SIGNAL(clicked()), this, SLOT(clear()));
38}
39
40void ShortcutView::setController(ShortcutController* controller) {
41 m_controller = controller;
42 m_ui.shortcutTable->setModel(controller);
43}
44
45void ShortcutView::setInputController(InputController* controller) {
46 if (m_input) {
47 m_input->releaseFocus(this);
48 }
49 m_input = controller;
50 m_input->stealFocus(this);
51}
52
53void ShortcutView::load(const QModelIndex& index) {
54 if (!m_controller) {
55 return;
56 }
57 if (m_controller->isMenuAt(index)) {
58 return;
59 }
60 int shortcut = m_controller->shortcutAt(index);
61 if (index.column() == 1) {
62 m_ui.keyboardButton->click();
63 } else if (index.column() == 2) {
64 m_ui.gamepadButton->click();
65 }
66 bool blockSignals = m_ui.keyEdit->blockSignals(true);
67 m_ui.keyEdit->setFocus(Qt::MouseFocusReason);
68 if (m_ui.gamepadButton->isChecked()) {
69 m_ui.keyEdit->setValueButton(-1); // There are no default bindings
70 } else {
71 m_ui.keyEdit->setValueKey(shortcut);
72 }
73 m_ui.keyEdit->blockSignals(blockSignals);
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.keyEdit->setValueKey(-1);
90 }
91}
92
93void ShortcutView::updateButton(int button) {
94 if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
95 return;
96 }
97 if (m_ui.gamepadButton->isChecked()) {
98 m_controller->updateButton(m_ui.shortcutTable->selectionModel()->currentIndex(), button);
99 } else {
100 m_controller->updateKey(m_ui.shortcutTable->selectionModel()->currentIndex(), button);
101 }
102}
103
104void ShortcutView::updateAxis(int axis, int direction) {
105 if (!m_controller || m_controller->isMenuAt(m_ui.shortcutTable->selectionModel()->currentIndex())) {
106 return;
107 }
108 m_controller->updateAxis(m_ui.shortcutTable->selectionModel()->currentIndex(), axis,
109 static_cast<GamepadAxisEvent::Direction>(direction));
110}
111
112void ShortcutView::closeEvent(QCloseEvent*) {
113 if (m_input) {
114 m_input->releaseFocus(this);
115 }
116}
117
118bool ShortcutView::event(QEvent* event) {
119 if (m_input) {
120 if (event->type() == QEvent::WindowActivate) {
121 m_input->stealFocus(this);
122 } else if (event->type() == QEvent::WindowDeactivate) {
123 m_input->releaseFocus(this);
124 }
125 }
126 return QWidget::event(event);
127}