Qt: Add Clear and Reset buttons to cheat manager

This commit is contained in:
Connor McLaughlin 2021-02-12 00:20:04 +10:00
parent e75f1c1b6d
commit de688615ff
6 changed files with 119 additions and 17 deletions

View file

@ -607,10 +607,16 @@ CheatList::Format CheatList::DetectFileFormat(const std::string& str)
bool CheatList::LoadFromFile(const char* filename, Format format) bool CheatList::LoadFromFile(const char* filename, Format format)
{ {
std::optional<std::string> str = FileSystem::ReadFileToString(filename); if (!FileSystem::FileExists(filename))
if (!str.has_value() || str->empty())
return false; return false;
std::optional<std::string> str = FileSystem::ReadFileToString(filename);
if (!str.has_value())
return false;
if (str->empty())
return true;
return LoadFromString(str.value(), format); return LoadFromString(str.value(), format);
} }

View file

@ -33,11 +33,13 @@ static QString formatHexAndDecValue(u32 value, u8 size, bool is_signed)
if (is_signed) if (is_signed)
{ {
u32 value_raw = value; u32 value_raw = value;
if (size==2) if (size == 2)
value_raw &= 0xFF; value_raw &= 0xFF;
else if (size==4) else if (size == 4)
value_raw &= 0xFFFF; value_raw &= 0xFFFF;
return QStringLiteral("0x%1 (%2)").arg(static_cast<u32>(value_raw), size, 16, QChar('0')).arg(static_cast<int>(value)); return QStringLiteral("0x%1 (%2)")
.arg(static_cast<u32>(value_raw), size, 16, QChar('0'))
.arg(static_cast<int>(value));
} }
else else
return QStringLiteral("0x%1 (%2)").arg(static_cast<u32>(value), size, 16, QChar('0')).arg(static_cast<uint>(value)); return QStringLiteral("0x%1 (%2)").arg(static_cast<u32>(value), size, 16, QChar('0')).arg(static_cast<uint>(value));
@ -85,6 +87,8 @@ void CheatManagerDialog::connectUi()
connect(m_ui.cheatListActivate, &QPushButton::clicked, this, &CheatManagerDialog::activateCodeClicked); connect(m_ui.cheatListActivate, &QPushButton::clicked, this, &CheatManagerDialog::activateCodeClicked);
connect(m_ui.cheatListImport, &QPushButton::clicked, this, &CheatManagerDialog::importClicked); connect(m_ui.cheatListImport, &QPushButton::clicked, this, &CheatManagerDialog::importClicked);
connect(m_ui.cheatListExport, &QPushButton::clicked, this, &CheatManagerDialog::exportClicked); connect(m_ui.cheatListExport, &QPushButton::clicked, this, &CheatManagerDialog::exportClicked);
connect(m_ui.cheatListClear, &QPushButton::clicked, this, &CheatManagerDialog::clearClicked);
connect(m_ui.cheatListReset, &QPushButton::clicked, this, &CheatManagerDialog::resetClicked);
connect(m_ui.scanValue, &QLineEdit::textChanged, this, &CheatManagerDialog::updateScanValue); connect(m_ui.scanValue, &QLineEdit::textChanged, this, &CheatManagerDialog::updateScanValue);
connect(m_ui.scanValueBase, QOverload<int>::of(&QComboBox::currentIndexChanged), connect(m_ui.scanValueBase, QOverload<int>::of(&QComboBox::currentIndexChanged),
@ -304,6 +308,11 @@ CheatList* CheatManagerDialog::getCheatList() const
return list; return list;
} }
void CheatManagerDialog::queueUpdateCheatList()
{
QMetaObject::invokeMethod(this, &CheatManagerDialog::updateCheatList, Qt::QueuedConnection);
}
void CheatManagerDialog::updateCheatList() void CheatManagerDialog::updateCheatList()
{ {
QSignalBlocker sb(m_ui.cheatList); QSignalBlocker sb(m_ui.cheatList);
@ -647,6 +656,36 @@ void CheatManagerDialog::exportClicked()
QMessageBox::critical(this, tr("Error"), tr("Failed to save cheat file. The log may contain more information.")); QMessageBox::critical(this, tr("Error"), tr("Failed to save cheat file. The log may contain more information."));
} }
void CheatManagerDialog::clearClicked()
{
if (QMessageBox::question(this, tr("Confirm Clear"),
tr("Are you sure you want to remove all cheats? This is not reversible.")) !=
QMessageBox::Yes)
{
return;
}
QtHostInterface::GetInstance()->executeOnEmulationThread([] { QtHostInterface::GetInstance()->ClearCheatList(true); },
true);
updateCheatList();
}
void CheatManagerDialog::resetClicked()
{
if (QMessageBox::question(
this, tr("Confirm Reset"),
tr(
"Are you sure you want to reset the cheat list? Any cheats not in the DuckStation database WILL BE LOST.")) !=
QMessageBox::Yes)
{
return;
}
QtHostInterface::GetInstance()->executeOnEmulationThread([] { QtHostInterface::GetInstance()->DeleteCheatList(); },
true);
updateCheatList();
}
void CheatManagerDialog::addToWatchClicked() void CheatManagerDialog::addToWatchClicked()
{ {
const int index = getSelectedResultIndex(); const int index = getSelectedResultIndex();
@ -675,9 +714,9 @@ void CheatManagerDialog::addManualWatchAddressClicked()
if (index < 0 || !ok) if (index < 0 || !ok)
return; return;
if (index==1 || index==4) if (index == 1 || index == 4)
address.value() &= 0xFFFFFFFE; address.value() &= 0xFFFFFFFE;
else if (index==2 || index==5) else if (index == 2 || index == 5)
address.value() &= 0xFFFFFFFC; address.value() &= 0xFFFFFFFC;
m_watch.AddEntry(StringUtil::StdStringFromFormat("0x%08x", address.value()), address.value(), m_watch.AddEntry(StringUtil::StdStringFromFormat("0x%08x", address.value()), address.value(),

View file

@ -40,6 +40,8 @@ private Q_SLOTS:
void importFromFileTriggered(); void importFromFileTriggered();
void importFromTextTriggered(); void importFromTextTriggered();
void exportClicked(); void exportClicked();
void clearClicked();
void resetClicked();
void addToWatchClicked(); void addToWatchClicked();
void addManualWatchAddressClicked(); void addManualWatchAddressClicked();
@ -73,6 +75,7 @@ private:
int getSelectedCheatIndex() const; int getSelectedCheatIndex() const;
int getSelectedResultIndex() const; int getSelectedResultIndex() const;
int getSelectedWatchIndex() const; int getSelectedWatchIndex() const;
void queueUpdateCheatList();
Ui::CheatManagerDialog m_ui; Ui::CheatManagerDialog m_ui;

View file

@ -84,6 +84,20 @@
</property> </property>
</widget> </widget>
</item> </item>
<item>
<widget class="QPushButton" name="cheatListClear">
<property name="text">
<string>Clear</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="cheatListReset">
<property name="text">
<string>Reset</string>
</property>
</widget>
</item>
<item> <item>
<spacer name="horizontalSpacer"> <spacer name="horizontalSpacer">
<property name="orientation"> <property name="orientation">

View file

@ -2722,6 +2722,40 @@ bool CommonHostInterface::SaveCheatList(const char* filename)
return true; return true;
} }
bool CommonHostInterface::DeleteCheatList()
{
if (!System::IsValid())
return false;
const std::string filename(GetCheatFileName());
if (!filename.empty())
{
if (!FileSystem::DeleteFile(filename.c_str()))
return false;
AddFormattedOSDMessage(5.0f, TranslateString("OSDMessage", "Deleted cheat list '%s'."), filename.c_str());
}
System::SetCheatList(nullptr);
return true;
}
void CommonHostInterface::ClearCheatList(bool save_to_file)
{
if (!System::IsValid())
return;
CheatList* cl = System::GetCheatList();
if (!cl)
return;
while (cl->GetCodeCount() > 0)
cl->RemoveCode(cl->GetCodeCount() - 1);
if (save_to_file)
SaveCheatList();
}
void CommonHostInterface::SetCheatCodeState(u32 index, bool enabled, bool save_to_file) void CommonHostInterface::SetCheatCodeState(u32 index, bool enabled, bool save_to_file)
{ {
if (!System::IsValid() || !System::HasCheatList()) if (!System::IsValid() || !System::HasCheatList())

View file

@ -220,6 +220,12 @@ public:
/// Saves the current cheat list to the specified file. /// Saves the current cheat list to the specified file.
bool SaveCheatList(const char* filename); bool SaveCheatList(const char* filename);
/// Deletes the cheat list, if present.
bool DeleteCheatList();
/// Removes all cheats from the cheat list.
void ClearCheatList(bool save_to_file);
/// Enables/disabled the specified cheat code. /// Enables/disabled the specified cheat code.
void SetCheatCodeState(u32 index, bool enabled, bool save_to_file); void SetCheatCodeState(u32 index, bool enabled, bool save_to_file);