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 "GamepadButtonEvent.h"
9
10#include <QKeyEvent>
11
12using namespace QGBA;
13
14KeyEditor::KeyEditor(QWidget* parent)
15 : QLineEdit(parent)
16 , m_direction(InputController::NEUTRAL)
17{
18 setAlignment(Qt::AlignCenter);
19}
20
21void KeyEditor::setValue(int key) {
22 if (m_button) {
23 setText(QString::number(key));
24 } else {
25 setText(QKeySequence(key).toString(QKeySequence::NativeText));
26 }
27 m_key = key;
28 emit valueChanged(key);
29}
30
31void KeyEditor::setValueKey(int key) {
32 m_button = false;
33 setValue(key);
34}
35
36void KeyEditor::setValueButton(int button) {
37 m_button = true;
38 m_direction = InputController::NEUTRAL;
39 setValue(button);
40}
41
42void KeyEditor::setValueAxis(int axis, int32_t value) {
43 m_button = true;
44 m_key = axis;
45 m_direction = value < 0 ? InputController::NEGATIVE : InputController::POSITIVE;
46 setText((value < 0 ? "-" : "+") + QString::number(axis));
47 emit axisChanged(axis, m_direction);
48}
49
50QSize KeyEditor::sizeHint() const {
51 QSize hint = QLineEdit::sizeHint();
52 hint.setWidth(40);
53 return hint;
54}
55
56void KeyEditor::keyPressEvent(QKeyEvent* event) {
57 if (!m_button) {
58 setValue(event->key());
59 }
60 event->accept();
61}
62
63bool KeyEditor::event(QEvent* event) {
64 if (!m_button) {
65 return QWidget::event(event);
66 }
67 if (event->type() == GamepadButtonEvent::Down()) {
68 setValueButton(static_cast<GamepadButtonEvent*>(event)->value());
69 event->accept();
70 return true;
71 }
72 return QWidget::event(event);
73}