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