-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileModelDelegate.cpp
More file actions
64 lines (51 loc) · 2.14 KB
/
FileModelDelegate.cpp
File metadata and controls
64 lines (51 loc) · 2.14 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
#include "FileModelDelegate.h"
FileModelDelegate::FileModelDelegate(QObject* parent)
: QStyledItemDelegate(parent)
{
}
void FileModelDelegate::paint(QPainter* painter, const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
painter->save();
QStyleOptionViewItem opt = option;
initStyleOption(&opt, index);
if (option.state & QStyle::State_Selected) {
painter->fillRect(option.rect, option.palette.highlight());
}
QIcon icon = qvariant_cast<QIcon>(index.data(Qt::DecorationRole));
QString name = index.data(Qt::DisplayRole).toString();
QVariant sizeVar = index.data(Qt::UserRole);
QString size = index.data(Qt::UserRole + 3).toString();
QString type = index.data(Qt::UserRole + 1).toString();
QString lastModified = index.data(Qt::UserRole + 4).toString();
int padding = 5;
int iconSize = 32;
QRect iconRect(option.rect.x() + padding, option.rect.y() + padding,
iconSize, iconSize);
QRect textRect(option.rect.x() + iconSize + 2 * padding,
option.rect.y() + padding,
option.rect.width() - iconSize - 3 * padding,
option.rect.height() - 2 * padding);
icon.paint(painter, iconRect, Qt::AlignCenter);
painter->setPen(option.state & QStyle::State_Selected ?
option.palette.highlightedText().color() :
option.palette.text().color());
QFont nameFont = painter->font();
nameFont.setBold(true);
painter->setFont(nameFont);
painter->drawText(textRect, Qt::AlignLeft | Qt::AlignTop, name);
QFont normalFont = painter->font();
normalFont.setBold(false);
painter->setFont(normalFont);
painter->drawText(textRect.adjusted(0, 20, 0, 0),
Qt::AlignLeft | Qt::AlignTop,
size + (size.isEmpty() ? "" : " - ") + type + " | " + lastModified);
painter->restore();
}
QSize FileModelDelegate::sizeHint(const QStyleOptionViewItem& option,
const QModelIndex& index) const
{
Q_UNUSED(option);
Q_UNUSED(index);
return QSize(200, 50);
}