| 131 | } |
| 132 | |
| 133 | void CodeLensDelegate::drawText(QPainter *painter, const QStyleOptionViewItem &option, |
| 134 | const QRect &rect, const QModelIndex &index) const |
| 135 | { |
| 136 | const auto text = itemText(index); |
| 137 | const int termStart = index.data(CodeLensItemRole::TermStartRole).toInt(); |
| 138 | const auto termEnd = index.data(CodeLensItemRole::TermEndRole).toInt(); |
| 139 | int termLength = termEnd - termStart; |
| 140 | if (termStart < 0 || termStart >= text.length() || termLength < 1) { |
| 141 | QItemDelegate::drawDisplay(painter, |
| 142 | option, |
| 143 | rect, |
| 144 | QString(text).replace(QLatin1Char('\t'), tabStr)); |
| 145 | return; |
| 146 | } |
| 147 | |
| 148 | // clip searchTermLength to end of line |
| 149 | termLength = qMin(termLength, text.length() - termStart); |
| 150 | const int textMargin = QApplication::style()->pixelMetric(QStyle::PM_FocusFrameHMargin) + 1; |
| 151 | const QString textBefore = text.left(termStart).replace(QLatin1Char('\t'), tabStr); |
| 152 | const QString textHighlight = text.mid(termStart, termLength).replace(QLatin1Char('\t'), tabStr); |
| 153 | const QString textAfter = text.mid(termStart + termLength).replace(QLatin1Char('\t'), tabStr); |
| 154 | int searchTermStartPixels = option.fontMetrics.horizontalAdvance(textBefore); |
| 155 | int searchTermLengthPixels = option.fontMetrics.horizontalAdvance(textHighlight); |
| 156 | int textAfterLengthPixels = option.fontMetrics.horizontalAdvance(textAfter); |
| 157 | |
| 158 | // rects |
| 159 | QRect beforeHighlightRect(rect); |
| 160 | beforeHighlightRect.setRight(beforeHighlightRect.left() + searchTermStartPixels); |
| 161 | |
| 162 | QRect resultHighlightRect(rect); |
| 163 | resultHighlightRect.setLeft(beforeHighlightRect.right()); |
| 164 | resultHighlightRect.setRight(resultHighlightRect.left() + searchTermLengthPixels); |
| 165 | |
| 166 | QRect afterHighlightRect(rect); |
| 167 | afterHighlightRect.setLeft(resultHighlightRect.right()); |
| 168 | afterHighlightRect.setRight(afterHighlightRect.left() + textAfterLengthPixels); |
| 169 | |
| 170 | // paint all highlight backgrounds |
| 171 | bool isSelected = option.state & QStyle::State_Selected; |
| 172 | QPalette::ColorGroup cg = option.state & QStyle::State_Enabled |
| 173 | ? QPalette::Normal |
| 174 | : QPalette::Disabled; |
| 175 | if (cg == QPalette::Normal && !(option.state & QStyle::State_Active)) |
| 176 | cg = QPalette::Inactive; |
| 177 | QStyleOptionViewItem baseOption = option; |
| 178 | baseOption.state &= ~QStyle::State_Selected; |
| 179 | if (isSelected) { |
| 180 | painter->fillRect(beforeHighlightRect.adjusted(textMargin, 0, textMargin, 0), |
| 181 | option.palette.brush(cg, QPalette::Highlight)); |
| 182 | painter->fillRect(afterHighlightRect.adjusted(textMargin, 0, textMargin, 0), |
| 183 | option.palette.brush(cg, QPalette::Highlight)); |
| 184 | } |
| 185 | |
| 186 | QColor highlightBackground("#ffef0b"); |
| 187 | if (DGuiApplicationHelper::instance()->themeType() == DGuiApplicationHelper::DarkType) |
| 188 | highlightBackground = QColor("#8a7f2c"); |
| 189 | painter->fillRect(resultHighlightRect.adjusted(textMargin, 0, textMargin - 1, 0), |
| 190 | QBrush(highlightBackground)); |
no test coverage detected