Gui: move handing of style sheets to Application::setStyleSheet to avoid code duplication

This commit is contained in:
wmayer
2020-03-19 15:57:20 +01:00
parent f4b79637f3
commit d496e714c9
4 changed files with 83 additions and 122 deletions

View File

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

View File

@@ -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

View File

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

View File

@@ -56,7 +56,6 @@ private:
private:
std::unique_ptr<Ui_DlgGeneral> ui;
QString selectedStyleSheet;
};
} // namespace Dialog