diff --git a/src/dialogs/EditVariablesDialog.cpp b/src/dialogs/EditVariablesDialog.cpp index 3706aef1..03cc3e65 100644 --- a/src/dialogs/EditVariablesDialog.cpp +++ b/src/dialogs/EditVariablesDialog.cpp @@ -4,9 +4,10 @@ #include #include #include +#include -EditVariablesDialog::EditVariablesDialog(RVA offset, QWidget *parent) : +EditVariablesDialog::EditVariablesDialog(RVA offset, QString initialVar, QWidget *parent) : QDialog(parent), ui(new Ui::EditVariablesDialog) { @@ -18,8 +19,18 @@ EditVariablesDialog::EditVariablesDialog(RVA offset, QWidget *parent) : setWindowTitle(tr("Set Variable Types for Function: %1").arg(fcnName)); variables = Core()->getVariables(offset); + int currentItemIndex = -1; + int index = 0; for (const VariableDescription &var : variables) { ui->dropdownLocalVars->addItem(var.name, QVariant::fromValue(var)); + if (var.name == initialVar) { + currentItemIndex = index; + } + index++; + } + ui->dropdownLocalVars->setCurrentIndex(currentItemIndex); + if (currentItemIndex != -1) { + ui->nameEdit->setFocus(); } populateTypesComboBox(); @@ -31,8 +42,17 @@ EditVariablesDialog::~EditVariablesDialog() delete ui; } +bool EditVariablesDialog::empty() const +{ + return ui->dropdownLocalVars->count() == 0; +} + void EditVariablesDialog::applyFields() { + if (ui->dropdownLocalVars->currentIndex() < 0) { + // nothing was selected or list is empty + return; + } VariableDescription desc = ui->dropdownLocalVars->currentData().value(); Core()->cmdRaw(QString("afvt %1 %2").arg(desc.name).arg(ui->typeComboBox->currentText())); @@ -48,6 +68,13 @@ void EditVariablesDialog::applyFields() void EditVariablesDialog::updateFields() { + bool hasSelection = ui->dropdownLocalVars->currentIndex() >= 0; + auto okButton = ui->buttonBox->button(QDialogButtonBox::Ok); + okButton->setEnabled(hasSelection); + if (!hasSelection) { + ui->nameEdit->clear(); + return; + } VariableDescription desc = ui->dropdownLocalVars->currentData().value(); ui->nameEdit->setText(desc.name); ui->typeComboBox->setCurrentText(desc.type); diff --git a/src/dialogs/EditVariablesDialog.h b/src/dialogs/EditVariablesDialog.h index 5f5681ea..5a017ca7 100644 --- a/src/dialogs/EditVariablesDialog.h +++ b/src/dialogs/EditVariablesDialog.h @@ -13,9 +13,10 @@ class EditVariablesDialog : public QDialog Q_OBJECT public: - explicit EditVariablesDialog(RVA offset, QWidget *parent = nullptr); + explicit EditVariablesDialog(RVA offset, QString initialVar = QString(), QWidget *parent = nullptr); ~EditVariablesDialog(); + bool empty() const; private slots: void applyFields(); void updateFields(); diff --git a/src/menus/DisassemblyContextMenu.cpp b/src/menus/DisassemblyContextMenu.cpp index 3f0db0a4..5664eaec 100644 --- a/src/menus/DisassemblyContextMenu.cpp +++ b/src/menus/DisassemblyContextMenu.cpp @@ -309,6 +309,8 @@ void DisassemblyContextMenu::updateTargetMenuActions(const QVectoroffset = offset; + + this->actionSetFunctionVarTypes.setVisible(true); } void DisassemblyContextMenu::setCanCopy(bool enabled) @@ -409,7 +411,8 @@ void DisassemblyContextMenu::aboutToShowSlot() // Only show retype for local vars if in a function if (in_fcn) { - actionSetFunctionVarTypes.setVisible(true); + auto vars = Core()->getVariables(offset); + actionSetFunctionVarTypes.setVisible(!vars.empty()); actionEditFunction.setVisible(true); actionEditFunction.setText(tr("Edit function \"%1\"").arg(in_fcn->name)); } else { @@ -771,7 +774,10 @@ void DisassemblyContextMenu::on_actionSetFunctionVarTypes_triggered() return; } - EditVariablesDialog dialog(Core()->getOffset(), this); + EditVariablesDialog dialog(Core()->getOffset(), curHighlightedWord, this); + if (dialog.empty()) { // don't show the dialog if there are no variables + return; + } dialog.exec(); }