Settings: Use a custom LineEdit directly instead of using an event filter, and address other comments.

This commit is contained in:
Chin 2015-02-06 22:33:30 -05:00 committed by chinhodado
parent 2e5677e22a
commit 5653916c53
9 changed files with 179 additions and 212 deletions

View file

@ -21,7 +21,7 @@ set(SRCS
bootmanager.cpp
hotkeys.cpp
inputs.cpp
KeyPressEater.cpp
QLineEditKeyConfig.cpp
main.cpp
citra-qt.rc
)
@ -47,7 +47,7 @@ set(HEADERS
bootmanager.h
hotkeys.h
inputs.h
KeyPressEater.h
QLineEditKeyConfig.h
main.h
version.h
)

View file

@ -1,80 +0,0 @@
#include <KeyPressEater.h>
#include <inputs.h>
#include <QKeyEvent>
#include <QLineEdit>
KeyPressEater::KeyPressEater(QObject* parent) : QObject(parent) {}
bool KeyPressEater::eventFilter(QObject* obj, QEvent* event) {
if (event->type() == QEvent::KeyPress) {
QKeyEvent* key_event = static_cast<QKeyEvent*>(event);
QLineEdit* line_edit = static_cast<QLineEdit*>(obj);
Button button_modified = GInputsDialog::edit_input_map[line_edit->objectName()];
QString key_name = GInputsDialog::getKeyName(key_event->key());
line_edit->setText(key_name);
int key = key_event->key();
switch (button_modified) {
case Button::A:
GInputsDialog::temp_settings.pad_a_key = key;
break;
case Button::B:
GInputsDialog::temp_settings.pad_b_key = key;
break;
case Button::X:
GInputsDialog::temp_settings.pad_x_key = key;
break;
case Button::Y:
GInputsDialog::temp_settings.pad_y_key = key;
break;
case Button::L:
GInputsDialog::temp_settings.pad_l_key = key;
break;
case Button::R:
GInputsDialog::temp_settings.pad_r_key = key;
break;
case Button::Start:
GInputsDialog::temp_settings.pad_start_key = key;
break;
case Button::Select:
GInputsDialog::temp_settings.pad_select_key = key;
break;
case Button::Home:
GInputsDialog::temp_settings.pad_home_key = key;
break;
case Button::DUp:
GInputsDialog::temp_settings.pad_dup_key = key;
break;
case Button::DDown:
GInputsDialog::temp_settings.pad_ddown_key = key;
break;
case Button::DLeft:
GInputsDialog::temp_settings.pad_dleft_key = key;
break;
case Button::DRight:
GInputsDialog::temp_settings.pad_dright_key = key;
break;
case Button::SUp:
GInputsDialog::temp_settings.pad_sup_key = key;
break;
case Button::SDown:
GInputsDialog::temp_settings.pad_sdown_key = key;
break;
case Button::SLeft:
GInputsDialog::temp_settings.pad_sleft_key = key;
break;
case Button::SRight:
GInputsDialog::temp_settings.pad_sright_key = key;
break;
default:
break;
}
return true;
}
else {
// standard event processing
return QObject::eventFilter(obj, event);
}
}

View file

@ -1,10 +0,0 @@
#include <QObject>
#include <QEvent>
/// The event filter used for LineEdits to handle key presses
class KeyPressEater : public QObject {
Q_OBJECT
public:
KeyPressEater(QObject* parent);
bool eventFilter(QObject *obj, QEvent *event);
};

View file

@ -0,0 +1,72 @@
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <QLineEditKeyConfig.h>
#include <inputs.h>
#include <QKeyEvent>
#include <QLineEdit>
QLineEditKeyConfig::QLineEditKeyConfig(QWidget* parent) : QLineEdit(parent) {}
void QLineEditKeyConfig::keyPressEvent(QKeyEvent* event) {
QString key_name = GInputsDialog::getKeyName(event->key());
this->setText(key_name);
int key = event->key();
switch (this->button) {
case Button::A:
GInputsDialog::temp_settings.pad_a_key = key;
break;
case Button::B:
GInputsDialog::temp_settings.pad_b_key = key;
break;
case Button::X:
GInputsDialog::temp_settings.pad_x_key = key;
break;
case Button::Y:
GInputsDialog::temp_settings.pad_y_key = key;
break;
case Button::L:
GInputsDialog::temp_settings.pad_l_key = key;
break;
case Button::R:
GInputsDialog::temp_settings.pad_r_key = key;
break;
case Button::Start:
GInputsDialog::temp_settings.pad_start_key = key;
break;
case Button::Select:
GInputsDialog::temp_settings.pad_select_key = key;
break;
case Button::Home:
GInputsDialog::temp_settings.pad_home_key = key;
break;
case Button::DUp:
GInputsDialog::temp_settings.pad_dup_key = key;
break;
case Button::DDown:
GInputsDialog::temp_settings.pad_ddown_key = key;
break;
case Button::DLeft:
GInputsDialog::temp_settings.pad_dleft_key = key;
break;
case Button::DRight:
GInputsDialog::temp_settings.pad_dright_key = key;
break;
case Button::SUp:
GInputsDialog::temp_settings.pad_sup_key = key;
break;
case Button::SDown:
GInputsDialog::temp_settings.pad_sdown_key = key;
break;
case Button::SLeft:
GInputsDialog::temp_settings.pad_sleft_key = key;
break;
case Button::SRight:
GInputsDialog::temp_settings.pad_sright_key = key;
break;
default:
break;
}
}

View file

@ -0,0 +1,26 @@
// Copyright 2014 Citra Emulator Project
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#pragma once
#include <QLineEdit>
class QKeyEvent;
/// An enum for the buttons on the 3DS
enum Button {
A, B, X, Y, L, R, Start, Select, Home,
DUp, DDown, DLeft, DRight,
SUp, SDown, SLeft, SRight
};
/// The LineEdits used for button configuration
class QLineEditKeyConfig : public QLineEdit {
Q_OBJECT
public:
Button button;
QLineEditKeyConfig(QWidget* parent);
void keyPressEvent(QKeyEvent* event) override;
};

View file

@ -2,48 +2,43 @@
// Licensed under GPLv2 or any later version
// Refer to the license.txt file included.
#include <QKeySequence>
#include <QSettings>
#include <QtWidgets>
#include "inputs.h"
#include "KeyPressEater.h"
#include "config.h"
#include "common/log.h"
std::map<QString, Button> GInputsDialog::edit_input_map;
bool GInputsDialog::edit_input_map_initialized = false;
#include <QKeySequence>
#include <QSettings>
#include <QPushButton>
#include "config.h"
#include "inputs.h"
#include "QLineEditKeyConfig.h"
Settings::Values GInputsDialog::temp_settings;
GInputsDialog::GInputsDialog(QWidget* parent) : QDialog(parent) {
ui.setupUi(this);
// initialize the edit_input_map
if (!edit_input_map_initialized) {
GInputsDialog::edit_input_map = std::map<QString, Button> {
{ this->ui.lineEdit_A->objectName(), Button::A},
{ this->ui.lineEdit_B->objectName(), Button::B },
{ this->ui.lineEdit_X->objectName(), Button::X },
{ this->ui.lineEdit_Y->objectName(), Button::Y },
{ this->ui.lineEdit_L->objectName(), Button::L },
{ this->ui.lineEdit_R->objectName(), Button::R },
{ this->ui.lineEdit_Start->objectName(), Button::Start },
{ this->ui.lineEdit_Select->objectName(), Button::Select },
{ this->ui.lineEdit_Home->objectName(), Button::Home },
{ this->ui.lineEdit_dUp->objectName(), Button::DUp },
{ this->ui.lineEdit_dDown->objectName(), Button::DDown },
{ this->ui.lineEdit_dLeft->objectName(), Button::DLeft },
{ this->ui.lineEdit_dRight->objectName(), Button::DRight },
{ this->ui.lineEdit_sUp->objectName(), Button::SUp },
{ this->ui.lineEdit_sDown->objectName(), Button::SDown },
{ this->ui.lineEdit_sLeft->objectName(), Button::SLeft },
{ this->ui.lineEdit_sRight->objectName(), Button::SRight },
};
edit_input_map_initialized = true;
}
// setup the LineEdits
this->ui.lineEdit_A->button = Button::A;
this->ui.lineEdit_B->button = Button::B;
this->ui.lineEdit_X->button = Button::X;
this->ui.lineEdit_Y->button = Button::Y;
this->ui.lineEdit_L->button = Button::L;
this->ui.lineEdit_R->button = Button::R;
this->ui.lineEdit_Start->button = Button::Start;
this->ui.lineEdit_Select->button = Button::Select;
this->ui.lineEdit_Home->button = Button::Home;
this->ui.lineEdit_dUp->button = Button::DUp;
this->ui.lineEdit_dDown->button = Button::DDown;
this->ui.lineEdit_dLeft->button = Button::DLeft;
this->ui.lineEdit_dRight->button = Button::DRight;
this->ui.lineEdit_sUp->button = Button::SUp;
this->ui.lineEdit_sDown->button = Button::SDown;
this->ui.lineEdit_sLeft->button = Button::SLeft;
this->ui.lineEdit_sRight->button = Button::SRight;
// set up event handlers for the buttons
QPushButton *resetButton = this->ui.buttonBox->button(QDialogButtonBox::Reset);
connect(resetButton, SIGNAL(clicked()), this, SLOT(OnResetClicked()));
QPushButton* defaultButton = this->ui.buttonBox->button(QDialogButtonBox::RestoreDefaults);
connect(defaultButton, SIGNAL(clicked()), this, SLOT(OnDefaultClicked()));
QPushButton* okButton = this->ui.buttonBox->button(QDialogButtonBox::Ok);
connect(okButton, SIGNAL(clicked()), this, SLOT(OnOkClicked()));
@ -53,29 +48,9 @@ GInputsDialog::GInputsDialog(QWidget* parent) : QDialog(parent) {
// display current key settings
this->displayButtonSettings(Settings::values);
// setup event filters for the LineEdits
KeyPressEater* keyPressEater = new KeyPressEater(this);
this->ui.lineEdit_A->installEventFilter(keyPressEater);
this->ui.lineEdit_B->installEventFilter(keyPressEater);
this->ui.lineEdit_X->installEventFilter(keyPressEater);
this->ui.lineEdit_Y->installEventFilter(keyPressEater);
this->ui.lineEdit_L->installEventFilter(keyPressEater);
this->ui.lineEdit_R->installEventFilter(keyPressEater);
this->ui.lineEdit_Start->installEventFilter(keyPressEater);
this->ui.lineEdit_Select->installEventFilter(keyPressEater);
this->ui.lineEdit_Home->installEventFilter(keyPressEater);
this->ui.lineEdit_dUp->installEventFilter(keyPressEater);
this->ui.lineEdit_dDown->installEventFilter(keyPressEater);
this->ui.lineEdit_dLeft->installEventFilter(keyPressEater);
this->ui.lineEdit_dRight->installEventFilter(keyPressEater);
this->ui.lineEdit_sUp->installEventFilter(keyPressEater);
this->ui.lineEdit_sDown->installEventFilter(keyPressEater);
this->ui.lineEdit_sLeft->installEventFilter(keyPressEater);
this->ui.lineEdit_sRight->installEventFilter(keyPressEater);
}
void GInputsDialog::displayButtonSettings(Settings::Values values) {
void GInputsDialog::displayButtonSettings(const Settings::Values& values) {
this->ui.lineEdit_A->setText(GInputsDialog::getKeyName(values.pad_a_key));
this->ui.lineEdit_B->setText(GInputsDialog::getKeyName(values.pad_b_key));
this->ui.lineEdit_X->setText(GInputsDialog::getKeyName(values.pad_x_key));
@ -96,14 +71,22 @@ void GInputsDialog::displayButtonSettings(Settings::Values values) {
}
QString GInputsDialog::getKeyName(int key_code) {
if (key_code == Qt::Key_Shift) { return tr("Shift"); }
else if (key_code == Qt::Key_Control) { return tr("Ctrl"); }
else if (key_code == Qt::Key_Alt) { return tr("Alt"); }
else if (key_code == Qt::Key_Meta) { return tr("Meta"); }
else return QKeySequence(key_code).toString();
if (key_code == Qt::Key_Shift)
return tr("Shift");
if (key_code == Qt::Key_Control)
return tr("Ctrl");
if (key_code == Qt::Key_Alt)
return tr("Alt");
if (key_code == Qt::Key_Meta)
return tr("Meta");
return QKeySequence(key_code).toString();
}
void GInputsDialog::OnResetClicked() {
void GInputsDialog::OnDefaultClicked() {
// load the default button settings into temp_settings
GInputsDialog::temp_settings.pad_a_key = Qt::Key_A;
GInputsDialog::temp_settings.pad_b_key = Qt::Key_S;

View file

@ -3,30 +3,18 @@
// Refer to the license.txt file included.
#pragma once
#include <QDialog>
#include <map>
#include "core/settings.h"
#include "ui_inputs.h"
/// An enum for the buttons on the 3DS
enum Button {
A, B, X, Y, L, R, Start, Select, Home,
DUp, DDown, DLeft, DRight,
SUp, SDown, SLeft, SRight
};
/// The input configuration dialog
class GInputsDialog : public QDialog {
Q_OBJECT
public:
GInputsDialog(QWidget* parent = NULL);
/// A map between the name of a LineEdit and the button that it modifies
static std::map<QString, Button> edit_input_map;
/// true if edit_input_map is initialized
static bool edit_input_map_initialized;
GInputsDialog(QWidget* parent = nullptr);
/// Temporary settings used when configuration is changed but not saved yet
static Settings::Values temp_settings;
@ -38,9 +26,9 @@ private:
Ui::inputs ui;
/// Display the button settings on the LineEdits from the given values
void displayButtonSettings(Settings::Values values);
void displayButtonSettings(const Settings::Values& values);
private slots:
void OnResetClicked();
void OnDefaultClicked();
void OnOkClicked();
};

View file

@ -11,7 +11,7 @@
</rect>
</property>
<property name="windowTitle">
<string>Input setting</string>
<string>Input Settings</string>
</property>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="geometry">
@ -26,7 +26,7 @@
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::Reset</set>
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::RestoreDefaults</set>
</property>
</widget>
<widget class="QGroupBox" name="groupBox">
@ -39,9 +39,9 @@
</rect>
</property>
<property name="title">
<string>C-Pad</string>
<string>Circle Pad</string>
</property>
<widget class="QLineEdit" name="lineEdit_sRight">
<widget class="QLineEditKeyConfig" name="lineEdit_sRight">
<property name="geometry">
<rect>
<x>160</x>
@ -67,7 +67,7 @@
<string>Left</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_sDown">
<widget class="QLineEditKeyConfig" name="lineEdit_sDown">
<property name="geometry">
<rect>
<x>110</x>
@ -80,7 +80,7 @@
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_sLeft">
<widget class="QLineEditKeyConfig" name="lineEdit_sLeft">
<property name="geometry">
<rect>
<x>40</x>
@ -119,7 +119,7 @@
<string>Right</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_sUp">
<widget class="QLineEditKeyConfig" name="lineEdit_sUp">
<property name="geometry">
<rect>
<x>110</x>
@ -158,7 +158,7 @@
<property name="title">
<string>D-Pad</string>
</property>
<widget class="QLineEdit" name="lineEdit_dDown">
<widget class="QLineEditKeyConfig" name="lineEdit_dDown">
<property name="geometry">
<rect>
<x>110</x>
@ -171,7 +171,7 @@
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_dLeft">
<widget class="QLineEditKeyConfig" name="lineEdit_dLeft">
<property name="geometry">
<rect>
<x>40</x>
@ -197,7 +197,7 @@
<string>Up</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_dRight">
<widget class="QLineEditKeyConfig" name="lineEdit_dRight">
<property name="geometry">
<rect>
<x>160</x>
@ -226,7 +226,7 @@
<string>Down</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_dUp">
<widget class="QLineEditKeyConfig" name="lineEdit_dUp">
<property name="geometry">
<rect>
<x>110</x>
@ -276,9 +276,9 @@
</rect>
</property>
<property name="title">
<string>Main</string>
<string>Main Buttons</string>
</property>
<widget class="QLineEdit" name="lineEdit_B">
<widget class="QLineEditKeyConfig" name="lineEdit_B">
<property name="geometry">
<rect>
<x>90</x>
@ -291,7 +291,7 @@
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_Y">
<widget class="QLineEditKeyConfig" name="lineEdit_Y">
<property name="geometry">
<rect>
<x>20</x>
@ -317,7 +317,7 @@
<string>Y</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_A">
<widget class="QLineEditKeyConfig" name="lineEdit_A">
<property name="geometry">
<rect>
<x>140</x>
@ -369,7 +369,7 @@
<string>A</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_X">
<widget class="QLineEditKeyConfig" name="lineEdit_X">
<property name="geometry">
<rect>
<x>90</x>
@ -382,22 +382,6 @@
<set>Qt::AlignCenter</set>
</property>
</widget>
<zorder>lineEdit_B</zorder>
<zorder>label_Y</zorder>
<zorder>lineEdit_Y</zorder>
<zorder>lineEdit_A</zorder>
<zorder>label_X</zorder>
<zorder>label_B</zorder>
<zorder>label_A</zorder>
<zorder>lineEdit_X</zorder>
<zorder>lineEdit_B</zorder>
<zorder>lineEdit_Y</zorder>
<zorder>label_Y</zorder>
<zorder>lineEdit_A</zorder>
<zorder>label_B</zorder>
<zorder>label_X</zorder>
<zorder>label_A</zorder>
<zorder>lineEdit_X</zorder>
</widget>
<widget class="QGroupBox" name="groupBox_4">
<property name="geometry">
@ -409,9 +393,9 @@
</rect>
</property>
<property name="title">
<string>Others</string>
<string>Other Buttons</string>
</property>
<widget class="QLineEdit" name="lineEdit_Start">
<widget class="QLineEditKeyConfig" name="lineEdit_Start">
<property name="geometry">
<rect>
<x>360</x>
@ -450,7 +434,7 @@
<string>Select</string>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_Select">
<widget class="QLineEditKeyConfig" name="lineEdit_Select">
<property name="geometry">
<rect>
<x>90</x>
@ -463,7 +447,7 @@
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_Home">
<widget class="QLineEditKeyConfig" name="lineEdit_Home">
<property name="geometry">
<rect>
<x>230</x>
@ -500,9 +484,9 @@
</rect>
</property>
<property name="title">
<string>Shoulder</string>
<string>Bumpers</string>
</property>
<widget class="QLineEdit" name="lineEdit_R">
<widget class="QLineEditKeyConfig" name="lineEdit_R">
<property name="geometry">
<rect>
<x>340</x>
@ -515,7 +499,7 @@
<set>Qt::AlignCenter</set>
</property>
</widget>
<widget class="QLineEdit" name="lineEdit_L">
<widget class="QLineEditKeyConfig" name="lineEdit_L">
<property name="geometry">
<rect>
<x>110</x>
@ -556,6 +540,13 @@
</widget>
</widget>
</widget>
<customwidgets>
<customwidget>
<class>QLineEditKeyConfig</class>
<extends>QLineEdit</extends>
<header>QLineEditKeyConfig.h</header>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>

View file

@ -171,9 +171,6 @@
<property name="text">
<string>Configure &amp;Inputs</string>
</property>
<property name="toolTip">
<string>Configure Inputs</string>
</property>
</action>
</widget>
<resources/>