-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileItemDelegate.cpp
More file actions
67 lines (53 loc) · 2.27 KB
/
FileItemDelegate.cpp
File metadata and controls
67 lines (53 loc) · 2.27 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
#include "FileItemDelegate.h"
FileItemDelegate::FileItemDelegate(QObject *parent)
: QStyledItemDelegate(parent), showEditIcon(false), showDeleteIcon(false) {
}
void FileItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option,
const QModelIndex &index) const {
painter->save();
QRect rect = option.rect;
QString name = index.data(Qt::DisplayRole).toString();
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
// 检查是否是空文件夹提示项(没有图标且没有文本的项)
bool isEmptyFolderItem = icon.isNull() && name.isEmpty() && index.row() > 0; // row>0 排除面包屑导航
if (!isEmptyFolderItem) {
// 背景
QColor bg = (option.state & QStyle::State_Selected) ? QColor("#333333") : QColor("#2b2b2b");
painter->setBrush(bg);
painter->setPen(Qt::NoPen);
painter->setRenderHint(QPainter::Antialiasing);
painter->drawRoundedRect(rect.adjusted(2, 2, -2, -2), 12, 12);
// 图标
QRect iconRect(rect.left() + 14, rect.top() + 10, 22, 22);
icon.paint(painter, iconRect);
// 文件名
QRect textRect = rect.adjusted(44, 0, -28, 0);
painter->setPen(Qt::white);
painter->drawText(textRect, Qt::AlignVCenter | Qt::AlignLeft, name);
// 箭头、编辑图标或删除图标
painter->setPen(QColor("#888888"));
if (showDeleteIcon) {
painter->drawText(QRect(rect.right() - 24, rect.top(), 16, rect.height()), Qt::AlignCenter, "✖");
} else if (showEditIcon) {
painter->drawText(QRect(rect.right() - 24, rect.top(), 16, rect.height()), Qt::AlignCenter, "✎");
} else {
painter->drawText(QRect(rect.right() - 24, rect.top(), 16, rect.height()), Qt::AlignCenter, ">");
}
}
painter->restore();
}
QSize FileItemDelegate::sizeHint(const QStyleOptionViewItem &, const QModelIndex &) const {
return QSize(100, 44);
}
void FileItemDelegate::setShowEditIcon(bool show) {
showEditIcon = show;
}
bool FileItemDelegate::getShowEditIcon() const {
return showEditIcon;
}
void FileItemDelegate::setShowDeleteIcon(bool show) {
showDeleteIcon = show;
}
bool FileItemDelegate::getShowDeleteIcon() const {
return showDeleteIcon;
}