From 0170052f5d8d00dddceab47eea513431b1d8c27f Mon Sep 17 00:00:00 2001 From: spycrab Date: Sun, 6 May 2018 18:25:37 +0200 Subject: [PATCH] Qt: Allow custom stylesheets --- Source/Core/Common/CommonPaths.h | 1 + Source/Core/Common/FileUtil.cpp | 1 + Source/Core/Common/FileUtil.h | 1 + Source/Core/DolphinQt2/Settings.cpp | 37 ++++++++++++++ Source/Core/DolphinQt2/Settings.h | 5 ++ .../DolphinQt2/Settings/InterfacePane.cpp | 51 +++++++++++++++++-- .../Core/DolphinQt2/Settings/InterfacePane.h | 4 ++ Source/Core/UICommon/UICommon.cpp | 1 + 8 files changed, 98 insertions(+), 3 deletions(-) diff --git a/Source/Core/Common/CommonPaths.h b/Source/Core/Common/CommonPaths.h index 877846dd88..e641b33294 100644 --- a/Source/Core/Common/CommonPaths.h +++ b/Source/Core/Common/CommonPaths.h @@ -57,6 +57,7 @@ #define WII_WC24CONF_DIR "shared2" DIR_SEP "wc24" #define RESOURCES_DIR "Resources" #define THEMES_DIR "Themes" +#define STYLES_DIR "Styles" #define ANAGLYPH_DIR "Anaglyph" #define PIPES_DIR "Pipes" #define MEMORYWATCHER_DIR "MemoryWatcher" diff --git a/Source/Core/Common/FileUtil.cpp b/Source/Core/Common/FileUtil.cpp index 88b17c99c6..66f1decdc5 100644 --- a/Source/Core/Common/FileUtil.cpp +++ b/Source/Core/Common/FileUtil.cpp @@ -774,6 +774,7 @@ static void RebuildUserDirectories(unsigned int dir_index) s_user_paths[D_LOGS_IDX] = s_user_paths[D_USER_IDX] + LOGS_DIR DIR_SEP; s_user_paths[D_MAILLOGS_IDX] = s_user_paths[D_LOGS_IDX] + MAIL_LOGS_DIR DIR_SEP; s_user_paths[D_THEMES_IDX] = s_user_paths[D_USER_IDX] + THEMES_DIR DIR_SEP; + s_user_paths[D_STYLES_IDX] = s_user_paths[D_USER_IDX] + STYLES_DIR DIR_SEP; s_user_paths[D_PIPES_IDX] = s_user_paths[D_USER_IDX] + PIPES_DIR DIR_SEP; s_user_paths[D_WFSROOT_IDX] = s_user_paths[D_USER_IDX] + WFSROOT_DIR DIR_SEP; s_user_paths[D_BACKUP_IDX] = s_user_paths[D_USER_IDX] + BACKUP_DIR DIR_SEP; diff --git a/Source/Core/Common/FileUtil.h b/Source/Core/Common/FileUtil.h index 2024893b5d..bc7ec4e70c 100644 --- a/Source/Core/Common/FileUtil.h +++ b/Source/Core/Common/FileUtil.h @@ -44,6 +44,7 @@ enum D_LOGS_IDX, D_MAILLOGS_IDX, D_THEMES_IDX, + D_STYLES_IDX, D_PIPES_IDX, D_MEMORYWATCHER_IDX, D_WFSROOT_IDX, diff --git a/Source/Core/DolphinQt2/Settings.cpp b/Source/Core/DolphinQt2/Settings.cpp index f25349293e..e78c4d487c 100644 --- a/Source/Core/DolphinQt2/Settings.cpp +++ b/Source/Core/DolphinQt2/Settings.cpp @@ -2,7 +2,9 @@ // Licensed under GPLv2+ // Refer to the license.txt file included. +#include #include +#include #include #include @@ -26,6 +28,8 @@ Settings::Settings() Config::AddConfigChangedCallback( [this] { QueueOnObject(this, [this] { emit ConfigChanged(); }); }); + + SetCurrentUserStyle(GetCurrentUserStyle()); } Settings& Settings::Instance() @@ -48,6 +52,39 @@ void Settings::SetThemeName(const QString& theme_name) emit ThemeChanged(); } +QString Settings::GetCurrentUserStyle() const +{ + return GetQSettings().value(QStringLiteral("userstyle/path"), false).toString(); +} + +void Settings::SetCurrentUserStyle(const QString& stylesheet_path) +{ + QString stylesheet_contents; + + if (!stylesheet_path.isEmpty() && AreUserStylesEnabled()) + { + // Load custom user stylesheet + QFile stylesheet(stylesheet_path); + + if (stylesheet.open(QFile::ReadOnly)) + stylesheet_contents = QString::fromUtf8(stylesheet.readAll().data()); + } + + qApp->setStyleSheet(stylesheet_contents); + + GetQSettings().setValue(QStringLiteral("userstyle/path"), stylesheet_path); +} + +bool Settings::AreUserStylesEnabled() const +{ + return GetQSettings().value(QStringLiteral("userstyle/enabled"), false).toBool(); +} + +void Settings::SetUserStylesEnabled(bool enabled) +{ + GetQSettings().setValue(QStringLiteral("userstyle/enabled"), enabled); +} + QStringList Settings::GetPaths() const { QStringList list; diff --git a/Source/Core/DolphinQt2/Settings.h b/Source/Core/DolphinQt2/Settings.h index 5ea267afb8..7b8549efa9 100644 --- a/Source/Core/DolphinQt2/Settings.h +++ b/Source/Core/DolphinQt2/Settings.h @@ -44,6 +44,11 @@ public: // UI void SetThemeName(const QString& theme_name); + void SetCurrentUserStyle(const QString& stylesheet_path); + QString GetCurrentUserStyle() const; + + void SetUserStylesEnabled(bool enabled); + bool AreUserStylesEnabled() const; bool IsInDevelopmentWarningEnabled() const; bool IsLogVisible() const; diff --git a/Source/Core/DolphinQt2/Settings/InterfacePane.cpp b/Source/Core/DolphinQt2/Settings/InterfacePane.cpp index f812573da0..49d4e51f05 100644 --- a/Source/Core/DolphinQt2/Settings/InterfacePane.cpp +++ b/Source/Core/DolphinQt2/Settings/InterfacePane.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -73,8 +74,8 @@ static QComboBox* MakeLanguageComboBox() InterfacePane::InterfacePane(QWidget* parent) : QWidget(parent) { CreateLayout(); - ConnectLayout(); LoadConfig(); + ConnectLayout(); } void InterfacePane::CreateLayout() @@ -106,9 +107,9 @@ void InterfacePane::CreateUI() combobox_layout->addRow(tr("&Theme:"), m_combobox_theme); // List avalable themes - auto file_search_results = + auto theme_search_results = Common::DoFileSearch({File::GetUserPath(D_THEMES_IDX), File::GetSysDirectory() + THEMES_DIR}); - for (const std::string& filename : file_search_results) + for (const std::string& filename : theme_search_results) { std::string name, ext; SplitPath(filename, nullptr, &name, &ext); @@ -117,13 +118,32 @@ void InterfacePane::CreateUI() m_combobox_theme->addItem(qt_name); } + // User Style Combobox + m_combobox_userstyle = new QComboBox; + m_label_userstyle = new QLabel(tr("User Style:")); + combobox_layout->addRow(m_label_userstyle, m_combobox_userstyle); + + auto userstyle_search_results = Common::DoFileSearch({File::GetUserPath(D_STYLES_IDX)}); + + m_combobox_userstyle->addItem(tr("(None)"), QStringLiteral("")); + + for (const std::string& filename : userstyle_search_results) + { + std::string name, ext; + SplitPath(filename, nullptr, &name, &ext); + QString qt_name = QString::fromStdString(name); + m_combobox_userstyle->addItem(qt_name, QString::fromStdString(filename)); + } + // Checkboxes m_checkbox_top_window = new QCheckBox(tr("Keep Window on Top")); m_checkbox_use_builtin_title_database = new QCheckBox(tr("Use Built-In Database of Game Names")); + m_checkbox_use_userstyle = new QCheckBox(tr("Use Custom User Style")); m_checkbox_show_debugging_ui = new QCheckBox(tr("Show Debugging UI")); groupbox_layout->addWidget(m_checkbox_top_window); groupbox_layout->addWidget(m_checkbox_use_builtin_title_database); + groupbox_layout->addWidget(m_checkbox_use_userstyle); groupbox_layout->addWidget(m_checkbox_show_debugging_ui); } @@ -157,6 +177,9 @@ void InterfacePane::ConnectLayout() connect(m_checkbox_show_debugging_ui, &QCheckBox::clicked, this, &InterfacePane::OnSaveConfig); connect(m_combobox_theme, static_cast(&QComboBox::activated), &Settings::Instance(), &Settings::SetThemeName); + connect(m_combobox_userstyle, + static_cast(&QComboBox::currentIndexChanged), this, + &InterfacePane::OnSaveConfig); connect(m_combobox_language, static_cast(&QComboBox::activated), this, &InterfacePane::OnSaveConfig); connect(m_checkbox_confirm_on_stop, &QCheckBox::clicked, this, &InterfacePane::OnSaveConfig); @@ -165,6 +188,7 @@ void InterfacePane::ConnectLayout() connect(m_checkbox_pause_on_focus_lost, &QCheckBox::clicked, this, &InterfacePane::OnSaveConfig); connect(m_checkbox_hide_mouse, &QCheckBox::clicked, &Settings::Instance(), &Settings::SetHideCursor); + connect(m_checkbox_use_userstyle, &QCheckBox::toggled, this, &InterfacePane::OnSaveConfig); } void InterfacePane::LoadConfig() @@ -178,6 +202,20 @@ void InterfacePane::LoadConfig() m_combobox_theme->setCurrentIndex( m_combobox_theme->findText(QString::fromStdString(SConfig::GetInstance().theme_name))); + const QString userstyle = Settings::Instance().GetCurrentUserStyle(); + + if (userstyle.isEmpty()) + m_combobox_userstyle->setCurrentIndex(0); + else + m_combobox_userstyle->setCurrentText(userstyle); + + m_checkbox_use_userstyle->setChecked(Settings::Instance().AreUserStylesEnabled()); + + const bool visible = m_checkbox_use_userstyle->isChecked(); + + m_combobox_userstyle->setVisible(visible); + m_label_userstyle->setVisible(visible); + // In Game Options m_checkbox_confirm_on_stop->setChecked(startup_params.bConfirmStop); m_checkbox_use_panic_handlers->setChecked(startup_params.bUsePanicHandlers); @@ -193,6 +231,13 @@ void InterfacePane::OnSaveConfig() Settings::Instance().SetKeepWindowOnTop(m_checkbox_top_window->isChecked()); settings.m_use_builtin_title_database = m_checkbox_use_builtin_title_database->isChecked(); Settings::Instance().SetDebugModeEnabled(m_checkbox_show_debugging_ui->isChecked()); + Settings::Instance().SetCurrentUserStyle(m_combobox_userstyle->currentData().toString()); + Settings::Instance().SetUserStylesEnabled(m_checkbox_use_userstyle->isChecked()); + + const bool visible = m_checkbox_use_userstyle->isChecked(); + + m_combobox_userstyle->setVisible(visible); + m_label_userstyle->setVisible(visible); // In Game Options settings.bConfirmStop = m_checkbox_confirm_on_stop->isChecked(); diff --git a/Source/Core/DolphinQt2/Settings/InterfacePane.h b/Source/Core/DolphinQt2/Settings/InterfacePane.h index 06f791b33f..e3c2a707bd 100644 --- a/Source/Core/DolphinQt2/Settings/InterfacePane.h +++ b/Source/Core/DolphinQt2/Settings/InterfacePane.h @@ -8,6 +8,7 @@ class QCheckBox; class QComboBox; +class QLabel; class QVBoxLayout; class InterfacePane final : public QWidget @@ -28,8 +29,11 @@ private: QComboBox* m_combobox_language; QComboBox* m_combobox_theme; + QComboBox* m_combobox_userstyle; + QLabel* m_label_userstyle; QCheckBox* m_checkbox_top_window; QCheckBox* m_checkbox_use_builtin_title_database; + QCheckBox* m_checkbox_use_userstyle; QCheckBox* m_checkbox_show_debugging_ui; QCheckBox* m_checkbox_confirm_on_stop; diff --git a/Source/Core/UICommon/UICommon.cpp b/Source/Core/UICommon/UICommon.cpp index 557ddcc22b..7ac4c226fb 100644 --- a/Source/Core/UICommon/UICommon.cpp +++ b/Source/Core/UICommon/UICommon.cpp @@ -153,6 +153,7 @@ void CreateDirectories() File::CreateFullPath(File::GetUserPath(D_STATESAVES_IDX)); #ifndef ANDROID File::CreateFullPath(File::GetUserPath(D_THEMES_IDX)); + File::CreateFullPath(File::GetUserPath(D_STYLES_IDX)); #endif }