forked from qgis/QGIS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
qgsdetaileditemdelegate.cpp
398 lines (357 loc) · 12.4 KB
/
qgsdetaileditemdelegate.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
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
/***************************************************************************
qgsdetailedlistwidget.cpp - A rich QItemDelegate subclass
-------------------
begin : Sat May 17 2008
copyright : (C) 2008 Tim Sutton
email : [email protected]
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#include "qgsdetaileditemdelegate.h"
#include "qgsdetaileditemwidget.h"
#include "qgsdetaileditemdata.h"
#include <QPainter>
#include <QFont>
#include <QFontMetrics>
#include <QStyleOptionViewItem>
#include <QModelIndex>
#include <QCheckBox>
#include <QLinearGradient>
QgsDetailedItemDelegate::QgsDetailedItemDelegate( QObject *parent )
: QAbstractItemDelegate( parent )
, mpWidget( new QgsDetailedItemWidget() )
, mpCheckBox( new QCheckBox() )
{
//mpWidget->setFixedHeight(80);
mpCheckBox->resize( mpCheckBox->sizeHint().height(), mpCheckBox->sizeHint().height() );
setVerticalSpacing( 3 );
setHorizontalSpacing( 5 );
}
QgsDetailedItemDelegate::~QgsDetailedItemDelegate()
{
delete mpCheckBox;
delete mpWidget;
}
void QgsDetailedItemDelegate::paint( QPainter *thepPainter,
const QStyleOptionViewItem &option,
const QModelIndex &index ) const
{
// After painting we need to restore the painter to its original state
thepPainter->save();
if ( index.data( Qt::UserRole ).canConvert<QgsDetailedItemData>() )
{
QgsDetailedItemData myData =
index.data( Qt::UserRole ).value<QgsDetailedItemData>();
if ( myData.isRenderedAsWidget() )
{
paintAsWidget( thepPainter, option, myData );
}
else //render by manually painting
{
paintManually( thepPainter, option, myData );
}
} //can convert item data
thepPainter->restore();
}
QSize QgsDetailedItemDelegate::sizeHint(
const QStyleOptionViewItem &option,
const QModelIndex &index ) const
{
if ( index.data( Qt::UserRole ).canConvert<QgsDetailedItemData>() )
{
QgsDetailedItemData myData =
index.data( Qt::UserRole ).value<QgsDetailedItemData>();
if ( myData.isRenderedAsWidget() )
{
return QSize( 378, mpWidget->height() );
}
else // fall back to hand calculated & hand drawn item
{
//for some reason itmes are non selectable if using rect.width() on osx and win
return QSize( 50, height( option, myData ) );
//return QSize(theOption.rect.width(), myHeight + myVerticalSpacer);
}
}
else //can't convert to qgsdetaileditemdata
{
return QSize( 50, 50 ); //fallback
}
}
void QgsDetailedItemDelegate::paintManually( QPainter *thepPainter,
const QStyleOptionViewItem &option,
const QgsDetailedItemData &data ) const
{
//
// Get the strings and checkbox properties
//
//bool myCheckState = index.model()->data(theIndex, Qt::CheckStateRole).toBool();
mpCheckBox->setChecked( data.isChecked() );
mpCheckBox->setEnabled( data.isEnabled() );
QPixmap myCbxPixmap( mpCheckBox->size() );
mpCheckBox->render( &myCbxPixmap ); //we will draw this onto the widget further down
//
// Calculate the widget height and other metrics
//
QFontMetrics myTitleMetrics( titleFont( option ) );
QFontMetrics myDetailMetrics( detailFont( option ) );
int myTextStartX = option.rect.x() + horizontalSpacing();
int myTextStartY = option.rect.y() + verticalSpacing();
int myHeight = myTitleMetrics.height() + verticalSpacing();
//
// Draw the item background with a gradient if its highlighted
//
if ( option.state & QStyle::State_Selected )
{
drawHighlight( option, thepPainter, height( option, data ) );
thepPainter->setPen( option.palette.highlightedText().color() );
}
else
{
thepPainter->setPen( option.palette.text().color() );
}
//
// Draw the checkbox
//
if ( data.isCheckable() )
{
thepPainter->drawPixmap( option.rect.x(),
option.rect.y() + mpCheckBox->height(),
myCbxPixmap );
myTextStartX = option.rect.x() + myCbxPixmap.width() + horizontalSpacing();
}
//
// Draw the decoration (pixmap)
//
bool myIconFlag = false;
QPixmap myDecoPixmap = data.icon();
if ( !myDecoPixmap.isNull() )
{
myIconFlag = true;
int iconWidth = 32, iconHeight = 32;
if ( myDecoPixmap.width() <= iconWidth && myDecoPixmap.height() <= iconHeight )
{
// the pixmap has reasonable size
int offsetX = 0, offsetY = 0;
if ( myDecoPixmap.width() < iconWidth )
offsetX = ( iconWidth - myDecoPixmap.width() ) / 2;
if ( myDecoPixmap.height() < iconHeight )
offsetY = ( iconHeight - myDecoPixmap.height() ) / 2;
thepPainter->drawPixmap( myTextStartX + offsetX,
myTextStartY + offsetY,
myDecoPixmap );
}
else
{
// shrink the pixmap, it's too big
thepPainter->drawPixmap( myTextStartX, myTextStartY, iconWidth, iconHeight, myDecoPixmap );
}
myTextStartX += iconWidth + horizontalSpacing();
}
//
// Draw the title
//
myTextStartY += myHeight / 2;
thepPainter->setFont( titleFont( option ) );
thepPainter->drawText( myTextStartX,
myTextStartY,
data.title() );
//
// Draw the description with word wrapping if needed
//
thepPainter->setFont( detailFont( option ) ); //return to original font set by client
if ( myIconFlag )
{
myTextStartY += verticalSpacing();
}
else
{
myTextStartY += myDetailMetrics.height() + verticalSpacing();
}
QStringList myList =
wordWrap( data.detail(), myDetailMetrics, option.rect.width() - myTextStartX );
QStringListIterator myLineWrapIterator( myList );
while ( myLineWrapIterator.hasNext() )
{
QString myLine = myLineWrapIterator.next();
thepPainter->drawText( myTextStartX,
myTextStartY,
myLine );
myTextStartY += myDetailMetrics.height() - verticalSpacing();
}
//
// Draw the category. Not sure if we need word wrapping for it.
//
thepPainter->setFont( categoryFont( option ) ); //return to original font set by client
thepPainter->drawText( myTextStartX,
myTextStartY,
data.category() );
//
// Draw the category with word wrapping if needed
//
/*
myTextStartY += verticalSpacing();
if ( myIconFlag )
{
myTextStartY += verticalSpacing();
}
else
{
myTextStartY += myCategoryMetrics.height() + verticalSpacing();
}
myList =
wordWrap( data.category(), myCategoryMetrics, option.rect.width() - myTextStartX );
QStringListIterator myLineWrapIter( myList );
while ( myLineWrapIter.hasNext() )
{
QString myLine = myLineWrapIter.next();
thepPainter->drawText( myTextStartX,
myTextStartY,
myLine );
myTextStartY += myCategoryMetrics.height() - verticalSpacing();
}
*/
} //render by manual painting
void QgsDetailedItemDelegate::paintAsWidget( QPainter *thepPainter,
const QStyleOptionViewItem &option,
const QgsDetailedItemData &data ) const
{
mpWidget->setChecked( data.isChecked() );
mpWidget->setData( data );
mpWidget->resize( option.rect.width(), mpWidget->height() );
mpWidget->setAutoFillBackground( true );
//mpWidget->setAttribute(Qt::WA_OpaquePaintEvent);
mpWidget->repaint();
if ( option.state & QStyle::State_Selected )
{
drawHighlight( option, thepPainter, height( option, data ) );
}
QPixmap myPixmap = QPixmap::grabWidget( mpWidget );
thepPainter->drawPixmap( option.rect.x(),
option.rect.y(),
myPixmap );
}//render as widget
void QgsDetailedItemDelegate::drawHighlight( const QStyleOptionViewItem &option,
QPainter *thepPainter,
int height ) const
{
QColor myColor1 = option.palette.highlight().color();
QColor myColor2 = myColor1;
myColor2 = myColor2.lighter( 110 ); //10% lighter
QLinearGradient myGradient( QPointF( 0, option.rect.y() ),
QPointF( 0, option.rect.y() + height ) );
myGradient.setColorAt( 0, myColor1 );
myGradient.setColorAt( 0.1, myColor2 );
myGradient.setColorAt( 0.5, myColor1 );
myGradient.setColorAt( 0.9, myColor2 );
myGradient.setColorAt( 1, myColor2 );
thepPainter->fillRect( option.rect, QBrush( myGradient ) );
}
int QgsDetailedItemDelegate::height( const QStyleOptionViewItem &option,
const QgsDetailedItemData &data ) const
{
QFontMetrics myTitleMetrics( titleFont( option ) );
QFontMetrics myDetailMetrics( detailFont( option ) );
QFontMetrics myCategoryMetrics( categoryFont( option ) );
//we don't word wrap the title so its easy to measure
int myHeight = myTitleMetrics.height() + verticalSpacing();
//the detail needs to be measured though
QStringList myList = wordWrap( data.detail(),
myDetailMetrics,
option.rect.width() - ( mpCheckBox->width() + horizontalSpacing() ) );
myHeight += ( myList.count() + 1 ) * ( myDetailMetrics.height() - verticalSpacing() );
//we don't word wrap the category so its easy to measure
myHeight += myCategoryMetrics.height() + verticalSpacing();
#if 0
// if category should be wrapped use this code
myList = wordWrap( data.category(),
myCategoryMetrics,
option.rect.width() - ( mpCheckBox->width() + horizontalSpacing() ) );
myHeight += ( myList.count() + 1 ) * ( myCategoryMetrics.height() - verticalSpacing() );
#endif
return myHeight;
}
QFont QgsDetailedItemDelegate::detailFont( const QStyleOptionViewItem &option ) const
{
QFont myFont = option.font;
return myFont;
}
QFont QgsDetailedItemDelegate::categoryFont( const QStyleOptionViewItem &option ) const
{
QFont myFont = option.font;
myFont.setBold( true );
return myFont;
}
QFont QgsDetailedItemDelegate::titleFont( const QStyleOptionViewItem &option ) const
{
QFont myTitleFont = detailFont( option );
myTitleFont.setBold( true );
myTitleFont.setPointSize( myTitleFont.pointSize() );
return myTitleFont;
}
QStringList QgsDetailedItemDelegate::wordWrap( const QString &string,
const QFontMetrics &metrics,
int width ) const
{
if ( string.isEmpty() )
return QStringList();
if ( 50 >= width )
return QStringList() << string;
//QString myDebug = QString("Word wrapping: %1 into %2 pixels").arg(theString).arg(theWidth);
//qDebug(myDebug.toLocal8Bit());
//iterate the string
QStringList myList;
QString myCumulativeLine;
QString myStringToPreviousSpace;
int myPreviousSpacePos = 0;
for ( int i = 0; i < string.count(); ++i )
{
QChar myChar = string.at( i );
if ( myChar == QChar( ' ' ) )
{
myStringToPreviousSpace = myCumulativeLine;
myPreviousSpacePos = i;
}
myCumulativeLine += myChar;
if ( metrics.width( myCumulativeLine ) >= width )
{
//time to wrap
//TODO deal with long strings that have no spaces
//forcing a break at current pos...
myList << myStringToPreviousSpace.trimmed();
i = myPreviousSpacePos;
myStringToPreviousSpace.clear();
myCumulativeLine.clear();
}
}//end of i loop
//add whatever is left in the string to the list
if ( !myCumulativeLine.trimmed().isEmpty() )
{
myList << myCumulativeLine.trimmed();
}
//qDebug("Wrapped legend entry:");
//qDebug(theString);
//qDebug(myList.join("\n").toLocal8Bit());
return myList;
}
int QgsDetailedItemDelegate::verticalSpacing() const
{
return mVerticalSpacing;
}
void QgsDetailedItemDelegate::setVerticalSpacing( int value )
{
mVerticalSpacing = value;
}
int QgsDetailedItemDelegate::horizontalSpacing() const
{
return mHorizontalSpacing;
}
void QgsDetailedItemDelegate::setHorizontalSpacing( int value )
{
mHorizontalSpacing = value;
}