+
+QVariant UiStyle::nickViewItemData(const QModelIndex &index, int role) const
+{
+ NetworkModel::ItemType type = (NetworkModel::ItemType)index.data(NetworkModel::ItemTypeRole).toInt();
+
+ if (role == Qt::DecorationRole) {
+ if (!_showNickViewIcons)
+ return QVariant();
+
+ switch (type) {
+ case NetworkModel::UserCategoryItemType:
+ {
+ int categoryId = index.data(TreeModel::SortRole).toInt();
+ if (categoryId <= _opIconLimit)
+ return _categoryOpIcon;
+ if (categoryId <= _voiceIconLimit)
+ return _categoryVoiceIcon;
+ return _userOnlineIcon;
+ }
+ case NetworkModel::IrcUserItemType:
+ if (index.data(NetworkModel::ItemActiveRole).toBool())
+ return _userOnlineIcon;
+ else
+ return _userAwayIcon;
+ default:
+ return QVariant();
+ }
+ }
+
+ QTextCharFormat fmt = _listItemFormats.value(ItemFormatType::NickViewItem);
+
+ switch (type) {
+ case NetworkModel::IrcUserItemType:
+ fmt.merge(_listItemFormats.value(ItemFormatType::NickViewItem | ItemFormatType::IrcUserItem));
+ if (!index.data(NetworkModel::ItemActiveRole).toBool()) {
+ fmt.merge(_listItemFormats.value(ItemFormatType::NickViewItem | ItemFormatType::UserAway));
+ fmt.merge(_listItemFormats.value(ItemFormatType::NickViewItem | ItemFormatType::IrcUserItem | ItemFormatType::UserAway));
+ }
+ break;
+ case NetworkModel::UserCategoryItemType:
+ fmt.merge(_listItemFormats.value(ItemFormatType::NickViewItem | ItemFormatType::UserCategoryItem));
+ break;
+ default:
+ return QVariant();
+ }
+
+ return itemData(role, fmt);
+}
+
+
+QVariant UiStyle::itemData(int role, const QTextCharFormat &format) const
+{
+ switch (role) {
+ case Qt::FontRole:
+ return format.font();
+ case Qt::ForegroundRole:
+ return format.property(QTextFormat::ForegroundBrush);
+ case Qt::BackgroundRole:
+ return format.property(QTextFormat::BackgroundBrush);
+ default:
+ return QVariant();
+ }
+}
+
+
+/******** Caching *******/
+
+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(const Format &format, MessageLabel messageLabel) const
+{
+ return _formatCache.value(formatKey(format, messageLabel), QTextCharFormat());
+}
+
+
+void UiStyle::setCachedFormat(const QTextCharFormat &charFormat, const Format &format, MessageLabel messageLabel) const
+{
+ _formatCache[formatKey(format, messageLabel)] = charFormat;
+}
+
+
+QFontMetricsF *UiStyle::fontMetrics(FormatType ftype, MessageLabel label) const
+{
+ // QFontMetricsF is not assignable, so we need to store pointers :/
+ quint64 key = ftype | label;
+
+ if (_metricsCache.contains(key))
+ return _metricsCache.value(key);
+
+ return (_metricsCache[key] = new QFontMetricsF(format({ftype, {}, {}}, label).font()));
+}
+
+
+/******** Generate formats ********/
+
+// 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(const Format &format, MessageLabel label) const
+{
+ if (format.type == FormatType::Invalid)
+ return {};
+
+ // 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::Last); mask <<= 1) {
+ 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::Last); mask <<= 1) {
+ if (static_cast<quint32>(label) & mask) {
+ mergeColors(charFormat, format, label & mask);
+ }
+ }
+ }
+
+ setCachedFormat(charFormat, format, label);
+ return charFormat;
+}
+
+
+void UiStyle::mergeFormat(QTextCharFormat &charFormat, const Format &format, MessageLabel label) const
+{
+ 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 ((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);
+ }
+ }
+ }
+}
+
+
+// Merge a subelement format into an existing message format
+void UiStyle::mergeSubElementFormat(QTextCharFormat &fmt, FormatType ftype, MessageLabel label) const
+{
+ quint64 key = ftype | label;
+ 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);
+}
+
+
+UiStyle::FormatType UiStyle::formatType(Message::Type msgType)
+{
+ switch (msgType) {