-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileExplorerDelegate.cpp
More file actions
95 lines (79 loc) · 2.85 KB
/
Copy pathFileExplorerDelegate.cpp
File metadata and controls
95 lines (79 loc) · 2.85 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
91
92
93
94
95
#include "project/FileExplorerDelegate.h"
#include <QFileInfo>
#include <QFileSystemModel>
#include <QPainter>
#include <QSortFilterProxyModel>
namespace cold {
FileExplorerDelegate::FileExplorerDelegate(QObject* parent) : QStyledItemDelegate(parent) {}
void FileExplorerDelegate::setStatusMap(const QHash<QString, GitStatus>* status) {
m_status = status;
}
QColor FileExplorerDelegate::badgeColor(GitStatus status) {
switch (status) {
case GitStatus::Modified:
return QColor(QStringLiteral("#E5A84B"));
case GitStatus::Added:
case GitStatus::Untracked:
return QColor(QStringLiteral("#73C991"));
case GitStatus::Deleted:
return QColor(QStringLiteral("#F14C4C"));
case GitStatus::Renamed:
return QColor(QStringLiteral("#56B6C2"));
default:
return {};
}
}
QString FileExplorerDelegate::badgeText(GitStatus status) {
return gitStatusBadge(status);
}
void FileExplorerDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const {
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
GitStatus status = GitStatus::None;
if (m_status && index.isValid()) {
QModelIndex src = index;
const auto* proxy = qobject_cast<const QSortFilterProxyModel*>(index.model());
if (proxy) {
src = proxy->mapToSource(index);
}
const auto* fs =
qobject_cast<const QFileSystemModel*>(proxy ? proxy->sourceModel() : index.model());
if (fs) {
const QString path = fs->filePath(src);
if (QFileInfo(path).isFile()) {
const QString canonical = QFileInfo(path).canonicalFilePath();
const QString key = canonical.isEmpty() ? path : canonical;
status = m_status->value(key, GitStatus::None);
}
}
}
const bool hasBadge = status != GitStatus::None;
if (hasBadge) {
opt.rect.adjust(0, 0, -22, 0);
}
QStyledItemDelegate::paint(painter, opt, index);
if (!hasBadge) {
return;
}
const QString badge = badgeText(status);
const QColor color = badgeColor(status);
painter->save();
QFont font = opt.font;
font.setBold(true);
font.setPointSizeF(font.pointSizeF() - 0.5);
painter->setFont(font);
painter->setPen(color);
const QRect badgeRect = option.rect.adjusted(option.rect.width() - 20, 0, -4, 0);
painter->drawText(badgeRect, Qt::AlignRight | Qt::AlignVCenter, badge);
painter->restore();
}
QSize FileExplorerDelegate::sizeHint(const QStyleOptionViewItem& option,
const QModelIndex& index) const {
QSize size = QStyledItemDelegate::sizeHint(option, index);
if (m_status && index.isValid()) {
size.rwidth() += 22;
}
return size;
}
} // namespace cold