Skip to content

Commit

Permalink
Qt: Simplify stylesheet logic
Browse files Browse the repository at this point in the history
  • Loading branch information
Megamouse committed Feb 7, 2021
1 parent 632e36a commit 4b57a46
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 102 deletions.
98 changes: 54 additions & 44 deletions rpcs3/rpcs3qt/gui_application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -465,59 +464,70 @@ 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))
else
{
QString config_dir = qstr(fs::get_config_dir());
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();
}
}

// Remove old fonts
QFontDatabase::removeAllApplicationFonts();
if (QFile file(stylesheet_path); !stylesheet_path.isEmpty() && file.open(QIODevice::ReadOnly | QIODevice::Text))
{
const QString config_dir = qstr(fs::get_config_dir());

// 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());
// Remove old fonts
QFontDatabase::removeAllApplicationFonts();

// 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());
// 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());

// 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);
// 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);
setStyleSheet(gui::stylesheets::default_style_sheet);
}
}

gui::stylesheet = styleSheet();
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/rpcs3qt/gui_application.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class gui_application : public QApplication, public main_application
bool m_use_cli_style = false;

private Q_SLOTS:
void OnChangeStyleSheetRequest(const QString& path);
void OnChangeStyleSheetRequest();
void OnEmuSettingsChange();

Q_SIGNALS:
Expand Down
52 changes: 0 additions & 52 deletions rpcs3/rpcs3qt/gui_settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
#include "localized.h"

#include "Emu/System.h"
#include "Utilities/File.h"
#include "Utilities/StrUtil.h"

#include <QCheckBox>
#include <QCoreApplication>
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion rpcs3/rpcs3qt/gui_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,6 @@ class gui_settings : public settings
bool GetGamelistColVisibility(int col);
QColor GetCustomColor(int col);
QStringList GetConfigEntries();
QString GetCurrentStylesheetPath();
QStringList GetStylesheetEntries();
QStringList GetGameListCategoryFilters();

Expand Down
2 changes: 1 addition & 1 deletion rpcs3/rpcs3qt/main_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/rpcs3qt/main_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ class main_window : public QMainWindow

Q_SIGNALS:
void RequestLanguageChange(const QString& language);
void RequestGlobalStylesheetChange(const QString& stylesheet_path);
void RequestGlobalStylesheetChange();
void RequestTrophyManagerRepaint();
void NotifyEmuSettingsChange();

Expand Down
2 changes: 1 addition & 1 deletion rpcs3/rpcs3qt/settings_dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion rpcs3/rpcs3qt/settings_dialog.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ class settings_dialog : public QDialog
int exec() override;
Q_SIGNALS:
void GuiSettingsSyncRequest(bool configure_all);
void GuiStylesheetRequest(const QString& path);
void GuiStylesheetRequest();
void GuiSettingsSaveRequest();
void GuiRepaintRequest();
void EmuSettingsApplied();
Expand Down

0 comments on commit 4b57a46

Please sign in to comment.