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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
#include "instancedelegate.h"
#include <QPainter>
#include <QTextOption>
#include <QTextLayout>
#include <QApplication>
#include <QtCore/qmath.h>
// Origin: Qt
static void viewItemTextLayout ( QTextLayout &textLayout, int lineWidth, qreal &height, qreal &widthUsed )
{
height = 0;
widthUsed = 0;
textLayout.beginLayout();
while ( true )
{
QTextLine line = textLayout.createLine();
if ( !line.isValid() )
break;
line.setLineWidth ( lineWidth );
line.setPosition ( QPointF ( 0, height ) );
height += line.height();
widthUsed = qMax ( widthUsed, line.naturalTextWidth() );
}
textLayout.endLayout();
}
#define QFIXED_MAX (INT_MAX/256)
ListViewDelegate::ListViewDelegate ( QObject* parent ) : QStyledItemDelegate ( parent )
{
}
void drawSelectionRect(QPainter *painter, const QStyleOptionViewItemV4 &option, const QRect &rect)
{
if (!(option.state & QStyle::State_Selected))
return;
painter->fillRect ( rect, option.palette.brush ( QPalette::Highlight ) );
}
void drawFocusRect(QPainter *painter, const QStyleOptionViewItemV4 &option, const QRect &rect)
{
if (!(option.state & QStyle::State_HasFocus))
return;
QStyleOptionFocusRect opt;
opt.direction = option.direction;
opt.fontMetrics = option.fontMetrics;
opt.palette = option.palette;
opt.rect = rect;
//opt.state = option.state | QStyle::State_KeyboardFocusChange | QStyle::State_Item;
auto col = option.state & QStyle::State_Selected ? QPalette::Highlight : QPalette::Base;
opt.backgroundColor = option.palette.color(col);
// Apparently some widget styles expect this hint to not be set
painter->setRenderHint(QPainter::Antialiasing, false);
QStyle *style = option.widget ? option.widget->style() : QApplication::style();
style->drawPrimitive(QStyle::PE_FrameFocusRect, &opt, painter, option.widget);
painter->setRenderHint(QPainter::Antialiasing);
}
static QSize viewItemTextSize ( const QStyleOptionViewItemV4 *option )
{
QStyle *style = option->widget ? option->widget->style() : QApplication::style();
QTextOption textOption;
textOption.setWrapMode ( QTextOption::WrapAtWordBoundaryOrAnywhere );
QTextLayout textLayout;
textLayout.setTextOption ( textOption );
textLayout.setFont ( option->font );
textLayout.setText ( option->text );
const int textMargin = style->pixelMetric ( QStyle::PM_FocusFrameHMargin, option, option->widget ) + 1;
QRect bounds ( 0,0,100 - 2*textMargin,600 );
qreal height = 0, widthUsed = 0;
viewItemTextLayout ( textLayout, bounds.width(), height, widthUsed );
const QSize size ( qCeil ( widthUsed ), qCeil ( height ) );
return QSize ( size.width() + 2 * textMargin, size.height() );
}
void ListViewDelegate::paint ( QPainter* painter, const QStyleOptionViewItem& option, const QModelIndex& index ) const
{
QStyleOptionViewItemV4 opt = option;
initStyleOption ( &opt, index );
painter->save();
painter->setClipRect ( opt.rect );
opt.features |= QStyleOptionViewItem::WrapText;
opt.text = index.data().toString();
opt.textElideMode = Qt::ElideRight;
opt.displayAlignment = Qt::AlignTop | Qt::AlignHCenter;
QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
//const int iconSize = style->pixelMetric(QStyle::PM_IconViewIconSize);
const int iconSize = 48;
QRect iconbox = opt.rect;
const int textMargin = style->pixelMetric ( QStyle::PM_FocusFrameHMargin, 0, opt.widget ) + 1;
QRect textRect = opt.rect;
QRect textHighlightRect = textRect;
// clip the decoration on top, remove width padding
textRect.adjust ( textMargin,iconSize + textMargin + 5,-textMargin,0 );
textHighlightRect.adjust ( 0,iconSize + 5,0,0 );
// draw background
{
QSize textSize = viewItemTextSize ( &opt );
QPalette::ColorGroup cg;
QStyleOptionViewItemV4 opt2(opt);
if((opt.widget && opt.widget->isEnabled()) || (opt.state & QStyle::State_Enabled))
{
if(! ( opt.state & QStyle::State_Active ))
cg = QPalette::Inactive;
else
cg = QPalette::Normal;
}
else
{
cg = QPalette::Disabled;
}
opt2.palette.setCurrentColorGroup(cg);
// fill in background, if any
if ( opt.backgroundBrush.style() != Qt::NoBrush )
{
QPointF oldBO = painter->brushOrigin();
painter->setBrushOrigin ( opt.rect.topLeft() );
painter->fillRect ( opt.rect, opt.backgroundBrush );
painter->setBrushOrigin ( oldBO );
}
if ( opt.showDecorationSelected )
{
drawSelectionRect(painter,opt2, opt.rect);
drawFocusRect(painter,opt2, opt.rect);
//painter->fillRect ( opt.rect, opt.palette.brush ( cg, QPalette::Highlight ) );
}
else
{
//if ( opt.state & QStyle::State_Selected )
{
//QRect textRect = subElementRect ( QStyle::SE_ItemViewItemText, opt, opt.widget );
//painter->fillRect ( textHighlightRect, opt.palette.brush ( cg, QPalette::Highlight ) );
drawSelectionRect(painter,opt2, textHighlightRect);
drawFocusRect(painter,opt2, textHighlightRect);
}
}
}
// draw the icon
{
QIcon::Mode mode = QIcon::Normal;
if ( ! ( opt.state & QStyle::State_Enabled ) )
mode = QIcon::Disabled;
else if ( opt.state & QStyle::State_Selected )
mode = QIcon::Selected;
QIcon::State state = opt.state & QStyle::State_Open ? QIcon::On : QIcon::Off;
iconbox.setHeight ( iconSize );
opt.icon.paint ( painter, iconbox, Qt::AlignCenter, mode, state );
}
// set the text colors
QPalette::ColorGroup cg = opt.state & QStyle::State_Enabled ? QPalette::Normal : QPalette::Disabled;
if ( cg == QPalette::Normal && ! ( opt.state & QStyle::State_Active ) )
cg = QPalette::Inactive;
if ( opt.state & QStyle::State_Selected )
{
painter->setPen ( opt.palette.color ( cg, QPalette::HighlightedText ) );
}
else
{
painter->setPen ( opt.palette.color ( cg, QPalette::Text ) );
}
// draw the text
QTextOption textOption;
textOption.setWrapMode ( QTextOption::WrapAtWordBoundaryOrAnywhere );
textOption.setTextDirection ( opt.direction );
textOption.setAlignment ( QStyle::visualAlignment ( opt.direction, opt.displayAlignment ) );
QTextLayout textLayout;
textLayout.setTextOption ( textOption );
textLayout.setFont ( opt.font );
textLayout.setText ( opt.text );
qreal width, height;
viewItemTextLayout ( textLayout, iconbox.width(), height, width );
const int lineCount = textLayout.lineCount();
const QRect layoutRect = QStyle::alignedRect ( opt.direction, opt.displayAlignment, QSize ( iconbox.width(), int ( height ) ), textRect );
const QPointF position = layoutRect.topLeft();
for ( int i = 0; i < lineCount; ++i )
{
const QTextLine line = textLayout.lineAt ( i );
line.draw ( painter, position );
}
painter->restore();
}
QSize ListViewDelegate::sizeHint ( const QStyleOptionViewItem & option, const QModelIndex & index ) const
{
QStyleOptionViewItemV4 opt = option;
initStyleOption ( &opt, index );
opt.features |= QStyleOptionViewItem::WrapText;
opt.text = index.data().toString();
opt.textElideMode = Qt::ElideRight;
opt.displayAlignment = Qt::AlignTop | Qt::AlignHCenter;
QStyle *style = opt.widget ? opt.widget->style() : QApplication::style();
const int textMargin = style->pixelMetric ( QStyle::PM_FocusFrameHMargin, &option, opt.widget ) + 1;
int height = 48 + textMargin * 2 + 5; // TODO: turn constants into variables
QSize szz = viewItemTextSize ( &opt );
height += szz.height();
// FIXME: maybe the icon items could scale and keep proportions?
QSize sz ( 100,height );
return sz;
}
|