-
Notifications
You must be signed in to change notification settings - Fork 7
/
delegate.cpp
145 lines (117 loc) · 3.79 KB
/
delegate.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#include "delegate.h"
#include <QPainter>
Delegate::Delegate(QObject *parent) : QStyledItemDelegate(parent),
m_ptr(new DelegatePrivate)
{
}
Delegate::~Delegate()
{
delete m_ptr;
}
QSize Delegate::iconSize() const
{
return m_ptr->iconSize;
}
void Delegate::setIconSize(int width, int height)
{
m_ptr->iconSize = QSize(width, height);
}
QMargins Delegate::contentsMargins() const
{
return m_ptr->margins;
}
void Delegate::setContentsMargins(int left, int top, int right, int bottom)
{
m_ptr->margins = QMargins(left, top, right, bottom);
}
int Delegate::horizontalSpacing() const
{
return m_ptr->spacingHorizontal;
}
void Delegate::setHorizontalSpacing(int spacing)
{
m_ptr->spacingHorizontal = spacing;
}
int Delegate::verticalSpacing() const
{
return m_ptr->spacingVertical;
}
void Delegate::setVerticalSpacing(int spacing)
{
m_ptr->spacingVertical = spacing;
}
void Delegate::paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const
{
QStyleOptionViewItem opt(option);
initStyleOption(&opt, index);
const QPalette &palette(opt.palette);
const QRect &rect(opt.rect);
const QRect &contentRect(rect.adjusted(m_ptr->margins.left(), m_ptr->margins.top(), -m_ptr->margins.right(), -m_ptr->margins.bottom()));
const bool lastIndex = (index.model()->rowCount() - 1) == index.row();
const bool hasIcon = !opt.icon.isNull();
const int bottomEdge = rect.bottom();
QFont f(opt.font);
f.setPointSize(opt.font.pointSize()-1); // top text font size
painter->save();
painter->setClipping(true);
painter->setClipRect(rect);
painter->setFont(opt.font);
// Draw background
painter->fillRect(rect, opt.state & QStyle::State_Selected ?
palette.window().color().darker(96) :
palette.base().color());
// Draw bottom line
painter->setPen(lastIndex ? palette.dark().color()
: palette.mid().color());
painter->drawLine(lastIndex ? rect.left() : m_ptr->margins.left(),
bottomEdge, rect.right(), bottomEdge);
// Draw text icon
if (hasIcon)
painter->drawPixmap(contentRect.left(), contentRect.top(),
opt.icon.pixmap(m_ptr->iconSize));
// Draw topText
QRect topTextRect(m_ptr->topTextBox(opt, index));
topTextRect.moveTo(m_ptr->margins.left() + m_ptr->iconSize.width()
+ m_ptr->spacingHorizontal, contentRect.top());
painter->setFont(f);
painter->setPen(palette.text().color());
painter->drawText(topTextRect, Qt::TextSingleLine,
index.data(Qt::UserRole).toString());
// Draw text
QRect textRect(m_ptr->textBox(opt));
textRect.moveTo(topTextRect.left(), topTextRect.bottom()
+ m_ptr->spacingVertical);
painter->setFont(opt.font);
painter->setPen(palette.windowText().color());
painter->drawText(textRect, Qt::TextSingleLine, opt.text);
painter->restore();
}
QSize Delegate::sizeHint(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
QStyleOptionViewItem opt(option);
initStyleOption(&opt, index);
int textHeight = m_ptr->topTextBox(opt, index).height()
+ m_ptr->spacingVertical + m_ptr->textBox(opt).height();
int iconHeight = m_ptr->iconSize.height();
int h = textHeight > iconHeight ? textHeight : iconHeight;
return QSize(opt.rect.width(), m_ptr->margins.top() + h
+ m_ptr->margins.bottom());
}
DelegatePrivate::DelegatePrivate() :
iconSize(24, 24),
margins(4, 2, 4, 2),
spacingHorizontal(0),
spacingVertical(2)
{
}
QRect DelegatePrivate::topTextBox(const QStyleOptionViewItem &option,
const QModelIndex &index) const
{
return QFontMetrics(option.font).boundingRect(index.data(Qt::UserRole).toString())
.adjusted(0, 0, 1, 1);
}
QRect DelegatePrivate::textBox(const QStyleOptionViewItem &option) const
{
return option.fontMetrics.boundingRect(option.text).adjusted(0, 0, 1, 1);
}