| 1 | // Copyright 2019 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/PatchInstructionDialog.h" |
| 5 | |
| 6 | #include <QDialogButtonBox> |
| 7 | #include <QLabel> |
| 8 | #include <QLineEdit> |
| 9 | #include <QPushButton> |
| 10 | #include <QVBoxLayout> |
| 11 | |
| 12 | #include "Common/GekkoDisassembler.h" |
| 13 | |
| 14 | PatchInstructionDialog::PatchInstructionDialog(QWidget* parent, u32 address, u32 value) |
| 15 | : QDialog(parent), m_address(address) |
| 16 | { |
| 17 | setWindowModality(Qt::WindowModal); |
| 18 | setWindowTitle(tr("Instruction")); |
| 19 | |
| 20 | CreateWidgets(); |
| 21 | ConnectWidgets(); |
| 22 | |
| 23 | m_input_edit->setText(QStringLiteral("%1").arg(value, 8, 16, QLatin1Char('0'))); |
| 24 | } |
| 25 | |
| 26 | void PatchInstructionDialog::CreateWidgets() |
| 27 | { |
| 28 | auto* layout = new QVBoxLayout; |
| 29 | |
| 30 | m_input_edit = new QLineEdit; |
| 31 | m_preview_label = new QLabel; |
| 32 | m_button_box = new QDialogButtonBox(QDialogButtonBox::Ok | QDialogButtonBox::Cancel); |
| 33 | |
| 34 | layout->addWidget(new QLabel(tr("New instruction:"))); |
| 35 | layout->addWidget(m_input_edit); |
| 36 | layout->addWidget(m_preview_label); |
| 37 | layout->addWidget(m_button_box); |
| 38 | |
| 39 | setLayout(layout); |
| 40 | } |
| 41 | |
| 42 | void PatchInstructionDialog::ConnectWidgets() |
| 43 | { |
| 44 | connect(m_button_box, &QDialogButtonBox::accepted, this, &QDialog::accept); |
| 45 | connect(m_button_box, &QDialogButtonBox::rejected, this, &QDialog::reject); |
| 46 | |
| 47 | connect(m_input_edit, &QLineEdit::textChanged, this, &PatchInstructionDialog::OnEditChanged); |
| 48 | } |
| 49 | |
| 50 | void PatchInstructionDialog::OnEditChanged() |
| 51 | { |
| 52 | bool good; |
| 53 | m_code = m_input_edit->text().toUInt(&good, 16); |
| 54 | |
| 55 | m_button_box->button(QDialogButtonBox::Ok)->setEnabled(good); |
| 56 | |
| 57 | m_preview_label->setText( |
| 58 | tr("Instruction: %1") |
| 59 | .arg(QString::fromStdString(Common::GekkoDisassembler::Disassemble(m_code, m_address)))); |
| 60 | } |
| 61 | |
| 62 | u32 PatchInstructionDialog::GetCode() const |
| 63 | { |
| 64 | return m_code; |
| 65 | } |