all repos — mgba @ 851d942cdd8ceebfe08f9a1be8940d4c3a62e689

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