forked from GLDsuh-a/qt-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
persondelegate.cpp
119 lines (97 loc) · 4.05 KB
/
persondelegate.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include "persondelegate.h"
PersonDelegate::PersonDelegate(QObject *parent)
: QStyledItemDelegate(parent)
{
}
void PersonDelegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.data().canConvert<Person>())
{
paintPerson(painter, option, index);
}
else
{
paintLetter(painter, option, index);
}
}
QSize PersonDelegate::sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const
{
if(index.data().canConvert<Person>())
{
return QSize(option.rect.width(), 45);
}
else
{
return QSize(option.rect.width(), 25);
}
}
void PersonDelegate::paintPerson(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
const bool isSelected = option.state & QStyle::State_Selected;
const bool isHovered = option.state & QStyle::State_MouseOver;
Person tp = index.data().value<Person>();
painter->save();
QLinearGradient backgroundGradient(QPoint(option.rect.x(), option.rect.y()), QPoint(option.rect.x(), option.rect.y() option.rect.height()));
if(isSelected)
{
// painter->fillRect(option.rect, QBrush(QColor(49, 49, 49)));
backgroundGradient.setColorAt(0, QColor(109, 164, 219));
backgroundGradient.setColorAt(1, QColor(61, 138, 212));
painter->fillRect(option.rect, QBrush(backgroundGradient));
// painter->fillRect(option.rect, QBrush(QColor(225, 225, 225)));
}
else
{
// painter->fillRect(option.rect, QBrush(QColor(244, 244, 244)));
backgroundGradient.setColorAt(0, QColor(245, 245, 245));
backgroundGradient.setColorAt(1, QColor(240, 240, 240));
painter->fillRect(option.rect, QBrush(backgroundGradient));
}
painter->setPen(QColor(225, 225, 225));
if(isSelected)
{
painter->setPen(QColor(37, 105, 169));
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
painter->setPen(Qt::transparent);
}
painter->drawLine(option.rect.topLeft(), option.rect.topRight());
if(!isSelected)
{
painter->setPen(QColor(248, 248, 248));
painter->drawLine(QPoint(option.rect.x(), option.rect.y() 1), QPoint(option.rect.x() option.rect.width(), option.rect.y() 1));
}
// QString text = index.model()->data(index, Qt::DisplayRole).toString();
QRect textRect(option.rect.x() 10, option.rect.y(), option.rect.width()-10, option.rect.height());
painter->setPen(QColor(69, 69, 69));
QFont textFont(painter->font());
textFont.setPixelSize(18);
if(isSelected)
{
painter->setPen(QColor(229, 229, 229));
}
textFont.setFamily("Helvetica Neue");
painter->setFont(textFont);
painter->drawText(textRect, Qt::AlignLeft|Qt::AlignVCenter, tp.name());
painter->restore();
}
void PersonDelegate::paintLetter(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
const bool isSelected = option.state & QStyle::State_Selected;
const bool isHovered = option.state & QStyle::State_MouseOver;
painter->save();
QLinearGradient backgroundGradient(QPoint(option.rect.x(), option.rect.y()), QPoint(option.rect.x(), option.rect.y() option.rect.height()));
// painter->fillRect(option.rect, QBrush(QColor(225, 225, 225)));
backgroundGradient.setColorAt(0, QColor(215, 215, 215));
backgroundGradient.setColorAt(1, QColor(230, 230, 230));
painter->fillRect(option.rect, QBrush(backgroundGradient));
painter->setPen(QColor(213, 213, 213));
painter->drawLine(option.rect.bottomLeft(), option.rect.bottomRight());
QFont textFont(painter->font());
textFont.setPixelSize(18);
textFont.setFamily("Helvetica Neue");
painter->setFont(textFont);
QRect textRect(option.rect.x() 10, option.rect.y(), option.rect.width()-10, option.rect.height());
painter->setPen(QColor(39, 39, 39));
painter->drawText(textRect, Qt::AlignLeft|Qt::AlignVCenter, index.model()->data(index).toString());
painter->restore();
}