Add --qss parameter to load a custom stylesheet
authorManuel Nickschas <sputnick@quassel-irc.org>
Wed, 24 Jun 2009 07:44:38 +0000 (09:44 +0200)
committerManuel Nickschas <sputnick@quassel-irc.org>
Thu, 6 Aug 2009 18:25:05 +0000 (20:25 +0200)
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
src/uisupport/qssparser.cpp
src/uisupport/qssparser.h
src/uisupport/uistyle.cpp
src/uisupport/uistyle.h

index 8669f1d..97a0570 100644 (file)
@@ -95,6 +95,7 @@ int main(int argc, char **argv) {
 
 #ifndef BUILD_CORE
   // put client-only arguments here
 
 #ifndef BUILD_CORE
   // put client-only arguments here
+  cliParser->addOption("qss <file.qss>", 0, "Load a custom application stylesheet");
   cliParser->addSwitch("debugbufferswitches", 0, "Enables debugging for bufferswitches");
   cliParser->addSwitch("debugmodel", 0, "Enables debugging for models");
 #endif
   cliParser->addSwitch("debugbufferswitches", 0, "Enables debugging for bufferswitches");
   cliParser->addSwitch("debugmodel", 0, "Enables debugging for models");
 #endif
index 585b769..91ecac5 100644 (file)
@@ -51,20 +51,7 @@ QssParser::QssParser()
   _paletteColorRoles["window-text"] = QPalette::WindowText;
 }
 
   _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;
 
   if(ss.isEmpty())
     return;
 
index 37a642e..c92b406 100644 (file)
@@ -29,7 +29,7 @@ class QssParser {
   public:
     QssParser();
 
   public:
     QssParser();
 
-    void loadStyleSheet(const QString &sheet);
+    void processStyleSheet(QString &sheet);
 
     inline QPalette palette() const { return _palette; }
     inline const QHash<quint64, QTextCharFormat>& formats() const { return _formats; }
 
     inline QPalette palette() const { return _palette; }
     inline const QHash<quint64, QTextCharFormat>& formats() const { return _formats; }
index ed5156c..6478f8c 100644 (file)
@@ -56,6 +56,50 @@ UiStyle::~ UiStyle() {
   qDeleteAll(_metricsCache);
 }
 
   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());
 }
 QTextCharFormat UiStyle::cachedFormat(quint64 key) const {
   return _formatCache.value(key, QTextCharFormat());
 }
@@ -543,19 +587,3 @@ QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList) {
   }
   return in;
 }
   }
   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();
-}
index f031085..98e15ed 100644 (file)
@@ -103,6 +103,8 @@ public:
 
   class StyledMessage;
 
 
   class StyledMessage;
 
+  void loadStyleSheet();
+
   static FormatType formatType(Message::Type msgType);
   static StyledString styleString(const QString &string, quint32 baseFormat = None);
   static QString mircToInternal(const QString &);
   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<QTextLayout::FormatRange> toTextLayoutList(const FormatList &, int textLength, quint32 messageLabel = 0);
 
 protected:
   QList<QTextLayout::FormatRange> 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;
 
   QTextCharFormat cachedFormat(quint64 key) const;
   QTextCharFormat cachedFormat(quint32 formatType, quint32 messageLabel = 0) const;