Qt: Hook up console settings

This commit is contained in:
Connor McLaughlin 2020-01-01 14:01:58 +10:00
parent 62e1829ec9
commit 77a47c87d2
10 changed files with 202 additions and 26 deletions

View file

@ -1,8 +1,17 @@
#include "consolesettingswidget.h" #include "consolesettingswidget.h"
#include "settingwidgetbinder.h"
ConsoleSettingsWidget::ConsoleSettingsWidget(QWidget* parent /*= nullptr*/) : QWidget(parent) ConsoleSettingsWidget::ConsoleSettingsWidget(QtHostInterface* host_interface, QWidget* parent /* = nullptr */)
: QWidget(parent), m_host_interface(host_interface)
{ {
m_ui.setupUi(this); m_ui.setupUi(this);
SettingWidgetBinder::BindWidgetToSetting(m_host_interface, m_ui.region, &Settings::region);
SettingWidgetBinder::BindWidgetToSetting(m_host_interface, m_ui.biosPath, &Settings::bios_path);
SettingWidgetBinder::BindWidgetToSetting(m_host_interface, m_ui.enableTTYOutput, &Settings::bios_patch_tty_enable);
SettingWidgetBinder::BindWidgetToSetting(m_host_interface, m_ui.fastBoot, &Settings::bios_patch_fast_boot);
SettingWidgetBinder::BindWidgetToSetting(m_host_interface, m_ui.enableSpeedLimiter, &Settings::speed_limiter_enabled);
SettingWidgetBinder::BindWidgetToSetting(m_host_interface, m_ui.pauseOnStart, &Settings::start_paused);
} }
ConsoleSettingsWidget::~ConsoleSettingsWidget() = default; ConsoleSettingsWidget::~ConsoleSettingsWidget() = default;

View file

@ -4,14 +4,18 @@
#include "ui_consolesettingswidget.h" #include "ui_consolesettingswidget.h"
class QtHostInterface;
class ConsoleSettingsWidget : public QWidget class ConsoleSettingsWidget : public QWidget
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit ConsoleSettingsWidget(QWidget* parent = nullptr); explicit ConsoleSettingsWidget(QtHostInterface* host_interface, QWidget* parent = nullptr);
~ConsoleSettingsWidget(); ~ConsoleSettingsWidget();
private: private:
Ui::ConsoleSettingsWidget m_ui; Ui::ConsoleSettingsWidget m_ui;
QtHostInterface* m_host_interface;
}; };

View file

@ -40,13 +40,13 @@
</property> </property>
</item> </item>
<item> <item>
<property name="text"> <property name="text">
<string>NTSC-U (US)</string> <string>NTSC-J (Japan)</string>
</property> </property>
</item> </item>
<item> <item>
<property name="text"> <property name="text">
<string>NTSC-J (Japan)</string> <string>NTSC-U (US)</string>
</property> </property>
</item> </item>
<item> <item>

View file

@ -47,6 +47,7 @@
<ClCompile Include="settingsdialog.cpp" /> <ClCompile Include="settingsdialog.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="settingwidgetbinder.h" />
<QtMoc Include="consolesettingswidget.h" /> <QtMoc Include="consolesettingswidget.h" />
<QtMoc Include="gamelistsettingswidget.h" /> <QtMoc Include="gamelistsettingswidget.h" />
<QtMoc Include="gamelistwidget.h" /> <QtMoc Include="gamelistwidget.h" />

View file

@ -11,30 +11,41 @@
<ClCompile Include="gamelistsettingswidget.cpp" /> <ClCompile Include="gamelistsettingswidget.cpp" />
<ClCompile Include="qtsettingsinterface.cpp" /> <ClCompile Include="qtsettingsinterface.cpp" />
<ClCompile Include="qtutils.cpp" /> <ClCompile Include="qtutils.cpp" />
<ClCompile Include="$(IntDir)moc_consolesettingswidget.cpp" />
<ClCompile Include="$(IntDir)moc_gamelistsettingswidget.cpp" />
<ClCompile Include="$(IntDir)moc_gamelistwidget.cpp" />
<ClCompile Include="$(IntDir)moc_mainwindow.cpp" />
<ClCompile Include="$(IntDir)moc_opengldisplaywindow.cpp" />
<ClCompile Include="$(IntDir)moc_qthostinterface.cpp" />
<ClCompile Include="$(IntDir)moc_settingsdialog.cpp" />
<ClCompile Include="$(IntDir)qrc_icons.cpp" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="mainwindow.h" />
<ClInclude Include="gamelistwidget.h" />
<ClInclude Include="settingsdialog.h" />
<ClInclude Include="consolesettingswidget.h" />
<ClInclude Include="opengldisplaywindow.h" />
<ClInclude Include="qthostinterface.h" />
<ClInclude Include="gamelistsettingswidget.h" />
<ClInclude Include="qtsettingsinterface.h" /> <ClInclude Include="qtsettingsinterface.h" />
<ClInclude Include="qtutils.h" /> <ClInclude Include="qtutils.h" />
</ItemGroup> <ClInclude Include="settingwidgetbinder.h" />
<ItemGroup>
<None Include="consolesettingswidget.ui" />
<None Include="gamelistsettingswidget.ui" />
<None Include="mainwindow.ui" />
<None Include="settingsdialog.ui" />
<None Include="resources\icons.qrc">
<Filter>resources</Filter>
</None>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Filter Include="resources"> <Filter Include="resources">
<UniqueIdentifier>{3b2587ae-ce3b-4eb5-ada2-237e853620cf}</UniqueIdentifier> <UniqueIdentifier>{3b2587ae-ce3b-4eb5-ada2-237e853620cf}</UniqueIdentifier>
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup>
<QtResource Include="resources\icons.qrc" />
</ItemGroup>
<ItemGroup>
<QtMoc Include="consolesettingswidget.h" />
<QtMoc Include="gamelistsettingswidget.h" />
<QtMoc Include="gamelistwidget.h" />
<QtMoc Include="mainwindow.h" />
<QtMoc Include="opengldisplaywindow.h" />
<QtMoc Include="qthostinterface.h" />
<QtMoc Include="settingsdialog.h" />
</ItemGroup>
<ItemGroup>
<QtUi Include="consolesettingswidget.ui" />
<QtUi Include="gamelistsettingswidget.ui" />
<QtUi Include="mainwindow.ui" />
<QtUi Include="settingsdialog.ui" />
</ItemGroup>
</Project> </Project>

View file

@ -36,10 +36,15 @@ void QtHostInterface::ReportMessage(const char* message)
void QtHostInterface::setDefaultSettings() void QtHostInterface::setDefaultSettings()
{ {
QtSettingsInterface si(m_qsettings);
m_settings.SetDefaults(); m_settings.SetDefaults();
updateQSettings();
}
void QtHostInterface::updateQSettings()
{
QtSettingsInterface si(m_qsettings);
m_settings.Save(si); m_settings.Save(si);
m_qsettings.sync(); // m_qsettings.sync();
} }
void QtHostInterface::applySettings() void QtHostInterface::applySettings()
@ -69,6 +74,8 @@ void QtHostInterface::checkSettings()
m_qsettings.setValue(settings_version_key, expected_version); m_qsettings.setValue(settings_version_key, expected_version);
setDefaultSettings(); setDefaultSettings();
} }
applySettings();
} }
void QtHostInterface::createGameList() void QtHostInterface::createGameList()

View file

@ -25,8 +25,13 @@ public:
const QSettings& getQSettings() const { return m_qsettings; } const QSettings& getQSettings() const { return m_qsettings; }
QSettings& getQSettings() { return m_qsettings; } QSettings& getQSettings() { return m_qsettings; }
void setDefaultSettings(); void setDefaultSettings();
void updateQSettings();
void applySettings(); void applySettings();
const Settings& GetCoreSettings() const { return m_settings; }
Settings& GetCoreSettings() { return m_settings; }
//void UpdateCoreSettingsGPU();
const GameList* getGameList() const { return m_game_list.get(); } const GameList* getGameList() const { return m_game_list.get(); }
GameList* getGameList() { return m_game_list.get(); } GameList* getGameList() { return m_game_list.get(); }
void updateGameListDatabase(bool refresh_list = true); void updateGameListDatabase(bool refresh_list = true);

View file

@ -9,7 +9,7 @@ SettingsDialog::SettingsDialog(QtHostInterface* host_interface, QWidget* parent
{ {
m_ui.setupUi(this); m_ui.setupUi(this);
m_console_settings = new ConsoleSettingsWidget(m_ui.settingsContainer); m_console_settings = new ConsoleSettingsWidget(host_interface, m_ui.settingsContainer);
m_game_list_settings = new GameListSettingsWidget(host_interface, m_ui.settingsContainer); m_game_list_settings = new GameListSettingsWidget(host_interface, m_ui.settingsContainer);
m_cpu_settings = new QWidget(m_ui.settingsContainer); m_cpu_settings = new QWidget(m_ui.settingsContainer);
m_gpu_settings = new QWidget(m_ui.settingsContainer); m_gpu_settings = new QWidget(m_ui.settingsContainer);

View file

@ -102,7 +102,7 @@
<enum>Qt::Horizontal</enum> <enum>Qt::Horizontal</enum>
</property> </property>
<property name="standardButtons"> <property name="standardButtons">
<set>QDialogButtonBox::Apply|QDialogButtonBox::Cancel|QDialogButtonBox::Ok|QDialogButtonBox::RestoreDefaults</set> <set>QDialogButtonBox::Close|QDialogButtonBox::RestoreDefaults</set>
</property> </property>
</widget> </widget>
</item> </item>

View file

@ -0,0 +1,139 @@
#pragma once
#include <type_traits>
#include "core/settings.h"
#include "qthostinterface.h"
#include <QtWidgets/QCheckBox>
#include <QtWidgets/QComboBox>
#include <QtWidgets/QLineEdit>
namespace SettingWidgetBinder {
template<typename T>
struct SettingAccessor
{
static bool getBoolValue(const T* widget);
static void setBoolValue(T* widget, bool value);
static int getIntValue(const T* widget);
static void setIntValue(T* widget, int value);
static QString getStringValue(const T* widget);
static void setStringValue(T* widget, const QString& value);
template<typename F>
static void connectValueChanged(T* widget, F func);
};
template<>
struct SettingAccessor<QLineEdit>
{
static bool getBoolValue(const QLineEdit* widget) { return widget->text().toInt() != 0; }
static void setBoolValue(QLineEdit* widget, bool value)
{
widget->setText(value ? QStringLiteral("1") : QStringLiteral("0"));
}
static int getIntValue(const QLineEdit* widget) { return widget->text().toInt(); }
static void setIntValue(QLineEdit* widget, int value) { widget->setText(QStringLiteral("%1").arg(value)); }
static QString getStringValue(const QLineEdit* widget) { return widget->text(); }
static void setStringValue(QLineEdit* widget, const QString& value) { widget->setText(value); }
template<typename F>
static void connectValueChanged(QLineEdit* widget, F func)
{
widget->connect(widget, &QLineEdit::textChanged, func);
}
};
template<>
struct SettingAccessor<QComboBox>
{
static bool getBoolValue(const QComboBox* widget) { return widget->currentText() > 0; }
static void setBoolValue(QComboBox* widget, bool value) { widget->setCurrentIndex(value ? 1 : 0); }
static int getIntValue(const QComboBox* widget) { return widget->currentIndex(); }
static void setIntValue(QComboBox* widget, int value) { widget->setCurrentIndex(value); }
static QString getStringValue(const QComboBox* widget) { return widget->currentText(); }
static void setStringValue(QComboBox* widget, const QString& value) { widget->setCurrentText(value); }
template<typename F>
static void connectValueChanged(QComboBox* widget, F func)
{
widget->connect(widget, static_cast<void(QComboBox::*)(int)>(&QComboBox::currentIndexChanged), func);
}
};
template<>
struct SettingAccessor<QCheckBox>
{
static bool getBoolValue(const QCheckBox* widget) { return widget->isChecked(); }
static void setBoolValue(QCheckBox* widget, bool value) { widget->setChecked(value); }
static int getIntValue(const QCheckBox* widget) { return widget->isChecked() ? 1 : 0; }
static void setIntValue(QCheckBox* widget, int value) { widget->setChecked(value != 0); }
static QString getStringValue(const QCheckBox* widget)
{
return widget->isChecked() ? QStringLiteral("1") : QStringLiteral("0");
}
static void setStringValue(QCheckBox* widget, const QString& value) { widget->setChecked(value.toInt() != 0); }
template<typename F>
static void connectValueChanged(QCheckBox* widget, F func)
{
widget->connect(widget, &QCheckBox::stateChanged, func);
}
};
/// Binds a widget's value to a setting, updating it when the value changes.
// template<typename WidgetType, typename DataType, typename = void>
// void BindWidgetToSetting(QtHostInterface* hi, WidgetType* widget, DataType Settings::*settings_ptr);
template<typename WidgetType>
void BindWidgetToSetting(QtHostInterface* hi, WidgetType* widget, bool Settings::*settings_ptr)
{
using Accessor = SettingAccessor<WidgetType>;
Accessor::setBoolValue(widget, hi->GetCoreSettings().*settings_ptr);
Accessor::connectValueChanged(widget, [hi, widget, settings_ptr]() {
(hi->GetCoreSettings().*settings_ptr) = Accessor::getBoolValue(widget);
hi->updateQSettings();
});
}
template<typename WidgetType>
void BindWidgetToSetting(QtHostInterface* hi, WidgetType* widget, std::string Settings::*settings_ptr)
{
using Accessor = SettingAccessor<WidgetType>;
Accessor::setStringValue(widget, QString::fromStdString(hi->GetCoreSettings().*settings_ptr));
Accessor::connectValueChanged(widget, [hi, widget, settings_ptr]() {
const QString value = Accessor::getStringValue(widget);
(hi->GetCoreSettings().*settings_ptr) = value.toStdString();
hi->updateQSettings();
});
}
template<typename WidgetType, typename DataType>
void BindWidgetToSetting(QtHostInterface* hi, WidgetType* widget, DataType Settings::*settings_ptr,
std::enable_if_t<std::is_enum_v<DataType>, int>* v = nullptr)
{
using Accessor = SettingAccessor<WidgetType>;
using UnderlyingType = std::underlying_type_t<DataType>;
Accessor::setIntValue(widget, static_cast<int>(static_cast<UnderlyingType>(hi->GetCoreSettings().*settings_ptr)));
Accessor::connectValueChanged(widget, [hi, widget, settings_ptr](int) {
const int value = Accessor::getIntValue(widget);
(hi->GetCoreSettings().*settings_ptr) = static_cast<DataType>(static_cast<UnderlyingType>(value));
hi->updateQSettings();
});
}
} // namespace SettingWidgetBinder