src/platform/qt/KeyEditor.cpp (view raw)
1/* Copyright (c) 2013-2014 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 "KeyEditor.h"
7
8#include "GamepadAxisEvent.h"
9#include "GamepadButtonEvent.h"
10#include "ShortcutController.h"
11
12#include <QKeyEvent>
13
14using namespace QGBA;
15
16KeyEditor::KeyEditor(QWidget* parent)
17 : QLineEdit(parent)
18 , m_direction(GamepadAxisEvent::NEUTRAL)
19 , m_key(-1)
20 , m_axis(-1)
21 , m_button(false)
22{
23 setAlignment(Qt::AlignCenter);
24 setFocusPolicy(Qt::ClickFocus);
25}
26
27void KeyEditor::setValue(int key) {
28 m_key = key;
29 if (m_button) {
30 updateButtonText();
31 } else {
32 if (key < 0) {
33 setText(tr("---"));
34 } else {
35 setText(QKeySequence(key).toString(QKeySequence::NativeText));
36 }
37 }
38 emit valueChanged(key);
39}
40
41void KeyEditor::setValueKey(int key) {
42 m_button = false;
43 setValue(key);
44}
45
46void KeyEditor::setValueButton(int button) {
47 m_button = true;
48 setValue(button);
49}
50
51void KeyEditor::setValueAxis(int axis, int32_t value) {
52 m_button = true;
53 m_axis = axis;
54 m_direction = value < 0 ? GamepadAxisEvent::NEGATIVE : GamepadAxisEvent::POSITIVE;
55 updateButtonText();
56 emit axisChanged(axis, m_direction);
57}
58
59void KeyEditor::clearButton() {
60 m_button = true;
61 setValue(-1);
62}
63
64void KeyEditor::clearAxis() {
65 m_button = true;
66 m_axis = -1;
67 m_direction = GamepadAxisEvent::NEUTRAL;
68 updateButtonText();
69 emit axisChanged(m_axis, m_direction);
70}
71
72QSize KeyEditor::sizeHint() const {
73 QSize hint = QLineEdit::sizeHint();
74 hint.setWidth(40);
75 return hint;
76}
77
78void KeyEditor::keyPressEvent(QKeyEvent* event) {
79 if (!m_button) {
80 if (m_key < 0) {
81 m_key = 0;
82 }
83 if (ShortcutController::isModifierKey(event->key())) {
84 switch (event->key()) {
85 case Qt::Key_Shift:
86 setValue(m_key | Qt::ShiftModifier);
87 break;
88 case Qt::Key_Control:
89 setValue(m_key | Qt::ControlModifier);
90 break;
91 case Qt::Key_Alt:
92 setValue(m_key | Qt::AltModifier);
93 break;
94 case Qt::Key_Meta:
95 setValue(m_key | Qt::MetaModifier);
96 break;
97 default:
98 setValue(m_key);
99 }
100 } else {
101 setValue(event->key() | (m_key & (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier)));
102 }
103 }
104 event->accept();
105}
106
107bool KeyEditor::event(QEvent* event) {
108 if (!m_button) {
109 if (event->type() == QEvent::KeyPress) {
110 QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
111 if (keyEvent->key() != Qt::Key_Tab && keyEvent->key() != Qt::Key_Backtab) {
112 return QWidget::event(event);
113 }
114 if (!(keyEvent->modifiers() & ~Qt::ShiftModifier)) {
115 keyPressEvent(keyEvent);
116 keyEvent->accept();
117 return true;
118 }
119 }
120 } else {
121 if (event->type() == GamepadButtonEvent::Down()) {
122 setValueButton(static_cast<GamepadButtonEvent*>(event)->value());
123 event->accept();
124 return true;
125 }
126 if (event->type() == GamepadAxisEvent::Type()) {
127 GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event);
128 if (gae->isNew()) {
129 setValueAxis(gae->axis(), gae->direction());
130 }
131 event->accept();
132 return true;
133 }
134 }
135 return QWidget::event(event);
136}
137
138void KeyEditor::updateButtonText() {
139 QStringList text;
140 if (m_key >= 0) {
141 text.append(QString::number(m_key));
142 }
143 if (m_direction != GamepadAxisEvent::NEUTRAL) {
144 text.append((m_direction == GamepadAxisEvent::NEGATIVE ? "-" : "+") + QString::number(m_axis));
145 }
146 if (text.isEmpty()) {
147 setText(tr("---"));
148 } else {
149 setText(text.join("/"));
150 }
151}