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