Select highlighted variable in EditVariablesDialog if possible. (#1727)

This commit is contained in:
karliss 2019-09-04 19:39:33 +03:00 committed by GitHub
parent 77ab80a5b2
commit 6fe473948b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 4 deletions

View File

@ -4,9 +4,10 @@
#include <QMetaType>
#include <QComboBox>
#include <QMetaType>
#include <QPushButton>
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<VariableDescription>();
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<VariableDescription>();
ui->nameEdit->setText(desc.name);
ui->typeComboBox->setCurrentText(desc.type);

View File

@ -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();

View File

@ -309,6 +309,8 @@ void DisassemblyContextMenu::updateTargetMenuActions(const QVector<ThingUsedHere
void DisassemblyContextMenu::setOffset(RVA offset)
{
this->offset = 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();
}