X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=blobdiff_plain;f=src%2Fuisupport%2Fqssparser.cpp;h=52ce79ac2ef610b642ab8826b62c064adba9af8f;hp=d93c7b2d0447ebe994f22f95e6eea7814fc9b44b;hb=0ff679f6bdd6729d12bee1ed7a2b6ac812c3e31c;hpb=526eccc444029c5a0d80694487c3aec6707cbeda diff --git a/src/uisupport/qssparser.cpp b/src/uisupport/qssparser.cpp index d93c7b2d..52ce79ac 100644 --- a/src/uisupport/qssparser.cpp +++ b/src/uisupport/qssparser.cpp @@ -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 @@ -83,14 +74,13 @@ void QssParser::loadStyleSheet(const QString &styleSheet) { while((pos = blockrx.indexIn(ss, pos)) >= 0) { //qDebug() << blockrx.cap(1) << blockrx.cap(2); - if(blockrx.cap(2) == "ChatLine") + if(blockrx.cap(1).startsWith("ChatLine")) parseChatLineData(blockrx.cap(1).trimmed(), blockrx.cap(2).trimmed()); //else // TODO: add moar here - pos += blockrx.matchedLength(); + ss.remove(pos, blockrx.matchedLength()); } - } void QssParser::parseChatLineData(const QString &decl, const QString &contents) { @@ -98,8 +88,46 @@ void QssParser::parseChatLineData(const QString &decl, const QString &contents) if(fmtType == UiStyle::Invalid) return; - + QTextCharFormat format; + foreach(QString line, contents.split(';', QString::SkipEmptyParts)) { + int idx = line.indexOf(':'); + if(idx <= 0) { + qWarning() << Q_FUNC_INFO << tr("Invalid property declaration: %1").arg(line.trimmed()); + continue; + } + QString property = line.left(idx).trimmed(); + QString value = line.mid(idx + 1).simplified(); + + if(property == "background" || property == "background-color") + format.setBackground(parseBrush(value)); + else if(property == "foreground" || property == "color") + 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); + } + } + + _formats[fmtType] = format; } quint64 QssParser::parseFormatType(const QString &decl) { @@ -117,15 +145,17 @@ quint64 QssParser::parseFormatType(const QString &decl) { // First determine the subelement if(!subElement.isEmpty()) { - if(subElement == "Timestamp") + if(subElement == "timestamp") fmtType |= UiStyle::Timestamp; - else if(subElement == "Sender") + else if(subElement == "sender") fmtType |= UiStyle::Sender; - else if(subElement == "Nick") + else if(subElement == "nick") fmtType |= UiStyle::Nick; - else if(subElement == "Hostmask") + else if(subElement == "contents") + fmtType |= UiStyle::Contents; + else if(subElement == "hostmask") fmtType |= UiStyle::Hostmask; - else if(subElement == "ModeFlags") + else if(subElement == "modeflags") fmtType |= UiStyle::ModeFlags; else { qWarning() << Q_FUNC_INFO << tr("Invalid subelement name in %1").arg(decl); @@ -135,35 +165,41 @@ quint64 QssParser::parseFormatType(const QString &decl) { // Now, figure out the message type if(!msgType.isEmpty()) { - if(msgType == "Plain") + if(msgType == "plain") fmtType |= UiStyle::PlainMsg; - else if(msgType == "Notice") + else if(msgType == "notice") fmtType |= UiStyle::NoticeMsg; - else if(msgType == "Server") - fmtType |= UiStyle::ServerMsg; - else if(msgType == "Error") - fmtType |= UiStyle::ErrorMsg; - else if(msgType == "Join") + 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") + else if(msgType == "part") fmtType |= UiStyle::PartMsg; - else if(msgType == "Quit") + else if(msgType == "quit") fmtType |= UiStyle::QuitMsg; - else if(msgType == "Kick") + 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); } } // 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)) { @@ -191,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 } } @@ -242,7 +304,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); @@ -251,10 +313,15 @@ void QssParser::parsePaletteData(const QString &decl, const QString &contents) { } } -QBrush QssParser::parseBrushValue(const QString &str) { - QColor c = parseColorValue(str); - if(c.isValid()) +QBrush QssParser::parseBrush(const QString &str, bool *ok) { + if(ok) + *ok = false; + QColor c = parseColor(str); + if(c.isValid()) { + if(ok) + *ok = true; return QBrush(c); + } if(str.startsWith("palette")) { // Palette color role QRegExp rx("palette\\s*\\(\\s*([a-z-]+)\\s*\\)"); @@ -286,6 +353,8 @@ QBrush QssParser::parseBrushValue(const QString &str) { } QLinearGradient gradient(x1, y1, x2, y2); gradient.setStops(stops); + if(ok) + *ok = true; return QBrush(gradient); } else if(str.startsWith("qconicalgradient")) { @@ -305,6 +374,8 @@ QBrush QssParser::parseBrushValue(const QString &str) { } QConicalGradient gradient(cx, cy, angle); gradient.setStops(stops); + if(ok) + *ok = true; return QBrush(gradient); } else if(str.startsWith("qradialgradient")) { @@ -326,13 +397,15 @@ QBrush QssParser::parseBrushValue(const QString &str) { } QRadialGradient gradient(cx, cy, radius, fx, fy); gradient.setStops(stops); + if(ok) + *ok = true; return QBrush(gradient); } 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) @@ -398,7 +471,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); @@ -409,3 +482,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); +}