From d496e714c93dedfe80eec396c91dc760befa2540 Mon Sep 17 00:00:00 2001 From: wmayer Date: Thu, 19 Mar 2020 15:57:20 +0100 Subject: [PATCH] Gui: move handing of style sheets to Application::setStyleSheet to avoid code duplication --- src/Gui/Application.cpp | 123 ++++++++++++++++++++++---------------- src/Gui/Application.h | 6 ++ src/Gui/DlgGeneralImp.cpp | 75 ++--------------------- src/Gui/DlgGeneralImp.h | 1 - 4 files changed, 83 insertions(+), 122 deletions(-) diff --git a/src/Gui/Application.cpp b/src/Gui/Application.cpp index c8387c4e97..957485b0de 100644 --- a/src/Gui/Application.cpp +++ b/src/Gui/Application.cpp @@ -2088,9 +2088,6 @@ void Application::runApplication(void) } hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/MainWindow"); - QMdiArea* mdi = mw.findChild(); - mdi->setProperty("showImage", hGrp->GetBool("TiledBackground", false)); - std::string style = hGrp->GetASCII("StyleSheet"); if (style.empty()) { // check the branding settings @@ -2099,54 +2096,8 @@ void Application::runApplication(void) if (it != config.end()) style = it->second; } - if (!style.empty()) { - // 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()); - - ActionStyleEvent e(ActionStyleEvent::Clear); - qApp->sendEvent(&mw, &e); - } - } - else { - if (hGrp->GetBool("TiledBackground", false)) { - mdi->setBackground(QPixmap(QLatin1String("images:background.png"))); - } -#if QT_VERSION == 0x050600 && defined(Q_OS_WIN32) - // Under Windows the tree indicator branch gets corrupted after a while. - // For more details see also https://bugreports.qt.io/browse/QTBUG-52230 - // and https://codereview.qt-project.org/#/c/154357/2//ALL,unified - // A workaround for Qt 5.6.0 is to set a minimal style sheet. - QString qss = QString::fromLatin1( - "QTreeView::branch:closed:has-children {\n" - " image: url(:/icons/style/windows_branch_closed.png);\n" - "}\n" - "\n" - "QTreeView::branch:open:has-children {\n" - " image: url(:/icons/style/windows_branch_open.png);\n" - "}\n"); - qApp->setStyleSheet(qss); -#endif - } + app.setStyleSheet(QLatin1String(style.c_str()), hGrp->GetBool("TiledBackground", false)); //initialize spaceball. mainApp.initSpaceball(&mw); @@ -2210,6 +2161,78 @@ void Application::runApplication(void) Base::Console().Log("Finish: Event loop left\n"); } +void Application::setStyleSheet(const QString& qssFile, bool tiledBackground) +{ + Gui::MainWindow* mw = getMainWindow(); + QMdiArea* mdi = mw->findChild(); + mdi->setProperty("showImage", tiledBackground); + + QString current = mw->property("fc_currentStyleSheet").toString(); + mw->setProperty("fc_currentStyleSheet", qssFile); + + if (!qssFile.isEmpty() && current != qssFile) { + // 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 + qssFile)) { + f.setFileName(user + qssFile); + } + else if (QFile::exists(system + qssFile)) { + f.setFileName(system + qssFile); + } + else if (QFile::exists(resources + qssFile)) { + f.setFileName(resources + qssFile); + } + else { + } + + if (f.open(QFile::ReadOnly | QFile::Text)) { + mdi->setBackground(QBrush(Qt::NoBrush)); + QTextStream str(&f); + qApp->setStyleSheet(str.readAll()); + + ActionStyleEvent e(ActionStyleEvent::Clear); + qApp->sendEvent(mw, &e); + } + } + + if (qssFile.isEmpty()) { + if (tiledBackground) { + qApp->setStyleSheet(QString()); + ActionStyleEvent e(ActionStyleEvent::Restore); + qApp->sendEvent(getMainWindow(), &e); + mdi->setBackground(QPixmap(QLatin1String("images:background.png"))); + } + else { + qApp->setStyleSheet(QString()); + ActionStyleEvent e(ActionStyleEvent::Restore); + qApp->sendEvent(getMainWindow(), &e); + mdi->setBackground(QBrush(QColor(160,160,160))); + } +#if QT_VERSION == 0x050600 && defined(Q_OS_WIN32) + // Under Windows the tree indicator branch gets corrupted after a while. + // For more details see also https://bugreports.qt.io/browse/QTBUG-52230 + // and https://codereview.qt-project.org/#/c/154357/2//ALL,unified + // A workaround for Qt 5.6.0 is to set a minimal style sheet. + QString qss = QString::fromLatin1( + "QTreeView::branch:closed:has-children {\n" + " image: url(:/icons/style/windows_branch_closed.png);\n" + "}\n" + "\n" + "QTreeView::branch:open:has-children {\n" + " image: url(:/icons/style/windows_branch_open.png);\n" + "}\n"); + qApp->setStyleSheet(qss); +#endif + } + + if (mdi->style()) + mdi->style()->unpolish(qApp); +} + void Application::checkForPreviousCrashes() { QDir tmp = QString::fromUtf8(App::Application::getTempPath().c_str()); diff --git a/src/Gui/Application.h b/src/Gui/Application.h index 98537ddbd4..df5df231c8 100644 --- a/src/Gui/Application.h +++ b/src/Gui/Application.h @@ -198,6 +198,12 @@ public: void setupContextMenu(const char* recipient, MenuItem*) const; //@} + /** @name Appearance */ + //@{ + /// Activate a named workbench + void setStyleSheet(const QString& qssFile, bool tiledBackground); + //@} + /** @name User Commands */ //@{ /// Get macro manager diff --git a/src/Gui/DlgGeneralImp.cpp b/src/Gui/DlgGeneralImp.cpp index 35b7cc0df8..d30ccd9af6 100644 --- a/src/Gui/DlgGeneralImp.cpp +++ b/src/Gui/DlgGeneralImp.cpp @@ -162,77 +162,10 @@ void DlgGeneralImp::saveSettings() hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/MainWindow"); hGrp->SetBool("TiledBackground", ui->tiledBackground->isChecked()); - QMdiArea* mdi = getMainWindow()->findChild(); - mdi->setProperty("showImage", ui->tiledBackground->isChecked()); QVariant sheet = ui->StyleSheets->itemData(ui->StyleSheets->currentIndex()); - if (this->selectedStyleSheet != sheet.toString()) { - this->selectedStyleSheet = sheet.toString(); - hGrp->SetASCII("StyleSheet", (const char*)sheet.toByteArray()); - - if (!sheet.toString().isEmpty()) { - // 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); - qApp->setStyleSheet(str.readAll()); - - ActionStyleEvent e(ActionStyleEvent::Clear); - qApp->sendEvent(getMainWindow(), &e); - } - } - } - - if (sheet.toString().isEmpty()) { - if (ui->tiledBackground->isChecked()) { - qApp->setStyleSheet(QString()); - ActionStyleEvent e(ActionStyleEvent::Restore); - qApp->sendEvent(getMainWindow(), &e); - mdi->setBackground(QPixmap(QLatin1String("images:background.png"))); - } - else { - qApp->setStyleSheet(QString()); - ActionStyleEvent e(ActionStyleEvent::Restore); - qApp->sendEvent(getMainWindow(), &e); - mdi->setBackground(QBrush(QColor(160,160,160))); - } - -#if QT_VERSION == 0x050600 && defined(Q_OS_WIN32) - // Under Windows the tree indicator branch gets corrupted after a while. - // For more details see also https://bugreports.qt.io/browse/QTBUG-52230 - // and https://codereview.qt-project.org/#/c/154357/2//ALL,unified - // A workaround for Qt 5.6.0 is to set a minimal style sheet. - QString qss = QString::fromLatin1( - "QTreeView::branch:closed:has-children {\n" - " image: url(:/icons/style/windows_branch_closed.png);\n" - "}\n" - "\n" - "QTreeView::branch:open:has-children {\n" - " image: url(:/icons/style/windows_branch_open.png);\n" - "}\n"); - qApp->setStyleSheet(qss); -#endif - } - - if (mdi->style()) - mdi->style()->unpolish(qApp); + hGrp->SetASCII("StyleSheet", (const char*)sheet.toByteArray()); + Application::Instance->setStyleSheet(sheet.toString(), ui->tiledBackground->isChecked()); } void DlgGeneralImp::loadSettings() @@ -335,8 +268,8 @@ void DlgGeneralImp::loadSettings() ui->StyleSheets->addItem(it.key(), it.value()); } - this->selectedStyleSheet = QString::fromLatin1(hGrp->GetASCII("StyleSheet").c_str()); - index = ui->StyleSheets->findData(this->selectedStyleSheet); + QString selectedStyleSheet = QString::fromLatin1(hGrp->GetASCII("StyleSheet").c_str()); + index = ui->StyleSheets->findData(selectedStyleSheet); if (index > -1) ui->StyleSheets->setCurrentIndex(index); } diff --git a/src/Gui/DlgGeneralImp.h b/src/Gui/DlgGeneralImp.h index 6316f00ec4..eac13fa8b8 100644 --- a/src/Gui/DlgGeneralImp.h +++ b/src/Gui/DlgGeneralImp.h @@ -56,7 +56,6 @@ private: private: std::unique_ptr ui; - QString selectedStyleSheet; }; } // namespace Dialog