CheatsManager: Create ARCodeWidget and GeckoCodeWidget only once.

Create ARCodeWidget and GeckoCodeWidget once on startup rather than
every time a game is launched or shutdown.

In addition to losing focus on the tab (since the previous widget and
tab no longer existed), the behavior prior to this commit could cause a
crash if the user initiated a game shutdown and then opened a code edit
window since the AR/GeckoCodeWidget would get deleted in the meantime.
This commit is contained in:
Dentomologist
2024-07-30 19:55:27 -07:00
parent 232d24109d
commit f4db168a8e
6 changed files with 96 additions and 72 deletions

View File

@ -40,6 +40,22 @@ ARCodeWidget::ARCodeWidget(std::string game_id, u16 game_revision, bool restart_
ARCodeWidget::~ARCodeWidget() = default;
void ARCodeWidget::ChangeGame(std::string game_id, const u16 game_revision)
{
m_game_id = std::move(game_id);
m_game_revision = game_revision;
m_restart_required = false;
m_ar_codes.clear();
// If a CheatCodeEditor is open, it's now trying to add or edit a code in the previous game's code
// list which is no longer loaded. Letting the user save the code wouldn't make sense, so close
// the dialog instead.
m_cheat_code_editor->reject();
LoadCodes();
}
void ARCodeWidget::CreateWidgets()
{
m_warning = new CheatWarningWidget(m_game_id, m_restart_required, this);
@ -51,6 +67,8 @@ void ARCodeWidget::CreateWidgets()
m_code_edit = new NonDefaultQPushButton(tr("&Edit Code..."));
m_code_remove = new NonDefaultQPushButton(tr("&Remove Code"));
m_cheat_code_editor = new CheatCodeEditor(this);
m_code_list->setContextMenuPolicy(Qt::CustomContextMenu);
auto* button_layout = new QHBoxLayout;
@ -250,10 +268,9 @@ void ARCodeWidget::OnCodeAddClicked()
ActionReplay::ARCode ar;
ar.enabled = true;
CheatCodeEditor ed(this);
ed.SetARCode(&ar);
SetQWidgetWindowDecorations(&ed);
if (ed.exec() == QDialog::Rejected)
m_cheat_code_editor->SetARCode(&ar);
SetQWidgetWindowDecorations(m_cheat_code_editor);
if (m_cheat_code_editor->exec() == QDialog::Rejected)
return;
m_ar_codes.push_back(std::move(ar));
@ -270,23 +287,19 @@ void ARCodeWidget::OnCodeEditClicked()
const auto* const selected = items[0];
auto& current_ar = m_ar_codes[m_code_list->row(selected)];
SetQWidgetWindowDecorations(m_cheat_code_editor);
CheatCodeEditor ed(this);
if (current_ar.user_defined)
{
ed.SetARCode(&current_ar);
SetQWidgetWindowDecorations(&ed);
if (ed.exec() == QDialog::Rejected)
m_cheat_code_editor->SetARCode(&current_ar);
if (m_cheat_code_editor->exec() == QDialog::Rejected)
return;
}
else
{
ActionReplay::ARCode ar = current_ar;
ed.SetARCode(&ar);
SetQWidgetWindowDecorations(&ed);
if (ed.exec() == QDialog::Rejected)
m_cheat_code_editor->SetARCode(&ar);
if (m_cheat_code_editor->exec() == QDialog::Rejected)
return;
m_ar_codes.push_back(std::move(ar));

View File

@ -15,6 +15,7 @@ namespace ActionReplay
struct ARCode;
}
class CheatCodeEditor;
class CheatWarningWidget;
#ifdef USE_RETRO_ACHIEVEMENTS
class HardcoreWarningWidget;
@ -31,6 +32,7 @@ public:
explicit ARCodeWidget(std::string game_id, u16 game_revision, bool restart_required = true);
~ARCodeWidget() override;
void ChangeGame(std::string game_id, u16 game_revision);
void AddCode(ActionReplay::ARCode code);
signals:
@ -71,6 +73,8 @@ private:
QPushButton* m_code_edit;
QPushButton* m_code_remove;
CheatCodeEditor* m_cheat_code_editor;
std::vector<ActionReplay::ARCode> m_ar_codes;
bool m_restart_required;
};

View File

@ -45,6 +45,29 @@ GeckoCodeWidget::GeckoCodeWidget(std::string game_id, std::string gametdb_id, u1
LoadCodes();
}
void GeckoCodeWidget::ChangeGame(std::string game_id, std::string gametdb_id,
const u16 game_revision)
{
m_game_id = std::move(game_id);
m_gametdb_id = std::move(gametdb_id);
m_game_revision = game_revision;
m_restart_required = false;
m_gecko_codes.clear();
m_code_list->clear();
m_name_label->clear();
m_creator_label->clear();
m_code_description->clear();
m_code_view->clear();
// If a CheatCodeEditor is open, it's now trying to add or edit a code in the previous game's code
// list which is no longer loaded. Letting the user save the code wouldn't make sense, so close
// the dialog instead.
m_cheat_code_editor->reject();
LoadCodes();
}
GeckoCodeWidget::~GeckoCodeWidget() = default;
void GeckoCodeWidget::CreateWidgets()
@ -78,6 +101,8 @@ void GeckoCodeWidget::CreateWidgets()
m_remove_code = new NonDefaultQPushButton(tr("&Remove Code"));
m_download_codes = new NonDefaultQPushButton(tr("Download Codes"));
m_cheat_code_editor = new CheatCodeEditor(this);
m_download_codes->setToolTip(tr("Download Codes from the WiiRD Database"));
auto* layout = new QVBoxLayout;
@ -187,10 +212,9 @@ void GeckoCodeWidget::AddCode()
Gecko::GeckoCode code;
code.enabled = true;
CheatCodeEditor ed(this);
ed.SetGeckoCode(&code);
SetQWidgetWindowDecorations(&ed);
if (ed.exec() == QDialog::Rejected)
m_cheat_code_editor->SetGeckoCode(&code);
SetQWidgetWindowDecorations(m_cheat_code_editor);
if (m_cheat_code_editor->exec() == QDialog::Rejected)
return;
m_gecko_codes.push_back(std::move(code));
@ -206,10 +230,9 @@ void GeckoCodeWidget::EditCode()
const int index = item->data(Qt::UserRole).toInt();
CheatCodeEditor ed(this);
ed.SetGeckoCode(&m_gecko_codes[index]);
SetQWidgetWindowDecorations(&ed);
if (ed.exec() == QDialog::Rejected)
m_cheat_code_editor->SetGeckoCode(&m_gecko_codes[index]);
SetQWidgetWindowDecorations(m_cheat_code_editor);
if (m_cheat_code_editor->exec() == QDialog::Rejected)
return;
SaveCodes();

View File

@ -10,6 +10,7 @@
#include "Common/CommonTypes.h"
class CheatCodeEditor;
class CheatWarningWidget;
#ifdef USE_RETRO_ACHIEVEMENTS
class HardcoreWarningWidget;
@ -33,6 +34,8 @@ public:
bool restart_required = true);
~GeckoCodeWidget() override;
void ChangeGame(std::string game_id, std::string gametdb_id, u16 game_revision);
signals:
void OpenGeneralSettings();
#ifdef USE_RETRO_ACHIEVEMENTS
@ -75,6 +78,7 @@ private:
QPushButton* m_edit_code;
QPushButton* m_remove_code;
QPushButton* m_download_codes;
CheatCodeEditor* m_cheat_code_editor;
std::vector<Gecko::GeckoCode> m_gecko_codes;
bool m_restart_required;
};