mgba/src/platform/qt/KeyEditor.cpp

87 lines
1.9 KiB
C++
Raw Normal View History

/* Copyright (c) 2013-2014 Jeffrey Pfau
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
2014-11-07 11:11:44 +00:00
#include "KeyEditor.h"
#include "GamepadAxisEvent.h"
#include "GamepadButtonEvent.h"
2014-11-07 11:11:44 +00:00
#include <QKeyEvent>
using namespace QGBA;
KeyEditor::KeyEditor(QWidget* parent)
: QLineEdit(parent)
, m_direction(GamepadAxisEvent::NEUTRAL)
2014-11-07 11:11:44 +00:00
{
setAlignment(Qt::AlignCenter);
}
void KeyEditor::setValue(int key) {
2014-12-15 05:11:22 +00:00
if (m_button) {
if (key < 0) {
clear();
} else {
setText(QString::number(key));
}
2014-12-03 08:07:56 +00:00
} else {
setText(QKeySequence(key).toString(QKeySequence::NativeText));
}
2014-11-07 11:11:44 +00:00
m_key = key;
emit valueChanged(key);
}
2014-12-15 05:11:22 +00:00
void KeyEditor::setValueKey(int key) {
m_button = false;
setValue(key);
}
void KeyEditor::setValueButton(int button) {
m_button = true;
m_direction = GamepadAxisEvent::NEUTRAL;
2014-12-15 05:11:22 +00:00
setValue(button);
}
void KeyEditor::setValueAxis(int axis, int32_t value) {
m_button = true;
m_key = axis;
m_direction = value < 0 ? GamepadAxisEvent::NEGATIVE : GamepadAxisEvent::POSITIVE;
2014-12-15 05:11:22 +00:00
setText((value < 0 ? "-" : "+") + QString::number(axis));
emit axisChanged(axis, m_direction);
}
2014-11-07 11:11:44 +00:00
QSize KeyEditor::sizeHint() const {
QSize hint = QLineEdit::sizeHint();
hint.setWidth(40);
return hint;
}
void KeyEditor::keyPressEvent(QKeyEvent* event) {
2014-12-15 05:11:22 +00:00
if (!m_button) {
2014-12-03 08:07:56 +00:00
setValue(event->key());
}
2014-11-07 11:11:44 +00:00
event->accept();
}
bool KeyEditor::event(QEvent* event) {
if (!m_button) {
return QWidget::event(event);
}
if (event->type() == GamepadButtonEvent::Down()) {
setValueButton(static_cast<GamepadButtonEvent*>(event)->value());
event->accept();
return true;
}
if (event->type() == GamepadAxisEvent::Type()) {
GamepadAxisEvent* gae = static_cast<GamepadAxisEvent*>(event);
if (gae->isNew()) {
setValueAxis(gae->axis(), gae->direction());
}
event->accept();
return true;
}
return QWidget::event(event);
}