From: Manuel Nickschas Date: Wed, 7 Mar 2018 00:31:34 +0000 (+0100) Subject: uistyle: Support extended mIRC colors X-Git-Tag: travis-deploy-test~169 X-Git-Url: https://git.quassel-irc.org/?p=quassel.git;a=commitdiff_plain;h=e6f810169117c836c22ae1146e469e61dec95f6f;hp=ac7a58dd970833da2336f6ce035ec55515bac0f1 uistyle: Support extended mIRC colors Support mIRC color values between 16 and 98, as specified in [1]. Extended color values are not styleable. [1] https://modern.ircdocs.horse/formatting.html --- diff --git a/src/qtui/chatitem.cpp b/src/qtui/chatitem.cpp index e9922d24..3dd3b2a7 100644 --- a/src/qtui/chatitem.cpp +++ b/src/qtui/chatitem.cpp @@ -294,7 +294,7 @@ QVector ChatItem::selectionFormats() const if (!hasSelection()) return QVector(); - int start, end; + quint16 start, end; if (_selectionMode == FullSelection) { start = 0; end = data(MessageModel::DisplayRole).toString().length(); diff --git a/src/qtui/chatline.cpp b/src/qtui/chatline.cpp index 0c666220..649b7618 100644 --- a/src/qtui/chatline.cpp +++ b/src/qtui/chatline.cpp @@ -217,13 +217,13 @@ void ChatLine::paint(QPainter *painter, const QStyleOptionGraphicsItem *option, Message::Type type = (Message::Type)myIdx.data(MessageModel::TypeRole).toInt(); UiStyle::MessageLabel label = myIdx.data(ChatLineModel::MsgLabelRole).value(); - QTextCharFormat msgFmt = QtUi::style()->format(UiStyle::formatType(type), label); + QTextCharFormat msgFmt = QtUi::style()->format({UiStyle::formatType(type), {}, {}}, label); if (msgFmt.hasProperty(QTextFormat::BackgroundBrush)) { painter->fillRect(boundingRect(), msgFmt.background()); } if (_selection & Selected) { - QTextCharFormat selFmt = QtUi::style()->format(UiStyle::formatType(type), label | UiStyle::MessageLabel::Selected); + QTextCharFormat selFmt = QtUi::style()->format({UiStyle::formatType(type), {}, {}}, label | UiStyle::MessageLabel::Selected); if (selFmt.hasProperty(QTextFormat::BackgroundBrush)) { qreal left = item((ChatLineModel::ColumnType)(_selection & ItemMask))->pos().x(); QRectF selectRect(left, 0, width() - left, height()); diff --git a/src/qtui/chatlinemodelitem.cpp b/src/qtui/chatlinemodelitem.cpp index 5582fe87..66de6161 100644 --- a/src/qtui/chatlinemodelitem.cpp +++ b/src/qtui/chatlinemodelitem.cpp @@ -111,7 +111,7 @@ QVariant ChatLineModelItem::timestampData(int role) const case ChatLineModel::SelectedBackgroundRole: return backgroundBrush(UiStyle::FormatType::Timestamp, true); case ChatLineModel::FormatRole: - return QVariant::fromValue({std::make_pair(quint16{0}, UiStyle::Format{UiStyle::formatType(_styledMsg.type()) | UiStyle::FormatType::Timestamp})}); + return QVariant::fromValue({std::make_pair(quint16{0}, UiStyle::Format{UiStyle::formatType(_styledMsg.type()) | UiStyle::FormatType::Timestamp, {}, {}})}); } return QVariant(); } @@ -129,7 +129,7 @@ QVariant ChatLineModelItem::senderData(int role) const case ChatLineModel::SelectedBackgroundRole: return backgroundBrush(UiStyle::FormatType::Sender, true); case ChatLineModel::FormatRole: - return QVariant::fromValue({std::make_pair(quint16{0}, UiStyle::Format{UiStyle::formatType(_styledMsg.type()) | UiStyle::FormatType::Sender})}); + return QVariant::fromValue({std::make_pair(quint16{0}, UiStyle::Format{UiStyle::formatType(_styledMsg.type()) | UiStyle::FormatType::Sender, {}, {}})}); } return QVariant(); } @@ -171,7 +171,7 @@ UiStyle::MessageLabel ChatLineModelItem::messageLabel() const QVariant ChatLineModelItem::backgroundBrush(UiStyle::FormatType subelement, bool selected) const { - QTextCharFormat fmt = QtUi::style()->format(UiStyle::formatType(_styledMsg.type()) | subelement, + QTextCharFormat fmt = QtUi::style()->format({UiStyle::formatType(_styledMsg.type()) | subelement, {}, {}}, messageLabel() | (selected ? UiStyle::MessageLabel::Selected : UiStyle::MessageLabel::None)); if (fmt.hasProperty(QTextFormat::BackgroundBrush)) return QVariant::fromValue(fmt.background()); diff --git a/src/uisupport/uistyle.cpp b/src/uisupport/uistyle.cpp index fc9ccb13..714fe91f 100644 --- a/src/uisupport/uistyle.cpp +++ b/src/uisupport/uistyle.cpp @@ -21,6 +21,7 @@ #include #include +#include #include #include "buffersettings.h" @@ -37,6 +38,28 @@ QString UiStyle::_systemTimestampFormatString; /// Cached copy of system locale bool UiStyle::_showSenderPrefixes; /// If true, show prefixmodes before sender names bool UiStyle::_showSenderBrackets; /// If true, show brackets around sender names +namespace { + +// Extended mIRC colors as defined in https://modern.ircdocs.horse/formatting.html#colors-16-98 +QColor extendedMircColor(int number) +{ + static const std::vector colorMap = { + "#470000", "#472100", "#474700", "#324700", "#004700", "#00472c", "#004747", "#002747", "#000047", "#2e0047", "#470047", "#47002a", + "#740000", "#743a00", "#747400", "#517400", "#007400", "#007449", "#007474", "#004074", "#000074", "#4b0074", "#740074", "#740045", + "#b50000", "#b56300", "#b5b500", "#7db500", "#00b500", "#00b571", "#00b5b5", "#0063b5", "#0000b5", "#7500b5", "#b500b5", "#b5006b", + "#ff0000", "#ff8c00", "#ffff00", "#b2ff00", "#00ff00", "#00ffa0", "#00ffff", "#008cff", "#0000ff", "#a500ff", "#ff00ff", "#ff0098", + "#ff5959", "#ffb459", "#ffff71", "#cfff60", "#6fff6f", "#65ffc9", "#6dffff", "#59b4ff", "#5959ff", "#c459ff", "#ff66ff", "#ff59bc", + "#ff9c9c", "#ffd39c", "#ffff9c", "#e2ff9c", "#9cff9c", "#9cffdb", "#9cffff", "#9cd3ff", "#9c9cff", "#dc9cff", "#ff9cff", "#ff94d3", + "#000000", "#131313", "#282828", "#363636", "#4d4d4d", "#656565", "#818181", "#9f9f9f", "#bcbcbc", "#e2e2e2", "#ffffff" + }; + if (number < 16) + return {}; + size_t index = number - 16; + return (index < colorMap.size() ? colorMap[index] : QColor{}); +} + +} + UiStyle::UiStyle(QObject *parent) : QObject(parent), _channelJoinedIcon(QIcon::fromTheme("irc-channel-joined", QIcon(":/icons/irc-channel-joined.png"))), @@ -400,16 +423,27 @@ QTextCharFormat UiStyle::format(quint64 key) const return _formats.value(key, QTextCharFormat()); } +namespace { -QTextCharFormat UiStyle::cachedFormat(FormatType formatType, MessageLabel messageLabel) const +// Create unique key for given Format object and message label +QString formatKey(const UiStyle::Format &format, UiStyle::MessageLabel label) { - return _formatCache.value(formatType | messageLabel, QTextCharFormat()); + return QString::number(format.type | label, 16) + + (format.foreground.isValid() ? format.foreground.name() : "#------") + + (format.background.isValid() ? format.background.name() : "#------"); } +} -void UiStyle::setCachedFormat(const QTextCharFormat &format, FormatType formatType, MessageLabel messageLabel) const +QTextCharFormat UiStyle::cachedFormat(const Format &format, MessageLabel messageLabel) const { - _formatCache[formatType | messageLabel] = format; + return _formatCache.value(formatKey(format, messageLabel), QTextCharFormat()); +} + + +void UiStyle::setCachedFormat(const QTextCharFormat &charFormat, const Format &format, MessageLabel messageLabel) const +{ + _formatCache[formatKey(format, messageLabel)] = charFormat; } @@ -421,7 +455,7 @@ QFontMetricsF *UiStyle::fontMetrics(FormatType ftype, MessageLabel label) const if (_metricsCache.contains(key)) return _metricsCache.value(key); - return (_metricsCache[key] = new QFontMetricsF(format(ftype, label).font())); + return (_metricsCache[key] = new QFontMetricsF(format({ftype, {}, {}}, label).font())); } @@ -429,38 +463,39 @@ QFontMetricsF *UiStyle::fontMetrics(FormatType ftype, MessageLabel label) const // NOTE: This and the following functions are intimately tied to the values in FormatType. Don't change this // until you _really_ know what you do! -QTextCharFormat UiStyle::format(FormatType ftype, MessageLabel label) const +QTextCharFormat UiStyle::format(const Format &format, MessageLabel label) const { - if (ftype == FormatType::Invalid) + if (format.type == FormatType::Invalid) return {}; - // check if we have exactly this format readily cached already - QTextCharFormat fmt = cachedFormat(ftype, label); - if (fmt.properties().count()) - return fmt; + // Check if we have exactly this format readily cached already + QTextCharFormat charFormat = cachedFormat(format, label); + if (charFormat.properties().count()) + return charFormat; - mergeFormat(fmt, ftype, label & 0xffff0000); // keep nickhash in label + mergeFormat(charFormat, format, label & 0xffff0000); // keep nickhash in label for (quint32 mask = 0x00000001; mask <= static_cast(MessageLabel::Selected); mask <<= 1) { - if (static_cast(label) & mask) - mergeFormat(fmt, ftype, label & (mask | 0xffff0000)); + if (static_cast(label) & mask) { + mergeFormat(charFormat, {format.type, {}, {}}, label & (mask | 0xffff0000)); // Don't re-apply extended colors + } } - setCachedFormat(fmt, ftype, label); - return fmt; + setCachedFormat(charFormat, format, label); + return charFormat; } -void UiStyle::mergeFormat(QTextCharFormat &fmt, FormatType ftype, MessageLabel label) const +void UiStyle::mergeFormat(QTextCharFormat &charFormat, const Format &format, MessageLabel label) const { - mergeSubElementFormat(fmt, ftype & 0x00ff, label); + mergeSubElementFormat(charFormat, format.type & 0x00ff, label); // TODO: allow combinations for mirc formats and colors (each), e.g. setting a special format for "bold and italic" // or "foreground 01 and background 03" - if ((ftype & 0xfff00) != FormatType::Base) { // element format + if ((format.type & 0xfff00) != FormatType::Base) { // element format for (quint32 mask = 0x00100; mask <= 0x40000; mask <<= 1) { - if ((ftype & mask) != FormatType::Base) { - mergeSubElementFormat(fmt, ftype & (mask | 0xff), label); + if ((format.type & mask) != FormatType::Base) { + mergeSubElementFormat(charFormat, format.type & (mask | 0xff), label); } } } @@ -468,17 +503,24 @@ void UiStyle::mergeFormat(QTextCharFormat &fmt, FormatType ftype, MessageLabel l // Now we handle color codes // We assume that those can't be combined with subelement and message types. if (_allowMircColors) { - if ((ftype & 0x00400000) != FormatType::Base) - mergeSubElementFormat(fmt, ftype & 0x0f400000, label); // foreground - if ((ftype & 0x00800000) != FormatType::Base) - mergeSubElementFormat(fmt, ftype & 0xf0800000, label); // background - if ((ftype & 0x00c00000) == static_cast(0x00c00000)) - mergeSubElementFormat(fmt, ftype & 0xffc00000, label); // combination + // Classic mIRC colors (styleable) + if ((format.type & 0x00400000) != FormatType::Base) + mergeSubElementFormat(charFormat, format.type & 0x0f400000, label); // foreground + if ((format.type & 0x00800000) != FormatType::Base) + mergeSubElementFormat(charFormat, format.type & 0xf0800000, label); // background + if ((format.type & 0x00c00000) == static_cast(0x00c00000)) + mergeSubElementFormat(charFormat, format.type & 0xffc00000, label); // combination + + // Extended mIRC colors (hardcoded) + if (format.foreground.isValid()) + charFormat.setForeground(format.foreground); + if (format.background.isValid()) + charFormat.setBackground(format.background); } // URL - if ((ftype & FormatType::Url) != FormatType::Base) - mergeSubElementFormat(fmt, ftype & (FormatType::Url | static_cast(0x000000ff)), label); + if ((format.type & FormatType::Url) != FormatType::Base) + mergeSubElementFormat(charFormat, format.type & (FormatType::Url | static_cast(0x000000ff)), label); } @@ -559,7 +601,7 @@ QList UiStyle::toTextLayoutList(const FormatList &form QTextLayout::FormatRange range; size_t i = 0; for (i = 0; i < formatList.size(); i++) { - range.format = format(formatList.at(i).second.type, messageLabel); + range.format = format(formatList.at(i).second, messageLabel); range.start = formatList.at(i).first; if (i > 0) formatRanges.last().length = range.start - formatRanges.last().start; @@ -577,7 +619,7 @@ UiStyle::StyledString UiStyle::styleString(const QString &s_, FormatType baseFor { QString s = s_; StyledString result; - result.formatList.emplace_back(std::make_pair(quint16{0}, Format{baseFormat})); + result.formatList.emplace_back(std::make_pair(quint16{0}, Format{baseFormat, {}, {}})); if (s.length() > 65535) { // We use quint16 for indexes @@ -586,7 +628,7 @@ UiStyle::StyledString UiStyle::styleString(const QString &s_, FormatType baseFor return result; } - FormatType curfmt = baseFormat; + Format curfmt{baseFormat, {}, {}}; int pos = 0; quint16 length = 0; for (;;) { @@ -599,26 +641,45 @@ UiStyle::StyledString UiStyle::styleString(const QString &s_, FormatType baseFor } if (s[pos+1] == 'D' && s[pos+2] == 'c') { // color code if (s[pos+3] == '-') { // color off - curfmt &= 0x003fffff; + curfmt.type &= 0x003fffff; + curfmt.foreground = QColor{}; + curfmt.background = QColor{}; length = 4; } else { - int color = 10 * s[pos+4].digitValue() + s[pos+5].digitValue(); - //TODO: use 99 as transparent color (re mirc color "standard") - color &= 0x0f; + quint32 color = 10 * s[pos+4].digitValue() + s[pos+5].digitValue(); + // Color values 0-15 are traditional mIRC colors, defined in the stylesheet and thus going through the format engine + // Larger color values are hardcoded and applied separately (cf. https://modern.ircdocs.horse/formatting.html#colors-16-98) if (s[pos+3] == 'f') { - curfmt &= 0xf0ffffff; - curfmt |= (quint32)(color << 24) | 0x00400000; + if (color < 16) { + // Traditional mIRC color, defined in the stylesheet + curfmt.type &= 0xf0ffffff; + curfmt.type |= color << 24 | 0x00400000; + curfmt.foreground = QColor{}; + } + else { + curfmt.type &= 0xf0bfffff; // mask out traditional foreground color + curfmt.foreground = extendedMircColor(color); + } } else { - curfmt &= 0x0fffffff; - curfmt |= (quint32)(color << 28) | 0x00800000; + if (color < 16) { + curfmt.type &= 0x0fffffff; + curfmt.type |= color << 28 | 0x00800000; + curfmt.background = QColor{}; + } + else { + curfmt.type &= 0x0f7fffff; // mask out traditional background color + curfmt.background = extendedMircColor(color); + } } length = 6; } } else if (s[pos+1] == 'O') { // reset formatting - curfmt &= 0x000000ff; // we keep message type-specific formatting + curfmt.type &= 0x000000ff; // we keep message type-specific formatting + curfmt.foreground = QColor{}; + curfmt.background = QColor{}; length = 2; } else if (s[pos+1] == 'R') { // reverse @@ -635,14 +696,14 @@ UiStyle::StyledString UiStyle::styleString(const QString &s_, FormatType baseFor qWarning() << (QString("Invalid format code in string: %1").arg(s)); continue; } - curfmt ^= ftype; + curfmt.type ^= ftype; length = code.length(); } s.remove(pos, length); if (pos == result.formatList.back().first) - result.formatList.back().second.type = curfmt; + result.formatList.back().second = curfmt; else - result.formatList.emplace_back(std::make_pair(pos, Format{curfmt})); + result.formatList.emplace_back(std::make_pair(pos, curfmt)); } result.plainText = s; return result; @@ -694,24 +755,29 @@ QString UiStyle::mircToInternal(const QString &mirc_) // Note: We use the "mirc standard" as described in . // This means that we don't accept something like \x03,5 (even though others, like WeeChat, do). int pos = 0; - for (;;) { + while (true) { pos = mirc.indexOf('\x03', pos); - if (pos < 0) break; // no more mirc color codes + if (pos < 0) + break; // no more mirc color codes QString ins, num; int l = mirc.length(); int i = pos + 1; // check for fg color if (i < l && mirc[i].isDigit()) { num = mirc[i++]; - if (i < l && mirc[i].isDigit()) num.append(mirc[i++]); - else num.prepend('0'); + if (i < l && mirc[i].isDigit()) + num.append(mirc[i++]); + else + num.prepend('0'); ins = QString("%Dcf%1").arg(num); if (i+1 < l && mirc[i] == ',' && mirc[i+1].isDigit()) { i++; num = mirc[i++]; - if (i < l && mirc[i].isDigit()) num.append(mirc[i++]); - else num.prepend('0'); + if (i < l && mirc[i].isDigit()) + num.append(mirc[i++]); + else + num.prepend('0'); ins += QString("%Dcb%1").arg(num); } } @@ -1127,7 +1193,10 @@ QDataStream &operator<<(QDataStream &out, const UiStyle::FormatList &formatList) out << static_cast(formatList.size()); UiStyle::FormatList::const_iterator it = formatList.begin(); while (it != formatList.end()) { - out << it->first << static_cast(it->second.type); + out << it->first + << static_cast(it->second.type) + << it->second.foreground + << it->second.background; ++it; } return out; @@ -1139,9 +1208,12 @@ QDataStream &operator>>(QDataStream &in, UiStyle::FormatList &formatList) quint16 cnt; in >> cnt; for (quint16 i = 0; i < cnt; i++) { - quint16 pos; quint32 ftype; - in >> pos >> ftype; - formatList.emplace_back(std::make_pair(quint16{pos}, UiStyle::Format{static_cast(ftype)})); + quint16 pos; + quint32 ftype; + QColor foreground; + QColor background; + in >> pos >> ftype >> foreground >> background; + formatList.emplace_back(std::make_pair(quint16{pos}, UiStyle::Format{static_cast(ftype), foreground, background})); } return in; } diff --git a/src/uisupport/uistyle.h b/src/uisupport/uistyle.h index 7d1a8bcd..28ba4054 100644 --- a/src/uisupport/uistyle.h +++ b/src/uisupport/uistyle.h @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -158,6 +159,8 @@ public: struct Format { FormatType type; + QColor foreground; + QColor background; }; using FormatList = std::vector>; @@ -242,7 +245,7 @@ public: */ static QString timestampFormatString(); - QTextCharFormat format(FormatType formatType, MessageLabel messageLabel) const; + QTextCharFormat format(const Format &format, MessageLabel messageLabel) const; QFontMetricsF *fontMetrics(FormatType formatType, MessageLabel messageLabel) const; QList toTextLayoutList(const FormatList &, int textLength, MessageLabel messageLabel) const; @@ -264,10 +267,10 @@ protected: QString loadStyleSheet(const QString &name, bool shouldExist = false); QTextCharFormat format(quint64 key) const; - QTextCharFormat cachedFormat(FormatType formatType, MessageLabel messageLabel) const; - void setCachedFormat(const QTextCharFormat &format, FormatType formatType, MessageLabel messageLabel) const; - void mergeFormat(QTextCharFormat &format, FormatType formatType, MessageLabel messageLabel) const; - void mergeSubElementFormat(QTextCharFormat &format, FormatType formatType, MessageLabel messageLabel) const; + QTextCharFormat cachedFormat(const Format &format, MessageLabel messageLabel) const; + void setCachedFormat(const QTextCharFormat &charFormat, const Format &format, MessageLabel messageLabel) const; + void mergeFormat(QTextCharFormat &charFormat, const Format &format, MessageLabel messageLabel) const; + void mergeSubElementFormat(QTextCharFormat &charFormat, FormatType formatType, MessageLabel messageLabel) const; static FormatType formatType(const QString &code); static QString formatCode(FormatType); @@ -319,7 +322,7 @@ private: QVector _uiStylePalette; QBrush _markerLineBrush; QHash _formats; - mutable QHash _formatCache; + mutable QHash _formatCache; mutable QHash _metricsCache; QHash _listItemFormats; static QHash _formatCodes;