citra-emu
/
citra
Archived
1
0
Fork 0

citra_qt: Implement UI for adding/editing/deleting cheats

The UI file is rewritten, to better make use of Qt's layouts (instead of depending on abstract geometry). "Add Cheat", "Save", "Delete" buttons are also added.

The UI logic should be rather easy and usable (IMO), but the code may seem a bit dirty. If anyone has a better idea regarding UI logic design or code implementation, feel free to tell me about it.
This commit is contained in:
zhupengfei 2019-01-30 23:06:01 +08:00
parent 573036b38e
commit 433176a9b9
No known key found for this signature in database
GPG Key ID: DD129E108BD09378
3 changed files with 395 additions and 180 deletions

View File

@ -3,10 +3,12 @@
// Refer to the license.txt file included. // Refer to the license.txt file included.
#include <QCheckBox> #include <QCheckBox>
#include <QMessageBox>
#include <QTableWidgetItem> #include <QTableWidgetItem>
#include "citra_qt/cheats.h" #include "citra_qt/cheats.h"
#include "core/cheats/cheat_base.h" #include "core/cheats/cheat_base.h"
#include "core/cheats/cheats.h" #include "core/cheats/cheats.h"
#include "core/cheats/gateway_cheat.h"
#include "core/core.h" #include "core/core.h"
#include "core/hle/kernel/process.h" #include "core/hle/kernel/process.h"
#include "ui_cheats.h" #include "ui_cheats.h"
@ -21,14 +23,23 @@ CheatDialog::CheatDialog(QWidget* parent)
ui->tableCheats->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed); ui->tableCheats->horizontalHeader()->setSectionResizeMode(0, QHeaderView::Fixed);
ui->tableCheats->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch); ui->tableCheats->horizontalHeader()->setSectionResizeMode(1, QHeaderView::Stretch);
ui->tableCheats->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed); ui->tableCheats->horizontalHeader()->setSectionResizeMode(2, QHeaderView::Fixed);
ui->textDetails->setEnabled(false); ui->lineName->setEnabled(false);
ui->textCode->setEnabled(false);
ui->textNotes->setEnabled(false); ui->textNotes->setEnabled(false);
const auto game_id = fmt::format( const auto game_id = fmt::format(
"{:016X}", Core::System::GetInstance().Kernel().GetCurrentProcess()->codeset->program_id); "{:016X}", Core::System::GetInstance().Kernel().GetCurrentProcess()->codeset->program_id);
ui->labelTitle->setText(tr("Title ID: %1").arg(QString::fromStdString(game_id))); ui->labelTitle->setText(tr("Title ID: %1").arg(QString::fromStdString(game_id)));
connect(ui->buttonClose, &QPushButton::released, this, &CheatDialog::OnCancel); connect(ui->buttonClose, &QPushButton::released, this, &CheatDialog::OnCancel);
connect(ui->buttonAddCheat, &QPushButton::released, this, &CheatDialog::OnAddCheat);
connect(ui->tableCheats, &QTableWidget::cellClicked, this, &CheatDialog::OnRowSelected); connect(ui->tableCheats, &QTableWidget::cellClicked, this, &CheatDialog::OnRowSelected);
connect(ui->lineName, &QLineEdit::textEdited, this, &CheatDialog::OnTextEdited);
connect(ui->textNotes, &QPlainTextEdit::textChanged, this, &CheatDialog::OnTextEdited);
connect(ui->textCode, &QPlainTextEdit::textChanged, this, &CheatDialog::OnTextEdited);
connect(ui->buttonSave, &QPushButton::released,
[this] { SaveCheat(ui->tableCheats->currentRow()); });
connect(ui->buttonDelete, &QPushButton::released, this, &CheatDialog::OnDeleteCheat);
LoadCheats(); LoadCheats();
} }
@ -36,7 +47,7 @@ CheatDialog::CheatDialog(QWidget* parent)
CheatDialog::~CheatDialog() = default; CheatDialog::~CheatDialog() = default;
void CheatDialog::LoadCheats() { void CheatDialog::LoadCheats() {
const auto& cheats = Core::System::GetInstance().CheatEngine().GetCheats(); cheats = Core::System::GetInstance().CheatEngine().GetCheats();
ui->tableCheats->setRowCount(cheats.size()); ui->tableCheats->setRowCount(cheats.size());
@ -56,20 +67,184 @@ void CheatDialog::LoadCheats() {
} }
} }
bool CheatDialog::CheckSaveCheat() {
auto answer = QMessageBox::warning(
this, tr("Cheats"), tr("Would you like to save the current cheat?"),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel, QMessageBox::Cancel);
if (answer == QMessageBox::Yes) {
return SaveCheat(last_row);
} else {
return answer != QMessageBox::Cancel;
}
}
bool CheatDialog::SaveCheat(int row) {
if (ui->lineName->text().isEmpty()) {
QMessageBox::critical(this, tr("Save Cheat"), tr("Please enter a cheat name."));
return false;
}
if (ui->textCode->toPlainText().isEmpty()) {
QMessageBox::critical(this, tr("Save Cheat"), tr("Please enter the cheat code."));
return false;
}
// Check if the cheat lines are valid
auto code_lines = ui->textCode->toPlainText().split("\n", QString::SkipEmptyParts);
for (int i = 0; i < code_lines.size(); ++i) {
Cheats::GatewayCheat::CheatLine cheat_line(code_lines[i].toStdString());
if (cheat_line.valid)
continue;
auto answer = QMessageBox::warning(
this, tr("Save Cheat"),
tr("Cheat code line %1 is not valid.\nWould you like to ignore the error and continue?")
.arg(i + 1),
QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
if (answer == QMessageBox::No)
return false;
}
auto cheat = std::make_shared<Cheats::GatewayCheat>(ui->lineName->text().toStdString(),
ui->textCode->toPlainText().toStdString(),
ui->textNotes->toPlainText().toStdString());
if (newly_created) {
Core::System::GetInstance().CheatEngine().AddCheat(cheat);
newly_created = false;
} else {
Core::System::GetInstance().CheatEngine().UpdateCheat(row, cheat);
}
Core::System::GetInstance().CheatEngine().SaveCheatFile();
int previous_row = ui->tableCheats->currentRow();
int previous_col = ui->tableCheats->currentColumn();
LoadCheats();
ui->tableCheats->setCurrentCell(previous_row, previous_col);
edited = false;
ui->buttonSave->setEnabled(false);
ui->buttonAddCheat->setEnabled(true);
return true;
}
void CheatDialog::closeEvent(QCloseEvent* event) {
if (edited && !CheckSaveCheat()) {
event->ignore();
return;
}
event->accept();
}
void CheatDialog::OnCancel() { void CheatDialog::OnCancel() {
close(); close();
} }
void CheatDialog::OnRowSelected(int row, int column) { void CheatDialog::OnRowSelected(int row, int column) {
ui->textDetails->setEnabled(true); if (row == last_row) {
return;
}
if (edited && !CheckSaveCheat()) {
ui->tableCheats->setCurrentCell(last_row, last_col);
return;
}
if (row < cheats.size()) {
if (newly_created) {
// Remove the newly created dummy item
newly_created = false;
ui->tableCheats->setRowCount(ui->tableCheats->rowCount() - 1);
}
const auto& current_cheat = cheats[row];
ui->lineName->setText(QString::fromStdString(current_cheat->GetName()));
ui->textNotes->setPlainText(QString::fromStdString(current_cheat->GetComments()));
ui->textCode->setPlainText(QString::fromStdString(current_cheat->GetCode()));
}
edited = false;
ui->buttonSave->setEnabled(false);
ui->buttonDelete->setEnabled(true);
ui->buttonAddCheat->setEnabled(true);
ui->lineName->setEnabled(true);
ui->textCode->setEnabled(true);
ui->textNotes->setEnabled(true); ui->textNotes->setEnabled(true);
const auto& current_cheat = Core::System::GetInstance().CheatEngine().GetCheats()[row];
ui->textNotes->setPlainText(QString::fromStdString(current_cheat->GetComments())); last_row = row;
ui->textDetails->setPlainText(QString::fromStdString(current_cheat->ToString())); last_col = column;
} }
void CheatDialog::OnCheckChanged(int state) { void CheatDialog::OnCheckChanged(int state) {
const QCheckBox* checkbox = qobject_cast<QCheckBox*>(sender()); const QCheckBox* checkbox = qobject_cast<QCheckBox*>(sender());
int row = static_cast<int>(checkbox->property("row").toInt()); int row = static_cast<int>(checkbox->property("row").toInt());
Core::System::GetInstance().CheatEngine().GetCheats()[row]->SetEnabled(state); cheats[row]->SetEnabled(state);
Core::System::GetInstance().CheatEngine().SaveCheatFile();
}
void CheatDialog::OnTextEdited() {
edited = true;
ui->buttonSave->setEnabled(true);
}
void CheatDialog::OnDeleteCheat() {
if (newly_created) {
newly_created = false;
} else {
Core::System::GetInstance().CheatEngine().RemoveCheat(ui->tableCheats->currentRow());
Core::System::GetInstance().CheatEngine().SaveCheatFile();
}
LoadCheats();
if (cheats.empty()) {
ui->lineName->setText("");
ui->textCode->setPlainText("");
ui->textNotes->setPlainText("");
ui->lineName->setEnabled(false);
ui->textCode->setEnabled(false);
ui->textNotes->setEnabled(false);
ui->buttonDelete->setEnabled(false);
last_row = last_col = -1;
} else {
if (last_row >= ui->tableCheats->rowCount()) {
last_row = ui->tableCheats->rowCount() - 1;
}
ui->tableCheats->setCurrentCell(last_row, last_col);
const auto& current_cheat = cheats[last_row];
ui->lineName->setText(QString::fromStdString(current_cheat->GetName()));
ui->textNotes->setPlainText(QString::fromStdString(current_cheat->GetComments()));
ui->textCode->setPlainText(QString::fromStdString(current_cheat->GetCode()));
}
edited = false;
ui->buttonSave->setEnabled(false);
ui->buttonAddCheat->setEnabled(true);
}
void CheatDialog::OnAddCheat() {
if (edited && !CheckSaveCheat()) {
return;
}
int row = ui->tableCheats->rowCount();
ui->tableCheats->setRowCount(row + 1);
ui->tableCheats->setCurrentCell(row, 1);
// create a dummy item
ui->tableCheats->setItem(row, 1, new QTableWidgetItem(tr("[new cheat]")));
ui->tableCheats->setItem(row, 2, new QTableWidgetItem(""));
ui->lineName->setText("");
ui->lineName->setPlaceholderText(tr("[new cheat]"));
ui->textCode->setPlainText("");
ui->textNotes->setPlainText("");
ui->lineName->setEnabled(true);
ui->textCode->setEnabled(true);
ui->textNotes->setEnabled(true);
ui->buttonSave->setEnabled(true);
ui->buttonDelete->setEnabled(true);
ui->buttonAddCheat->setEnabled(false);
edited = false;
newly_created = true;
last_row = row;
last_col = 1;
} }

View File

@ -7,6 +7,10 @@
#include <memory> #include <memory>
#include <QDialog> #include <QDialog>
namespace Cheats {
class CheatBase;
}
namespace Ui { namespace Ui {
class CheatDialog; class CheatDialog;
} // namespace Ui } // namespace Ui
@ -19,12 +23,38 @@ public:
~CheatDialog(); ~CheatDialog();
private: private:
std::unique_ptr<Ui::CheatDialog> ui; /**
* Loads the cheats from the CheatEngine, and populates the table.
*/
void LoadCheats(); void LoadCheats();
/**
* Pops up a message box asking if the user wants to save the current cheat.
* If the user selected Yes, attempts to save the current cheat.
* @return true if the user selected No, or if the cheat was saved successfully
* false if the user selected Cancel, or if the user selected Yes but saving failed
*/
bool CheckSaveCheat();
/**
* Saves the current cheat as the row-th cheat in the cheat list.
* @return true if the cheat is saved successfully, false otherwise
*/
bool SaveCheat(int row);
void closeEvent(QCloseEvent* event) override;
private slots: private slots:
void OnCancel(); void OnCancel();
void OnRowSelected(int row, int column); void OnRowSelected(int row, int column);
void OnCheckChanged(int state); void OnCheckChanged(int state);
void OnTextEdited();
void OnDeleteCheat();
void OnAddCheat();
private:
std::unique_ptr<Ui::CheatDialog> ui;
std::vector<std::shared_ptr<Cheats::CheatBase>> cheats;
bool edited = false, newly_created = false;
int last_row = -1, last_col = -1;
}; };

View File

@ -22,182 +22,192 @@
<property name="windowTitle"> <property name="windowTitle">
<string>Cheats</string> <string>Cheats</string>
</property> </property>
<widget class="QLabel" name="labelTitle"> <layout class="QVBoxLayout">
<property name="geometry"> <item>
<rect> <layout class="QHBoxLayout">
<x>10</x> <item>
<y>10</y> <widget class="QLabel" name="labelTitle">
<width>300</width> <property name="font">
<height>31</height> <font>
</rect> <pointsize>10</pointsize>
</property> </font>
<property name="font">
<font>
<pointsize>10</pointsize>
</font>
</property>
<property name="text">
<string>Title ID:</string>
</property>
</widget>
<widget class="QWidget" name="horizontalLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>570</y>
<width>841</width>
<height>41</height>
</rect>
</property>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="buttonClose">
<property name="text">
<string>Close</string>
</property>
</widget>
</item>
</layout>
</widget>
<widget class="QWidget" name="verticalLayoutWidget">
<property name="geometry">
<rect>
<x>10</x>
<y>80</y>
<width>551</width>
<height>471</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QTableWidget" name="tableCheats">
<property name="editTriggers">
<set>QAbstractItemView::NoEditTriggers</set>
</property>
<property name="selectionBehavior">
<enum>QAbstractItemView::SelectRows</enum>
</property>
<property name="showGrid">
<bool>false</bool>
</property>
<property name="columnCount">
<number>3</number>
</property>
<attribute name="horizontalHeaderVisible">
<bool>true</bool>
</attribute>
<attribute name="verticalHeaderVisible">
<bool>false</bool>
</attribute>
<column>
<property name="text">
<string/>
</property> </property>
</column>
<column>
<property name="text"> <property name="text">
<string>Name</string> <string>Title ID:</string>
</property> </property>
</column> </widget>
<column> </item>
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="buttonAddCheat">
<property name="text"> <property name="text">
<string>Type</string> <string>Add Cheat</string>
</property> </property>
</column> </widget>
</widget> </item>
</item> </layout>
</layout> </item>
</widget> <item>
<widget class="QLabel" name="labelAvailableCheats"> <layout class="QHBoxLayout">
<property name="geometry"> <item>
<rect> <layout class="QVBoxLayout">
<x>10</x> <item>
<y>60</y> <widget class="QLabel" name="labelAvailableCheats">
<width>121</width> <property name="geometry">
<height>16</height> <rect>
</rect> <x>10</x>
</property> <y>60</y>
<property name="text"> <width>121</width>
<string>Available Cheats:</string> <height>16</height>
</property> </rect>
</widget> </property>
<widget class="QWidget" name="verticalLayoutWidget_2"> <property name="text">
<property name="geometry"> <string>Available Cheats:</string>
<rect> </property>
<x>580</x> </widget>
<y>440</y> </item>
<width>271</width> <item>
<height>111</height> <widget class="QTableWidget" name="tableCheats">
</rect> <property name="editTriggers">
</property> <set>QAbstractItemView::NoEditTriggers</set>
<layout class="QVBoxLayout" name="verticalLayout_2"> </property>
<item> <property name="selectionBehavior">
<widget class="QPlainTextEdit" name="textNotes"> <enum>QAbstractItemView::SelectRows</enum>
<property name="readOnly"> </property>
<bool>true</bool> <property name="selectionMode">
</property> <enum>QAbstractItemView::SingleSelection</enum>
</widget> </property>
</item> <property name="showGrid">
</layout> <bool>false</bool>
</widget> </property>
<widget class="QLabel" name="labelNotes"> <property name="columnCount">
<property name="geometry"> <number>3</number>
<rect> </property>
<x>580</x> <attribute name="horizontalHeaderVisible">
<y>420</y> <bool>true</bool>
<width>111</width> </attribute>
<height>16</height> <attribute name="verticalHeaderVisible">
</rect> <bool>false</bool>
</property> </attribute>
<property name="text"> <column>
<string>Notes:</string> <property name="text">
</property> <string/>
</widget> </property>
<widget class="QWidget" name="verticalLayoutWidget_3"> </column>
<property name="geometry"> <column>
<rect> <property name="text">
<x>580</x> <string>Name</string>
<y>80</y> </property>
<width>271</width> </column>
<height>311</height> <column>
</rect> <property name="text">
</property> <string>Type</string>
<layout class="QVBoxLayout" name="verticalLayout_3"> </property>
<item> </column>
<widget class="QPlainTextEdit" name="textDetails"> </widget>
<property name="readOnly"> </item>
<bool>true</bool> </layout>
</property> </item>
</widget> <item>
</item> <layout class="QVBoxLayout">
</layout> <item>
</widget> <layout class="QHBoxLayout">
<widget class="QLabel" name="labelDetails"> <item>
<property name="geometry"> <spacer>
<rect> <property name="orientation">
<x>580</x> <enum>Qt::Horizontal</enum>
<y>60</y> </property>
<width>55</width> </spacer>
<height>16</height> </item>
</rect> <item>
</property> <widget class="QPushButton" name="buttonSave">
<property name="text"> <property name="text">
<string>Code:</string> <string>Save</string>
</property> </property>
</widget> <property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="buttonDelete">
<property name="text">
<string>Delete</string>
</property>
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout">
<item>
<layout class="QHBoxLayout">
<item>
<widget class="QLabel" name="labelName">
<property name="text">
<string>Name:</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineName"/>
</item>
</layout>
</item>
<item>
<widget class="QLabel" name="labelNotes">
<property name="text">
<string>Notes:</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="textNotes"/>
</item>
<item>
<widget class="QLabel" name="labelCode">
<property name="text">
<string>Code:</string>
</property>
</widget>
</item>
<item>
<widget class="QPlainTextEdit" name="textCode"/>
</item>
</layout>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout">
<item>
<spacer>
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
</spacer>
</item>
<item>
<widget class="QPushButton" name="buttonClose">
<property name="text">
<string>Close</string>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget> </widget>
<resources/> <resources/>
<connections/> <connections/>