Skip to content

Commit

Permalink
Merge pull request dolphin-emu#8028 from spycrab/issue_11690
Browse files Browse the repository at this point in the history
Qt/NewPatchDialog: Fix crashes on entry removal
  • Loading branch information
spycrab authored Apr 24, 2019
2 parents ac879b2 + a6e7a90 commit 8feacce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 26 deletions.
62 changes: 38 additions & 24 deletions Source/Core/DolphinQt/Config/NewPatchDialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ NewPatchDialog::NewPatchDialog(QWidget* parent, PatchEngine::Patch& patch)

for (size_t i = 0; i < m_patch.entries.size(); i++)
{
m_entry_layout->addWidget(CreateEntry(static_cast<int>(i)));
m_entry_layout->addWidget(CreateEntry(m_patch.entries[i]));
}

if (m_patch.entries.empty())
Expand Down Expand Up @@ -81,10 +81,24 @@ void NewPatchDialog::AddEntry()
{
m_patch.entries.emplace_back();

m_entry_layout->addWidget(CreateEntry(static_cast<int>(m_patch.entries.size() - 1)));
m_entry_layout->addWidget(CreateEntry(m_patch.entries[m_patch.entries.size() - 1]));
}

QGroupBox* NewPatchDialog::CreateEntry(int index)
static bool PatchEq(const PatchEngine::PatchEntry& a, const PatchEngine::PatchEntry& b)
{
if (a.address != b.address)
return false;

if (a.type != b.type)
return false;

if (a.value != b.value)
return false;

return true;
}

QGroupBox* NewPatchDialog::CreateEntry(PatchEngine::PatchEntry& entry)
{
QGroupBox* box = new QGroupBox();

Expand Down Expand Up @@ -117,9 +131,9 @@ QGroupBox* NewPatchDialog::CreateEntry(int index)
box->setLayout(layout);

connect(offset, static_cast<void (QLineEdit::*)(const QString&)>(&QLineEdit::textEdited),
[this, index, offset](const QString& text) {
[&entry, offset](const QString& text) {
bool okay = true;
m_patch.entries[index].address = text.toUInt(&okay, 16);
entry.address = text.toUInt(&okay, 16);

QFont font;
QPalette palette;
Expand All @@ -134,9 +148,9 @@ QGroupBox* NewPatchDialog::CreateEntry(int index)
});

connect(value, static_cast<void (QLineEdit::*)(const QString&)>(&QLineEdit::textEdited),
[this, index, value](const QString& text) {
[&entry, value](const QString& text) {
bool okay;
m_patch.entries[index].value = text.toUInt(&okay, 16);
entry.value = text.toUInt(&okay, 16);

QFont font;
QPalette palette;
Expand All @@ -150,39 +164,39 @@ QGroupBox* NewPatchDialog::CreateEntry(int index)
value->setPalette(palette);
});

connect(remove, &QPushButton::pressed, [this, box, index] {
connect(remove, &QPushButton::pressed, [this, box, entry] {
if (m_patch.entries.size() > 1)
{
box->setVisible(false);
m_entry_layout->removeWidget(box);
m_patch.entries.erase(m_patch.entries.begin() + index);
m_id--;
box->deleteLater();
m_patch.entries.erase(
std::find_if(m_patch.entries.begin(), m_patch.entries.end(),
[entry](const PatchEngine::PatchEntry& e) { return PatchEq(e, entry); }));
}
});

connect(byte, &QRadioButton::toggled, [this, index](bool checked) {
connect(byte, &QRadioButton::toggled, [&entry](bool checked) {
if (checked)
m_patch.entries[index].type = PatchEngine::PatchType::Patch8Bit;
entry.type = PatchEngine::PatchType::Patch8Bit;
});

connect(word, &QRadioButton::toggled, [this, index](bool checked) {
connect(word, &QRadioButton::toggled, [&entry](bool checked) {
if (checked)
m_patch.entries[index].type = PatchEngine::PatchType::Patch16Bit;
entry.type = PatchEngine::PatchType::Patch16Bit;
});

connect(dword, &QRadioButton::toggled, [this, index](bool checked) {
connect(dword, &QRadioButton::toggled, [&entry](bool checked) {
if (checked)
m_patch.entries[index].type = PatchEngine::PatchType::Patch32Bit;
entry.type = PatchEngine::PatchType::Patch32Bit;
});

auto entry_type = m_patch.entries[index].type;

byte->setChecked(entry_type == PatchEngine::PatchType::Patch8Bit);
word->setChecked(entry_type == PatchEngine::PatchType::Patch16Bit);
dword->setChecked(entry_type == PatchEngine::PatchType::Patch32Bit);
byte->setChecked(entry.type == PatchEngine::PatchType::Patch8Bit);
word->setChecked(entry.type == PatchEngine::PatchType::Patch16Bit);
dword->setChecked(entry.type == PatchEngine::PatchType::Patch32Bit);

offset->setText(
QStringLiteral("%1").arg(m_patch.entries[index].address, 8, 16, QLatin1Char('0')));
value->setText(QStringLiteral("%1").arg(m_patch.entries[index].value, 8, 16, QLatin1Char('0')));
offset->setText(QStringLiteral("%1").arg(entry.address, 8, 16, QLatin1Char('0')));
value->setText(QStringLiteral("%1").arg(entry.value, 8, 16, QLatin1Char('0')));

return box;
}
Expand Down
4 changes: 2 additions & 2 deletions Source/Core/DolphinQt/Config/NewPatchDialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace PatchEngine
{
struct Patch;
struct PatchEntry;
}

class QDialogButtonBox;
Expand All @@ -32,7 +33,7 @@ class NewPatchDialog : public QDialog

void accept() override;

QGroupBox* CreateEntry(int index);
QGroupBox* CreateEntry(PatchEngine::PatchEntry& entry);

QLineEdit* m_name_edit;
QWidget* m_entry_widget;
Expand All @@ -43,5 +44,4 @@ class NewPatchDialog : public QDialog
std::vector<QLineEdit*> m_edits;

PatchEngine::Patch& m_patch;
int m_id = 0;
};

0 comments on commit 8feacce

Please sign in to comment.