Use relative path to stylesheet fix #4130

This commit is contained in:
triplus
2020-03-17 14:39:10 +01:00
committed by wwmayer
parent 3079fea303
commit 873769d021
3 changed files with 64 additions and 7 deletions

View File

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

View File

@@ -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();
}
}
}

View File

@@ -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("<!--QSS-->","<style type=\"text/css\">"+ALTCSS+"</style>")
# 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("<!--QSS-->","<style type=\"text/css\">"+ALTCSS+"</style>")
# turn tips off if needed