23 #include <QFontMetrics>
24 #include <QStyleOptionViewItem>
25 #include <QModelIndex>
27 #include <QLinearGradient>
29 QAbstractItemDelegate( parent ),
31 mpCheckBox( new QCheckBox() )
35 mpCheckBox->resize( mpCheckBox->sizeHint().height(), mpCheckBox->sizeHint().height() );
47 const QStyleOptionViewItem & theOption,
48 const QModelIndex & theIndex )
const
58 paintAsWidget( thepPainter, theOption, myData );
62 paintManually( thepPainter, theOption, myData );
65 thepPainter->restore();
71 const QStyleOptionViewItem & theOption,
72 const QModelIndex & theIndex )
const
80 return QSize( 378, mpWidget->height() );
85 return QSize( 50, height( theOption, myData ) );
91 return QSize( 50, 50 );
95 void QgsDetailedItemDelegate::paintManually( QPainter *thepPainter,
96 const QStyleOptionViewItem &theOption,
103 mpCheckBox->setChecked( theData.
isChecked() );
104 mpCheckBox->setEnabled( theData.
isEnabled() );
105 QPixmap myCbxPixmap( mpCheckBox->size() );
106 mpCheckBox->render( &myCbxPixmap );
112 QFontMetrics myTitleMetrics( titleFont( theOption ) );
113 QFontMetrics myDetailMetrics( detailFont( theOption ) );
121 if ( theOption.state & QStyle::State_Selected )
123 drawHighlight( theOption, thepPainter, height( theOption, theData ) );
124 thepPainter->setPen( theOption.palette.highlightedText().color() );
128 thepPainter->setPen( theOption.palette.text().color() );
137 thepPainter->drawPixmap( theOption.rect.x(),
138 theOption.rect.y() + mpCheckBox->height(),
145 bool myIconFlag =
false;
146 QPixmap myDecoPixmap = theData.
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( theOption ) );
178 thepPainter->drawText( myTextStartX,
184 thepPainter->setFont( detailFont( theOption ) );
194 wordWrap( theData.
detail(), myDetailMetrics, theOption.rect.width() - myTextStartX );
195 QStringListIterator myLineWrapIterator( myList );
196 while ( myLineWrapIterator.hasNext() )
198 QString myLine = myLineWrapIterator.next();
199 thepPainter->drawText( myTextStartX,
208 thepPainter->setFont( categoryFont( theOption ) );
209 thepPainter->drawText( myTextStartX,
241 void QgsDetailedItemDelegate::paintAsWidget( QPainter *thepPainter,
242 const QStyleOptionViewItem &theOption,
248 mpWidget->resize( theOption.rect.width(), mpWidget->height() );
249 mpWidget->setAutoFillBackground(
true );
252 if ( theOption.state & QStyle::State_Selected )
254 drawHighlight( theOption, thepPainter, height( theOption, theData ) );
256 QPixmap myPixmap = QPixmap::grabWidget( mpWidget );
257 thepPainter->drawPixmap( theOption.rect.x(),
262 void QgsDetailedItemDelegate::drawHighlight(
const QStyleOptionViewItem &theOption,
263 QPainter * thepPainter,
264 int theHeight )
const
266 QColor myColor1 = theOption.palette.highlight().color();
267 QColor myColor2 = myColor1;
268 myColor2 = myColor2.lighter( 110 );
269 QLinearGradient myGradient( QPointF( 0, theOption.rect.y() ),
270 QPointF( 0, theOption.rect.y() + theHeight ) );
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( theOption.rect, QBrush( myGradient ) );
279 int QgsDetailedItemDelegate::height(
const QStyleOptionViewItem &theOption,
282 QFontMetrics myTitleMetrics( titleFont( theOption ) );
283 QFontMetrics myDetailMetrics( detailFont( theOption ) );
284 QFontMetrics myCategoryMetrics( categoryFont( theOption ) );
288 QStringList myList = wordWrap( theData.
detail(),
291 myHeight += ( myList.count() + 1 ) * ( myDetailMetrics.height() -
verticalSpacing() );
303 QFont QgsDetailedItemDelegate::detailFont(
const QStyleOptionViewItem &theOption )
const
305 QFont myFont = theOption.font;
309 QFont QgsDetailedItemDelegate::categoryFont(
const QStyleOptionViewItem &theOption )
const
311 QFont myFont = theOption.font;
312 myFont.setBold(
true );
316 QFont QgsDetailedItemDelegate::titleFont(
const QStyleOptionViewItem &theOption )
const
318 QFont myTitleFont = detailFont( theOption );
319 myTitleFont.setBold(
true );
320 myTitleFont.setPointSize( myTitleFont.pointSize() );
325 QStringList QgsDetailedItemDelegate::wordWrap( QString theString,
326 QFontMetrics theMetrics,
329 if ( theString.isEmpty() )
330 return QStringList();
331 if ( 50 >= theWidth )
332 return QStringList() << theString;
337 QString myCumulativeLine =
"";
338 QString myStringToPreviousSpace =
"";
339 int myPreviousSpacePos = 0;
340 for (
int i = 0; i < theString.count(); ++i )
342 QChar myChar = theString.at( i );
343 if ( myChar == QChar(
' ' ) )
345 myStringToPreviousSpace = myCumulativeLine;
346 myPreviousSpacePos = i;
348 myCumulativeLine += myChar;
349 if ( theMetrics.width( myCumulativeLine ) >= theWidth )
354 myList << myStringToPreviousSpace.trimmed();
355 i = myPreviousSpacePos;
356 myStringToPreviousSpace =
"";
357 myCumulativeLine =
"";
361 if ( !myCumulativeLine.trimmed().isEmpty() )
363 myList << myCumulativeLine.trimmed();
377 return mVerticalSpacing;
383 mVerticalSpacing = theValue;
389 return mHorizontalSpacing;
395 mHorizontalSpacing = theValue;