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 "InputModel.h"
11
12#include <QKeyEvent>
13
14using namespace QGBA;
15
16ShortcutView::ShortcutView(QWidget* parent)
17 : QWidget(parent)
18 , m_model()
19{
20 m_ui.setupUi(this);
21 m_ui.keyEdit->setValueKey(0);
22 m_ui.shortcutTable->setModel(&m_model);
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, &KeyEditor::valueChanged, this, &ShortcutView::updateButton);
35 connect(m_ui.keyEdit, &KeyEditor::axisChanged, this, &ShortcutView::updateAxis);
36 connect(m_ui.shortcutTable, &QAbstractItemView::doubleClicked, this, &ShortcutView::load);
37 connect(m_ui.clearButton, &QAbstractButton::clicked, this, &ShortcutView::clear);
38#ifdef BUILD_SDL
39 connect(m_ui.gamepadName, static_cast<void (QComboBox::*)(int)>(&QComboBox::currentIndexChanged), [this](int index) {
40 m_input->setGamepad(SDL_BINDING_BUTTON, index);
41 });
42#endif
43}
44
45ShortcutView::~ShortcutView() {
46 m_input->releaseFocus(this);
47}
48
49void ShortcutView::setModel(InputIndex* model) {
50 m_model.clone(*model);
51}
52
53void ShortcutView::setInputController(InputController* controller) {
54 if (m_input) {
55 m_input->releaseFocus(this);
56 }
57 m_input = controller;
58 m_input->stealFocus(this);
59 updateGamepads();
60}
61
62void ShortcutView::updateGamepads() {
63 if (!m_input) {
64 return;
65 }
66#ifdef BUILD_SDL
67 m_ui.gamepadName->clear();
68
69 QStringList gamepads = m_input->connectedGamepads(SDL_BINDING_BUTTON);
70 int activeGamepad = m_input->gamepad(SDL_BINDING_BUTTON);
71
72 for (const auto& gamepad : gamepads) {
73 m_ui.gamepadName->addItem(gamepad);
74 }
75 m_ui.gamepadName->setCurrentIndex(activeGamepad);
76#endif
77
78}
79
80void ShortcutView::load(const QModelIndex& index) {
81 InputItem* item = m_model.itemAt(index);
82 if (!item) {
83 return;
84 }
85 int shortcut = item->shortcut();
86 if (index.column() == 1) {
87 m_ui.keyboardButton->click();
88 } else if (index.column() == 2) {
89 m_ui.gamepadButton->click();
90 }
91 bool blockSignals = m_ui.keyEdit->blockSignals(true);
92 m_ui.keyEdit->setFocus(Qt::MouseFocusReason);
93 if (m_ui.gamepadButton->isChecked()) {
94 m_ui.keyEdit->setValueButton(-1); // There are no default bindings
95 } else {
96 m_ui.keyEdit->setValueKey(shortcut);
97 }
98 m_ui.keyEdit->blockSignals(blockSignals);
99}
100
101void ShortcutView::clear() {
102 QModelIndex index = m_ui.shortcutTable->selectionModel()->currentIndex();
103 InputItem* item = m_model.itemAt(index);
104 if (!item) {
105 return;
106 }
107 if (m_ui.gamepadButton->isChecked()) {
108 item->clearButton();
109 m_ui.keyEdit->setValueButton(-1);
110 } else {
111 item->clearShortcut();
112 m_ui.keyEdit->setValueKey(-1);
113 }
114}
115
116void ShortcutView::updateButton(int button) {
117 InputItem* item = m_model.itemAt(m_ui.shortcutTable->selectionModel()->currentIndex());
118 if (!item) {
119 return;
120 }
121 if (m_ui.gamepadButton->isChecked()) {
122 item->setButton(button);
123 } else {
124 item->setShortcut(button);
125 }
126}
127
128void ShortcutView::updateAxis(int axis, int direction) {
129 InputItem* item = m_model.itemAt(m_ui.shortcutTable->selectionModel()->currentIndex());
130 if (!item) {
131 return;
132 }
133 item->setAxis(axis, static_cast<GamepadAxisEvent::Direction>(direction));
134}
135
136void ShortcutView::closeEvent(QCloseEvent*) {
137 if (m_input) {
138 m_input->releaseFocus(this);
139 }
140}
141
142bool ShortcutView::event(QEvent* event) {
143 if (m_input) {
144 QEvent::Type type = event->type();
145 if (type == QEvent::WindowActivate || type == QEvent::Show) {
146 m_input->stealFocus(this);
147 } else if (type == QEvent::WindowDeactivate || type == QEvent::Hide) {
148 m_input->releaseFocus(this);
149 }
150 }
151 return QWidget::event(event);
152}