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