-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathheaderview.cpp
More file actions
157 lines (135 loc) · 4.2 KB
/
headerview.cpp
File metadata and controls
157 lines (135 loc) · 4.2 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
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
146
147
148
149
150
151
152
153
154
155
156
157
#include "headerview.h"
#include <QPainter>
HeaderView::HeaderView(Qt::Orientation orientation, bool wrap, QWidget *parent) :
QHeaderView(orientation, parent)
{
wrapping = wrap;
setSectionsClickable(true);
setStretchLastSection(true);
}
void HeaderView::setWordWrap(bool wrap)
{
wrapping = wrap;
}
bool HeaderView::wordWrap() const
{
return wrapping;
}
void HeaderView::paintSection(QPainter *painter, const QRect &rect, int logicalIndex) const
{
if(wrapping)
{
if (!rect.isValid())
{
return;
}
// get the state of the section
QStyleOptionHeader opt;
initStyleOption(&opt);
QStyle::State state = QStyle::State_None;
if (isEnabled())
{
state |= QStyle::State_Enabled;
}
if (window()->isActiveWindow())
{
state |= QStyle::State_Active;
}
if (isSortIndicatorShown() && sortIndicatorSection() == logicalIndex)
{
opt.sortIndicator = (sortIndicatorOrder() == Qt::AscendingOrder)
? QStyleOptionHeader::SortDown : QStyleOptionHeader::SortUp;
}
// setup the style options structure
QVariant textAlignment = model()->headerData(logicalIndex, orientation(),
Qt::TextAlignmentRole);
opt.rect = rect;
opt.section = logicalIndex;
opt.state |= state;
opt.textAlignment = Qt::Alignment(textAlignment.isValid()
? Qt::Alignment(textAlignment.toInt())
: defaultAlignment());
opt.iconAlignment = Qt::AlignVCenter;
opt.text = model()->headerData(logicalIndex, orientation(),
Qt::DisplayRole).toString();
if (textElideMode() != Qt::ElideNone)
{
opt.text = opt.fontMetrics.elidedText(opt.text, textElideMode() , rect.width() - 4);
}
QVariant variant = model()->headerData(logicalIndex, orientation(),
Qt::DecorationRole);
opt.icon = qvariant_cast<QIcon>(variant);
if (opt.icon.isNull())
{
opt.icon = qvariant_cast<QPixmap>(variant);
}
QVariant foregroundBrush = model()->headerData(logicalIndex, orientation(),
Qt::ForegroundRole);
if (foregroundBrush.canConvert(QMetaType::QBrush))
{
opt.palette.setBrush(QPalette::ButtonText, qvariant_cast<QBrush>(foregroundBrush));
}
QPointF oldBO = painter->brushOrigin();
QVariant backgroundBrush = model()->headerData(logicalIndex, orientation(),
Qt::BackgroundRole);
if (backgroundBrush.canConvert(QMetaType::QBrush))
{
opt.palette.setBrush(QPalette::Button, qvariant_cast<QBrush>(backgroundBrush));
opt.palette.setBrush(QPalette::Window, qvariant_cast<QBrush>(backgroundBrush));
painter->setBrushOrigin(opt.rect.topLeft());
}
// the section position
int visual = visualIndex(logicalIndex);
Q_ASSERT(visual != -1);
if (count() == 1)
{
opt.position = QStyleOptionHeader::OnlyOneSection;
}
else if (visual == 0)
{
opt.position = QStyleOptionHeader::Beginning;
}
else if (visual == count() - 1)
{
opt.position = QStyleOptionHeader::End;
}
else
{
opt.position = QStyleOptionHeader::Middle;
}
opt.orientation = orientation();
// draw the section
style()->drawControl(QStyle::CE_HeaderSection, &opt, painter, this);
// draw the text
QRect headerLabelRect = style()->subElementRect(QStyle::SE_HeaderLabel, &opt, this);
style()->drawItemText(painter, headerLabelRect, Qt::TextWordWrap | Qt::AlignCenter, style()->standardPalette(), true, opt.text);
// draw the indicators if need
if (isSortIndicatorShown() && sortIndicatorSection() == logicalIndex)
{
opt.rect = style()->subElementRect(QStyle::SE_HeaderArrow, &opt, this);
style()->drawPrimitive(QStyle::PE_IndicatorHeaderArrow, &opt, painter, this);
}
painter->setBrushOrigin(oldBO);
}
else
{
QHeaderView::paintSection(painter, rect, logicalIndex);
}
}
QSize HeaderView::sectionSizeFromContents(int logicalIndex) const
{
if(wrapping)
{
int width = sectionSize(logicalIndex);
int margin = style()->pixelMetric(QStyle::PM_HeaderMargin, 0, this) *2;
QString s = model()->headerData(logicalIndex, Qt::Horizontal).toString();
QFont font = this->font();
font.setBold(true);
QFontMetrics fm(font);
QSize size = fm.boundingRect(0, 0, width, 2000, Qt::TextWordWrap, s).size();
size.rwidth() += margin;
size.rheight() += margin;
return size;
}
return QHeaderView::sectionSizeFromContents(logicalIndex);
}