From af5e7abe46e3b54cc9e938b804e5eeacfe693642 Mon Sep 17 00:00:00 2001 From: triplus Date: Tue, 17 Mar 2020 14:39:10 +0100 Subject: [PATCH] Use relative path to stylesheet fix #4130 --- src/Gui/Application.cpp | 21 ++++++++++++++++++-- src/Gui/DlgGeneralImp.cpp | 21 ++++++++++++++++++-- src/Mod/Start/StartPage/StartPage.py | 29 +++++++++++++++++++++++++--- 3 files changed, 64 insertions(+), 7 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index be4cef5e98a3..c8387c4e9746 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -2100,8 +2100,25 @@ void Application::runApplication(void) style = it->second; } if (!style.empty()) { - QFile f(QLatin1String(style.c_str())); - if (f.open(QFile::ReadOnly)) { + // Search for stylesheet in user, system and resources location + QString user = QString::fromUtf8((App::Application::getUserAppDataDir() + "Gui/Stylesheets/").c_str()); + QString system = QString::fromUtf8((App::Application::getResourceDir() + "Gui/Stylesheets/").c_str()); + QString resources = QLatin1String(":/stylesheets/"); + + QFile f; + if (QFile::exists(user + QLatin1String(style.c_str()))) { + f.setFileName(user + QLatin1String(style.c_str())); + } + else if (QFile::exists(system + QLatin1String(style.c_str()))) { + f.setFileName(system + QLatin1String(style.c_str())); + } + else if (QFile::exists(resources + QLatin1String(style.c_str()))) { + f.setFileName(resources + QLatin1String(style.c_str())); + } + else { + } + + if (f.open(QFile::ReadOnly | QFile::Text)) { mdi->setBackground(QBrush(Qt::NoBrush)); QTextStream str(&f); qApp->setStyleSheet(str.readAll()); diff --git a/src/Gui/DlgGeneralImp.cpp b/src/Gui/DlgGeneralImp.cpp index ff893d920edb..35b7cc0df8bc 100644 --- a/src/Gui/DlgGeneralImp.cpp +++ b/src/Gui/DlgGeneralImp.cpp @@ -171,7 +171,24 @@ void DlgGeneralImp::saveSettings() hGrp->SetASCII("StyleSheet", (const char*)sheet.toByteArray()); if (!sheet.toString().isEmpty()) { - QFile f(sheet.toString()); + // Search for stylesheet in user, system and resources location + QString user = QString::fromUtf8((App::Application::getUserAppDataDir() + "Gui/Stylesheets/").c_str()); + QString system = QString::fromUtf8((App::Application::getResourceDir() + "Gui/Stylesheets/").c_str()); + QString resources = QLatin1String(":/stylesheets/"); + + QFile f; + if (QFile::exists(user + sheet.toString())) { + f.setFileName(user + sheet.toString()); + } + else if (QFile::exists(system + sheet.toString())) { + f.setFileName(system + sheet.toString()); + } + else if (QFile::exists(resources + sheet.toString())) { + f.setFileName(resources + sheet.toString()); + } + else { + } + if (f.open(QFile::ReadOnly)) { mdi->setBackground(QBrush(Qt::NoBrush)); QTextStream str(&f); @@ -307,7 +324,7 @@ void DlgGeneralImp::loadSettings() fileNames = dir.entryInfoList(filter, QDir::Files, QDir::Name); for (QFileInfoList::iterator jt = fileNames.begin(); jt != fileNames.end(); ++jt) { if (cssFiles.find(jt->baseName()) == cssFiles.end()) { - cssFiles[jt->baseName()] = jt->absoluteFilePath(); + cssFiles[jt->baseName()] = jt->fileName(); } } } diff --git a/src/Mod/Start/StartPage/StartPage.py b/src/Mod/Start/StartPage/StartPage.py index 59832d82f4ab..1de6f5eba315 100644 --- a/src/Mod/Start/StartPage/StartPage.py +++ b/src/Mod/Start/StartPage/StartPage.py @@ -299,9 +299,32 @@ def handle(): if FreeCAD.ParamGet("User parameter:BaseApp/Preferences/Mod/Start").GetBool("UseStyleSheet",False): qssfile = FreeCAD.ParamGet("User parameter:BaseApp/Preferences/MainWindow").GetString("StyleSheet","") if qssfile: - with open(qssfile, 'r') as f: - ALTCSS = encode(f.read()) - HTML = HTML.replace("","") + # Search for stylesheet in user, system and resources locations + user = os.path.join(FreeCAD.getUserAppDataDir(), "Gui", "Stylesheets") + system = os.path.join(FreeCAD.getResourceDir(), "Gui", "Stylesheets") + resources = ":/stylesheets" + + res = False + if QtCore.QFile.exists(os.path.join(user, qssfile)): + path = os.path.join(user, qssfile) + elif QtCore.QFile.exists(os.path.join(system, qssfile)): + path = os.path.join(system, qssfile) + elif QtCore.QFile.exists(os.path.join(resources, qssfile)): + res = True + path = os.path.join(resources, qssfile) + else: + path = None + + if path: + if res: + f = QtCore.QFile(path) + if f.open(QtCore.QIODevice.ReadOnly | QtCore.QFile.Text): + ALTCSS = encode(QtCore.QTextStream(f).readAll()) + else: + with open(path, 'r') as f: + ALTCSS = encode(f.read()) + + HTML = HTML.replace("","") # turn tips off if needed