| 42 | } |
| 43 | |
| 44 | void GrepOutputDelegate::paint( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const |
| 45 | { |
| 46 | // there is no function in QString to left-trim. A call to remove this regexp does the job |
| 47 | static const QRegularExpression leftspaces(QStringLiteral("^\\s*")); |
| 48 | |
| 49 | // rich text component |
| 50 | const auto* model = qobject_cast<const GrepOutputModel*>(index.model()); |
| 51 | const auto *item = dynamic_cast<const GrepOutputItem *>(model->itemFromIndex(index)); |
| 52 | |
| 53 | QStyleOptionViewItem options = option; |
| 54 | initStyleOption(&options, index); |
| 55 | |
| 56 | // building item representation |
| 57 | QTextDocument doc; |
| 58 | QTextCursor cur(&doc); |
| 59 | |
| 60 | QPalette::ColorGroup cg = (options.state & QStyle::State_Enabled) |
| 61 | ? QPalette::Normal : QPalette::Disabled; |
| 62 | QPalette::ColorRole cr = (options.state & QStyle::State_Selected) |
| 63 | ? QPalette::HighlightedText : QPalette::Text; |
| 64 | QTextCharFormat fmt = cur.charFormat(); |
| 65 | fmt.setFont(options.font); |
| 66 | |
| 67 | if(item && item->isText()) |
| 68 | { |
| 69 | // Use custom manual highlighting |
| 70 | |
| 71 | const KTextEditor::Range rng = item->change()->m_range; |
| 72 | |
| 73 | // the line number appears grayed |
| 74 | fmt.setForeground(options.palette.brush(QPalette::Disabled, cr)); |
| 75 | cur.insertText(i18n("Line %1: ",item->lineNumber()), fmt); |
| 76 | |
| 77 | // switch to normal color |
| 78 | fmt.setForeground(options.palette.brush(cg, cr)); |
| 79 | cur.insertText(item->text().left(rng.start().column()).remove(leftspaces), fmt); |
| 80 | |
| 81 | fmt.setFontWeight(QFont::Bold); |
| 82 | if ( !(options.state & QStyle::State_Selected) ) { |
| 83 | QColor bgHighlight = option.palette.color(QPalette::AlternateBase); |
| 84 | fmt.setBackground(bgHighlight); |
| 85 | } |
| 86 | cur.insertText(item->text().mid(rng.start().column(), rng.end().column() - rng.start().column()), fmt); |
| 87 | fmt.clearBackground(); |
| 88 | |
| 89 | fmt.setFontWeight(QFont::Normal); |
| 90 | cur.insertText(item->text().mid(rng.end().column()), fmt); |
| 91 | }else{ |
| 92 | QString text; |
| 93 | if(item) |
| 94 | text = item->text(); |
| 95 | else |
| 96 | text = index.data().toString(); |
| 97 | // Simply insert the text as html. We use this for the titles. |
| 98 | doc.setHtml(text); |
| 99 | } |
| 100 | |
| 101 | painter->save(); |
nothing calls this directly
no test coverage detected