-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDirTreeDelegate.cpp
More file actions
66 lines (53 loc) · 2.08 KB
/
DirTreeDelegate.cpp
File metadata and controls
66 lines (53 loc) · 2.08 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
#include "DirTreeDelegate.h"
DirTreeDelegate::DirTreeDelegate(QObject* parent)
: QStyledItemDelegate(parent) {
}
void DirTreeDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index) const {
painter->save();
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
QRect rect = option.rect;
rect.setX(0);
rect.setWidth(option.widget->width() - 6);
// цвет фона
QColor background;
if (opt.state & QStyle::State_Selected) {
background = QColor("#3e3e3e");
}
else if (opt.state & QStyle::State_MouseOver) {
background = QColor("#4b4b4b");
}
else {
background = opt.palette.base().color();
}
painter->fillRect(rect, background);
// стрелка
int branchWidth = 20;
QRect branchRect(option.rect.x(), option.rect.y(), branchWidth, option.rect.height());
if (index.model()->hasChildren(index)) {
QPoint center(branchRect.center().x(), branchRect.center().y());
int size = 8;
QIcon icon;
if (opt.state & QStyle::State_Open) {
//вниз
icon = QIcon(":/FileManager/Resources/arrow-down.svg");
} else {
// вправо
icon = QIcon(":/FileManager/Resources/arrow-right.svg");
}
painter->drawImage(center.x() - size / 2, center.y() - size / 2, icon.pixmap(size, size).toImage());
}
int padding = 8;
int iconSize = 16;
QRect iconRect(option.rect.x() + branchWidth + padding, option.rect.y(), iconSize, option.rect.height());
QRect textRect(option.rect.x() + branchWidth + iconSize + padding + 4, option.rect.y(), option.rect.width() - (branchWidth + iconSize + padding + 4), option.rect.height());
opt.icon.paint(painter, iconRect, Qt::AlignVCenter);
if (opt.state & QStyle::State_Selected) {
painter->setPen(opt.palette.highlightedText().color());
}
else {
painter->setPen(opt.palette.text().color());
}
painter->drawText(textRect, Qt::AlignVCenter, index.data().toString());
painter->restore();
}