From ecc16958e69e56a0844967b89937055c4d0f9f23 Mon Sep 17 00:00:00 2001 From: Paddle Date: Mon, 14 Aug 2023 08:06:06 +0200 Subject: [PATCH] Core: Stylesheets: Adds support for variables in stylesheets. --- src/Gui/Application.cpp | 27 ++++++++++++++++++++++++++- src/Gui/Application.h | 3 ++- 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index e511f3d35e..188712d585 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -2480,7 +2480,10 @@ void Application::setStyleSheet(const QString& qssFile, bool tiledBackground) if (!f.fileName().isEmpty() && f.open(QFile::ReadOnly | QFile::Text)) { mdi->setBackground(QBrush(Qt::NoBrush)); QTextStream str(&f); - qApp->setStyleSheet(str.readAll()); + + QString styleSheetContent = replaceVariablesInQss(str.readAll()); + + qApp->setStyleSheet(styleSheetContent); ActionStyleEvent e(ActionStyleEvent::Clear); qApp->sendEvent(mw, &e); @@ -2532,6 +2535,28 @@ void Application::setStyleSheet(const QString& qssFile, bool tiledBackground) } } +QString Application::replaceVariablesInQss(QString qssText) +{ + //First we fetch the colors from preferences, + ParameterGrp::handle hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/Themes"); + unsigned long longAccentColor1 = hGrp->GetUnsigned("ThemeAccentColor1", 0); + unsigned long longAccentColor2 = hGrp->GetUnsigned("ThemeAccentColor2", 0); + unsigned long longAccentColor3 = hGrp->GetUnsigned("ThemeAccentColor3", 0); + + //convert them to hex. + //Note: the ulong contains alpha channels so 8 hex characters when we need 6 here. + QString accentColor1 = QString::fromLatin1("#%1").arg(longAccentColor1, 8, 16, QLatin1Char('0')).toUpper().mid(0, 7); + QString accentColor2 = QString::fromLatin1("#%1").arg(longAccentColor2, 8, 16, QLatin1Char('0')).toUpper().mid(0, 7); + QString accentColor3 = QString::fromLatin1("#%1").arg(longAccentColor3, 8, 16, QLatin1Char('0')).toUpper().mid(0, 7); + + qssText = qssText.replace(QString::fromLatin1("@ThemeAccentColor1"), accentColor1); + qssText = qssText.replace(QString::fromLatin1("@ThemeAccentColor2"), accentColor2); + qssText = qssText.replace(QString::fromLatin1("@ThemeAccentColor3"), accentColor3); + + //Base::Console().Warning("%s\n", qssText.toStdString()); + return qssText; +} + void Application::checkForDeprecatedSettings() { // From 0.21, `FCBak` will be the intended default backup format diff --git a/src/Gui/Application.h b/src/Gui/Application.h index bee284c459..f1c6916438 100644 --- a/src/Gui/Application.h +++ b/src/Gui/Application.h @@ -209,8 +209,9 @@ public: /** @name Appearance */ //@{ - /// Activate a named workbench + /// Activate a stylesheet void setStyleSheet(const QString& qssFile, bool tiledBackground); + QString replaceVariablesInQss(QString qssText); //@} /** @name User Commands */