+ // 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);
+ }