2018-02-16 13:53:52 +00:00
|
|
|
// Copyright 2018 Dolphin Emulator Project
|
|
|
|
// Licensed under GPLv2+
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
2018-07-06 22:40:15 +00:00
|
|
|
#include "DolphinQt/Config/NewPatchDialog.h"
|
2018-02-16 13:53:52 +00:00
|
|
|
|
|
|
|
#include <QDialogButtonBox>
|
|
|
|
#include <QGridLayout>
|
|
|
|
#include <QGroupBox>
|
|
|
|
#include <QLabel>
|
|
|
|
#include <QLineEdit>
|
|
|
|
#include <QPushButton>
|
|
|
|
#include <QRadioButton>
|
|
|
|
#include <QScrollArea>
|
|
|
|
#include <QVBoxLayout>
|
|
|
|
|
2018-05-13 19:34:38 +00:00
|
|
|
#include "Core/PatchEngine.h"
|
2019-03-04 19:49:00 +00:00
|
|
|
#include "DolphinQt/QtUtils/ModalMessageBox.h"
|
2018-05-13 19:34:38 +00:00
|
|
|
|
2018-05-06 16:02:39 +00:00
|
|
|
NewPatchDialog::NewPatchDialog(QWidget* parent, PatchEngine::Patch& patch)
|
|
|
|
: QDialog(parent), m_patch(patch)
|
2018-02-16 13:53:52 +00:00
|
|
|
{
|
|
|
|
setWindowTitle(tr("Patch Editor"));
|
2018-05-05 00:29:16 +00:00
|
|
|
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
|
|
|
|
|
2018-02-16 13:53:52 +00:00
|
|
|
CreateWidgets();
|
|
|
|
ConnectWidgets();
|
|
|
|
|
|
|
|
for (size_t i = 0; i < m_patch.entries.size(); i++)
|
|
|
|
{
|
2019-04-23 16:46:38 +00:00
|
|
|
m_entry_layout->addWidget(CreateEntry(m_patch.entries[i]));
|
2018-02-16 13:53:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (m_patch.entries.empty())
|
|
|
|
{
|
|
|
|
AddEntry();
|
2020-12-10 11:58:27 +00:00
|
|
|
m_patch.enabled = true;
|
2018-02-16 13:53:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewPatchDialog::CreateWidgets()
|
|
|
|
{
|
|
|
|
m_name_edit = new QLineEdit;
|
|
|
|
m_name_edit->setPlaceholderText(tr("Patch name"));
|
|
|
|
m_name_edit->setText(QString::fromStdString(m_patch.name));
|
|
|
|
|
|
|
|
m_entry_widget = new QWidget;
|
|
|
|
m_entry_layout = new QVBoxLayout;
|
|
|
|
|
|
|
|
auto* scroll_area = new QScrollArea;
|
|
|
|
m_entry_widget->setLayout(m_entry_layout);
|
|
|
|
scroll_area->setWidget(m_entry_widget);
|
|
|
|
scroll_area->setWidgetResizable(true);
|
|
|
|
|
|
|
|
m_add_button = new QPushButton(tr("Add"));
|
|
|
|
|
|
|
|
m_button_box = new QDialogButtonBox(QDialogButtonBox::Save | QDialogButtonBox::Cancel);
|
|
|
|
|
|
|
|
auto* layout = new QGridLayout;
|
|
|
|
|
|
|
|
layout->addWidget(m_name_edit, 0, 0);
|
|
|
|
layout->addWidget(scroll_area, 1, 0);
|
|
|
|
layout->addWidget(m_add_button, 2, 0);
|
|
|
|
layout->addWidget(m_button_box, 3, 0);
|
|
|
|
|
|
|
|
setLayout(layout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewPatchDialog::ConnectWidgets()
|
|
|
|
{
|
2019-07-30 13:35:46 +00:00
|
|
|
connect(m_name_edit, qOverload<const QString&>(&QLineEdit::textEdited),
|
2018-02-16 13:53:52 +00:00
|
|
|
[this](const QString& name) { m_patch.name = name.toStdString(); });
|
|
|
|
|
2019-07-23 22:18:58 +00:00
|
|
|
connect(m_add_button, &QPushButton::clicked, this, &NewPatchDialog::AddEntry);
|
2018-02-16 13:53:52 +00:00
|
|
|
|
|
|
|
connect(m_button_box, &QDialogButtonBox::accepted, this, &NewPatchDialog::accept);
|
|
|
|
connect(m_button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewPatchDialog::AddEntry()
|
|
|
|
{
|
2018-05-13 17:53:49 +00:00
|
|
|
m_patch.entries.emplace_back();
|
2018-02-16 13:53:52 +00:00
|
|
|
|
2019-04-23 16:46:38 +00:00
|
|
|
m_entry_layout->addWidget(CreateEntry(m_patch.entries[m_patch.entries.size() - 1]));
|
2018-02-16 13:53:52 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 16:46:38 +00:00
|
|
|
static bool PatchEq(const PatchEngine::PatchEntry& a, const PatchEngine::PatchEntry& b)
|
|
|
|
{
|
|
|
|
if (a.address != b.address)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (a.type != b.type)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if (a.value != b.value)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
QGroupBox* NewPatchDialog::CreateEntry(PatchEngine::PatchEntry& entry)
|
2018-02-16 13:53:52 +00:00
|
|
|
{
|
|
|
|
QGroupBox* box = new QGroupBox();
|
|
|
|
|
|
|
|
auto* type = new QGroupBox(tr("Type"));
|
|
|
|
auto* type_layout = new QHBoxLayout;
|
|
|
|
auto* remove = new QPushButton(tr("Remove"));
|
|
|
|
|
2018-03-04 18:08:57 +00:00
|
|
|
auto* byte = new QRadioButton(tr("8-bit"));
|
|
|
|
auto* word = new QRadioButton(tr("16-bit"));
|
|
|
|
auto* dword = new QRadioButton(tr("32-bit"));
|
2018-02-16 13:53:52 +00:00
|
|
|
|
|
|
|
type_layout->addWidget(byte);
|
|
|
|
type_layout->addWidget(word);
|
|
|
|
type_layout->addWidget(dword);
|
|
|
|
type->setLayout(type_layout);
|
|
|
|
|
|
|
|
auto* offset = new QLineEdit;
|
|
|
|
auto* value = new QLineEdit;
|
|
|
|
|
|
|
|
m_edits.push_back(offset);
|
|
|
|
m_edits.push_back(value);
|
|
|
|
|
|
|
|
auto* layout = new QGridLayout;
|
|
|
|
layout->addWidget(type, 0, 0, 1, -1);
|
|
|
|
layout->addWidget(new QLabel(tr("Offset:")), 1, 0);
|
|
|
|
layout->addWidget(offset, 1, 1);
|
|
|
|
layout->addWidget(new QLabel(tr("Value:")), 2, 0);
|
|
|
|
layout->addWidget(value, 2, 1);
|
|
|
|
layout->addWidget(remove, 3, 0, 1, -1);
|
|
|
|
box->setLayout(layout);
|
|
|
|
|
2019-07-30 13:35:46 +00:00
|
|
|
connect(offset, qOverload<const QString&>(&QLineEdit::textEdited),
|
2019-04-23 16:46:38 +00:00
|
|
|
[&entry, offset](const QString& text) {
|
2018-02-16 13:53:52 +00:00
|
|
|
bool okay = true;
|
2019-04-23 16:46:38 +00:00
|
|
|
entry.address = text.toUInt(&okay, 16);
|
2018-02-16 13:53:52 +00:00
|
|
|
|
|
|
|
QFont font;
|
|
|
|
QPalette palette;
|
|
|
|
|
|
|
|
font.setBold(!okay);
|
|
|
|
|
|
|
|
if (!okay)
|
|
|
|
palette.setColor(QPalette::Text, Qt::red);
|
|
|
|
|
|
|
|
offset->setFont(font);
|
|
|
|
offset->setPalette(palette);
|
|
|
|
});
|
|
|
|
|
2019-07-30 13:35:46 +00:00
|
|
|
connect(value, qOverload<const QString&>(&QLineEdit::textEdited),
|
2019-04-23 16:46:38 +00:00
|
|
|
[&entry, value](const QString& text) {
|
2018-02-16 13:53:52 +00:00
|
|
|
bool okay;
|
2019-04-23 16:46:38 +00:00
|
|
|
entry.value = text.toUInt(&okay, 16);
|
2018-02-16 13:53:52 +00:00
|
|
|
|
|
|
|
QFont font;
|
|
|
|
QPalette palette;
|
|
|
|
|
|
|
|
font.setBold(!okay);
|
|
|
|
|
|
|
|
if (!okay)
|
|
|
|
palette.setColor(QPalette::Text, Qt::red);
|
|
|
|
|
|
|
|
value->setFont(font);
|
|
|
|
value->setPalette(palette);
|
|
|
|
});
|
|
|
|
|
2020-12-11 21:10:36 +00:00
|
|
|
connect(remove, &QPushButton::clicked, [this, box, offset, value, entry] {
|
2018-02-16 13:53:52 +00:00
|
|
|
if (m_patch.entries.size() > 1)
|
|
|
|
{
|
2019-04-23 16:46:38 +00:00
|
|
|
box->setVisible(false);
|
2018-02-16 13:53:52 +00:00
|
|
|
m_entry_layout->removeWidget(box);
|
2019-04-23 16:46:38 +00:00
|
|
|
box->deleteLater();
|
2020-12-11 21:10:36 +00:00
|
|
|
|
2019-04-23 16:46:38 +00:00
|
|
|
m_patch.entries.erase(
|
|
|
|
std::find_if(m_patch.entries.begin(), m_patch.entries.end(),
|
|
|
|
[entry](const PatchEngine::PatchEntry& e) { return PatchEq(e, entry); }));
|
2020-12-11 21:10:36 +00:00
|
|
|
|
|
|
|
const auto it =
|
|
|
|
std::remove_if(m_edits.begin(), m_edits.end(), [offset, value](QLineEdit* line_edit) {
|
|
|
|
return line_edit == offset || line_edit == value;
|
|
|
|
});
|
|
|
|
m_edits.erase(it, m_edits.end());
|
2018-02-16 13:53:52 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-04-23 16:46:38 +00:00
|
|
|
connect(byte, &QRadioButton::toggled, [&entry](bool checked) {
|
2018-02-16 13:53:52 +00:00
|
|
|
if (checked)
|
2019-04-23 16:46:38 +00:00
|
|
|
entry.type = PatchEngine::PatchType::Patch8Bit;
|
2018-02-16 13:53:52 +00:00
|
|
|
});
|
|
|
|
|
2019-04-23 16:46:38 +00:00
|
|
|
connect(word, &QRadioButton::toggled, [&entry](bool checked) {
|
2018-02-16 13:53:52 +00:00
|
|
|
if (checked)
|
2019-04-23 16:46:38 +00:00
|
|
|
entry.type = PatchEngine::PatchType::Patch16Bit;
|
2018-02-16 13:53:52 +00:00
|
|
|
});
|
|
|
|
|
2019-04-23 16:46:38 +00:00
|
|
|
connect(dword, &QRadioButton::toggled, [&entry](bool checked) {
|
2018-02-16 13:53:52 +00:00
|
|
|
if (checked)
|
2019-04-23 16:46:38 +00:00
|
|
|
entry.type = PatchEngine::PatchType::Patch32Bit;
|
2018-02-16 13:53:52 +00:00
|
|
|
});
|
|
|
|
|
2019-04-23 16:46:38 +00:00
|
|
|
byte->setChecked(entry.type == PatchEngine::PatchType::Patch8Bit);
|
|
|
|
word->setChecked(entry.type == PatchEngine::PatchType::Patch16Bit);
|
|
|
|
dword->setChecked(entry.type == PatchEngine::PatchType::Patch32Bit);
|
2018-02-16 13:53:52 +00:00
|
|
|
|
2019-04-23 16:46:38 +00:00
|
|
|
offset->setText(QStringLiteral("%1").arg(entry.address, 8, 16, QLatin1Char('0')));
|
|
|
|
value->setText(QStringLiteral("%1").arg(entry.value, 8, 16, QLatin1Char('0')));
|
2018-02-16 13:53:52 +00:00
|
|
|
|
|
|
|
return box;
|
|
|
|
}
|
|
|
|
|
|
|
|
void NewPatchDialog::accept()
|
|
|
|
{
|
|
|
|
if (m_name_edit->text().isEmpty())
|
|
|
|
{
|
2019-03-04 19:49:00 +00:00
|
|
|
ModalMessageBox::critical(this, tr("Error"), tr("You have to enter a name."));
|
2018-02-16 13:53:52 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool valid = true;
|
|
|
|
|
|
|
|
for (const auto* edit : m_edits)
|
|
|
|
{
|
|
|
|
edit->text().toUInt(&valid, 16);
|
|
|
|
if (!valid)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!valid)
|
|
|
|
{
|
2019-03-04 19:49:00 +00:00
|
|
|
ModalMessageBox::critical(
|
2018-02-16 13:53:52 +00:00
|
|
|
this, tr("Error"),
|
|
|
|
tr("Some values you provided are invalid.\nPlease check the highlighted values."));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
QDialog::accept();
|
|
|
|
}
|