Make sender-hash based styling ("colored nicks") work again
[quassel.git] / src / uisupport / qssparser.cpp
index 11a8f53..c25bceb 100644 (file)
@@ -107,12 +107,30 @@ void QssParser::parseChatLineData(const QString &decl, const QString &contents)
       continue;
     }
     QString property = line.left(idx).trimmed();
-    QString value = line.mid(idx + 1).trimmed();
+    QString value = line.mid(idx + 1).simplified();
 
     if(property == "background" || property == "background-color")
-      format.setBackground(parseBrushValue(value));
+      format.setBackground(parseBrush(value));
     else if(property == "foreground" || property == "color")
-      format.setForeground(parseBrushValue(value));
+      format.setForeground(parseBrush(value));
+
+    // font-related properties
+    else if(property.startsWith("font")) {
+      if(property == "font")
+        parseFont(value, &format);
+      else if(property == "font-style")
+        parseFontStyle(value, &format);
+      else if(property == "font-weight")
+        parseFontWeight(value, &format);
+      else if(property == "font-size")
+        parseFontSize(value, &format);
+      else if(property == "font-family")
+        parseFontFamily(value, &format);
+      else {
+        qWarning() << Q_FUNC_INFO << tr("Invalid font property: %1").arg(line);
+        continue;
+      }
+    }
 
     else {
       qWarning() << Q_FUNC_INFO << tr("Unknown ChatLine property: %1").arg(property);
@@ -143,6 +161,8 @@ quint64 QssParser::parseFormatType(const QString &decl) {
       fmtType |= UiStyle::Sender;
     else if(subElement == "nick")
       fmtType |= UiStyle::Nick;
+    else if(subElement == "contents")
+      fmtType |= UiStyle::Contents;
     else if(subElement == "hostmask")
       fmtType |= UiStyle::Hostmask;
     else if(subElement == "modeflags")
@@ -159,10 +179,12 @@ quint64 QssParser::parseFormatType(const QString &decl) {
       fmtType |= UiStyle::PlainMsg;
     else if(msgType == "notice")
       fmtType |= UiStyle::NoticeMsg;
-    else if(msgType == "server")
-      fmtType |= UiStyle::ServerMsg;
-    else if(msgType == "error")
-      fmtType |= UiStyle::ErrorMsg;
+    else if(msgType == "action")
+      fmtType |= UiStyle::ActionMsg;
+    else if(msgType == "nick")
+      fmtType |= UiStyle::NickMsg;
+    else if(msgType == "mode")
+      fmtType |= UiStyle::ModeMsg;
     else if(msgType == "join")
       fmtType |= UiStyle::JoinMsg;
     else if(msgType == "part")
@@ -171,12 +193,16 @@ quint64 QssParser::parseFormatType(const QString &decl) {
       fmtType |= UiStyle::QuitMsg;
     else if(msgType == "kick")
       fmtType |= UiStyle::KickMsg;
-    else if(msgType == "rename")
-      fmtType |= UiStyle::RenameMsg;
-    else if(msgType == "mode")
-      fmtType |= UiStyle::ModeMsg;
-    else if(msgType == "action")
-      fmtType |= UiStyle::ActionMsg;
+    else if(msgType == "kill")
+      fmtType |= UiStyle::KillMsg;
+    else if(msgType == "server")
+      fmtType |= UiStyle::ServerMsg;
+    else if(msgType == "info")
+      fmtType |= UiStyle::InfoMsg;
+    else if(msgType == "error")
+      fmtType |= UiStyle::ErrorMsg;
+    else if(msgType == "daychange")
+      fmtType |= UiStyle::DayChangeMsg;
     else {
       qWarning() << Q_FUNC_INFO << tr("Invalid message type in %1").arg(decl);
     }
@@ -211,8 +237,8 @@ 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;
@@ -262,7 +288,7 @@ void QssParser::parsePaletteData(const QString &decl, const QString &contents) {
       qWarning() << Q_FUNC_INFO << tr("Unknown palette role name: %1").arg(rolestr);
       continue;
     }
-    QBrush brush = parseBrushValue(brushstr);
+    QBrush brush = parseBrush(brushstr);
     if(colorGroups.count()) {
       foreach(QPalette::ColorGroup group, colorGroups)
         _palette.setBrush(group, _paletteColorRoles.value(rolestr), brush);
@@ -271,10 +297,10 @@ void QssParser::parsePaletteData(const QString &decl, const QString &contents) {
   }
 }
 
-QBrush QssParser::parseBrushValue(const QString &str, bool *ok) {
+QBrush QssParser::parseBrush(const QString &str, bool *ok) {
   if(ok)
     *ok = false;
-  QColor c = parseColorValue(str);
+  QColor c = parseColor(str);
   if(c.isValid()) {
     if(ok)
       *ok = true;
@@ -363,7 +389,7 @@ QBrush QssParser::parseBrushValue(const QString &str, bool *ok) {
   return QBrush();
 }
 
-QColor QssParser::parseColorValue(const QString &str) {
+QColor QssParser::parseColor(const QString &str) {
   if(str.startsWith("rgba")) {
     ColorTuple tuple = parseColorTuple(str.mid(4));
     if(tuple.count() == 4)
@@ -429,7 +455,7 @@ QGradientStops QssParser::parseGradientStops(const QString &str_) {
   int idx;
   while((idx = rx.indexIn(str)) == 0) {
     qreal x = rx.cap(1).toDouble();
-    QColor c = parseColorValue(rx.cap(3));
+    QColor c = parseColor(rx.cap(3));
     if(!c.isValid())
       return QGradientStops();
     result << QGradientStop(x, c);
@@ -440,3 +466,83 @@ QGradientStops QssParser::parseGradientStops(const QString &str_) {
 
   return result;
 }
+
+/******** Font Properties ********/
+
+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;
+  }
+  format->setFontItalic(false);
+  format->setFontWeight(QFont::Normal);
+  QStringList proplist = rx.cap(1).split(' ', QString::SkipEmptyParts);
+  foreach(QString prop, proplist) {
+    if(prop == "italic")
+      format->setFontItalic(true);
+    //else if(prop == "oblique")
+    //  format->setStyle(QFont::StyleOblique);
+    else if(prop == "bold")
+      format->setFontWeight(QFont::Bold);
+    else { // number
+      int w = prop.toInt();
+      format->setFontWeight(qMin(w / 8, 99)); // taken from Qt's qss parser
+    }
+  }
+
+  if(rx.cap(3) == "px")
+    format->setProperty(QTextFormat::FontPixelSize, rx.cap(2).toInt());
+  else
+    format->setFontPointSize(rx.cap(2).toInt());
+
+  format->setFontFamily(rx.cap(4));
+}
+
+void QssParser::parseFontStyle(const QString& value, QTextCharFormat* format) {
+  if(value == "normal")
+    format->setFontItalic(false);
+  else if(value == "italic")
+    format->setFontItalic(true);
+  //else if(value == "oblique")
+  //  format->setStyle(QFont::StyleOblique);
+  else {
+    qWarning() << Q_FUNC_INFO << tr("Invalid font style specification: %1").arg(value);
+  }
+}
+
+void QssParser::parseFontWeight(const QString& value, QTextCharFormat* format) {
+  if(value == "normal")
+    format->setFontWeight(QFont::Normal);
+  else if(value == "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;
+    }
+    format->setFontWeight(qMin(w / 8, 99)); // taken from Qt's qss parser
+  }
+}
+
+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;
+  }
+  if(rx.cap(2) == "px")
+    format->setProperty(QTextFormat::FontPixelSize, rx.cap(1).toInt());
+  else
+    format->setFontPointSize(rx.cap(1).toInt());
+}
+
+void QssParser::parseFontFamily(const QString& value, QTextCharFormat* format) {
+  QString family = value;
+  if(family.startsWith('"') && family.endsWith('"')) {
+    family = family.mid(1, family.length() - 2);
+  }
+  format->setFontFamily(family);
+}