Skip to content

Commit

Permalink
Gui: move handing of style sheets to Application::setStyleSheet to av…
Browse files Browse the repository at this point in the history
…oid code duplication
  • Loading branch information
wwmayer committed Mar 19, 2020
1 parent f4b7963 commit d496e71
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 122 deletions.
123 changes: 73 additions & 50 deletions src/Gui/Application.cpp
Expand Up @@ -2088,9 +2088,6 @@ void Application::runApplication(void)
}

hGrp = App::GetApplication().GetParameterGroupByPath("User parameter:BaseApp/Preferences/MainWindow");
QMdiArea* mdi = mw.findChild<QMdiArea*>();
mdi->setProperty("showImage", hGrp->GetBool("TiledBackground", false));

std::string style = hGrp->GetASCII("StyleSheet");
if (style.empty()) {
// check the branding settings
Expand All @@ -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);
Expand Down Expand Up @@ -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<QMdiArea*>();
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());
Expand Down
6 changes: 6 additions & 0 deletions src/Gui/Application.h
Expand Up @@ -198,6 +198,12 @@ class GuiExport Application
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
Expand Down
75 changes: 4 additions & 71 deletions src/Gui/DlgGeneralImp.cpp
Expand Up @@ -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<QMdiArea*>();
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()
Expand Down Expand Up @@ -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);
}

Expand Down
1 change: 0 additions & 1 deletion src/Gui/DlgGeneralImp.h
Expand Up @@ -56,7 +56,6 @@ class DlgGeneralImp : public PreferencePage

private:
std::unique_ptr<Ui_DlgGeneral> ui;
QString selectedStyleSheet;
};

} // namespace Dialog
Expand Down

0 comments on commit d496e71

Please sign in to comment.