all repos — mgba @ 08a2b24eac387539179b81f4c3f44469bbeab9e1

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 "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	m_lastKey.setSingleShot(true);
 26}
 27
 28void KeyEditor::setValue(int key) {
 29	m_key = key;
 30	if (m_button) {
 31		updateButtonText();
 32	} else {
 33		if (key < 0) {
 34			setText(tr("---"));
 35		} else {
 36			setText(QKeySequence(key).toString(QKeySequence::NativeText));
 37		}
 38	}
 39	emit valueChanged(key);
 40}
 41
 42void KeyEditor::setValueKey(int key) {
 43	m_button = false;
 44	setValue(key);
 45}
 46
 47void KeyEditor::setValueButton(int button) {
 48	m_button = true;
 49	setValue(button);
 50}
 51
 52void KeyEditor::setValueAxis(int axis, int32_t value) {
 53	m_button = true;
 54	m_axis = axis;
 55	m_direction = value < 0 ? GamepadAxisEvent::NEGATIVE : GamepadAxisEvent::POSITIVE;
 56	updateButtonText();
 57	emit axisChanged(axis, m_direction);
 58}
 59
 60void KeyEditor::clearButton() {
 61	m_button = true;
 62	setValue(-1);
 63}
 64
 65void KeyEditor::clearAxis() {
 66	m_button = true;
 67	m_axis = -1;
 68	m_direction = GamepadAxisEvent::NEUTRAL;
 69	updateButtonText();
 70	emit axisChanged(m_axis, m_direction);
 71}
 72
 73QSize KeyEditor::sizeHint() const {
 74	QSize hint = QLineEdit::sizeHint();
 75	hint.setWidth(40);
 76	return hint;
 77}
 78
 79void KeyEditor::keyPressEvent(QKeyEvent* event) {
 80	if (!m_button) {
 81		if (m_key < 0 || !m_lastKey.isActive()) {
 82			m_key = 0;
 83		}
 84		m_lastKey.start(KEY_TIME);
 85		if (m_key) {
 86			if (ShortcutController::isModifierKey(m_key)) {
 87				switch (event->key()) {
 88				case Qt::Key_Shift:
 89					setValue(Qt::ShiftModifier);
 90					break;
 91				case Qt::Key_Control:
 92					setValue(Qt::ControlModifier);
 93					break;
 94				case Qt::Key_Alt:
 95					setValue(Qt::AltModifier);
 96					break;
 97				case Qt::Key_Meta:
 98					setValue(Qt::MetaModifier);
 99					break;
100				}
101			}
102			if (ShortcutController::isModifierKey(event->key())) {
103				switch (event->key()) {
104				case Qt::Key_Shift:
105					setValue(m_key | Qt::ShiftModifier);
106					break;
107				case Qt::Key_Control:
108					setValue(m_key | Qt::ControlModifier);
109					break;
110				case Qt::Key_Alt:
111					setValue(m_key | Qt::AltModifier);
112					break;
113				case Qt::Key_Meta:
114					setValue(m_key | Qt::MetaModifier);
115					break;
116				}
117			} else {
118				setValue(event->key() | (m_key & (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier)));
119			}
120		} else {
121			setValue(event->key());
122		}
123	}
124	event->accept();
125}
126
127bool KeyEditor::event(QEvent* event) {
128	if (!m_button) {
129		if (event->type() == QEvent::KeyPress) {
130			QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
131			if (keyEvent->key() != Qt::Key_Tab && keyEvent->key() != Qt::Key_Backtab) {
132				return QWidget::event(event);
133			}
134			if (!(keyEvent->modifiers() & ~Qt::ShiftModifier)) {
135				keyPressEvent(keyEvent);
136				keyEvent->accept();
137				return true;
138			}
139		}
140	} else {
141		if (event->type() == GamepadButtonEvent::Down()) {
142			setValueButton(static_cast<GamepadButtonEvent*>(event)->value());
143			event->accept();
144			return true;
145		}
146		if (event->type() == GamepadAxisEvent::Type()) {
147			GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event);
148			if (gae->isNew()) {
149				setValueAxis(gae->axis(), gae->direction());
150			}
151			event->accept();
152			return true;
153		}
154	}
155	return QWidget::event(event);
156}
157
158void KeyEditor::updateButtonText() {
159	QStringList text;
160	if (m_key >= 0) {
161		text.append(QString::number(m_key));
162	}
163	if (m_direction != GamepadAxisEvent::NEUTRAL) {
164		text.append((m_direction == GamepadAxisEvent::NEGATIVE ? "-" : "+") + QString::number(m_axis));
165	}
166	if (text.isEmpty()) {
167		setText(tr("---"));
168	} else {
169		setText(text.join("/"));
170	}
171}