|
| 1 | +/* |
| 2 | + * This file is part of Notepad Next. |
| 3 | + * Copyright 2025 Justin Dailey |
| 4 | + * |
| 5 | + * Notepad Next is free software: you can redistribute it and/or modify |
| 6 | + * it under the terms of the GNU General Public License as published by |
| 7 | + * the Free Software Foundation, either version 3 of the License, or |
| 8 | + * (at your option) any later version. |
| 9 | + * |
| 10 | + * Notepad Next is distributed in the hope that it will be useful, |
| 11 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | + * GNU General Public License for more details. |
| 14 | + * |
| 15 | + * You should have received a copy of the GNU General Public License |
| 16 | + * along with Notepad Next. If not, see <https://www.gnu.org/licenses/>. |
| 17 | + */ |
| 18 | + |
| 19 | +#include "SearchResultHighlighterDelegate.h" |
| 20 | +#include "SearchResultData.h" |
| 21 | + |
| 22 | +#include <QPainter> |
| 23 | + |
| 24 | +void 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); |
| 82 | + |
| 83 | + painter->restore(); |
| 84 | +} |
0 commit comments