From 04c923c596fbde976b23979b92b0636635f97951 Mon Sep 17 00:00:00 2001 From: Manuel Nickschas Date: Wed, 24 Jun 2009 09:44:38 +0200 Subject: [PATCH] Add --qss parameter to load a custom stylesheet Because there doesn't seem to be sane way to hijack Qt's -stylesheet parameter, we introduce our own. This also allows us to add incompatible extensions to QSS, because we can remove them before handing over the pre-parsed stylesheet to QApplication. Note that -stylesheet is now ignored. The style engine now loads $datadir/default.qss, $configdir/custom.qss and --qss "file.qss" in that order. Later block declarations override earlier ones, if they are identical. That way, you can override parts of the system-installed default.qss in your own configdir or on the command line. --- src/common/main.cpp | 1 + src/uisupport/qssparser.cpp | 15 +--------- src/uisupport/qssparser.h | 2 +- src/uisupport/uistyle.cpp | 60 +++++++++++++++++++++++++++---------- src/uisupport/uistyle.h | 8 ++--- 5 files changed, 50 insertions(+), 36 deletions(-) diff --git a/src/common/main.cpp b/src/common/main.cpp index 8669f1dd..97a05704 100644 --- a/src/common/main.cpp +++ b/src/common/main.cpp @@ -95,6 +95,7 @@ int main(int argc, char **argv) { #ifndef BUILD_CORE // put client-only arguments here + cliParser->addOption("qss ", 0, "Load a custom application stylesheet"); cliParser->addSwitch("debugbufferswitches", 0, "Enables debugging for bufferswitches"); cliParser->addSwitch("debugmodel", 0, "Enables debugging for models"); #endif diff --git a/src/uisupport/qssparser.cpp b/src/uisupport/qssparser.cpp index 585b769d..91ecac5a 100644 --- a/src/uisupport/qssparser.cpp +++ b/src/uisupport/qssparser.cpp @@ -51,20 +51,7 @@ QssParser::QssParser() _paletteColorRoles["window-text"] = QPalette::WindowText; } -void QssParser::loadStyleSheet(const QString &styleSheet) { - QString ss = styleSheet; - ss = "file:////home/sputnick/devel/quassel/test.qss"; // FIXME - if(ss.startsWith("file:///")) { - ss.remove(0, 8); - QFile file(ss); - if(file.open(QFile::ReadOnly)) { - QTextStream stream(&file); - ss = stream.readAll(); - } else { - qWarning() << tr("Could not read stylesheet \"%1\"!").arg(file.fileName()); - return; - } - } +void QssParser::processStyleSheet(QString &ss) { if(ss.isEmpty()) return; diff --git a/src/uisupport/qssparser.h b/src/uisupport/qssparser.h index 37a642ef..c92b4060 100644 --- a/src/uisupport/qssparser.h +++ b/src/uisupport/qssparser.h @@ -29,7 +29,7 @@ class QssParser { public: QssParser(); - void loadStyleSheet(const QString &sheet); + void processStyleSheet(QString &sheet); inline QPalette palette() const { return _palette; } inline const QHash& formats() const { return _formats; } diff --git a/src/uisupport/uistyle.cpp b/src/uisupport/uistyle.cpp index ed5156ce..6478f8cc 100644 --- a/src/uisupport/uistyle.cpp +++ b/src/uisupport/uistyle.cpp @@ -56,6 +56,50 @@ UiStyle::~ UiStyle() { qDeleteAll(_metricsCache); } +void UiStyle::loadStyleSheet() { + qDeleteAll(_metricsCache); + _metricsCache.clear(); + _formatCache.clear(); + + QString styleSheet; + + styleSheet += loadStyleSheet("file:///" + Quassel::findDataFilePath("default.qss")); + styleSheet += loadStyleSheet("file:///" + Quassel::configDirPath() + "custom.qss"); + // styleSheet += loadStyleSheet("file:///" + some custom file name); FIXME + styleSheet += loadStyleSheet("file:///" + Quassel::optionValue("qss"), true); + + if(styleSheet.isEmpty()) + return; + + QssParser parser; + parser.processStyleSheet(styleSheet); + QApplication::setPalette(parser.palette()); + _formatCache = parser.formats(); + + qApp->setStyleSheet(styleSheet); // pass the remaining sections to the application +} + +QString UiStyle::loadStyleSheet(const QString &styleSheet, bool shouldExist) { + QString ss = styleSheet; + if(ss.startsWith("file:///")) { + ss.remove(0, 8); + if(ss.isEmpty()) + return QString(); + + QFile file(ss); + if(file.open(QFile::ReadOnly)) { + QTextStream stream(&file); + ss = stream.readAll(); + file.close(); + } else { + if(shouldExist) + qWarning() << "Could not open stylesheet file:" << file.fileName(); + return QString(); + } + } + return ss; +} + QTextCharFormat UiStyle::cachedFormat(quint64 key) const { return _formatCache.value(key, QTextCharFormat()); } @@ -543,19 +587,3 @@ QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList) { } return in; } - -/***********************************************************************************/ -// Stylesheet handling -/***********************************************************************************/ - -void UiStyle::loadStyleSheet() { - QssParser parser; - parser.loadStyleSheet(qApp->styleSheet()); - - // TODO handle results - QApplication::setPalette(parser.palette()); - - qDeleteAll(_metricsCache); - _metricsCache.clear(); - _formatCache = parser.formats(); -} diff --git a/src/uisupport/uistyle.h b/src/uisupport/uistyle.h index f031085e..98e15ed8 100644 --- a/src/uisupport/uistyle.h +++ b/src/uisupport/uistyle.h @@ -103,6 +103,8 @@ public: class StyledMessage; + void loadStyleSheet(); + static FormatType formatType(Message::Type msgType); static StyledString styleString(const QString &string, quint32 baseFormat = None); static QString mircToInternal(const QString &); @@ -115,11 +117,7 @@ public: QList toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel = 0); protected: - void loadStyleSheet(); - - //! Determines the format set to be used for the given hostmask - //int formatSetIndex(const QString &hostmask) const; - //int formatSetIndexForSelf() const; + QString loadStyleSheet(const QString &name, bool shouldExist = false); QTextCharFormat cachedFormat(quint64 key) const; QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel = 0) const; -- 2.20.1