| 529 | } |
| 530 | |
| 531 | QPair<QString, QList<QTextLayout::FormatRange>> SearchResultItemDelegate::adjustContent(const QModelIndex &index, |
| 532 | const QString &originalText, |
| 533 | const QList<QTextLayout::FormatRange> &formatList) const |
| 534 | { |
| 535 | auto adjustFormatRange = [&](int offset) { |
| 536 | if (offset == 0) |
| 537 | return formatList; |
| 538 | |
| 539 | QList<QTextLayout::FormatRange> adjustedFormats; |
| 540 | std::transform(formatList.cbegin(), formatList.cend(), std::back_inserter(adjustedFormats), |
| 541 | [&](const QTextLayout::FormatRange &format) { |
| 542 | return QTextLayout::FormatRange { format.start - offset, format.length, format.format }; |
| 543 | }); |
| 544 | return adjustedFormats; |
| 545 | }; |
| 546 | |
| 547 | const auto &matchedLength = index.data(MatchedLengthRole).toInt(); |
| 548 | int keywordOffset = index.data(ColumnRole).toInt(); |
| 549 | const auto &replaceText = index.data(ReplaceTextRole).toString(); |
| 550 | |
| 551 | auto displayText = originalText.trimmed(); |
| 552 | int offset = originalText.indexOf(displayText); |
| 553 | keywordOffset -= offset; |
| 554 | int replaceOffset = keywordOffset + matchedLength; |
| 555 | |
| 556 | int leftStart = std::max(0, keywordOffset - ContentLeftSideMaxLength); |
| 557 | int rightEnd = std::min(displayText.length(), replaceOffset + replaceText.length() + ContentRightSideMaxLength); |
| 558 | displayText = displayText.mid(leftStart, rightEnd - leftStart); |
| 559 | if (leftStart != 0) { |
| 560 | displayText.insert(0, leftPadding); |
| 561 | leftStart -= 1; |
| 562 | } |
| 563 | |
| 564 | return { displayText, adjustFormatRange(leftStart + offset) }; |
| 565 | } |