all repos — mgba @ 38a4e9988f8c6c210b2fd70790ff1f0f7006515b

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