| 1 | // SPDX-FileCopyrightText: 2023 UnionTech Software Technology Co., Ltd. |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "codelensdelegate.h" |
| 6 | #include "codelenstype.h" |
| 7 | |
| 8 | #include <QApplication> |
| 9 | |
| 10 | CodeLensDelegate::CodeLensDelegate(QObject *parent) |
| 11 | : QStyledItemDelegate (parent) |
| 12 | , characterStart(-1) |
| 13 | , characterEnd(-1) |
| 14 | , color(QColor(Qt::yellow)) |
| 15 | { |
| 16 | color.setAlpha(50); |
| 17 | } |
| 18 | |
| 19 | void CodeLensDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, |
| 20 | const QModelIndex &index) const |
| 21 | { |
| 22 | QString displayName = index.data().toString(); |
| 23 | QFont font = painter->font(); |
| 24 | QFontMetrics fontMetrics(font); |
| 25 | if (index.parent().isValid()){ |
| 26 | lsp::Range range; |
| 27 | QString codeText; |
| 28 | QColor heightColor; |
| 29 | QVariant rangeVar = index.data(CodeLensItemRole::Range); |
| 30 | if (rangeVar.canConvert<lsp::Range>()) { |
| 31 | range = rangeVar.value<lsp::Range>(); |
| 32 | } |
| 33 | QVariant codeTextVar = index.data(CodeLensItemRole::CodeText); |
| 34 | if (codeTextVar.canConvert<QString>()) { |
| 35 | codeText = codeTextVar.value<QString>(); |
| 36 | } |
| 37 | QVariant colorVar = index.data(CodeLensItemRole::HeightColor); |
| 38 | if (colorVar.canConvert<QColor>()) { |
| 39 | heightColor = colorVar.value<QColor>(); |
| 40 | } |
| 41 | |
| 42 | // QStyleOptionViewItem styleOptionViewItem(option); |
| 43 | // initStyleOption(&styleOptionViewItem, index); |
| 44 | |
| 45 | QString lineNumCurr = QString::number(range.start.line + 1); //展示时line从1开始 |
| 46 | int sepWidth = option.fontMetrics.horizontalAdvance(" " ); |
| 47 | int lineNumMaxWidth = option.fontMetrics.horizontalAdvance(QString("99999" )); |
| 48 | int lineNumCurrWidth = option.fontMetrics.horizontalAdvance(lineNumCurr); |
| 49 | int lineNumOffset = lineNumMaxWidth - lineNumCurrWidth; |
| 50 | int textHeight = option.fontMetrics.height(); |
| 51 | QRect lineNumDrawRect = option.rect.adjusted(lineNumOffset, textHeight, 0, 0); |
| 52 | painter->setPen(option.widget->palette().text().color()); |
| 53 | painter->drawText(lineNumDrawRect, lineNumCurr); |
| 54 | |
| 55 | int startCharacter = range.start.character; |
| 56 | int endCharacter = range.end.character; |
| 57 | QString heightText = codeText.mid(startCharacter, endCharacter - startCharacter); |
| 58 | QString frontText = codeText.mid(0, startCharacter); |
| 59 | int frontTextWidth = option.fontMetrics.horizontalAdvance(frontText); |
| 60 | int heightTextWidth = option.fontMetrics.horizontalAdvance(heightText); |
| 61 | QRect heightTextRect = option.rect.adjusted(sepWidth + lineNumMaxWidth+ frontTextWidth, 0, 0, 0); |
| 62 | heightTextRect.setSize(QSize(heightTextWidth, option.rect.height())); |
| 63 | // {, option.rect.y(), heightTextWidth, option.rect.height()}; |
| 64 | painter->setBrush(CodeLensDelegate::color); |
| 65 | painter->setPen(Qt::NoPen); |
| 66 | painter->drawRect(heightTextRect); |
| 67 | |
| 68 | painter->setPen(option.widget->palette().text().color()); |
| 69 | QRect codeTextDrawRect = option.rect.adjusted(sepWidth + lineNumMaxWidth, textHeight, 0, 0); |
| 70 | painter->drawText(codeTextDrawRect, codeText); |
| 71 | |
| 72 | } else { |
| 73 | QStyledItemDelegate::paint(painter, option, index); |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | void CodeLensDelegate::setHeightColor(const QColor &color) |
| 78 | { |
| 79 | CodeLensDelegate::color = color; |
| 80 | } |
| 81 | |
| 82 | void CodeLensDelegate::setHeightRange(int characterStart, int characterEnd) |
| 83 | { |
| 84 | CodeLensDelegate::characterStart = characterStart; |
| 85 | CodeLensDelegate::characterEnd = characterEnd; |
| 86 | } |
| 87 | |