From 93d4fe1031ebf1e913cb31bacb0adbf18cff5d5d Mon Sep 17 00:00:00 2001 From: georgemoralis Date: Sat, 24 May 2025 23:24:51 +0300 Subject: [PATCH] initial save classes for gui save file --- CMakeLists.txt | 6 ++- src/common/config.cpp | 57 --------------------------- src/common/config.h | 9 ----- src/qt_gui/gui_settings.cpp | 9 +++++ src/qt_gui/gui_settings.h | 23 +++++++++++ src/qt_gui/main_window.cpp | 15 ++++---- src/qt_gui/main_window.h | 4 +- src/qt_gui/settings.cpp | 77 +++++++++++++++++++++++++++++++++++++ src/qt_gui/settings.h | 56 +++++++++++++++++++++++++++ 9 files changed, 180 insertions(+), 76 deletions(-) create mode 100644 src/qt_gui/gui_settings.cpp create mode 100644 src/qt_gui/gui_settings.h create mode 100644 src/qt_gui/settings.cpp create mode 100644 src/qt_gui/settings.h diff --git a/CMakeLists.txt b/CMakeLists.txt index eb7a4f427..753b48661 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1014,7 +1014,7 @@ set(QT_GUI src/qt_gui/about_dialog.cpp src/qt_gui/cheats_patches.h src/qt_gui/compatibility_info.cpp src/qt_gui/compatibility_info.h - src/qt_gui/control_settings.cpp + src/qt_gui/control_settings.cpp src/qt_gui/control_settings.h src/qt_gui/control_settings.ui src/qt_gui/kbm_gui.cpp @@ -1047,6 +1047,10 @@ set(QT_GUI src/qt_gui/about_dialog.cpp src/qt_gui/settings_dialog.h src/qt_gui/settings_dialog.ui src/qt_gui/main.cpp + src/qt_gui/gui_settings.cpp + src/qt_gui/gui_settings.h + src/qt_gui/settings.cpp + src/qt_gui/settings.h ${EMULATOR} ${RESOURCE_FILES} ${TRANSLATIONS} diff --git a/src/common/config.cpp b/src/common/config.cpp index 6bccd0f37..69a7e3ea9 100644 --- a/src/common/config.cpp +++ b/src/common/config.cpp @@ -85,18 +85,12 @@ static std::vector settings_install_dirs = {}; std::vector install_dirs_enabled = {}; std::filesystem::path settings_addon_install_dir = {}; std::filesystem::path save_data_path = {}; -u32 main_window_geometry_x = 400; -u32 main_window_geometry_y = 400; -u32 main_window_geometry_w = 1280; -u32 main_window_geometry_h = 720; u32 mw_themes = 0; u32 m_icon_size = 36; u32 m_icon_size_grid = 69; u32 m_slider_pos = 0; u32 m_slider_pos_grid = 0; u32 m_table_mode = 0; -u32 m_window_size_W = 1280; -u32 m_window_size_H = 720; std::vector m_elf_viewer; std::vector m_recent_files; std::string emulator_language = "en_US"; @@ -520,13 +514,6 @@ void setCheckCompatibilityOnStartup(bool use) { checkCompatibilityOnStartup = use; } -void setMainWindowGeometry(u32 x, u32 y, u32 w, u32 h) { - main_window_geometry_x = x; - main_window_geometry_y = y; - main_window_geometry_w = w; - main_window_geometry_h = h; -} - bool addGameInstallDir(const std::filesystem::path& dir, bool enabled) { for (const auto& install_dir : settings_install_dirs) { if (install_dir.path == dir) { @@ -583,14 +570,6 @@ void setTableMode(u32 mode) { m_table_mode = mode; } -void setMainWindowWidth(u32 width) { - m_window_size_W = width; -} - -void setMainWindowHeight(u32 height) { - m_window_size_H = height; -} - void setElfViewer(const std::vector& elfList) { m_elf_viewer.resize(elfList.size()); m_elf_viewer = elfList; @@ -620,22 +599,6 @@ void setSaveDataPath(const std::filesystem::path& path) { save_data_path = path; } -u32 getMainWindowGeometryX() { - return main_window_geometry_x; -} - -u32 getMainWindowGeometryY() { - return main_window_geometry_y; -} - -u32 getMainWindowGeometryW() { - return main_window_geometry_w; -} - -u32 getMainWindowGeometryH() { - return main_window_geometry_h; -} - const std::vector getGameInstallDirs() { std::vector enabled_dirs; for (const auto& dir : settings_install_dirs) { @@ -686,14 +649,6 @@ u32 getTableMode() { return m_table_mode; } -u32 getMainWindowWidth() { - return m_window_size_W; -} - -u32 getMainWindowHeight() { - return m_window_size_H; -} - std::vector getElfViewer() { return m_elf_viewer; } @@ -836,8 +791,6 @@ void load(const std::filesystem::path& path) { m_slider_pos = toml::find_or(gui, "sliderPos", 0); m_slider_pos_grid = toml::find_or(gui, "sliderPosGrid", 0); mw_themes = toml::find_or(gui, "theme", 0); - m_window_size_W = toml::find_or(gui, "mw_width", 0); - m_window_size_H = toml::find_or(gui, "mw_height", 0); const auto install_dir_array = toml::find_or>(gui, "installDirs", {}); @@ -862,10 +815,6 @@ void load(const std::filesystem::path& path) { save_data_path = toml::find_fs_path_or(gui, "saveDataPath", {}); settings_addon_install_dir = toml::find_fs_path_or(gui, "addonInstallDir", {}); - main_window_geometry_x = toml::find_or(gui, "geometry_x", 0); - main_window_geometry_y = toml::find_or(gui, "geometry_y", 0); - main_window_geometry_w = toml::find_or(gui, "geometry_w", 0); - main_window_geometry_h = toml::find_or(gui, "geometry_h", 0); m_elf_viewer = toml::find_or>(gui, "elfDirs", {}); m_recent_files = toml::find_or>(gui, "recentFiles", {}); m_table_mode = toml::find_or(gui, "gameTableMode", 0); @@ -1071,18 +1020,12 @@ void saveMainWindow(const std::filesystem::path& path) { fmt::print("Saving new configuration file {}\n", fmt::UTF(path.u8string())); } - data["GUI"]["mw_width"] = m_window_size_W; - data["GUI"]["mw_height"] = m_window_size_H; data["GUI"]["theme"] = mw_themes; data["GUI"]["iconSize"] = m_icon_size; data["GUI"]["sliderPos"] = m_slider_pos; data["GUI"]["iconSizeGrid"] = m_icon_size_grid; data["GUI"]["sliderPosGrid"] = m_slider_pos_grid; data["GUI"]["gameTableMode"] = m_table_mode; - data["GUI"]["geometry_x"] = main_window_geometry_x; - data["GUI"]["geometry_y"] = main_window_geometry_y; - data["GUI"]["geometry_w"] = main_window_geometry_w; - data["GUI"]["geometry_h"] = main_window_geometry_h; data["GUI"]["elfDirs"] = m_elf_viewer; data["GUI"]["recentFiles"] = m_recent_files; diff --git a/src/common/config.h b/src/common/config.h index aba23621c..b017eb5d8 100644 --- a/src/common/config.h +++ b/src/common/config.h @@ -139,7 +139,6 @@ void setVkHostMarkersEnabled(bool enable); void setVkGuestMarkersEnabled(bool enable); // Gui -void setMainWindowGeometry(u32 x, u32 y, u32 w, u32 h); bool addGameInstallDir(const std::filesystem::path& dir, bool enabled = true); void removeGameInstallDir(const std::filesystem::path& dir); void setGameInstallDirEnabled(const std::filesystem::path& dir, bool enabled); @@ -150,16 +149,10 @@ void setIconSizeGrid(u32 size); void setSliderPosition(u32 pos); void setSliderPositionGrid(u32 pos); void setTableMode(u32 mode); -void setMainWindowWidth(u32 width); -void setMainWindowHeight(u32 height); void setElfViewer(const std::vector& elfList); void setRecentFiles(const std::vector& recentFiles); void setEmulatorLanguage(std::string language); -u32 getMainWindowGeometryX(); -u32 getMainWindowGeometryY(); -u32 getMainWindowGeometryW(); -u32 getMainWindowGeometryH(); const std::vector getGameInstallDirs(); const std::vector getGameInstallDirsEnabled(); std::filesystem::path getAddonInstallDir(); @@ -169,8 +162,6 @@ u32 getIconSizeGrid(); u32 getSliderPosition(); u32 getSliderPositionGrid(); u32 getTableMode(); -u32 getMainWindowWidth(); -u32 getMainWindowHeight(); std::vector getElfViewer(); std::vector getRecentFiles(); std::string getEmulatorLanguage(); diff --git a/src/qt_gui/gui_settings.cpp b/src/qt_gui/gui_settings.cpp new file mode 100644 index 000000000..bcfaa2c3a --- /dev/null +++ b/src/qt_gui/gui_settings.cpp @@ -0,0 +1,9 @@ +// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include "gui_settings.h" + +gui_settings::gui_settings(QObject* parent) : settings(parent) { + m_settings = std::make_unique(ComputeSettingsDir() + "qt_ui.ini", + QSettings::Format::IniFormat, parent); +} \ No newline at end of file diff --git a/src/qt_gui/gui_settings.h b/src/qt_gui/gui_settings.h new file mode 100644 index 000000000..b5d4e61ab --- /dev/null +++ b/src/qt_gui/gui_settings.h @@ -0,0 +1,23 @@ +// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include "settings.h" + +namespace gui { +// categories +const QString main_window = "main_window"; + +// main window settions +const gui_value mw_geometry = gui_value(main_window, "geometry", QByteArray()); + +} // namespace gui + +class gui_settings : public settings { + Q_OBJECT + +public: + explicit gui_settings(QObject* parent = nullptr); +}; diff --git a/src/qt_gui/main_window.cpp b/src/qt_gui/main_window.cpp index 1966aa52b..944d4c8f4 100644 --- a/src/qt_gui/main_window.cpp +++ b/src/qt_gui/main_window.cpp @@ -32,6 +32,7 @@ MainWindow::MainWindow(QWidget* parent) : QMainWindow(parent), ui(new Ui::MainWi ui->setupUi(this); installEventFilter(this); setAttribute(Qt::WA_DeleteOnClose); + m_gui_settings = std::make_shared(); } MainWindow::~MainWindow() { @@ -934,9 +935,10 @@ void MainWindow::RefreshGameTable() { } void MainWindow::ConfigureGuiFromSettings() { - setGeometry(Config::getMainWindowGeometryX(), Config::getMainWindowGeometryY(), - Config::getMainWindowGeometryW(), Config::getMainWindowGeometryH()); - + if (!restoreGeometry(m_gui_settings->GetValue(gui::mw_geometry).toByteArray())) { + // By default, set the window to 70% of the screen + resize(QGuiApplication::primaryScreen()->availableSize() * 0.7); + } ui->showGameListAct->setChecked(true); if (Config::getTableMode() == 0) { ui->setlistModeListAct->setChecked(true); @@ -948,11 +950,8 @@ void MainWindow::ConfigureGuiFromSettings() { BackgroundMusicPlayer::getInstance().setVolume(Config::getBGMvolume()); } -void MainWindow::SaveWindowState() const { - Config::setMainWindowWidth(this->width()); - Config::setMainWindowHeight(this->height()); - Config::setMainWindowGeometry(this->geometry().x(), this->geometry().y(), - this->geometry().width(), this->geometry().height()); +void MainWindow::SaveWindowState() { + m_gui_settings->SetValue(gui::mw_geometry, saveGeometry(), false); } void MainWindow::BootGame() { diff --git a/src/qt_gui/main_window.h b/src/qt_gui/main_window.h index a5ec08d36..ddeb99104 100644 --- a/src/qt_gui/main_window.h +++ b/src/qt_gui/main_window.h @@ -20,6 +20,7 @@ #include "game_info.h" #include "game_list_frame.h" #include "game_list_utils.h" +#include "gui_settings.h" #include "main_window_themes.h" #include "main_window_ui.h" @@ -42,7 +43,7 @@ public: private Q_SLOTS: void ConfigureGuiFromSettings(); - void SaveWindowState() const; + void SaveWindowState(); void SearchGameTable(const QString& text); void ShowGameList(); void RefreshGameTable(); @@ -103,6 +104,7 @@ private: std::make_shared(); QTranslator* translator; + std::shared_ptr m_gui_settings; protected: bool eventFilter(QObject* obj, QEvent* event) override; diff --git a/src/qt_gui/settings.cpp b/src/qt_gui/settings.cpp new file mode 100644 index 000000000..44133dac5 --- /dev/null +++ b/src/qt_gui/settings.cpp @@ -0,0 +1,77 @@ +// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#include +#include "settings.h" + +settings::settings(QObject* parent) : QObject(parent), m_settings_dir(ComputeSettingsDir()) {} + +settings::~settings() { + sync(); +} + +void settings::sync() { + if (m_settings) { + m_settings->sync(); + } +} + +QString settings::GetSettingsDir() const { + return m_settings_dir.absolutePath(); +} + +QString settings::ComputeSettingsDir() { + const auto config_dir = Common::FS::GetUserPath(Common::FS::PathType::UserDir); + return QString::fromStdString(config_dir.string() + "/"); +} + +void settings::RemoveValue(const QString& key, const QString& name, bool sync) const { + if (m_settings) { + m_settings->beginGroup(key); + m_settings->remove(name); + m_settings->endGroup(); + + if (sync) { + m_settings->sync(); + } + } +} + +void settings::RemoveValue(const gui_value& entry, bool sync) const { + RemoveValue(entry.key, entry.name, sync); +} + +QVariant settings::GetValue(const QString& key, const QString& name, const QVariant& def) const { + return m_settings ? m_settings->value(key + "/" + name, def) : def; +} + +QVariant settings::GetValue(const gui_value& entry) const { + return GetValue(entry.key, entry.name, entry.def); +} + +void settings::SetValue(const gui_value& entry, const QVariant& value, bool sync) const { + SetValue(entry.key, entry.name, value, sync); +} + +void settings::SetValue(const QString& key, const QVariant& value, bool sync) const { + if (m_settings) { + m_settings->setValue(key, value); + + if (sync) { + m_settings->sync(); + } + } +} + +void settings::SetValue(const QString& key, const QString& name, const QVariant& value, + bool sync) const { + if (m_settings) { + m_settings->beginGroup(key); + m_settings->setValue(name, value); + m_settings->endGroup(); + + if (sync) { + m_settings->sync(); + } + } +} diff --git a/src/qt_gui/settings.h b/src/qt_gui/settings.h new file mode 100644 index 000000000..2aa45cd25 --- /dev/null +++ b/src/qt_gui/settings.h @@ -0,0 +1,56 @@ +// SPDX-FileCopyrightText: Copyright 2025 shadPS4 Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +#pragma once + +#include +#include +#include +#include +#include "gui_save.h" + +struct gui_value { + QString key; + QString name; + QVariant def; + + gui_value() {} + + gui_value(const QString& k, const QString& n, const QVariant& d) : key(k), name(n), def(d) {} + + bool operator==(const gui_value& rhs) const noexcept { + return key == rhs.key && name == rhs.name && def == rhs.def; + } +}; + +class settings : public QObject { + Q_OBJECT + +public: + explicit settings(QObject* parent = nullptr); + ~settings(); + + void sync(); + + QString GetSettingsDir() const; + + QVariant GetValue(const QString& key, const QString& name, const QVariant& def) const; + QVariant GetValue(const gui_value& entry) const; + +public Q_SLOTS: + /** Remove entry */ + void RemoveValue(const QString& key, const QString& name, bool sync = true) const; + void RemoveValue(const gui_value& entry, bool sync = true) const; + + /** Write value to entry */ + void SetValue(const gui_value& entry, const QVariant& value, bool sync = true) const; + void SetValue(const QString& key, const QVariant& value, bool sync = true) const; + void SetValue(const QString& key, const QString& name, const QVariant& value, + bool sync = true) const; + +protected: + static QString ComputeSettingsDir(); + + std::unique_ptr m_settings; + QDir m_settings_dir; +};