| 1 | // Copyright 2025 Dolphin Emulator Project |
| 2 | // SPDX-License-Identifier: GPL-2.0-or-later |
| 3 | |
| 4 | #include "DolphinQt/Debugger/EditSymbolDialog.h" |
| 5 | |
| 6 | #include <QAbstractButton> |
| 7 | #include <QDialogButtonBox> |
| 8 | #include <QHBoxLayout> |
| 9 | #include <QLabel> |
| 10 | #include <QLineEdit> |
| 11 | #include <QRegularExpression> |
| 12 | #include <QSpinBox> |
| 13 | #include <QVBoxLayout> |
| 14 | |
| 15 | EditSymbolDialog::EditSymbolDialog(QWidget* parent, const u32 symbol_address, u32* symbol_size, |
| 16 | std::string* symbol_name, Type type) |
| 17 | : QDialog(parent), m_type(type), m_symbol_name(symbol_name), m_symbol_size(symbol_size), |
| 18 | m_symbol_address(symbol_address) |
| 19 | { |
| 20 | setWindowTitle(m_type == Type::Symbol ? tr("Edit Symbol") : tr("Edit Note")); |
| 21 | CreateWidgets(); |
| 22 | ConnectWidgets(); |
| 23 | } |
| 24 | |
| 25 | void EditSymbolDialog::CreateWidgets() |
| 26 | { |
| 27 | m_buttons = new QDialogButtonBox(QDialogButtonBox::Cancel | QDialogButtonBox::Ok); |
| 28 | m_buttons->addButton(tr("Reset"), QDialogButtonBox::ResetRole); |
| 29 | m_buttons->addButton(tr("Delete"), QDialogButtonBox::DestructiveRole); |
| 30 | |
| 31 | QLabel* info_label = |
| 32 | // i18n: %1 is an address. %2 is a warning message. |
| 33 | new QLabel((m_type == Type::Symbol ? tr("Editing symbol starting at: %1\n%2") : |
| 34 | tr("Editing note starting at: %1\n%2")) |
| 35 | .arg(QString::number(m_symbol_address, 16)) |
| 36 | .arg(tr("Warning: Must save the symbol map for changes to be kept."))); |
| 37 | m_name_edit = new QLineEdit(); |
| 38 | m_name_edit->setPlaceholderText(m_type == Type::Symbol ? tr("Symbol name") : tr("Note name")); |
| 39 | |
| 40 | auto* size_layout = new QHBoxLayout; |
| 41 | // i18n: The address where a symbol ends |
| 42 | QLabel* address_end_label = new QLabel(tr("End Address")); |
| 43 | // i18n: The number of lines of code |
| 44 | QLabel* size_lines_label = new QLabel(tr("Lines")); |
| 45 | // i18n: There's a text field immediately to the right of this label where 8 hexadecimal digits |
| 46 | // can be entered. The "0x" in this string acts as a prefix to the digits to indicate that they |
| 47 | // are hexadecimal. |
| 48 | QLabel* size_hex_label = new QLabel(tr("Size: 0x")); |
| 49 | m_address_end_edit = new QLineEdit(); |
| 50 | m_size_lines_spin = new QSpinBox(); |
| 51 | m_size_hex_edit = new QLineEdit(); |
| 52 | |
| 53 | size_hex_label->setAlignment(Qt::AlignCenter | Qt::AlignRight); |
| 54 | size_lines_label->setAlignment(Qt::AlignCenter | Qt::AlignRight); |
| 55 | |
| 56 | // Get system font and use to size boxes. |
| 57 | QFont font; |
| 58 | QFontMetrics fm(font); |
| 59 | const int width = fm.horizontalAdvance(QLatin1Char('0')) * 2; |
| 60 | m_address_end_edit->setFixedWidth(width * 6); |
| 61 | m_size_hex_edit->setFixedWidth(width * 5); |
| 62 | m_size_lines_spin->setFixedWidth(width * 5); |
| 63 | m_size_hex_edit->setMaxLength(7); |
| 64 | m_size_lines_spin->setRange(0, 99999); |
| 65 | |
| 66 | // Accept hex input only |
| 67 | QRegularExpression rx(QStringLiteral("[0-9a-fA-F]{0,8}")); |
| 68 | QValidator* validator = new QRegularExpressionValidator(rx, this); |
| 69 | m_address_end_edit->setValidator(validator); |
| 70 | m_size_hex_edit->setValidator(validator); |
| 71 | |
| 72 | size_layout->addWidget(address_end_label); |
| 73 | size_layout->addWidget(m_address_end_edit); |
| 74 | size_layout->addWidget(size_hex_label); |
| 75 | size_layout->addWidget(m_size_hex_edit); |
| 76 | size_layout->addWidget(size_lines_label); |
| 77 | size_layout->addWidget(m_size_lines_spin); |
| 78 | |
| 79 | auto* layout = new QVBoxLayout(); |
| 80 | layout->addWidget(info_label); |
| 81 | layout->addWidget(m_name_edit); |
| 82 | layout->addLayout(size_layout); |
| 83 | layout->addWidget(m_buttons); |
| 84 | |
| 85 | setLayout(layout); |
| 86 | |
| 87 | FillFunctionData(); |
| 88 | } |
| 89 | |
| 90 | void EditSymbolDialog::FillFunctionData() |
| 91 | { |
| 92 | m_name_edit->setText(QString::fromStdString(*m_symbol_name)); |
| 93 | m_size_lines_spin->setValue(*m_symbol_size / 4); |
| 94 | m_size_hex_edit->setText(QString::number(*m_symbol_size, 16)); |
| 95 | m_address_end_edit->setText( |
| 96 | QStringLiteral("%1").arg(m_symbol_address + *m_symbol_size, 8, 16, QLatin1Char('0'))); |
| 97 | } |
| 98 | |
| 99 | void EditSymbolDialog::UpdateAddressData(u32 size) |
| 100 | { |
| 101 | // Not sure what the max size should be. Definitely not a full 8, so set to 7. |
| 102 | size = size & 0xFFFFFFF; |
| 103 | |
| 104 | m_size_lines_spin->setValue(size / 4); |
| 105 | m_size_hex_edit->setText(QString::number(size, 16)); |
| 106 | m_address_end_edit->setText( |
| 107 | QStringLiteral("%1").arg(m_symbol_address + size, 8, 16, QLatin1Char('0'))); |
| 108 | } |
| 109 | |
| 110 | void EditSymbolDialog::ConnectWidgets() |
| 111 | { |
| 112 | connect(m_size_lines_spin, QOverload<int>::of(&QSpinBox::valueChanged), this, |
| 113 | [this](int value) { UpdateAddressData(value * 4); }); |
| 114 | |
| 115 | connect(m_size_hex_edit, &QLineEdit::editingFinished, this, [this] { |
| 116 | bool good; |
| 117 | const u32 size = m_size_hex_edit->text().toUInt(&good, 16); |
| 118 | if (good) |
| 119 | UpdateAddressData(size); |
| 120 | }); |
| 121 | |
| 122 | connect(m_address_end_edit, &QLineEdit::textEdited, this, [this] { |
| 123 | bool good; |
| 124 | const u32 end = m_address_end_edit->text().toUInt(&good, 16); |
| 125 | if (good && end > m_symbol_address) |
| 126 | UpdateAddressData(end - m_symbol_address); |
| 127 | }); |
| 128 | |
| 129 | connect(m_buttons, &QDialogButtonBox::accepted, this, &EditSymbolDialog::Accepted); |
| 130 | connect(m_buttons, &QDialogButtonBox::rejected, this, &EditSymbolDialog::reject); |
| 131 | connect(m_buttons, &QDialogButtonBox::clicked, this, [this](QAbstractButton* btn) { |
| 132 | const auto role = m_buttons->buttonRole(btn); |
| 133 | if (role == QDialogButtonBox::ButtonRole::ResetRole) |
| 134 | { |
| 135 | FillFunctionData(); |
| 136 | } |
| 137 | else if (role == QDialogButtonBox::ButtonRole::DestructiveRole) |
| 138 | { |
| 139 | m_delete_chosen = true; |
| 140 | QDialog::accept(); |
| 141 | } |
| 142 | }); |
| 143 | } |
| 144 | |
| 145 | void EditSymbolDialog::Accepted() |
| 146 | { |
| 147 | const std::string name = m_name_edit->text().toStdString(); |
| 148 | |
| 149 | if (*m_symbol_name != name) |
| 150 | *m_symbol_name = name; |
| 151 | |
| 152 | bool good; |
| 153 | const u32 size = m_size_hex_edit->text().toUInt(&good, 16); |
| 154 | |
| 155 | if (good && *m_symbol_size != size) |
| 156 | *m_symbol_size = size; |
| 157 | |
| 158 | QDialog::accept(); |
| 159 | } |