From 44e2a857faf4cfccccfb4bc91db49700d5e4a717 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Sun, 8 Nov 2020 00:22:04 +1000 Subject: [PATCH] Qt/CheatManager: Add import file/text options --- src/duckstation-qt/cheatmanagerdialog.cpp | 32 +++++++++++++++++++++++ src/duckstation-qt/cheatmanagerdialog.h | 2 ++ 2 files changed, 34 insertions(+) diff --git a/src/duckstation-qt/cheatmanagerdialog.cpp b/src/duckstation-qt/cheatmanagerdialog.cpp index ce20a4b3c..3392b3860 100644 --- a/src/duckstation-qt/cheatmanagerdialog.cpp +++ b/src/duckstation-qt/cheatmanagerdialog.cpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include #include @@ -542,6 +543,14 @@ void CheatManagerDialog::activateCodeClicked() } void CheatManagerDialog::importClicked() +{ + QMenu menu(this); + connect(menu.addAction(tr("From File...")), &QAction::triggered, this, &CheatManagerDialog::importFromFileTriggered); + connect(menu.addAction(tr("From Text...")), &QAction::triggered, this, &CheatManagerDialog::importFromTextTriggered); + menu.exec(QCursor::pos()); +} + +void CheatManagerDialog::importFromFileTriggered() { const QString filter(tr("PCSXR/Libretro Cheat Files (*.cht *.txt);;All Files (*.*)")); const QString filename(QFileDialog::getOpenFileName(this, tr("Import Cheats"), QString(), filter)); @@ -565,6 +574,29 @@ void CheatManagerDialog::importClicked() updateCheatList(); } +void CheatManagerDialog::importFromTextTriggered() +{ + const QString text = QInputDialog::getMultiLineText(this, tr("Import Cheats"), tr("Cheat File Text:")); + if (text.isEmpty()) + return; + + CheatList new_cheats; + if (!new_cheats.LoadFromString(text.toStdString(), CheatList::Format::Autodetect)) + { + QMessageBox::critical(this, tr("Error"), tr("Failed to parse cheat file. The log may contain more information.")); + return; + } + + QtHostInterface::GetInstance()->executeOnEmulationThread( + [&new_cheats]() { + DebugAssert(System::HasCheatList()); + System::GetCheatList()->MergeList(new_cheats); + QtHostInterface::GetInstance()->SaveCheatList(); + }, + true); + updateCheatList(); +} + void CheatManagerDialog::exportClicked() { const QString filter(tr("PCSXR Cheat Files (*.cht);;All Files (*.*)")); diff --git a/src/duckstation-qt/cheatmanagerdialog.h b/src/duckstation-qt/cheatmanagerdialog.h index 51865d6ee..4826f7d3a 100644 --- a/src/duckstation-qt/cheatmanagerdialog.h +++ b/src/duckstation-qt/cheatmanagerdialog.h @@ -37,6 +37,8 @@ private Q_SLOTS: void deleteCodeClicked(); void activateCodeClicked(); void importClicked(); + void importFromFileTriggered(); + void importFromTextTriggered(); void exportClicked(); void addToWatchClicked();