24 #include <QFontMetrics>
25 #include <QStyleOptionViewItem>
26 #include <QModelIndex>
28 #include <QLinearGradient>
30 : QAbstractItemDelegate( parent )
32 , mpCheckBox( new QCheckBox() )
36 mpCheckBox->resize( mpCheckBox->sizeHint().height(), mpCheckBox->sizeHint().height() );
48 const QStyleOptionViewItem &option,
49 const QModelIndex &index )
const
59 paintAsWidget( thepPainter, option, myData );
63 paintManually( thepPainter, option, myData );
71 const QStyleOptionViewItem &option,
72 const QModelIndex &index )
const
80 return QSize( 378, mpWidget->height() );
85 return QSize( 50, height( option, myData ) );
91 return QSize( 50, 50 );
95 void QgsDetailedItemDelegate::paintManually( QPainter *thepPainter,
96 const QStyleOptionViewItem &option,
103 mpCheckBox->setChecked( data.
isChecked() );
104 mpCheckBox->setEnabled( data.
isEnabled() );
105 QPixmap myCbxPixmap( mpCheckBox->size() );
106 mpCheckBox->render( &myCbxPixmap );
112 const QFontMetrics myTitleMetrics( titleFont( option ) );
113 const QFontMetrics myDetailMetrics( detailFont( option ) );
121 if ( option.state & QStyle::State_Selected )
123 drawHighlight( option, thepPainter, height( option, data ) );
124 thepPainter->setPen( option.palette.highlightedText().color() );
128 thepPainter->setPen( option.palette.text().color() );
137 thepPainter->drawPixmap( option.rect.x(),
138 option.rect.y() + mpCheckBox->height(),
145 bool myIconFlag =
false;
146 const QPixmap myDecoPixmap = data.
icon();
147 if ( !myDecoPixmap.isNull() )
150 int iconWidth = 32, iconHeight = 32;
152 if ( myDecoPixmap.width() <= iconWidth && myDecoPixmap.height() <= iconHeight )
155 int offsetX = 0, offsetY = 0;
156 if ( myDecoPixmap.width() < iconWidth )
157 offsetX = ( iconWidth - myDecoPixmap.width() ) / 2;
158 if ( myDecoPixmap.height() < iconHeight )
159 offsetY = ( iconHeight - myDecoPixmap.height() ) / 2;
161 thepPainter->drawPixmap( myTextStartX + offsetX,
162 myTextStartY + offsetY,
168 thepPainter->drawPixmap( myTextStartX, myTextStartY, iconWidth, iconHeight, myDecoPixmap );
176 myTextStartY += myHeight / 2;
177 thepPainter->setFont( titleFont( option ) );
178 thepPainter->drawText( myTextStartX,
184 thepPainter->setFont( detailFont( option ) );
193 const QStringList myList =
194 wordWrap( data.
detail(), myDetailMetrics, option.rect.width() - myTextStartX );
195 QStringListIterator myLineWrapIterator( myList );
196 while ( myLineWrapIterator.hasNext() )
198 const QString myLine = myLineWrapIterator.next();
199 thepPainter->drawText( myTextStartX,
208 thepPainter->setFont( categoryFont( option ) );
209 thepPainter->drawText( myTextStartX,
241 void QgsDetailedItemDelegate::paintAsWidget( QPainter *thepPainter,
242 const QStyleOptionViewItem &option,
248 mpWidget->resize( option.rect.width(), mpWidget->height() );
249 mpWidget->setAutoFillBackground(
true );
252 if ( option.state & QStyle::State_Selected )
254 drawHighlight( option, thepPainter, height( option, data ) );
256 const QPixmap myPixmap = mpWidget->grab();
257 thepPainter->drawPixmap( option.rect.x(),
262 void QgsDetailedItemDelegate::drawHighlight(
const QStyleOptionViewItem &option,
263 QPainter *thepPainter,
266 const QColor myColor1 = option.palette.highlight().color();
267 QColor myColor2 = myColor1;
268 myColor2 = myColor2.lighter( 110 );
269 QLinearGradient myGradient( QPointF( 0, option.rect.y() ),
270 QPointF( 0, option.rect.y() + height ) );
271 myGradient.setColorAt( 0, myColor1 );
272 myGradient.setColorAt( 0.1, myColor2 );
273 myGradient.setColorAt( 0.5, myColor1 );
274 myGradient.setColorAt( 0.9, myColor2 );
275 myGradient.setColorAt( 1, myColor2 );
276 thepPainter->fillRect( option.rect, QBrush( myGradient ) );
279 int QgsDetailedItemDelegate::height(
const QStyleOptionViewItem &option,
282 const QFontMetrics myTitleMetrics( titleFont( option ) );
283 const QFontMetrics myDetailMetrics( detailFont( option ) );
284 const QFontMetrics myCategoryMetrics( categoryFont( option ) );
288 const QStringList myList = wordWrap( data.
detail(),
291 myHeight += ( myList.count() + 1 ) * ( myDetailMetrics.height() -
verticalSpacing() );
299 myHeight += ( myList.count() + 1 ) * ( myCategoryMetrics.height() -
verticalSpacing() );
305 QFont QgsDetailedItemDelegate::detailFont(
const QStyleOptionViewItem &option )
const
307 const QFont myFont = option.font;
311 QFont QgsDetailedItemDelegate::categoryFont(
const QStyleOptionViewItem &option )
const
313 QFont myFont = option.font;
314 myFont.setBold(
true );
318 QFont QgsDetailedItemDelegate::titleFont(
const QStyleOptionViewItem &option )
const
320 QFont myTitleFont = detailFont( option );
321 myTitleFont.setBold(
true );
322 myTitleFont.setPointSize( myTitleFont.pointSize() );
327 QStringList QgsDetailedItemDelegate::wordWrap(
const QString &
string,
328 const QFontMetrics &metrics,
331 if (
string.isEmpty() )
332 return QStringList();
334 return QStringList() << string;
339 QString myCumulativeLine;
340 QString myStringToPreviousSpace;
341 int myPreviousSpacePos = 0;
342 for (
int i = 0; i <
string.count(); ++i )
344 const QChar myChar =
string.at( i );
345 if ( myChar == QChar(
' ' ) )
347 myStringToPreviousSpace = myCumulativeLine;
348 myPreviousSpacePos = i;
350 myCumulativeLine += myChar;
351 if ( metrics.boundingRect( myCumulativeLine ).width() >= width )
356 myList << myStringToPreviousSpace.trimmed();
357 i = myPreviousSpacePos;
358 myStringToPreviousSpace.clear();
359 myCumulativeLine.clear();
363 if ( !myCumulativeLine.trimmed().isEmpty() )
365 myList << myCumulativeLine.trimmed();
379 return mVerticalSpacing;
385 mVerticalSpacing = value;
391 return mHorizontalSpacing;
397 mHorizontalSpacing = value;