Qt: Simplify stylesheet logic

This commit is contained in:
Megamouse 2021-02-07 13:14:36 +01:00 committed by Ivan
parent 632e36ab44
commit 62ca7a9b60
8 changed files with 63 additions and 105 deletions

View File

@ -447,10 +447,9 @@ void gui_application::StopPlaytime()
}
/*
* Handle a request to change the stylesheet. May consider adding reporting of errors in future.
* Empty string means default.
* Handle a request to change the stylesheet based on the current entry in the settings.
*/
void gui_application::OnChangeStyleSheetRequest(const QString& path)
void gui_application::OnChangeStyleSheetRequest()
{
// skip stylesheets on first repaint if a style was set from command line
if (m_use_cli_style && gui::stylesheet.isEmpty())
@ -465,59 +464,71 @@ void gui_application::OnChangeStyleSheetRequest(const QString& path)
return;
}
QFile file(path);
const QString stylesheet = m_gui_settings->GetValue(gui::m_currentStylesheet).toString();
// If we can't open the file, try the /share or /Resources folder
#if !defined(_WIN32)
#ifdef __APPLE__
QString share_dir = QCoreApplication::applicationDirPath() + "/../Resources/";
#else
QString share_dir = QCoreApplication::applicationDirPath() + "/../share/rpcs3/";
#endif
QFile share_file(share_dir + "GuiConfigs/" + QFileInfo(file.fileName()).fileName());
#endif
if (path == "")
if (stylesheet.isEmpty() || stylesheet == gui::Default)
{
setStyleSheet(gui::stylesheets::default_style_sheet);
}
else if (path == "-")
else if (stylesheet == gui::None)
{
setStyleSheet("/* none */");
}
else if (file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QString config_dir = qstr(fs::get_config_dir());
// Remove old fonts
QFontDatabase::removeAllApplicationFonts();
// Add PS3 fonts
QDirIterator ps3_font_it(qstr(g_cfg.vfs.get_dev_flash() + "data/font/"), QStringList() << "*.ttf", QDir::Files, QDirIterator::Subdirectories);
while (ps3_font_it.hasNext())
QFontDatabase::addApplicationFont(ps3_font_it.next());
// Add custom fonts
QDirIterator custom_font_it(config_dir + "fonts/", QStringList() << "*.ttf", QDir::Files, QDirIterator::Subdirectories);
while (custom_font_it.hasNext())
QFontDatabase::addApplicationFont(custom_font_it.next());
// Set root for stylesheets
QDir::setCurrent(config_dir);
setStyleSheet(file.readAll());
file.close();
}
#if !defined(_WIN32)
else if (share_file.open(QIODevice::ReadOnly | QIODevice::Text))
{
QDir::setCurrent(share_dir);
setStyleSheet(share_file.readAll());
share_file.close();
}
#endif
else
{
setStyleSheet(gui::stylesheets::default_style_sheet);
QString stylesheet_path;
QString stylesheet_dir;
QList<QDir> locs;
locs << m_gui_settings->GetSettingsDir();
#if !defined(_WIN32)
#ifdef __APPLE__
locs << QCoreApplication::applicationDirPath() + "/../Resources/GuiConfigs/";
#else
locs << QCoreApplication::applicationDirPath() + "/../share/rpcs3/GuiConfigs/";
#endif
locs << QCoreApplication::applicationDirPath() + "/GuiConfigs/";
#endif
for (auto&& loc : locs)
{
QFileInfo file_info(loc.absoluteFilePath(stylesheet + QStringLiteral(".qss")));
if (file_info.exists())
{
loc.cdUp();
stylesheet_dir = loc.absolutePath();
stylesheet_path = file_info.absoluteFilePath();
break;
}
}
if (QFile file(stylesheet_path); !stylesheet_path.isEmpty() && file.open(QIODevice::ReadOnly | QIODevice::Text))
{
const QString config_dir = qstr(fs::get_config_dir());
// Remove old fonts
QFontDatabase::removeAllApplicationFonts();
// Add PS3 fonts
QDirIterator ps3_font_it(qstr(g_cfg.vfs.get_dev_flash() + "data/font/"), QStringList() << "*.ttf", QDir::Files, QDirIterator::Subdirectories);
while (ps3_font_it.hasNext())
QFontDatabase::addApplicationFont(ps3_font_it.next());
// Add custom fonts
QDirIterator custom_font_it(config_dir + "fonts/", QStringList() << "*.ttf", QDir::Files, QDirIterator::Subdirectories);
while (custom_font_it.hasNext())
QFontDatabase::addApplicationFont(custom_font_it.next());
// Set root for stylesheets
QDir::setCurrent(stylesheet_dir);
setStyleSheet(file.readAll());
file.close();
}
else
{
gui_log.error("Could not find stylesheet '%s'. Using default.", stylesheet.toStdString());
setStyleSheet(gui::stylesheets::default_style_sheet);
}
}
gui::stylesheet = styleSheet();

View File

@ -77,7 +77,7 @@ private:
bool m_use_cli_style = false;
private Q_SLOTS:
void OnChangeStyleSheetRequest(const QString& path);
void OnChangeStyleSheetRequest();
void OnEmuSettingsChange();
Q_SIGNALS:

View File

@ -4,8 +4,6 @@
#include "localized.h"
#include "Emu/System.h"
#include "Utilities/File.h"
#include "Utilities/StrUtil.h"
#include <QCheckBox>
#include <QCoreApplication>
@ -344,56 +342,6 @@ QStringList gui_settings::GetStylesheetEntries()
return res;
}
QString gui_settings::GetCurrentStylesheetPath()
{
const Localized localized;
const QString stylesheet = GetValue(gui::m_currentStylesheet).toString();
if (stylesheet == gui::Default)
{
return "";
}
else if (stylesheet == gui::None)
{
return "-";
}
QList<QDir> locs;
locs += m_settings_dir;
#if !defined(_WIN32)
#ifdef __APPLE__
QDir platformStylesheetDir = QCoreApplication::applicationDirPath() + "/../Resources/GuiConfigs/";
#else
QDir platformStylesheetDir = QCoreApplication::applicationDirPath() + "/../share/rpcs3/GuiConfigs/";
#endif
QDir appDir = QCoreApplication::applicationDirPath() + "/GuiConfigs/";
locs += platformStylesheetDir;
locs += appDir;
#endif
for (auto&& dir : locs)
{
QString path = dir.absoluteFilePath(stylesheet + ".qss");
QFile test(path);
if (test.exists())
{
test.open(QIODevice::ReadOnly);
std::string result = fs::get_cache_dir() + "temp.qss";
std::string sheet = test.readAll().toStdString();
// Fixup paths (replace resources in GuiConfigs with absolute paths) and store in temp file.
path.truncate(path.size() - stylesheet.size() - 4);
fs::write_file(result, fs::rewrite, fmt::replace_all(sheet, "url(\"GuiConfigs/", "url(\"" + path.toStdString()));
return QString::fromUtf8(result.data(), result.size());
}
}
return "";
}
QSize gui_settings::SizeFromSlider(int pos)
{
return gui::gl_icon_size_min + (gui::gl_icon_size_max - gui::gl_icon_size_min) * (1.f * pos / gui::gl_max_slider_pos);

View File

@ -253,7 +253,6 @@ public:
bool GetGamelistColVisibility(int col);
QColor GetCustomColor(int col);
QStringList GetConfigEntries();
QString GetCurrentStylesheetPath();
QStringList GetStylesheetEntries();
QStringList GetGameListCategoryFilters();

View File

@ -99,7 +99,7 @@ bool main_window::Init()
setMinimumSize(350, minimumSizeHint().height()); // seems fine on win 10
setWindowTitle(QString::fromStdString("RPCS3 " + rpcs3::get_version().to_string()));
Q_EMIT RequestGlobalStylesheetChange(m_gui_settings->GetCurrentStylesheetPath());
Q_EMIT RequestGlobalStylesheetChange();
ConfigureGuiFromSettings(true);
if (const std::string_view branch_name = rpcs3::get_full_branch(); branch_name != "RPCS3/rpcs3/master" && branch_name != "local_build")

View File

@ -87,7 +87,7 @@ public:
Q_SIGNALS:
void RequestLanguageChange(const QString& language);
void RequestGlobalStylesheetChange(const QString& stylesheet_path);
void RequestGlobalStylesheetChange();
void RequestTrophyManagerRepaint();
void NotifyEmuSettingsChange();

View File

@ -1926,7 +1926,7 @@ void settings_dialog::OnApplyStylesheet()
{
m_current_stylesheet = ui->combo_stylesheets->currentData().toString();
m_gui_settings->SetValue(gui::m_currentStylesheet, m_current_stylesheet);
Q_EMIT GuiStylesheetRequest(m_gui_settings->GetCurrentStylesheetPath());
Q_EMIT GuiStylesheetRequest();
}
int settings_dialog::exec()

View File

@ -26,7 +26,7 @@ public:
int exec() override;
Q_SIGNALS:
void GuiSettingsSyncRequest(bool configure_all);
void GuiStylesheetRequest(const QString& path);
void GuiStylesheetRequest();
void GuiSettingsSaveRequest();
void GuiRepaintRequest();
void EmuSettingsApplied();