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

View File

@ -3,30 +3,18 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#pragma once #pragma once
#include <QDialog> #include <QDialog>
#include <map>
#include "core/settings.h" #include "core/settings.h"
#include "ui_inputs.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 /// The input configuration dialog
class GInputsDialog : public QDialog { class GInputsDialog : public QDialog {
Q_OBJECT Q_OBJECT
public: public:
GInputsDialog(QWidget* parent = NULL); GInputsDialog(QWidget* parent = nullptr);
/// 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;
/// Temporary settings used when configuration is changed but not saved yet /// Temporary settings used when configuration is changed but not saved yet
static Settings::Values temp_settings; static Settings::Values temp_settings;
@ -38,9 +26,9 @@ private:
Ui::inputs ui; Ui::inputs ui;
/// Display the button settings on the LineEdits from the given values /// Display the button settings on the LineEdits from the given values
void displayButtonSettings(Settings::Values values); void displayButtonSettings(const Settings::Values& values);
private slots: private slots:
void OnResetClicked(); void OnDefaultClicked();
void OnOkClicked(); void OnOkClicked();
}; };

View File

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

View File

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