#include <vector>
#include <QApplication>
+#include <QColor>
#include <QIcon>
#include "buffersettings.h"
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<QColor> 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"))),
_opIconLimit(UserCategoryItem::categoryFromModes("o")),
_voiceIconLimit(UserCategoryItem::categoryFromModes("v"))
{
- // register FormatList if that hasn't happened yet
- // FIXME I don't think this actually avoids double registration... then again... does it hurt?
- if (QVariant::nameToType("UiStyle::FormatList") == QVariant::Invalid) {
- qRegisterMetaType<FormatList>("UiStyle::FormatList");
- qRegisterMetaTypeStreamOperators<FormatList>("UiStyle::FormatList");
- Q_ASSERT(QVariant::nameToType("UiStyle::FormatList") != QVariant::Invalid);
- }
+ static bool registered = []() {
+ qRegisterMetaType<FormatList>();
+ qRegisterMetaTypeStreamOperators<FormatList>();
+ return true;
+ }();
+ Q_UNUSED(registered)
_uiStylePalette = QVector<QBrush>(static_cast<int>(ColorRole::NumRoles), QBrush());
/******** Caching *******/
-QTextCharFormat UiStyle::format(quint64 key) const
+QTextCharFormat UiStyle::parsedFormat(quint64 key) const
{
return _formats.value(key, QTextCharFormat());
}
+namespace {
+
+// Create unique key for given Format object and message label
+QString formatKey(const UiStyle::Format &format, UiStyle::MessageLabel label)
+{
+ return QString::number(format.type | label, 16)
+ + (format.foreground.isValid() ? format.foreground.name() : "#------")
+ + (format.background.isValid() ? format.background.name() : "#------");
+}
+
+}
-QTextCharFormat UiStyle::cachedFormat(FormatType formatType, MessageLabel messageLabel) const
+QTextCharFormat UiStyle::cachedFormat(const Format &format, MessageLabel messageLabel) const
{
- return _formatCache.value(formatType | messageLabel, QTextCharFormat());
+ return _formatCache.value(formatKey(format, messageLabel), QTextCharFormat());
}
-void UiStyle::setCachedFormat(const QTextCharFormat &format, FormatType formatType, MessageLabel messageLabel) const
+void UiStyle::setCachedFormat(const QTextCharFormat &charFormat, const Format &format, MessageLabel messageLabel) const
{
- _formatCache[formatType | messageLabel] = format;
+ _formatCache[formatKey(format, messageLabel)] = charFormat;
}
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()));
}
// 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;
-
- mergeFormat(fmt, ftype, label & 0xffff0000); // keep nickhash in label
+ // Check if we have exactly this format readily cached already
+ QTextCharFormat charFormat = cachedFormat(format, label);
+ if (charFormat.properties().count())
+ return charFormat;
+ // Merge all formats except mIRC and extended colors
+ mergeFormat(charFormat, format, label & 0xffff0000); // keep nickhash in label
for (quint32 mask = 0x00000001; mask <= static_cast<quint32>(MessageLabel::Selected); mask <<= 1) {
- if (static_cast<quint32>(label) & mask)
- mergeFormat(fmt, ftype, label & (mask | 0xffff0000));
+ if (static_cast<quint32>(label) & mask) {
+ mergeFormat(charFormat, format, label & (mask | 0xffff0000));
+ }
+ }
+
+ // Merge mIRC and extended colors, if appropriate. These override any color set previously in the format,
+ // unless the AllowForegroundOverride or AllowBackgroundOverride properties are set (via stylesheet).
+ if (_allowMircColors) {
+ mergeColors(charFormat, format, MessageLabel::None);
+ for (quint32 mask = 0x00000001; mask <= static_cast<quint32>(MessageLabel::Selected); mask <<= 1) {
+ if (static_cast<quint32>(label) & mask) {
+ mergeColors(charFormat, format, label & mask);
+ }
+ }
}
- 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
- for (quint32 mask = 0x00100; mask <= 0x40000; mask <<= 1) {
- if ((ftype & mask) != FormatType::Base) {
- mergeSubElementFormat(fmt, ftype & (mask | 0xff), label);
+ if ((format.type & 0xfff00) != FormatType::Base) { // element format
+ for (quint32 mask = 0x00100; mask <= 0x80000; mask <<= 1) {
+ if ((format.type & mask) != FormatType::Base) {
+ mergeSubElementFormat(charFormat, format.type & (mask | 0xff), label);
}
}
}
-
- // 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<FormatType>(0x00c00000))
- mergeSubElementFormat(fmt, ftype & 0xffc00000, label); // combination
- }
-
- // URL
- if ((ftype & FormatType::Url) != FormatType::Base)
- mergeSubElementFormat(fmt, ftype & (FormatType::Url | static_cast<FormatType>(0x000000ff)), label);
}
void UiStyle::mergeSubElementFormat(QTextCharFormat &fmt, FormatType ftype, MessageLabel label) const
{
quint64 key = ftype | label;
- fmt.merge(format(key & 0x0000ffffffffff00ull)); // label + subelement
- fmt.merge(format(key & 0x0000ffffffffffffull)); // label + subelement + msgtype
- fmt.merge(format(key & 0xffffffffffffff00ull)); // label + subelement + nickhash
- fmt.merge(format(key & 0xffffffffffffffffull)); // label + subelement + nickhash + msgtype
+ fmt.merge(parsedFormat(key & 0x0000ffffffffff00ull)); // label + subelement
+ fmt.merge(parsedFormat(key & 0x0000ffffffffffffull)); // label + subelement + msgtype
+ fmt.merge(parsedFormat(key & 0xffffffffffffff00ull)); // label + subelement + nickhash
+ fmt.merge(parsedFormat(key & 0xffffffffffffffffull)); // label + subelement + nickhash + msgtype
+}
+
+
+void UiStyle::mergeColors(QTextCharFormat &charFormat, const Format &format, MessageLabel label) const
+{
+ bool allowFg = charFormat.property(static_cast<int>(FormatProperty::AllowForegroundOverride)).toBool();
+ bool allowBg = charFormat.property(static_cast<int>(FormatProperty::AllowBackgroundOverride)).toBool();
+
+ // Classic mIRC colors (styleable)
+ // We assume that those can't be combined with subelement and message types.
+ if (allowFg && (format.type & 0x00400000) != FormatType::Base)
+ charFormat.merge(parsedFormat((format.type & 0x0f400000) | label)); // foreground
+ if (allowBg && (format.type & 0x00800000) != FormatType::Base)
+ charFormat.merge(parsedFormat((format.type & 0xf0800000) | label)); // background
+ if (allowFg && allowBg && (format.type & 0x00c00000) == static_cast<FormatType>(0x00c00000))
+ charFormat.merge(parsedFormat((format.type & 0xffc00000) | label)); // combination
+
+ // Extended mIRC colors (hardcoded)
+ if (allowFg && format.foreground.isValid())
+ charFormat.setForeground(format.foreground);
+ if (allowBg && format.background.isValid())
+ charFormat.setBackground(format.background);
}
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;
{
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
return result;
}
- FormatType curfmt = baseFormat;
+ Format curfmt{baseFormat, {}, {}};
int pos = 0; quint16 length = 0;
for (;;) {
pos++;
continue;
}
- if (s[pos+1] == 'D' && s[pos+2] == 'c') { // color code
+ if (s[pos+1] == 'D' && s[pos+2] == 'c') { // mIRC 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] == 'D' && s[pos+2] == 'h') { // Hex color
+ QColor color{s.mid(pos+4, 7)};
+ if (s[pos+3] == 'f') {
+ curfmt.type &= 0xf0bfffff; // mask out mIRC foreground color
+ curfmt.foreground = std::move(color);
+ }
+ else {
+ curfmt.type &= 0x0f7fffff; // mask out mIRC background color
+ curfmt.background = std::move(color);
+ }
+ length = 11;
+ }
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
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;
QString mirc;
mirc.reserve(mirc_.size());
foreach (const QChar &c, mirc_) {
- if ((c < '\x20' || c == '\x7f') && c != '\x03') {
+ if ((c < '\x20' || c == '\x7f') && c != '\x03' && c != '\x04') {
switch (c.unicode()) {
case '\x02':
mirc += "%B";
// %Dc- turns color off.
// Note: We use the "mirc standard" as described in <http://www.mirc.co.uk/help/color.txt>.
// This means that we don't accept something like \x03,5 (even though others, like WeeChat, do).
- int pos = 0;
- for (;;) {
- pos = mirc.indexOf('\x03', pos);
- 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');
- ins = QString("%Dcf%1").arg(num);
-
- if (i+1 < l && mirc[i] == ',' && mirc[i+1].isDigit()) {
- i++;
+ {
+ int pos = 0;
+ while (true) {
+ pos = mirc.indexOf('\x03', pos);
+ 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');
- ins += QString("%Dcb%1").arg(num);
+ 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');
+ ins += QString("%Dcb%1").arg(num);
+ }
+ }
+ else {
+ ins = "%Dc-";
}
+ mirc.replace(pos, i-pos, ins);
}
- else {
- ins = "%Dc-";
+ }
+
+ // Hex colors, as specified in https://modern.ircdocs.horse/formatting.html#hex-color
+ // %Dhf#rrggbb is foreground, %Dhb#rrggbb is background
+ {
+ static const QRegExp rx{"[\\da-fA-F]{6}"};
+ int pos = 0;
+ while (true) {
+ if (pos >= mirc.length())
+ break;
+ pos = mirc.indexOf('\x04', pos);
+ if (pos < 0)
+ break;
+ int i = pos + 1;
+ QString ins;
+ auto num = mirc.mid(i, 6);
+ if (!num.isEmpty() && rx.exactMatch(num)) {
+ ins = "%Dhf#" + num.toLower();
+ i += 6;
+ if (i < mirc.length() && mirc[i] == ',' && !(num = mirc.mid(i + 1, 6)).isEmpty() && rx.exactMatch(num)) {
+ ins += "%Dhb#" + num.toLower();
+ i += 7;
+ }
+ }
+ else {
+ ins = "%Dc-";
+ }
+ mirc.replace(pos, i - pos, ins);
+ pos += ins.length();
}
- mirc.replace(pos, i-pos, ins);
}
+
return mirc;
}
out << static_cast<quint16>(formatList.size());
UiStyle::FormatList::const_iterator it = formatList.begin();
while (it != formatList.end()) {
- out << it->first << static_cast<quint32>(it->second.type);
+ out << it->first
+ << static_cast<quint32>(it->second.type)
+ << it->second.foreground
+ << it->second.background;
++it;
}
return out;
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<UiStyle::FormatType>(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<UiStyle::FormatType>(ftype), foreground, background}));
}
return in;
}