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 <QFontMetrics>
14#include <QKeyEvent>
15
16using namespace QGBA;
17
18ShortcutView::ShortcutView(QWidget* parent)
19 : QWidget(parent)
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, &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}
39
40ShortcutView::~ShortcutView() {
41 m_input->releaseFocus(this);
42}
43
44void ShortcutView::setController(ShortcutController* controller) {
45 m_controller = controller;
46 m_model = new ShortcutModel(this);
47 m_model->setController(controller);
48 m_ui.shortcutTable->setModel(m_model);
49}
50
51void ShortcutView::setInputController(InputController* controller) {
52 if (m_input) {
53 m_input->releaseFocus(this);
54 }
55 m_input = controller;
56 m_input->stealFocus(this);
57}
58
59void ShortcutView::load(const QModelIndex& index) {
60 if (!m_controller) {
61 return;
62 }
63 QString name = m_model->name(index);
64 const Shortcut* item = m_controller->shortcut(name);
65 if (!item || !item->action()) {
66 return;
67 }
68 int shortcut = item->shortcut();
69 if (index.column() == 1) {
70 m_ui.keyboardButton->click();
71 } else if (index.column() == 2) {
72 m_ui.gamepadButton->click();
73 }
74 bool blockSignals = m_ui.keyEdit->blockSignals(true);
75 m_ui.keyEdit->setFocus(Qt::MouseFocusReason);
76 if (m_ui.gamepadButton->isChecked()) {
77 m_ui.keyEdit->setValueButton(-1); // There are no default bindings
78 } else {
79 m_ui.keyEdit->setValueKey(shortcut);
80 }
81 m_ui.keyEdit->blockSignals(blockSignals);
82}
83
84void ShortcutView::clear() {
85 if (!m_controller) {
86 return;
87 }
88 QModelIndex index = m_ui.shortcutTable->selectionModel()->currentIndex();
89 QString name = m_model->name(index);
90 const Shortcut* item = m_controller->shortcut(name);
91 if (!item || !item->action()) {
92 return;
93 }
94 if (m_ui.gamepadButton->isChecked()) {
95 m_controller->clearButton(name);
96 m_controller->clearAxis(name);
97 m_ui.keyEdit->setValueButton(-1);
98 } else {
99 m_controller->clearKey(name);
100 m_ui.keyEdit->setValueKey(-1);
101 }
102}
103
104void ShortcutView::updateButton(int button) {
105 if (!m_controller) {
106 return;
107 }
108 QString name = m_model->name(m_ui.shortcutTable->selectionModel()->currentIndex());
109 const Shortcut* item = m_controller->shortcut(name);
110 if (!item || !item->action()) {
111 return;
112 }
113 if (m_ui.gamepadButton->isChecked()) {
114 m_controller->updateButton(name, button);
115 } else {
116 m_controller->updateKey(name, button);
117 }
118}
119
120void ShortcutView::updateAxis(int axis, int direction) {
121 if (!m_controller) {
122 return;
123 }
124 QString name = m_model->name(m_ui.shortcutTable->selectionModel()->currentIndex());
125 const Shortcut* item = m_controller->shortcut(name);
126 if (!item || !item->action()) {
127 return;
128 }
129 m_controller->updateAxis(name, axis, static_cast<GamepadAxisEvent::Direction>(direction));
130}
131
132void ShortcutView::closeEvent(QCloseEvent*) {
133 if (m_input) {
134 m_input->releaseFocus(this);
135 }
136}
137
138void ShortcutView::showEvent(QShowEvent*) {
139 QString longString("Ctrl+Alt+Shift+Tab");
140 int width = QFontMetrics(QFont()).width(longString);
141 QHeaderView* header = m_ui.shortcutTable->header();
142 header->resizeSection(0, header->length() - width * 2);
143 header->resizeSection(1, width);
144 header->resizeSection(2, width);
145}
146
147bool ShortcutView::event(QEvent* event) {
148 if (m_input) {
149 QEvent::Type type = event->type();
150 if (type == QEvent::WindowActivate || type == QEvent::Show) {
151 m_input->stealFocus(this);
152 } else if (type == QEvent::WindowDeactivate || type == QEvent::Hide) {
153 m_input->releaseFocus(this);
154 }
155 }
156 return QWidget::event(event);
157}