-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathraventreedelegate.cpp
More file actions
90 lines (74 loc) · 3.3 KB
/
raventreedelegate.cpp
File metadata and controls
90 lines (74 loc) · 3.3 KB
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
#include "raventreedelegate.h"
#include "raventreeitem.h"
#include <QApplication>
#include <QMouseEvent>
#include <QPainter>
RavenTreeDelegate::RavenTreeDelegate(QObject *parent) : QStyledItemDelegate(parent) {}
void RavenTreeDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const {
painter->save();
// Call base class' paint method to render default items
QStyledItemDelegate::paint(painter, option, index);
// Apply our changes on top of the default UI
if (index.isValid()) {
auto *treeItem = static_cast<RavenTreeItem *>(index.internalPointer());
// Applicable to non-heading items only
if (treeItem->heading) {
painter->restore();
return;
}
auto isItemSelected = (option.state & QStyle::State_Selected);
auto statusRect = option.rect;
// Increase yOffset for rect to show custom UI items.
statusRect.setY(statusRect.y() + 18);
// Status text (shows D/M/U text at end of row)
auto statusPoint = statusRect.topRight();
statusPoint.setX(statusPoint.x() - 20);
auto font = painter->font();
font.setWeight(QFont::Weight::Bold);
if (treeItem->deleted) {
// Show "Deleted" status
painter->setFont(font);
painter->setPen(isItemSelected ? option.palette.text().color() : Qt::red);
painter->drawText(statusPoint, "D");
} else if (treeItem->modified()) {
// Show "Uncommitted" status
painter->setFont(font);
painter->setPen(isItemSelected ? option.palette.text().color() : Qt::magenta);
painter->drawText(statusPoint, "M");
} else {
// Show "Uncommitted" status
painter->setFont(font);
painter->setPen(isItemSelected ? option.palette.text().color() : Qt::green);
painter->drawText(statusPoint, "U");
}
// Show button to + or - from Changes<->Staging Area
QRect buttonRect = option.rect;
buttonRect.setX(option.rect.topRight().x() - 60);
buttonRect.setWidth(24);
buttonRect.setHeight(24);
// Create button widget here
QStyleOptionButton buttonOption;
buttonOption.rect = buttonRect;
buttonOption.state = option.state;
buttonOption.features = QStyleOptionButton::Flat;
buttonOption.palette = option.palette;
QStyle *style = QApplication::style();
// WORKAROUND: Hide the `border-bottom` for `buttonOption` when treeview row is selected
QBrush buttonOptionBrush;
buttonOptionBrush.setColor(Qt::GlobalColor::transparent);
buttonOption.palette.setBrush(QPalette::ColorRole::HighlightedText, buttonOptionBrush);
if (treeItem->initiator == RavenTreeItem::STAGING) {
// Show - icon
buttonOption.text = "-";
style->drawControl(QStyle::CE_PushButton, &buttonOption, painter);
} else {
// Show + icon
buttonOption.text = "+";
style->drawControl(QStyle::CE_PushButton, &buttonOption, painter);
}
}
painter->restore();
}
QSize RavenTreeDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const {
return QStyledItemDelegate::sizeHint(option, index);
}