Parse mIRC colors in stylesheets
[quassel.git] / src / uisupport / qssparser.cpp
index 41bd0aa..52ce79a 100644 (file)
@@ -51,30 +51,21 @@ 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;
 
-  // Now we have the stylesheet itself in ss, start parsing
+  // Remove C-style comments /* */ or //
+  QRegExp commentRx("(//.*(\\n|$)|/\\*.*\\*/)");
+  commentRx.setMinimal(true);
+  ss.remove(commentRx);
+
   // Palette definitions first, so we can apply roles later on
   QRegExp paletterx("(Palette[^{]*)\\{([^}]+)\\}");
   int pos = 0;
   while((pos = paletterx.indexIn(ss, pos)) >= 0) {
     parsePaletteData(paletterx.cap(1).trimmed(), paletterx.cap(2).trimmed());
-    pos += paletterx.matchedLength();
+    ss.remove(pos, paletterx.matchedLength());
   }
 
   // Now we can parse the rest of our custom blocks
@@ -88,9 +79,8 @@ void QssParser::loadStyleSheet(const QString &styleSheet) {
     //else
     // TODO: add moar here
 
-    pos += blockrx.matchedLength();
+    ss.remove(pos, blockrx.matchedLength());
   }
-
 }
 
 void QssParser::parseChatLineData(const QString &decl, const QString &contents) {
@@ -116,24 +106,20 @@ void QssParser::parseChatLineData(const QString &decl, const QString &contents)
 
     // font-related properties
     else if(property.startsWith("font")) {
-      bool ok;
-      QFont font = format.font();
       if(property == "font")
-        ok = parseFont(value, &font);
+        parseFont(value, &format);
       else if(property == "font-style")
-        ok = parseFontStyle(value, &font);
+        parseFontStyle(value, &format);
       else if(property == "font-weight")
-        ok = parseFontWeight(value, &font);
+        parseFontWeight(value, &format);
       else if(property == "font-size")
-        ok = parseFontSize(value, &font);
+        parseFontSize(value, &format);
       else if(property == "font-family")
-        ok = parseFontFamily(value, &font);
+        parseFontFamily(value, &format);
       else {
         qWarning() << Q_FUNC_INFO << tr("Invalid font property: %1").arg(line);
         continue;
       }
-      if(ok)
-        format.setFont(font);
     }
 
     else {
@@ -213,7 +199,7 @@ quint64 QssParser::parseFormatType(const QString &decl) {
   }
 
   // Next up: conditional (formats, labels, nickhash)
-  QRegExp condRx("\\s*(\\w+)\\s*=\\s*\"(\\w+)\"\\s*");
+  QRegExp condRx("\\s*([\\w\\-]+)\\s*=\\s*\"(\\w+)\"\\s*");
   if(!conditions.isEmpty()) {
     foreach(const QString &cond, conditions.split(',', QString::SkipEmptyParts)) {
       if(!condRx.exactMatch(cond)) {
@@ -241,14 +227,40 @@ quint64 QssParser::parseFormatType(const QString &decl) {
               qWarning() << Q_FUNC_INFO << tr("Invalid senderhash specification: %1").arg(condValue);
               return UiStyle::Invalid;
             }
-            if(val >= 255) {
-              qWarning() << Q_FUNC_INFO << tr("Senderhash can be at most \"fe\"!");
+            if(val >= 16) {
+              qWarning() << Q_FUNC_INFO << tr("Senderhash can be at most \"0x0f\"!");
               return UiStyle::Invalid;
             }
             fmtType |= val << 48;
           }
+      } else if(condName == "format") {
+        if(condValue == "bold")
+          fmtType |= UiStyle::Bold;
+        else if(condValue == "italic")
+          fmtType |= UiStyle::Italic;
+        else if(condValue == "underline")
+          fmtType |= UiStyle::Underline;
+        else if(condValue == "reverse")
+          fmtType |= UiStyle::Reverse;
+        else {
+          qWarning() << Q_FUNC_INFO << tr("Invalid format name: %1").arg(condValue);
+          return UiStyle::Invalid;
+        }
+      } else if(condName == "fg-color" || condName == "bg-color") {
+        bool ok;
+        quint8 col = condValue.toUInt(&ok, 16);
+        if(!ok || col > 0x0f) {
+          qWarning() << Q_FUNC_INFO << tr("Illegal IRC color specification (must be between 00 and 0f): %1").arg(condValue);
+          return UiStyle::Invalid;
+        }
+        if(condName == "fg-color")
+          fmtType |= 0x00400000 | (col << 24);
+        else
+          fmtType |= 0x00800000 | (col << 28);
+      } else {
+        qWarning() << Q_FUNC_INFO << tr("Unhandled condition: %1").arg(condName);
+        return UiStyle::Invalid;
       }
-      // TODO: colors
     }
   }
 
@@ -473,86 +485,80 @@ QGradientStops QssParser::parseGradientStops(const QString &str_) {
 
 /******** Font Properties ********/
 
-bool QssParser::parseFont(const QString &value, QFont *font) {
+void QssParser::parseFont(const QString& value, QTextCharFormat* format) {
   QRegExp rx("((?:(?:normal|italic|oblique|bold|100|200|300|400|500|600|700|800|900) ){0,2}) ?(\\d+)(pt|px)? \"(.*)\"");
   if(!rx.exactMatch(value)) {
     qWarning() << Q_FUNC_INFO << tr("Invalid font specification: %1").arg(value);
-    return false;
+    return;
   }
-  font->setStyle(QFont::StyleNormal);
-  font->setWeight(QFont::Normal);
+  format->setFontItalic(false);
+  format->setFontWeight(QFont::Normal);
   QStringList proplist = rx.cap(1).split(' ', QString::SkipEmptyParts);
   foreach(QString prop, proplist) {
     if(prop == "italic")
-      font->setStyle(QFont::StyleItalic);
-    else if(prop == "oblique")
-      font->setStyle(QFont::StyleOblique);
+      format->setFontItalic(true);
+    //else if(prop == "oblique")
+    //  format->setStyle(QFont::StyleOblique);
     else if(prop == "bold")
-      font->setWeight(QFont::Bold);
+      format->setFontWeight(QFont::Bold);
     else { // number
       int w = prop.toInt();
-      font->setWeight(qMin(w / 8, 99)); // taken from Qt's qss parser
+      format->setFontWeight(qMin(w / 8, 99)); // taken from Qt's qss parser
     }
   }
 
   if(rx.cap(3) == "px")
-    font->setPixelSize(rx.cap(2).toInt());
+    format->setProperty(QTextFormat::FontPixelSize, rx.cap(2).toInt());
   else
-    font->setPointSize(rx.cap(2).toInt());
+    format->setFontPointSize(rx.cap(2).toInt());
 
-  font->setFamily(rx.cap(4));
-  return true;
+  format->setFontFamily(rx.cap(4));
 }
 
-bool QssParser::parseFontStyle(const QString &value, QFont *font) {
+void QssParser::parseFontStyle(const QString& value, QTextCharFormat* format) {
   if(value == "normal")
-    font->setStyle(QFont::StyleNormal);
+    format->setFontItalic(false);
   else if(value == "italic")
-    font->setStyle(QFont::StyleItalic);
-  else if(value == "oblique")
-    font->setStyle(QFont::StyleOblique);
+    format->setFontItalic(true);
+  //else if(value == "oblique")
+  //  format->setStyle(QFont::StyleOblique);
   else {
     qWarning() << Q_FUNC_INFO << tr("Invalid font style specification: %1").arg(value);
-    return false;
   }
-  return true;
 }
 
-bool QssParser::parseFontWeight(const QString &value, QFont *font) {
+void QssParser::parseFontWeight(const QString& value, QTextCharFormat* format) {
   if(value == "normal")
-    font->setWeight(QFont::Normal);
+    format->setFontWeight(QFont::Normal);
   else if(value == "bold")
-    font->setWeight(QFont::Bold);
+    format->setFontWeight(QFont::Bold);
   else {
     bool ok;
     int w = value.toInt(&ok);
     if(!ok) {
       qWarning() << Q_FUNC_INFO << tr("Invalid font weight specification: %1").arg(value);
-      return false;
+      return;
     }
-    font->setWeight(qMin(w / 8, 99)); // taken from Qt's qss parser
+    format->setFontWeight(qMin(w / 8, 99)); // taken from Qt's qss parser
   }
-  return true;
 }
 
-bool QssParser::parseFontSize(const QString &value, QFont *font) {
+void QssParser::parseFontSize(const QString& value, QTextCharFormat* format) {
   QRegExp rx("\\(d+)(pt|px)");
   if(!rx.exactMatch(value)) {
     qWarning() << Q_FUNC_INFO << tr("Invalid font size specification: %1").arg(value);
-    return false;
+    return;
   }
   if(rx.cap(2) == "px")
-    font->setPixelSize(rx.cap(1).toInt());
+    format->setProperty(QTextFormat::FontPixelSize, rx.cap(1).toInt());
   else
-    font->setPointSize(rx.cap(1).toInt());
-  return true;
+    format->setFontPointSize(rx.cap(1).toInt());
 }
 
-bool QssParser::parseFontFamily(const QString &value, QFont *font) {
+void QssParser::parseFontFamily(const QString& value, QTextCharFormat* format) {
   QString family = value;
   if(family.startsWith('"') && family.endsWith('"')) {
-    family = family.mid(1, family.length() -2);
+    family = family.mid(1, family.length() - 2);
   }
-  font->setFamily(family);
-  return true;
+  format->setFontFamily(family);
 }