+
+ painter->restore();
+}
+
+
+void ChatItem::overlayFormat(UiStyle::FormatList &fmtList, quint16 start, quint16 end, UiStyle::FormatType overlayFmt) const
+{
+ for (size_t i = 0; i < fmtList.size(); i++) {
+ int fmtStart = fmtList.at(i).first;
+ int fmtEnd = (i < fmtList.size()-1 ? fmtList.at(i+1).first : data(MessageModel::DisplayRole).toString().length());
+
+ if (fmtEnd <= start)
+ continue;
+ if (fmtStart >= end)
+ break;
+
+ // split the format if necessary
+ if (fmtStart < start) {
+ fmtList.insert(fmtList.begin() + i, fmtList.at(i));
+ fmtList[++i].first = start;
+ }
+ if (end < fmtEnd) {
+ fmtList.insert(fmtList.begin() + i, fmtList.at(i));
+ fmtList[i+1].first = end;
+ }
+
+ fmtList[i].second.type |= overlayFmt;
+ }
+}
+
+
+QVector<QTextLayout::FormatRange> ChatItem::additionalFormats() const
+{
+ // Calculate formats to overlay (only) if there's a selection, and/or a hovered clickable
+ if (!hasSelection() && !hasActiveClickable()) {
+ return {};
+ }
+
+ using Label = UiStyle::MessageLabel;
+ using Format = UiStyle::Format;
+
+ Label itemLabel = data(ChatLineModel::MsgLabelRole).value<Label>();
+ const auto &fmtList = formatList();
+
+ struct LabelFormat {
+ quint16 offset;
+ Format format;
+ Label label;
+ };
+
+ // Transform formatList() into an extended list of LabelFormats
+ std::vector<LabelFormat> labelFmtList;
+ std::transform(fmtList.cbegin(), fmtList.cend(), std::back_inserter(labelFmtList), [itemLabel](const std::pair<quint16, Format> &f) {
+ return LabelFormat{f.first, f.second, itemLabel};
+ });
+ // Append dummy element to avoid special-casing handling the last real format
+ labelFmtList.push_back(LabelFormat{quint16(data(MessageModel::DisplayRole).toString().length()), Format(), itemLabel});
+
+ // Apply the given label to the given range in the format list, splitting formats as necessary
+ auto applyLabel = [&labelFmtList](quint16 start, quint16 end, Label label) {
+ size_t i = 0;
+
+ // Skip unaffected formats
+ for (; i < labelFmtList.size() - 1; ++i) {
+ if (labelFmtList[i+1].offset > start)
+ break;
+ }
+ // Range start doesn't align; split affected format and let the index point to the newly inserted copy
+ if (labelFmtList[i].offset < start) {
+ labelFmtList.insert(labelFmtList.begin() + i, labelFmtList[i]);
+ labelFmtList[++i].offset = start;
+ }
+
+ // Apply label to formats fully affected
+ for (; i < labelFmtList.size() - 1; ++i) {
+ if (labelFmtList[i+1].offset <= end) {
+ labelFmtList[i].label |= label;
+ continue;
+ }
+ // Last affected format, split if end of range doesn't align
+ if (labelFmtList[i+1].offset > end) {
+ labelFmtList.insert(labelFmtList.begin() + i, labelFmtList[i]);
+ labelFmtList[i].label |= label;
+ labelFmtList[i+1].offset = end;
+ }
+ break;
+ }
+ };
+
+ // Apply selection label
+ if (hasSelection()) {
+ quint16 start, end;
+ if (_selectionMode == FullSelection) {
+ start = 0;
+ end = data(MessageModel::DisplayRole).toString().length();
+ }
+ else {
+ start = qMin(_selectionStart, _selectionEnd);
+ end = qMax(_selectionStart, _selectionEnd);
+ }
+ applyLabel(start, end, Label::Selected);
+ }
+
+ // Apply hovered label
+ if (hasActiveClickable()) {
+ applyLabel(activeClickableRange().first, activeClickableRange().second, Label::Hovered);
+ }
+
+ // Add all formats that have an extra label to the additionalFormats list
+ QList<QTextLayout::FormatRange> additionalFormats;
+ for (size_t i = 0; i < labelFmtList.size() - 1; ++i) {
+ if (labelFmtList[i].label != itemLabel) {
+ additionalFormats << QtUi::style()->toTextLayoutList({std::make_pair(labelFmtList[i].offset, labelFmtList[i].format)},
+ labelFmtList[i+1].offset,
+ labelFmtList[i].label);
+ }
+ }
+
+ return additionalFormats.toVector();
+}
+
+
+bool ChatItem::hasSelection() const
+{
+ if (_selectionMode == NoSelection)
+ return false;
+ if (_selectionMode == FullSelection)
+ return true;
+ // partial
+ return _selectionStart != _selectionEnd;
+}
+
+
+QString ChatItem::selection() const
+{
+ if (_selectionMode == FullSelection)
+ return data(MessageModel::DisplayRole).toString();
+ if (_selectionMode == PartialSelection)
+ return data(MessageModel::DisplayRole).toString().mid(qMin(_selectionStart, _selectionEnd), qAbs(_selectionStart - _selectionEnd));
+ return QString();
+}
+
+
+void ChatItem::setSelection(SelectionMode mode, qint16 start, qint16 end)
+{
+ _selectionMode = mode;
+ _selectionStart = start;
+ _selectionEnd = end;
+ chatLine()->update();
+}
+
+
+void ChatItem::setFullSelection()
+{
+ if (_selectionMode != FullSelection) {
+ _selectionMode = FullSelection;
+ chatLine()->update();
+ }