File: vcsannotationitemdelegate.cpp

package info (click to toggle)
kdevelop 4%3A25.04.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 73,508 kB
  • sloc: cpp: 291,803; python: 4,322; javascript: 3,518; sh: 1,316; ansic: 703; xml: 414; php: 95; lisp: 66; makefile: 31; sed: 12
file content (375 lines) | stat: -rw-r--r-- 14,887 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*
    SPDX-FileCopyrightText: 2017-2018 Friedrich W. H. Kossebau <kossebau@kde.org>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "vcsannotationitemdelegate.h"

#include <models/vcsannotationmodel.h>
#include <vcsannotation.h>
#include <debug.h>

#include <KTextEditor/AnnotationInterface>
#include <KTextEditor/View>
#include <KTextEditor/Attribute>
#include <KLocalizedString>

#include <QHelpEvent>
#include <QPainter>
#include <QAction>
#include <QMenu>
#include <QToolTip>
#include <QFontMetricsF>
#include <QDate>
#include <QStyle>
#include <QApplication>

#include <cmath>

using namespace KDevelop;

VcsAnnotationItemDelegate::VcsAnnotationItemDelegate(KTextEditor::View* view, KTextEditor::AnnotationModel* model,
                                                     QObject* parent)
    : KTextEditor::AbstractAnnotationItemDelegate(parent)
    , m_model(model)
{
    // dump background brushes on schema change
    connect(view, &KTextEditor::View::configChanged, this, &VcsAnnotationItemDelegate::resetBackgrounds);

    view->installEventFilter(this);
}

VcsAnnotationItemDelegate::~VcsAnnotationItemDelegate() = default;

static QString ageOfDate(const QDate& date)
{
    const auto now = QDate::currentDate();
    int ageInYears = now.year() - date.year();
    if (now < date.addYears(ageInYears)) {
        --ageInYears;
    }
    if (ageInYears > 0) {
        return i18ncp("@item age", "%1 year", "%1 years", ageInYears);
    }
    int ageInMonths = now.month() - date.month();
    if (now.day() < date.day()) {
        --ageInMonths;
    }
    if (ageInMonths < 0) {
        ageInMonths += 12;
    }
    if (ageInMonths > 0) {
        return i18ncp("@item age", "%1 month", "%1 months", ageInMonths);
    }
    const int ageInDays = date.daysTo(now);
    if (ageInDays > 0) {
        return i18ncp("@item age", "%1 day", "%1 days", ageInDays);
    }
    return i18nc("@item age", "Today");
}

void VcsAnnotationItemDelegate::doMessageLineLayout(const KTextEditor::StyleOptionAnnotationItem& option,
                                                    QRect* messageRect, QRect* ageRect) const
{
    Q_ASSERT(messageRect && messageRect->isValid());
    Q_ASSERT(ageRect);

    const QWidget* const widget = option.view;
    QStyle* const style = widget ? widget->style() : QApplication::style();
    const bool hasAge = ageRect->isValid();
    // "+ 1" as used in QItemDelegate
    const int textMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, widget) + 1;
    const int ageMargin = hasAge ? textMargin : 0;

    const int x = option.rect.left();
    const int y = option.rect.top();
    const int w = option.rect.width();
    const int h = option.rect.height();

    // add margins for fixed elements
    QSize ageSize(0, 0); // ageRect could be invalid, so use separate object for calculation
    if (hasAge) {
        ageSize = ageRect->size();
        ageSize.rwidth() += 2 * ageMargin;
    }

    // distribute space among layout items
    QRect message;
    QRect age;
    if (option.direction == Qt::LeftToRight) {
        message.setRect(x, y, w - ageSize.width(), h);
        age.setRect(message.right() + 1, y, ageSize.width(), h);
    } else {
        age.setRect(x, y, ageSize.width(), h);
        message.setRect(age.right() + 1, y, w - ageSize.width(), h);
    }
    // remove margins here, so renderMessageAndAge does not have to
    message.adjust(textMargin, 0, -textMargin, 0);
    age.adjust(ageMargin, 0, -ageMargin, 0);

    // return result
    *ageRect = age;
    *messageRect = QStyle::alignedRect(option.direction, Qt::AlignLeading,
                                       messageRect->size().boundedTo(message.size()), message);
}

void VcsAnnotationItemDelegate::doAuthorLineLayout(const KTextEditor::StyleOptionAnnotationItem& option,
                                                   QRect* authorRect) const
{
    Q_ASSERT(authorRect);

    // if invalid, nothing to be done, keep as is
    if (!authorRect->isValid()) {
        return;
    }

    const QWidget* const widget = option.view;
    QStyle* const style = widget ? widget->style() : QApplication::style();
    // "+ 1" as used in QItemDelegate
    const int authorMargin = style->pixelMetric(QStyle::PM_FocusFrameHMargin, nullptr, widget) + 1;

    QRect author = option.rect;
    // remove margins here, so renderAuthor does not have to
    author.adjust(authorMargin, 0, -authorMargin, 0);

    // return result
    *authorRect = QStyle::alignedRect(option.direction, Qt::AlignLeading,
                                      authorRect->size().boundedTo(author.size()), author);
}

void VcsAnnotationItemDelegate::renderBackground(QPainter* painter,
                                                 const KTextEditor::StyleOptionAnnotationItem& option,
                                                 const VcsAnnotationLine& annotationLine) const
{
    Q_UNUSED(option);

    const auto revision = annotationLine.revision();
    auto brushIt = m_backgrounds.find(revision);
    if (brushIt == m_backgrounds.end()) {
        const auto normalStyle = option.view->defaultStyleAttribute(KSyntaxHighlighting::Theme::TextStyle::Normal);
        const auto background = (normalStyle->hasProperty(QTextFormat::BackgroundBrush)) ? normalStyle->background().color() : QColor(Qt::white);
        const int background_y = background.red()*0.299 + 0.587*background.green()
                                                        + 0.114*background.blue();
        // get random, but reproducible 8-bit values from last two bytes of the revision hash
        const auto revisionHash = qHash(revision);
        const int u = static_cast<int>((0xFF & revisionHash));
        const int v = static_cast<int>((0xFF00 & revisionHash) >> 8);
        const int r = qRound(qMin(255.0, qMax(0.0, background_y + 1.402*(v-128))));
        const int g = qRound(qMin(255.0, qMax(0.0, background_y - 0.344*(u-128) - 0.714*(v-128))));
        const int b = qRound(qMin(255.0, qMax(0.0, background_y + 1.772*(u-128))));
        brushIt = m_backgrounds.insert(revision, QBrush(QColor(r, g, b)));
    }

    painter->fillRect(option.rect, brushIt.value());
}

void VcsAnnotationItemDelegate::renderMessageAndAge(QPainter* painter,
                                                    const KTextEditor::StyleOptionAnnotationItem& option,
                                                    const QRect& messageRect, const QString& messageText,
                                                    const QRect& ageRect, const QString& ageText) const
{
    Q_UNUSED(option);

    painter->save();

    const auto normalStyle = option.view->defaultStyleAttribute(KSyntaxHighlighting::Theme::TextStyle::Normal);
    painter->setPen(normalStyle->foreground().color());
    painter->drawText(messageRect, Qt::AlignLeading | Qt::AlignVCenter,
                      painter->fontMetrics().elidedText(messageText, Qt::ElideRight, messageRect.width()));

    // TODO: defaultStyleAttribute only returns reliably for TextStyle::Normal, so what to do for a comment-like color?
    const auto commentStyle = option.view->defaultStyleAttribute(KSyntaxHighlighting::Theme::TextStyle::Normal);
    painter->setPen(commentStyle->foreground().color());
    painter->drawText(ageRect, Qt::AlignTrailing | Qt::AlignVCenter, ageText);

    painter->restore();
}

void VcsAnnotationItemDelegate::renderAuthor(QPainter* painter,
                                             const KTextEditor::StyleOptionAnnotationItem& option,
                                             const QRect& authorRect, const QString& authorText) const
{
    Q_UNUSED(option);

    painter->save();

    // TODO: defaultStyleAttribute only returns reliably for TextStyle::Normal, so what to do for a comment-like color?
    const auto commentStyle = option.view->defaultStyleAttribute(KSyntaxHighlighting::Theme::TextStyle::Normal);
    painter->setPen(commentStyle->foreground().color());
    painter->drawText(authorRect, Qt::AlignLeading | Qt::AlignVCenter,
                      painter->fontMetrics().elidedText(authorText, Qt::ElideRight, authorRect.width()));

    painter->restore();
}

void VcsAnnotationItemDelegate::renderHighlight(QPainter* painter,
                                                const KTextEditor::StyleOptionAnnotationItem& option) const
{
    // Draw a border around all adjacent entries that have the same text as the currently hovered one
    if ((option.state & QStyle::State_MouseOver) &&
        (option.annotationItemGroupingPosition & KTextEditor::StyleOptionAnnotationItem::InGroup)) {
        const auto style = option.view->defaultStyleAttribute(KSyntaxHighlighting::Theme::TextStyle::Normal);
        painter->setPen(style->foreground().color());
        // Use floating point coordinates to support scaled rendering
        QRectF rect(option.rect);
        rect.adjust(0.5, 0.5, -0.5, -0.5);
        // draw left and right highlight borders
        painter->drawLine(rect.topLeft(), rect.bottomLeft());
        painter->drawLine(rect.topRight(), rect.bottomRight());

        if ((option.annotationItemGroupingPosition & KTextEditor::StyleOptionAnnotationItem::GroupBegin) &&
            (option.wrappedLine == 0)) {
            painter->drawLine(rect.topLeft(), rect.topRight());
        }

        if ((option.annotationItemGroupingPosition & KTextEditor::StyleOptionAnnotationItem::GroupEnd) &&
            (option.wrappedLine == (option.wrappedLineCount-1))) {
            painter->drawLine(rect.bottomLeft(), rect.bottomRight());
        }
    }
}

void VcsAnnotationItemDelegate::paint(QPainter* painter, const KTextEditor::StyleOptionAnnotationItem& option,
                                      KTextEditor::AnnotationModel* model, int line) const
{
    Q_ASSERT(painter);
    // we cannot use custom roles and data() API (cmp. VcsAnnotationModel dox), so accessing custom API instead
    auto* vcsModel = qobject_cast<VcsAnnotationModel*>(model);
    Q_ASSERT(vcsModel);
    if (!painter || !vcsModel) {
        return;
    }
    // test of line just for sake of completeness skipped here

    // Fetch data from the model
    const VcsAnnotationLine annotationLine = vcsModel->annotationLine(line);

    if (annotationLine.revision().revisionType() == VcsRevision::Invalid) {
        return;
    }

    // prepare
    painter->save();

    renderBackground(painter, option, annotationLine);

    // We use the normal UI font here, which usually is a proportimal one,
    // so more text fits into the available space.
    // Though we do this at the cost of not adapting to any scaled content font size,
    // as there is no zooming state info available, so we cannot adapt.
    // Tooltip font also is not scaled, and annotations could be considered to fall into
    // that category, so might be fine.
    painter->setFont(option.view->font());

    if (option.visibleWrappedLineInGroup == 0) {
        QRect ageRect;
        QString ageText;
        const auto date = annotationLine.date();
        if (date.isValid()) {
            ageText = ageOfDate(date.date());
            ageRect = QRect(QPoint(0, 0), QSize(option.fontMetrics.horizontalAdvance(ageText), option.rect.height()));
        }
        const auto messageText = annotationLine.commitMessage();
        auto messageRect =
            QRect(QPoint(0, 0), QSize(option.fontMetrics.horizontalAdvance(messageText), option.rect.height()));

        doMessageLineLayout(option, &messageRect, &ageRect);

        renderMessageAndAge(painter, option, messageRect, messageText, ageRect, ageText);
    } else if (option.visibleWrappedLineInGroup == 1) {
        const auto author = annotationLine.author();
        if (!author.isEmpty()) {
            const auto authorText = i18nc("By: commit author", "By: %1", author);
            auto authorRect =
                QRect(QPoint(0, 0), QSize(option.fontMetrics.horizontalAdvance(authorText), option.rect.height()));

            doAuthorLineLayout(option, &authorRect);

            renderAuthor(painter, option, authorRect, authorText);
        }
    }

    renderHighlight(painter, option);

    // done
    painter->restore();
}

bool VcsAnnotationItemDelegate::helpEvent(QHelpEvent* event, KTextEditor::View* view,
                                          const KTextEditor::StyleOptionAnnotationItem& option,
                                          KTextEditor::AnnotationModel* model, int line)
{
    Q_UNUSED(option);
    if (!model || event->type() != QEvent::ToolTip) {
        return false;
    }

    const QVariant data = model->data(line, Qt::ToolTipRole);
    if (!data.isValid()) {
        return false;
    }

    const QString toolTipText = data.toString();
    if (toolTipText.isEmpty()) {
        return false;
    }

    QToolTip::showText(event->globalPos(), toolTipText, view, option.rect);

    return true;
}

void VcsAnnotationItemDelegate::hideTooltip(KTextEditor::View *view)
{
    Q_UNUSED(view);
    QToolTip::hideText();
}

QSize VcsAnnotationItemDelegate::sizeHint(const KTextEditor::StyleOptionAnnotationItem& option,
                                          KTextEditor::AnnotationModel* model, int line) const
{
    Q_UNUSED(line);
    Q_ASSERT(model);
    if (!model) {
        return QSize(0, 0);
    }

    // Ideally the user could configure the width of the annotations, best interactively.
    // Until this is possible, the sizehint is: roughly 40 chars, but maximal 25 % of the view
    // See eventFilter for making sure we adapt the max 25 % to a changed width.

    const QFontMetricsF& fm(option.fontMetrics);
    // if only averageCharWidth would yield sane values,
    // multiply by 40 in average seemed okayish at least with english, showing enough of message
    m_lastCharBasedWidthHint = ceil(40 * fm.averageCharWidth());
    m_lastViewBasedWidthHint = widthHintFromViewWidth(option.view->width());
    return QSize(qMin(m_lastCharBasedWidthHint, m_lastViewBasedWidthHint), fm.height());
}

bool VcsAnnotationItemDelegate::eventFilter(QObject* object, QEvent* event)
{
    if (event->type() == QEvent::Resize) {
        auto resizeEvent = static_cast<QResizeEvent*>(event);
        const int viewBasedWidthHint = widthHintFromViewWidth(resizeEvent->size().width());
        if ((viewBasedWidthHint < m_lastCharBasedWidthHint) &&
            (viewBasedWidthHint != m_lastViewBasedWidthHint)) {
            // emit for first line only, assuming uniformAnnotationItemSizes is set to true
            emit sizeHintChanged(m_model, 0);
        }
    }

    return KTextEditor::AbstractAnnotationItemDelegate::eventFilter(object, event);
}

void VcsAnnotationItemDelegate::resetBackgrounds()
{
    m_backgrounds.clear();
}

int VcsAnnotationItemDelegate::widthHintFromViewWidth(int viewWidth) const
{
    return viewWidth * m_maxWidthViewPercent / 100;
}

#include "moc_vcsannotationitemdelegate.cpp"