2014-12-03 08:39:06 +00:00
|
|
|
/* 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"
|
|
|
|
|
2015-01-08 08:24:43 +00:00
|
|
|
#include "GamepadAxisEvent.h"
|
2015-01-04 12:23:20 +00:00
|
|
|
#include "GamepadButtonEvent.h"
|
2015-08-04 03:02:50 +00:00
|
|
|
#include "ShortcutController.h"
|
2015-01-04 12:23:20 +00:00
|
|
|
|
2016-01-31 22:56:50 +00:00
|
|
|
#include <QFontMetrics>
|
2014-11-07 11:11:44 +00:00
|
|
|
#include <QKeyEvent>
|
|
|
|
|
|
|
|
using namespace QGBA;
|
|
|
|
|
|
|
|
KeyEditor::KeyEditor(QWidget* parent)
|
|
|
|
: QLineEdit(parent)
|
2015-01-08 08:24:43 +00:00
|
|
|
, m_direction(GamepadAxisEvent::NEUTRAL)
|
2015-07-24 07:01:10 +00:00
|
|
|
, m_key(-1)
|
|
|
|
, m_axis(-1)
|
2015-04-23 03:18:54 +00:00
|
|
|
, m_button(false)
|
2014-11-07 11:11:44 +00:00
|
|
|
{
|
|
|
|
setAlignment(Qt::AlignCenter);
|
2015-08-04 03:02:50 +00:00
|
|
|
setFocusPolicy(Qt::ClickFocus);
|
2015-12-08 04:41:28 +00:00
|
|
|
m_lastKey.setSingleShot(true);
|
2014-11-07 11:11:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void KeyEditor::setValue(int key) {
|
2015-07-24 07:01:10 +00:00
|
|
|
m_key = key;
|
2014-12-15 05:11:22 +00:00
|
|
|
if (m_button) {
|
2015-07-24 07:01:10 +00:00
|
|
|
updateButtonText();
|
2014-12-03 08:07:56 +00:00
|
|
|
} else {
|
2015-08-04 03:02:50 +00:00
|
|
|
if (key < 0) {
|
|
|
|
setText(tr("---"));
|
|
|
|
} else {
|
|
|
|
setText(QKeySequence(key).toString(QKeySequence::NativeText));
|
|
|
|
}
|
2014-12-03 08:07:56 +00:00
|
|
|
}
|
2014-11-07 11:11:44 +00:00
|
|
|
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;
|
|
|
|
setValue(button);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyEditor::setValueAxis(int axis, int32_t value) {
|
|
|
|
m_button = true;
|
2015-07-24 07:01:10 +00:00
|
|
|
m_axis = axis;
|
2015-01-08 08:24:43 +00:00
|
|
|
m_direction = value < 0 ? GamepadAxisEvent::NEGATIVE : GamepadAxisEvent::POSITIVE;
|
2015-07-24 07:01:10 +00:00
|
|
|
updateButtonText();
|
2014-12-15 05:11:22 +00:00
|
|
|
emit axisChanged(axis, m_direction);
|
|
|
|
}
|
|
|
|
|
2015-07-26 04:55:28 +00:00
|
|
|
void KeyEditor::clearButton() {
|
|
|
|
m_button = true;
|
|
|
|
setValue(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyEditor::clearAxis() {
|
|
|
|
m_button = true;
|
|
|
|
m_axis = -1;
|
|
|
|
m_direction = GamepadAxisEvent::NEUTRAL;
|
|
|
|
updateButtonText();
|
|
|
|
emit axisChanged(m_axis, m_direction);
|
|
|
|
}
|
|
|
|
|
2014-11-07 11:11:44 +00:00
|
|
|
QSize KeyEditor::sizeHint() const {
|
|
|
|
QSize hint = QLineEdit::sizeHint();
|
2016-01-31 22:56:50 +00:00
|
|
|
QFontMetrics fm(font());
|
|
|
|
hint.setWidth(fm.height() * 3);
|
2014-11-07 11:11:44 +00:00
|
|
|
return hint;
|
|
|
|
}
|
|
|
|
|
|
|
|
void KeyEditor::keyPressEvent(QKeyEvent* event) {
|
2014-12-15 05:11:22 +00:00
|
|
|
if (!m_button) {
|
2015-12-08 04:41:28 +00:00
|
|
|
if (m_key < 0 || !m_lastKey.isActive()) {
|
2015-08-04 03:02:50 +00:00
|
|
|
m_key = 0;
|
|
|
|
}
|
2015-12-08 04:41:28 +00:00
|
|
|
m_lastKey.start(KEY_TIME);
|
|
|
|
if (m_key) {
|
|
|
|
if (ShortcutController::isModifierKey(m_key)) {
|
|
|
|
switch (event->key()) {
|
|
|
|
case Qt::Key_Shift:
|
|
|
|
setValue(Qt::ShiftModifier);
|
|
|
|
break;
|
|
|
|
case Qt::Key_Control:
|
|
|
|
setValue(Qt::ControlModifier);
|
|
|
|
break;
|
|
|
|
case Qt::Key_Alt:
|
|
|
|
setValue(Qt::AltModifier);
|
|
|
|
break;
|
|
|
|
case Qt::Key_Meta:
|
|
|
|
setValue(Qt::MetaModifier);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (ShortcutController::isModifierKey(event->key())) {
|
|
|
|
switch (event->key()) {
|
|
|
|
case Qt::Key_Shift:
|
|
|
|
setValue(m_key | Qt::ShiftModifier);
|
|
|
|
break;
|
|
|
|
case Qt::Key_Control:
|
|
|
|
setValue(m_key | Qt::ControlModifier);
|
|
|
|
break;
|
|
|
|
case Qt::Key_Alt:
|
|
|
|
setValue(m_key | Qt::AltModifier);
|
|
|
|
break;
|
|
|
|
case Qt::Key_Meta:
|
|
|
|
setValue(m_key | Qt::MetaModifier);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setValue(event->key() | (m_key & (Qt::ShiftModifier | Qt::ControlModifier | Qt::AltModifier | Qt::MetaModifier)));
|
2015-08-04 03:02:50 +00:00
|
|
|
}
|
|
|
|
} else {
|
2015-12-08 04:41:28 +00:00
|
|
|
setValue(event->key());
|
2015-08-04 03:02:50 +00:00
|
|
|
}
|
2014-12-03 08:07:56 +00:00
|
|
|
}
|
2014-11-07 11:11:44 +00:00
|
|
|
event->accept();
|
|
|
|
}
|
2015-01-04 12:23:20 +00:00
|
|
|
|
|
|
|
bool KeyEditor::event(QEvent* event) {
|
|
|
|
if (!m_button) {
|
2015-08-04 03:02:50 +00:00
|
|
|
if (event->type() == QEvent::KeyPress) {
|
|
|
|
QKeyEvent* keyEvent = static_cast<QKeyEvent*>(event);
|
|
|
|
if (keyEvent->key() != Qt::Key_Tab && keyEvent->key() != Qt::Key_Backtab) {
|
|
|
|
return QWidget::event(event);
|
|
|
|
}
|
|
|
|
if (!(keyEvent->modifiers() & ~Qt::ShiftModifier)) {
|
|
|
|
keyPressEvent(keyEvent);
|
|
|
|
keyEvent->accept();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
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;
|
2015-01-08 08:24:43 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-04 12:23:20 +00:00
|
|
|
return QWidget::event(event);
|
|
|
|
}
|
2015-07-24 07:01:10 +00:00
|
|
|
|
|
|
|
void KeyEditor::updateButtonText() {
|
|
|
|
QStringList text;
|
|
|
|
if (m_key >= 0) {
|
|
|
|
text.append(QString::number(m_key));
|
|
|
|
}
|
|
|
|
if (m_direction != GamepadAxisEvent::NEUTRAL) {
|
|
|
|
text.append((m_direction == GamepadAxisEvent::NEGATIVE ? "-" : "+") + QString::number(m_axis));
|
|
|
|
}
|
|
|
|
if (text.isEmpty()) {
|
|
|
|
setText(tr("---"));
|
|
|
|
} else {
|
|
|
|
setText(text.join("/"));
|
|
|
|
}
|
|
|
|
}
|