Qt/Properties: Implement "Gecko codes" tab

This commit is contained in:
spycrab 2017-09-10 19:10:45 +02:00
parent 5aecd61ede
commit f90e81b9db
12 changed files with 302 additions and 7 deletions

View File

@ -31,8 +31,10 @@ set(SRCS
Translation.cpp
WiiUpdate.cpp
WiiUpdate.h
Config/CheatWarningWidget.cpp
Config/ControllersWindow.cpp
Config/FilesystemWidget.cpp
Config/GeckoCodeWidget.cpp
Config/Graphics/AdvancedWidget.cpp
Config/Graphics/EnhancementsWidget.cpp
Config/Graphics/GeneralWidget.cpp

View File

@ -10,6 +10,8 @@
#include <QPushButton>
#include <QStyle>
#include <iostream>
#include "Core/ConfigManager.h"
#include "Core/Core.h"
#include "DolphinQt2/Settings.h"
@ -21,8 +23,10 @@ CheatWarningWidget::CheatWarningWidget(const std::string& game_id) : m_game_id(g
connect(&Settings::Instance(), &Settings::EnableCheatsChanged,
[this] { Update(Core::IsRunning()); });
connect(this, &CheatWarningWidget::EmulationStarted, [this] { Update(true); });
connect(this, &CheatWarningWidget::EmulationStopped, [this] { Update(false); });
connect(&Settings::Instance(), &Settings::EmulationStateChanged, [this](Core::State state) {
std::cout << (state == Core::State::Running) << std::endl;
Update(state == Core::State::Running);
});
Update(Core::IsRunning());
}

View File

@ -19,8 +19,6 @@ public:
signals:
void OpenCheatEnableSettings();
void EmulationStarted();
void EmulationStopped();
private:
void CreateWidgets();

View File

@ -0,0 +1,207 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include "DolphinQt2/Config/GeckoCodeWidget.h"
#include <QFontDatabase>
#include <QFormLayout>
#include <QLabel>
#include <QListWidget>
#include <QMessageBox>
#include <QPushButton>
#include <QTextEdit>
#include <QVBoxLayout>
#include "Common/FileUtil.h"
#include "Common/IniFile.h"
#include "Core/ConfigManager.h"
#include "Core/GeckoCodeConfig.h"
#include "DolphinQt2/Config/CheatWarningWidget.h"
#include "DolphinQt2/GameList/GameFile.h"
GeckoCodeWidget::GeckoCodeWidget(const GameFile& game)
: m_game(game), m_game_id(game.GetGameID().toStdString()), m_game_revision(game.GetRevision())
{
CreateWidgets();
ConnectWidgets();
IniFile game_ini_local;
// We don't use LoadLocalGameIni() here because user cheat codes that are installed via the UI
// will always be stored in GS/${GAMEID}.ini
game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini");
IniFile game_ini_default = SConfig::GetInstance().LoadDefaultGameIni(m_game_id, m_game_revision);
m_gecko_codes = Gecko::LoadCodes(game_ini_default, game_ini_local);
UpdateList();
}
void GeckoCodeWidget::CreateWidgets()
{
m_warning = new CheatWarningWidget(m_game_id);
m_code_list = new QListWidget;
m_name_label = new QLabel;
m_creator_label = new QLabel;
QFont monospace(QFontDatabase::systemFont(QFontDatabase::FixedFont).family());
const auto line_height = QFontMetrics(font()).lineSpacing();
m_code_description = new QTextEdit;
m_code_description->setFont(monospace);
m_code_description->setReadOnly(true);
m_code_description->setFixedHeight(line_height * 5);
m_code_view = new QTextEdit;
m_code_view->setFont(monospace);
m_code_view->setReadOnly(true);
m_code_view->setFixedHeight(line_height * 10);
m_download_codes = new QPushButton(tr("Download Codes (WiiRD Database)"));
m_download_codes->setEnabled(!m_game_id.empty());
m_download_codes->setSizePolicy(QSizePolicy::Fixed, QSizePolicy::Fixed);
auto* layout = new QVBoxLayout;
layout->addWidget(m_warning);
layout->addWidget(m_code_list);
auto* info_layout = new QFormLayout;
info_layout->addRow(tr("Name:"), m_name_label);
info_layout->addRow(tr("Creator:"), m_creator_label);
info_layout->addRow(tr("Description:"), static_cast<QWidget*>(nullptr));
info_layout->setFormAlignment(Qt::AlignLeft | Qt::AlignTop);
for (QLabel* label : {m_name_label, m_creator_label})
{
label->setTextInteractionFlags(Qt::TextSelectableByMouse);
label->setCursor(Qt::IBeamCursor);
}
layout->addLayout(info_layout);
layout->addWidget(m_code_description);
layout->addWidget(m_code_view);
layout->addWidget(m_download_codes, 0, Qt::AlignRight);
setLayout(layout);
}
void GeckoCodeWidget::ConnectWidgets()
{
connect(m_code_list, &QListWidget::itemSelectionChanged, this,
&GeckoCodeWidget::OnSelectionChanged);
connect(m_code_list, &QListWidget::itemChanged, this, &GeckoCodeWidget::OnItemChanged);
connect(m_download_codes, &QPushButton::pressed, this, &GeckoCodeWidget::DownloadCodes);
connect(m_warning, &CheatWarningWidget::OpenCheatEnableSettings, this,
&GeckoCodeWidget::OpenGeneralSettings);
}
void GeckoCodeWidget::OnSelectionChanged()
{
auto items = m_code_list->selectedItems();
if (items.empty())
return;
auto selected = items[0];
const auto& code = m_gecko_codes[m_code_list->row(selected)];
m_name_label->setText(QString::fromStdString(code.name));
m_creator_label->setText(QString::fromStdString(code.creator));
m_code_description->clear();
if (code.notes.empty())
m_code_description->append(tr("N/A"));
for (const auto& line : code.notes)
m_code_description->append(QString::fromStdString(line));
m_code_view->clear();
for (const auto& c : code.codes)
m_code_view->append(QStringLiteral("%1 %2")
.arg(c.address, 8, 16, QLatin1Char('0'))
.arg(c.data, 8, 16, QLatin1Char('0')));
}
void GeckoCodeWidget::OnItemChanged(QListWidgetItem* item)
{
m_gecko_codes[m_code_list->row(item)].enabled = (item->checkState() == Qt::Checked);
SaveCodes();
}
void GeckoCodeWidget::SaveCodes()
{
IniFile game_ini_local;
game_ini_local.Load(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini");
Gecko::SaveCodes(game_ini_local, m_gecko_codes);
game_ini_local.Save(File::GetUserPath(D_GAMESETTINGS_IDX) + m_game_id + ".ini");
}
void GeckoCodeWidget::UpdateList()
{
m_code_list->clear();
for (size_t i = 0; i < m_gecko_codes.size(); i++)
{
const auto& code = m_gecko_codes[i];
auto* item = new QListWidgetItem(QString::fromStdString(code.name)
.replace(QStringLiteral("&lt;"), QStringLiteral("<"))
.replace(QStringLiteral("&gt;"), QStringLiteral(">")));
item->setFlags(Qt::ItemIsEnabled | Qt::ItemIsSelectable | Qt::ItemIsUserCheckable);
item->setCheckState(code.enabled ? Qt::Checked : Qt::Unchecked);
m_code_list->addItem(item);
}
}
void GeckoCodeWidget::DownloadCodes()
{
bool success;
std::vector<Gecko::GeckoCode> codes = Gecko::DownloadCodes(m_game_id, &success);
if (!success)
{
QMessageBox::critical(this, tr("Error"), tr("Failed to download codes."));
return;
}
if (codes.empty())
{
QMessageBox::critical(this, tr("Error"), tr("File contained no codes."));
return;
}
size_t added_count = 0;
for (const auto& code : codes)
{
auto it = std::find(m_gecko_codes.begin(), m_gecko_codes.end(), code);
if (it == m_gecko_codes.end())
{
m_gecko_codes.push_back(code);
added_count++;
}
}
UpdateList();
SaveCodes();
QMessageBox::information(this, tr("Download complete"),
tr("Downloaded %1 codes. (added %2)")
.arg(QString::number(codes.size()), QString::number(added_count)));
}

View File

@ -0,0 +1,54 @@
// Copyright 2017 Dolphin Emulator Project
// Licensed under GPLv2+
// Refer to the license.txt file included.
#pragma once
#include <QWidget>
#include <string>
#include <vector>
#include "Common/CommonTypes.h"
#include "Core/GeckoCode.h"
class CheatWarningWidget;
class GameFile;
class QLabel;
class QListWidget;
class QListWidgetItem;
class QTextEdit;
class QPushButton;
class GeckoCodeWidget : public QWidget
{
Q_OBJECT
public:
explicit GeckoCodeWidget(const GameFile& game);
signals:
void OpenGeneralSettings();
private:
void OnSelectionChanged();
void OnItemChanged(QListWidgetItem* item);
void CreateWidgets();
void ConnectWidgets();
void UpdateList();
void DownloadCodes();
void SaveCodes();
const GameFile& m_game;
std::string m_game_id;
u8 m_game_revision;
CheatWarningWidget* m_warning;
QListWidget* m_code_list;
QLabel* m_name_label;
QLabel* m_creator_label;
QTextEdit* m_code_description;
QTextEdit* m_code_view;
QPushButton* m_download_codes;
std::vector<Gecko::GeckoCode> m_gecko_codes;
};

View File

@ -7,6 +7,7 @@
#include <QVBoxLayout>
#include "DolphinQt2/Config/FilesystemWidget.h"
#include "DolphinQt2/Config/GeckoCodeWidget.h"
#include "DolphinQt2/Config/InfoWidget.h"
#include "DolphinQt2/Config/PropertiesDialog.h"
@ -18,7 +19,14 @@ PropertiesDialog::PropertiesDialog(QWidget* parent, const GameFile& game) : QDia
QTabWidget* tab_widget = new QTabWidget(this);
InfoWidget* info = new InfoWidget(game);
GeckoCodeWidget* gecko = new GeckoCodeWidget(game);
connect(gecko, &GeckoCodeWidget::OpenGeneralSettings, this,
&PropertiesDialog::OpenGeneralSettings);
tab_widget->addTab(info, tr("Info"));
tab_widget->addTab(gecko, tr("Gecko Codes"));
if (DiscIO::IsDisc(game.GetPlatformID()))
{

View File

@ -13,4 +13,7 @@ class PropertiesDialog final : public QDialog
Q_OBJECT
public:
explicit PropertiesDialog(QWidget* parent, const GameFile& game);
signals:
void OpenGeneralSettings();
};

View File

@ -60,8 +60,10 @@
<!--NOTE: When adding moc'd files, you must list outputs in the ClCompile ItemGroup too!-->
<ItemGroup>
<QtMoc Include="AboutDialog.h" />
<QtMoc Include="Config\CheatWarningWidget.h" />
<QtMoc Include="Config\ControllersWindow.h" />
<QtMoc Include="Config\FilesystemWidget.h" />
<QtMoc Include="Config\GeckoCodeWidget.h" />
<QtMoc Include="Config\Mapping\IOWindow.h" />
<QtMoc Include="Config\Mapping\MappingButton.h" />
<QtMoc Include="Config\Mapping\MappingWidget.h" />
@ -111,14 +113,16 @@
<ClCompile Include="$(QtMocOutPrefix)AboutDialog.cpp" />
<ClCompile Include="$(QtMocOutPrefix)AudioPane.cpp" />
<ClCompile Include="$(QtMocOutPrefix)AdvancedWidget.cpp" />
<ClCompile Include="$(QtMocOutPrefix)EnhancementsWidget.cpp" />
<ClCompile Include="$(QtMocOutPrefix)CheatWarningWidget.cpp" />
<ClCompile Include="$(QtMocOutPrefix)ControllersWindow.cpp" />
<ClCompile Include="$(QtMocOutPrefix)EnhancementsWidget.cpp" />
<ClCompile Include="$(QtMocOutPrefix)FilesystemWidget.cpp" />
<ClCompile Include="$(QtMocOutPrefix)WindowActivationEventFilter.cpp" />
<ClCompile Include="$(QtMocOutPrefix)GameList.cpp" />
<ClCompile Include="$(QtMocOutPrefix)GameListDialog.cpp" />
<ClCompile Include="$(QtMocOutPrefix)GameListModel.cpp" />
<ClCompile Include="$(QtMocOutPrefix)GameTracker.cpp" />
<ClCompile Include="$(QtMocOutPrefix)GeckoCodeWidget.cpp" />
<ClCompile Include="$(QtMocOutPrefix)GeneralPane.cpp" />
<ClCompile Include="$(QtMocOutPrefix)GeneralWidget.cpp" />
<ClCompile Include="$(QtMocOutPrefix)GraphicsBool.cpp" />
@ -154,8 +158,10 @@
<ClCompile Include="$(QtMocOutPrefix)SoftwareRendererWidget.cpp" />
<ClCompile Include="$(QtMocOutPrefix)ToolBar.cpp" />
<ClCompile Include="AboutDialog.cpp" />
<ClCompile Include="Config\CheatWarningWidget.cpp" />
<ClCompile Include="Config\ControllersWindow.cpp" />
<ClCompile Include="Config\FilesystemWidget.cpp" />
<ClCompile Include="Config\GeckoCodeWidget.cpp" />
<ClCompile Include="Config\Graphics\AdvancedWidget.cpp" />
<ClCompile Include="Config\Graphics\EnhancementsWidget.cpp" />
<ClCompile Include="Config\Graphics\HacksWidget.cpp" />
@ -226,8 +232,8 @@
</ItemGroup>
<!--Put standard C/C++ headers here-->
<ItemGroup>
<ClInclude Include="Config\LogWidget.h" />
<ClInclude Include="Config\LogConfigWidget.h" />
<ClInclude Include="Config\CheatWarningWidget.h" />
<ClInclude Include="Config\GeckoCodeWidget.h" />
<ClInclude Include="Config\Mapping\GCKeyboardEmu.h" />
<ClInclude Include="Config\Mapping\GCPadEmu.h" />
<ClInclude Include="Config\Mapping\GCPadWiiU.h" />

View File

@ -239,6 +239,9 @@ void GameList::ShowContextMenu(const QPoint&)
void GameList::OpenProperties()
{
PropertiesDialog* properties = new PropertiesDialog(this, *GetSelectedGame());
connect(properties, &PropertiesDialog::OpenGeneralSettings, this, &GameList::OpenGeneralSettings);
properties->show();
}

View File

@ -31,6 +31,7 @@ signals:
void GameSelected();
void NetPlayHost(const QString& game_id);
void SelectionChanged(QSharedPointer<GameFile> game_file);
void OpenGeneralSettings();
private:
void ShowContextMenu(const QPoint&);

View File

@ -279,6 +279,8 @@ void MainWindow::ConnectGameList()
{
connect(m_game_list, &GameList::GameSelected, this, &MainWindow::Play);
connect(m_game_list, &GameList::NetPlayHost, this, &MainWindow::NetPlayHost);
connect(m_game_list, &GameList::OpenGeneralSettings, this, &MainWindow::ShowGeneralWindow);
}
void MainWindow::ConnectRenderWidget()
@ -551,6 +553,12 @@ void MainWindow::ShowAudioWindow()
ShowSettingsWindow();
}
void MainWindow::ShowGeneralWindow()
{
m_settings_window->SelectGeneralPane();
ShowSettingsWindow();
}
void MainWindow::ShowAboutDialog()
{
AboutDialog* about = new AboutDialog(this);

View File

@ -91,6 +91,7 @@ private:
void HideRenderWidget();
void ShowSettingsWindow();
void ShowGeneralWindow();
void ShowAudioWindow();
void ShowControllersWindow();
void ShowGraphicsWindow();