MCPcopy Create free account
hub / github.com/dail8859/NotepadNext / paint

Method paint

src/SearchResultHighlighterDelegate.cpp:24–84  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

22#include <QPainter>
23
24void SearchResultHighlighterDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
25{
26 // Only custom paint for column 1 with highlight flag set
27 if (index.column() != 1 || !index.data(SearchResultData::LineNumber).isValid()) {
28 QStyledItemDelegate::paint(painter, option, index);
29 return;
30 }
31
32 const QString text = index.data(Qt::DisplayRole).toString();
33 int start = index.data(SearchResultData::LinePosStart).toInt();
34 int end = index.data(SearchResultData::LinePosEnd).toInt();
35
36 QStyleOptionViewItem opt(option);
37 initStyleOption(&opt, index);
38
39 painter->save();
40 painter->setClipRect(opt.rect);
41
42 // Optional: draw background or selection as usual
43 if (opt.state & QStyle::State_Selected) {
44 painter->fillRect(opt.rect, opt.palette.highlight());
45 } else {
46 painter->fillRect(opt.rect, opt.backgroundBrush);
47 }
48
49 QRect textRect = opt.rect.adjusted(4, 0, -4, 0); // small padding
50 QFontMetrics fm(opt.font);
51 int y = textRect.top() + (textRect.height() + fm.ascent() - fm.descent()) / 2;
52 int x = textRect.left();
53
54 // Split the text
55 const QString before = text.left(start);
56 const QString match = text.mid(start, end - start);
57 const QString after = text.mid(end);
58
59 // Draw 'before' text (normal)
60 painter->setFont(opt.font);
61 painter->setPen(opt.palette.color(QPalette::Text));
62 painter->drawText(x, y, before);
63 x += fm.horizontalAdvance(before);
64
65 // Draw highlighted 'match' text (bold, red, yellow bg)
66 QFont boldFont = opt.font;
67 boldFont.setBold(true);
68 painter->setFont(boldFont);
69
70 int matchWidth = fm.horizontalAdvance(match);
71 QRect highlightRect(x, textRect.top(), matchWidth, textRect.height());
72 painter->fillRect(highlightRect, QColor(Qt::yellow));
73
74 painter->setPen(Qt::red);
75 painter->drawText(x, y, match);
76 x += matchWidth;
77
78 // Draw 'after' text (normal)
79 painter->setFont(opt.font);
80 painter->setPen(opt.palette.color(QPalette::Text));
81 painter->drawText(x, y, after);

Callers

nothing calls this directly

Calls 3

toStringMethod · 0.80
saveMethod · 0.80
dataMethod · 0.45

Tested by

no test coverage detected