all repos — mgba @ 811ad23e6175142fabaaee031ad3e6d58e2bb910

mGBA Game Boy Advance Emulator

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	setValue(button);
39}
40
41void KeyEditor::setValueAxis(int axis, int32_t value) {
42	m_button = true;
43	m_key = axis;
44	m_direction = value < 0 ? InputController::NEGATIVE : InputController::POSITIVE;
45	setText((value < 0 ? "-" : "+") + QString::number(axis));
46	emit axisChanged(axis, m_direction);
47}
48
49QSize KeyEditor::sizeHint() const {
50	QSize hint = QLineEdit::sizeHint();
51	hint.setWidth(40);
52	return hint;
53}
54
55void KeyEditor::keyPressEvent(QKeyEvent* event) {
56	if (!m_button) {
57		setValue(event->key());
58	}
59	event->accept();
60}
61
62bool KeyEditor::event(QEvent* event) {
63	if (!m_button) {
64		return QWidget::event(event);
65	}
66	if (event->type() == GamepadButtonEvent::Down()) {
67		setValueButton(static_cast<GamepadButtonEvent*>(event)->value());
68		event->accept();
69		return true;
70	}
71	return QWidget::event(event);
72}