Qt: Allow custom stylesheets

This commit is contained in:
spycrab 2018-05-06 18:25:37 +02:00
parent c3d88a622d
commit 0170052f5d
8 changed files with 98 additions and 3 deletions

View file

@ -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"

View file

@ -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;

View file

@ -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,

View file

@ -2,7 +2,9 @@
// Licensed under GPLv2+
// Refer to the license.txt file included.
#include <QApplication>
#include <QDir>
#include <QFile>
#include <QSettings>
#include <QSize>
@ -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;

View file

@ -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;

View file

@ -8,6 +8,7 @@
#include <QComboBox>
#include <QFormLayout>
#include <QGroupBox>
#include <QLabel>
#include <QMessageBox>
#include <QVBoxLayout>
#include <QWidget>
@ -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<void (QComboBox::*)(const QString&)>(&QComboBox::activated),
&Settings::Instance(), &Settings::SetThemeName);
connect(m_combobox_userstyle,
static_cast<void (QComboBox::*)(const QString&)>(&QComboBox::currentIndexChanged), this,
&InterfacePane::OnSaveConfig);
connect(m_combobox_language, static_cast<void (QComboBox::*)(int)>(&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();

View file

@ -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;

View file

@ -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
}