This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
prtlistitemdelegate.cpp
96 lines (81 loc) · 3.38 KB
/
prtlistitemdelegate.cpp
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
96
#include "stdafx.h"
#include "prtlistitemdelegate.h"
#if defined(Q_OS_WIN)
# define globalDefaultFontFamily "Microsoft YaHei"
#else
# define globalDefaultFontFamily "PingFang CS"
#endif
PRTListItemDelegate::PRTListItemDelegate(QListWidget *parent) : QAbstractItemDelegate(parent), m_parentListWidget(parent) {}
void PRTListItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QRect r = option.rect;
// Color: #C4C4C4
QPen linePen(QColor::fromRgb(211, 211, 211), 1, Qt::SolidLine);
// Color: #005A83
QPen lineMarkedPen(QColor::fromRgb(0, 90, 131), 1, Qt::SolidLine);
// Color: #333
QPen fontPen(QColor::fromRgb(51, 51, 51), 1, Qt::SolidLine);
// Color: #fff
QPen fontMarkedPen(Qt::white, 1, Qt::SolidLine);
if (option.state & QStyle::State_Selected)
{
QLinearGradient gradientSelected(r.left(), r.top(), r.left(), r.height() + r.top());
gradientSelected.setColorAt(0.0, QColor::fromRgb(119, 213, 247));
gradientSelected.setColorAt(0.9, QColor::fromRgb(27, 134, 183));
gradientSelected.setColorAt(1.0, QColor::fromRgb(0, 120, 174));
painter->setBrush(gradientSelected);
painter->drawRect(r);
// BORDER
painter->setPen(lineMarkedPen);
painter->drawLine(r.topLeft(), r.topRight());
painter->drawLine(r.topRight(), r.bottomRight());
painter->drawLine(r.bottomLeft(), r.bottomRight());
painter->drawLine(r.topLeft(), r.bottomLeft());
painter->setPen(fontMarkedPen);
}
else
{
// BACKGROUND
// ALTERNATING COLORS
painter->setBrush((index.row() % 2) ? Qt::white : QColor(252, 252, 252));
painter->drawRect(r);
// BORDER
painter->setPen(linePen);
painter->drawLine(r.topLeft(), r.topRight());
painter->drawLine(r.topRight(), r.bottomRight());
painter->drawLine(r.bottomLeft(), r.bottomRight());
painter->drawLine(r.topLeft(), r.bottomLeft());
painter->setPen(fontPen);
}
QString title = index.data(Qt::DisplayRole).toString();
QString topic = index.data(Qt::UserRole + 1).toString();
QString owner = index.data(Qt::UserRole + 2).toString();
QString content = index.data(Qt::UserRole + 3).toString();
painter->setFont(QFont(globalDefaultFontFamily,
#if defined(Q_OS_WIN)
12,
#else
14,
#endif
QFont::Normal));
painter->drawText(r, Qt::AlignLeft | Qt::AlignVCenter | Qt::TextWordWrap, title, &r);
}
QSize PRTListItemDelegate::sizeHint(const QStyleOptionViewItem & /*option*/, const QModelIndex &index) const
{
QFontMetrics fm(QFont(globalDefaultFontFamily,
#if defined(Q_OS_WIN)
12,
#else
14,
#endif
QFont::Normal));
QString title = index.data(Qt::DisplayRole).toString();
QRect rc = fm.boundingRect(title + "paddingpadding"); // since this method doesn't support word wrap detection
int width = m_parentListWidget->width() - 4;
#if defined(Q_OS_WIN)
if (m_parentListWidget->verticalScrollBar() && m_parentListWidget->verticalScrollBar()->isVisible())
width -= m_parentListWidget->verticalScrollBar()->width();
#endif
int height = (rc.width() / width + 1) * rc.height();
return QSize(width + 4, height + 4);
}