1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
#include "DolphinQt/Config/NewPatchDialog.h"
#include <QCheckBox>
#include <QDialogButtonBox>
#include <QGridLayout>
#include <QGroupBox>
#include <QLabel>
#include <QLineEdit>
#include <QPushButton>
#include <QRadioButton>
#include <QScrollArea>
#include <QVBoxLayout>
#include "Core/PatchEngine.h"
#include "DolphinQt/QtUtils/ModalMessageBox.h"
struct NewPatchEntry
{
NewPatchEntry() = default;
// These entries share the lifetime of their associated text widgets, and because they are
// captured by pointer by various edit handlers, they should not copied or moved.
NewPatchEntry(const NewPatchEntry&) = delete;
NewPatchEntry& operator=(const NewPatchEntry&) = delete;
NewPatchEntry(NewPatchEntry&&) = delete;
NewPatchEntry& operator=(NewPatchEntry&&) = delete;
QLineEdit* address = nullptr;
QLineEdit* value = nullptr;
QLineEdit* comparand = nullptr;
PatchEngine::PatchEntry entry;
};
NewPatchDialog::NewPatchDialog(QWidget* parent, PatchEngine::Patch& patch)
: QDialog(parent), m_patch(patch)
{
setWindowTitle(tr("Patch Editor"));
CreateWidgets();
ConnectWidgets();
for (size_t i = 0; i < m_patch.entries.size(); i++)
{
m_entry_layout->addWidget(CreateEntry(m_patch.entries[i]));
}
if (m_patch.entries.empty())
{
AddEntry();
m_patch.enabled = true;
}
}
NewPatchDialog::~NewPatchDialog() = default;
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()
{
connect(m_name_edit, &QLineEdit::textEdited,
[this](const QString& name) { m_patch.name = name.toStdString(); });
connect(m_add_button, &QPushButton::clicked, this, &NewPatchDialog::AddEntry);
connect(m_button_box, &QDialogButtonBox::accepted, this, &NewPatchDialog::accept);
connect(m_button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
}
void NewPatchDialog::AddEntry()
{
m_entry_layout->addWidget(CreateEntry({}));
}
static u32 OnTextEdited(QLineEdit* edit, const QString& text)
{
bool okay = false;
u32 value = text.toUInt(&okay, 16);
QFont font;
QPalette palette;
font.setBold(!okay);
if (!okay)
palette.setColor(QPalette::Text, Qt::red);
edit->setFont(font);
edit->setPalette(palette);
return value;
}
QGroupBox* NewPatchDialog::CreateEntry(const PatchEngine::PatchEntry& entry)
{
QGroupBox* box = new QGroupBox();
auto* type = new QGroupBox(tr("Type"));
auto* type_layout = new QHBoxLayout;
auto* remove = new QPushButton(tr("Remove"));
auto* byte = new QRadioButton(tr("8-bit"));
auto* word = new QRadioButton(tr("16-bit"));
auto* dword = new QRadioButton(tr("32-bit"));
type_layout->addWidget(byte);
type_layout->addWidget(word);
type_layout->addWidget(dword);
type->setLayout(type_layout);
auto* address = new QLineEdit;
auto* value = new QLineEdit;
auto* comparand = new QLineEdit;
auto* new_entry = m_entries.emplace_back(std::make_unique<NewPatchEntry>()).get();
new_entry->address = address;
new_entry->value = value;
new_entry->comparand = comparand;
new_entry->entry = entry;
auto* conditional = new QCheckBox(tr("Conditional"));
auto* comparand_label = new QLabel(tr("Comparand:"));
auto* layout = new QGridLayout;
layout->addWidget(type, 0, 0, 1, -1);
layout->addWidget(new QLabel(tr("Address:")), 1, 0);
layout->addWidget(address, 1, 1);
layout->addWidget(new QLabel(tr("Value:")), 2, 0);
layout->addWidget(value, 2, 1);
layout->addWidget(conditional, 3, 0, 1, -1);
layout->addWidget(comparand_label, 4, 0);
layout->addWidget(comparand, 4, 1);
layout->addWidget(remove, 5, 0, 1, -1);
box->setLayout(layout);
connect(address, &QLineEdit::textEdited, [new_entry](const QString& text) {
new_entry->entry.address = OnTextEdited(new_entry->address, text);
});
connect(value, &QLineEdit::textEdited, [new_entry](const QString& text) {
new_entry->entry.value = OnTextEdited(new_entry->value, text);
});
connect(comparand, &QLineEdit::textEdited, [new_entry](const QString& text) {
new_entry->entry.comparand = OnTextEdited(new_entry->comparand, text);
});
connect(remove, &QPushButton::clicked, [this, box, new_entry] {
if (m_entries.size() > 1)
{
box->setVisible(false);
m_entry_layout->removeWidget(box);
box->deleteLater();
m_entries.erase(
std::ranges::find(m_entries, new_entry, [](const auto& e) { return e.get(); }));
}
});
connect(byte, &QRadioButton::toggled, [new_entry](bool checked) {
if (checked)
new_entry->entry.type = PatchEngine::PatchType::Patch8Bit;
});
connect(word, &QRadioButton::toggled, [new_entry](bool checked) {
if (checked)
new_entry->entry.type = PatchEngine::PatchType::Patch16Bit;
});
connect(dword, &QRadioButton::toggled, [new_entry](bool checked) {
if (checked)
new_entry->entry.type = PatchEngine::PatchType::Patch32Bit;
});
byte->setChecked(entry.type == PatchEngine::PatchType::Patch8Bit);
word->setChecked(entry.type == PatchEngine::PatchType::Patch16Bit);
dword->setChecked(entry.type == PatchEngine::PatchType::Patch32Bit);
connect(conditional, &QCheckBox::toggled, [new_entry, comparand_label, comparand](bool checked) {
new_entry->entry.conditional = checked;
comparand_label->setVisible(checked);
comparand->setVisible(checked);
});
conditional->setChecked(entry.conditional);
comparand_label->setVisible(entry.conditional);
comparand->setVisible(entry.conditional);
address->setText(QStringLiteral("%1").arg(entry.address, 8, 16, QLatin1Char('0')));
value->setText(QStringLiteral("%1").arg(entry.value, 8, 16, QLatin1Char('0')));
comparand->setText(QStringLiteral("%1").arg(entry.comparand, 8, 16, QLatin1Char('0')));
return box;
}
void NewPatchDialog::accept()
{
if (m_name_edit->text().isEmpty())
{
ModalMessageBox::critical(this, tr("Error"), tr("You have to enter a name."));
return;
}
bool valid = true;
for (const auto& entry : m_entries)
{
entry->address->text().toUInt(&valid, 16);
if (!valid)
break;
entry->value->text().toUInt(&valid, 16);
if (!valid)
break;
if (entry->entry.conditional)
{
entry->comparand->text().toUInt(&valid, 16);
if (!valid)
break;
}
}
if (!valid)
{
ModalMessageBox::critical(
this, tr("Error"),
tr("Some values you provided are invalid.\nPlease check the highlighted values."));
return;
}
m_patch.entries.clear();
for (const auto& entry : m_entries)
{
m_patch.entries.emplace_back(entry->entry);
}
QDialog::accept();
}
|