dolphin/Source/Core/DolphinQt/CheatsManager.cpp

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

140 lines
4.2 KiB
C++
Raw Normal View History

2018-03-26 02:17:47 +00:00
// Copyright 2018 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later
2018-03-26 02:17:47 +00:00
2018-07-06 22:40:15 +00:00
#include "DolphinQt/CheatsManager.h"
2018-03-26 02:17:47 +00:00
2021-08-22 05:13:00 +00:00
#include <functional>
2018-03-26 02:17:47 +00:00
#include <QDialogButtonBox>
#include <QVBoxLayout>
#include "Core/ActionReplay.h"
2021-08-22 05:13:00 +00:00
#include "Core/CheatSearch.h"
2018-03-26 02:17:47 +00:00
#include "Core/ConfigManager.h"
#include "Core/Core.h"
2018-05-28 01:48:04 +00:00
2018-03-26 02:17:47 +00:00
#include "UICommon/GameFile.h"
2021-08-22 05:13:00 +00:00
#include "DolphinQt/CheatSearchFactoryWidget.h"
#include "DolphinQt/CheatSearchWidget.h"
2018-07-06 22:40:15 +00:00
#include "DolphinQt/Config/ARCodeWidget.h"
#include "DolphinQt/Config/GeckoCodeWidget.h"
#include "DolphinQt/Settings.h"
2021-08-22 05:13:00 +00:00
#include "QtUtils/PartiallyClosableTabWidget.h"
CheatsManager::CheatsManager(QWidget* parent) : QDialog(parent)
2018-03-26 02:17:47 +00:00
{
setWindowTitle(tr("Cheats Manager"));
2018-05-05 00:29:16 +00:00
setWindowFlags(windowFlags() & ~Qt::WindowContextHelpButtonHint);
2018-03-26 02:17:47 +00:00
connect(&Settings::Instance(), &Settings::EmulationStateChanged, this,
&CheatsManager::OnStateChanged);
CreateWidgets();
ConnectWidgets();
RefreshCodeTabs(Core::GetState(), true);
auto& settings = Settings::GetQSettings();
restoreGeometry(settings.value(QStringLiteral("cheatsmanager/geometry")).toByteArray());
2018-03-26 02:17:47 +00:00
}
CheatsManager::~CheatsManager()
{
auto& settings = Settings::GetQSettings();
settings.setValue(QStringLiteral("cheatsmanager/geometry"), saveGeometry());
}
2018-03-26 02:17:47 +00:00
void CheatsManager::OnStateChanged(Core::State state)
{
RefreshCodeTabs(state, false);
}
void CheatsManager::RefreshCodeTabs(Core::State state, bool force)
{
if (!force && (state == Core::State::Starting || state == Core::State::Stopping))
2018-03-26 02:17:47 +00:00
return;
const auto& game_id =
state != Core::State::Uninitialized ? SConfig::GetInstance().GetGameID() : std::string();
const auto& game_tdb_id = SConfig::GetInstance().GetGameTDBID();
2021-08-22 05:13:00 +00:00
const u16 revision = SConfig::GetInstance().GetRevision();
if (!force && m_game_id == game_id && m_game_tdb_id == game_tdb_id && m_revision == revision)
return;
m_game_id = game_id;
m_game_tdb_id = game_tdb_id;
m_revision = revision;
2021-08-22 05:13:00 +00:00
if (m_ar_code)
2018-03-26 02:17:47 +00:00
{
2021-08-22 05:13:00 +00:00
const int tab_index = m_tab_widget->indexOf(m_ar_code);
if (tab_index != -1)
m_tab_widget->removeTab(tab_index);
m_ar_code->deleteLater();
m_ar_code = nullptr;
}
2018-03-26 02:17:47 +00:00
2021-08-22 05:13:00 +00:00
if (m_gecko_code)
{
2021-08-22 05:13:00 +00:00
const int tab_index = m_tab_widget->indexOf(m_gecko_code);
if (tab_index != -1)
m_tab_widget->removeTab(tab_index);
m_gecko_code->deleteLater();
m_gecko_code = nullptr;
2018-03-26 02:17:47 +00:00
}
2021-08-22 05:13:00 +00:00
m_ar_code = new ARCodeWidget(m_game_id, m_revision, false);
m_gecko_code = new GeckoCodeWidget(m_game_id, m_game_tdb_id, m_revision, false);
m_tab_widget->insertTab(0, m_ar_code, tr("AR Code"));
m_tab_widget->insertTab(1, m_gecko_code, tr("Gecko Codes"));
m_tab_widget->setTabUnclosable(0);
m_tab_widget->setTabUnclosable(1);
connect(m_ar_code, &ARCodeWidget::OpenGeneralSettings, this, &CheatsManager::OpenGeneralSettings);
connect(m_gecko_code, &GeckoCodeWidget::OpenGeneralSettings, this,
&CheatsManager::OpenGeneralSettings);
2018-03-26 02:17:47 +00:00
}
void CheatsManager::CreateWidgets()
{
2021-08-22 05:13:00 +00:00
m_tab_widget = new PartiallyClosableTabWidget;
2018-03-26 02:17:47 +00:00
m_button_box = new QDialogButtonBox(QDialogButtonBox::Close);
2021-08-22 05:13:00 +00:00
m_cheat_search_new = new CheatSearchFactoryWidget();
m_tab_widget->addTab(m_cheat_search_new, tr("Start New Cheat Search"));
m_tab_widget->setTabUnclosable(0);
2018-03-26 02:17:47 +00:00
auto* layout = new QVBoxLayout;
layout->addWidget(m_tab_widget);
layout->addWidget(m_button_box);
setLayout(layout);
}
2021-08-22 05:13:00 +00:00
void CheatsManager::OnNewSessionCreated(const Cheats::CheatSearchSessionBase& session)
2018-03-26 02:17:47 +00:00
{
2021-08-22 05:13:00 +00:00
auto* w = new CheatSearchWidget(session.Clone());
const int tab_index = m_tab_widget->addTab(w, tr("Cheat Search"));
w->connect(w, &CheatSearchWidget::ActionReplayCodeGenerated, this,
[this](const ActionReplay::ARCode& ar_code) {
if (m_ar_code)
m_ar_code->AddCode(ar_code);
});
m_tab_widget->setCurrentIndex(tab_index);
2018-03-26 02:17:47 +00:00
}
2021-08-22 05:13:00 +00:00
void CheatsManager::OnTabCloseRequested(int index)
2018-03-26 02:17:47 +00:00
{
2021-08-22 05:13:00 +00:00
m_tab_widget->removeTab(index);
2018-03-26 02:17:47 +00:00
}
2021-08-22 05:13:00 +00:00
void CheatsManager::ConnectWidgets()
2018-03-26 02:17:47 +00:00
{
2021-08-22 05:13:00 +00:00
connect(m_button_box, &QDialogButtonBox::rejected, this, &QDialog::reject);
connect(m_cheat_search_new, &CheatSearchFactoryWidget::NewSessionCreated, this,
&CheatsManager::OnNewSessionCreated);
connect(m_tab_widget, &QTabWidget::tabCloseRequested, this, &CheatsManager::OnTabCloseRequested);
2018-03-26 02:17:47 +00:00
}