19#include "moc_qgsdetaileditemdelegate.cpp"
25#include <QFontMetrics>
26#include <QStyleOptionViewItem>
29#include <QLinearGradient>
31 : QAbstractItemDelegate( parent )
33 , mpCheckBox( new QCheckBox() )
37 mpCheckBox->resize( mpCheckBox->sizeHint().height(), mpCheckBox->sizeHint().height() );
52 if ( index.data( Qt::UserRole ).userType() == qMetaTypeId<QgsDetailedItemData>() )
57 paintAsWidget( thepPainter, option, myData );
61 paintManually( thepPainter, option, myData );
68 const QStyleOptionViewItem &option,
69 const QModelIndex &index
72 if ( index.data( Qt::UserRole ).userType() == qMetaTypeId<QgsDetailedItemData>() )
77 return QSize( 378, mpWidget->height() );
82 return QSize( 50, height( option, myData ) );
88 return QSize( 50, 50 );
92void QgsDetailedItemDelegate::paintManually( QPainter *thepPainter,
const QStyleOptionViewItem &option,
const QgsDetailedItemData &data )
const
98 mpCheckBox->setChecked( data.
isChecked() );
99 mpCheckBox->setEnabled( data.
isEnabled() );
100 QPixmap myCbxPixmap( mpCheckBox->size() );
101 mpCheckBox->render( &myCbxPixmap );
107 const QFontMetrics myTitleMetrics( titleFont( option ) );
108 const QFontMetrics myDetailMetrics( detailFont( option ) );
116 if ( option.state & QStyle::State_Selected )
118 drawHighlight( option, thepPainter, height( option, data ) );
119 thepPainter->setPen( option.palette.highlightedText().color() );
123 thepPainter->setPen( option.palette.text().color() );
132 thepPainter->drawPixmap( option.rect.x(), option.rect.y() + mpCheckBox->height(), myCbxPixmap );
138 bool myIconFlag =
false;
139 const QPixmap myDecoPixmap = data.
icon();
140 if ( !myDecoPixmap.isNull() )
143 int iconWidth = 32, iconHeight = 32;
145 if ( myDecoPixmap.width() <= iconWidth && myDecoPixmap.height() <= iconHeight )
148 int offsetX = 0, offsetY = 0;
149 if ( myDecoPixmap.width() < iconWidth )
150 offsetX = ( iconWidth - myDecoPixmap.width() ) / 2;
151 if ( myDecoPixmap.height() < iconHeight )
152 offsetY = ( iconHeight - myDecoPixmap.height() ) / 2;
154 thepPainter->drawPixmap( myTextStartX + offsetX, myTextStartY + offsetY, myDecoPixmap );
159 thepPainter->drawPixmap( myTextStartX, myTextStartY, iconWidth, iconHeight, myDecoPixmap );
167 myTextStartY += myHeight / 2;
168 thepPainter->setFont( titleFont( option ) );
169 thepPainter->drawText( myTextStartX, myTextStartY, data.
title() );
173 thepPainter->setFont( detailFont( option ) );
182 const QStringList myList = wordWrap( data.
detail(), myDetailMetrics, option.rect.width() - myTextStartX );
183 QStringListIterator myLineWrapIterator( myList );
184 while ( myLineWrapIterator.hasNext() )
186 const QString myLine = myLineWrapIterator.next();
187 thepPainter->drawText( myTextStartX, myTextStartY, myLine );
194 thepPainter->setFont( categoryFont( option ) );
195 thepPainter->drawText( myTextStartX, myTextStartY, data.
category() );
225void QgsDetailedItemDelegate::paintAsWidget( QPainter *thepPainter,
const QStyleOptionViewItem &option,
const QgsDetailedItemData &data )
const
229 mpWidget->resize( option.rect.width(), mpWidget->height() );
230 mpWidget->setAutoFillBackground(
true );
233 if ( option.state & QStyle::State_Selected )
235 drawHighlight( option, thepPainter, height( option, data ) );
237 const QPixmap myPixmap = mpWidget->grab();
238 thepPainter->drawPixmap( option.rect.x(), option.rect.y(), myPixmap );
241void QgsDetailedItemDelegate::drawHighlight(
const QStyleOptionViewItem &option, QPainter *thepPainter,
int height )
const
243 const QColor myColor1 = option.palette.highlight().color();
244 QColor myColor2 = myColor1;
245 myColor2 = myColor2.lighter( 110 );
246 QLinearGradient myGradient( QPointF( 0, option.rect.y() ), QPointF( 0, option.rect.y() + height ) );
247 myGradient.setColorAt( 0, myColor1 );
248 myGradient.setColorAt( 0.1, myColor2 );
249 myGradient.setColorAt( 0.5, myColor1 );
250 myGradient.setColorAt( 0.9, myColor2 );
251 myGradient.setColorAt( 1, myColor2 );
252 thepPainter->fillRect( option.rect, QBrush( myGradient ) );
255int QgsDetailedItemDelegate::height(
const QStyleOptionViewItem &option,
const QgsDetailedItemData &data )
const
257 const QFontMetrics myTitleMetrics( titleFont( option ) );
258 const QFontMetrics myDetailMetrics( detailFont( option ) );
259 const QFontMetrics myCategoryMetrics( categoryFont( option ) );
263 const QStringList myList = wordWrap( data.
detail(), myDetailMetrics, option.rect.width() - ( mpCheckBox->width() +
horizontalSpacing() ) );
264 myHeight += ( myList.count() + 1 ) * ( myDetailMetrics.height() -
verticalSpacing() );
272 myHeight += ( myList.count() + 1 ) * ( myCategoryMetrics.height() -
verticalSpacing() );
278QFont QgsDetailedItemDelegate::detailFont(
const QStyleOptionViewItem &option )
const
280 const QFont myFont = option.font;
284QFont QgsDetailedItemDelegate::categoryFont(
const QStyleOptionViewItem &option )
const
286 QFont myFont = option.font;
287 myFont.setBold(
true );
291QFont QgsDetailedItemDelegate::titleFont(
const QStyleOptionViewItem &option )
const
293 QFont myTitleFont = detailFont( option );
294 myTitleFont.setBold(
true );
295 myTitleFont.setPointSize( myTitleFont.pointSize() );
300QStringList QgsDetailedItemDelegate::wordWrap(
const QString &
string,
const QFontMetrics &metrics,
int width )
const
302 if (
string.isEmpty() )
303 return QStringList();
305 return QStringList() << string;
310 QString myCumulativeLine;
311 QString myStringToPreviousSpace;
312 int myPreviousSpacePos = 0;
313 for (
int i = 0; i <
string.count(); ++i )
315 const QChar myChar =
string.at( i );
316 if ( myChar == QChar(
' ' ) )
318 myStringToPreviousSpace = myCumulativeLine;
319 myPreviousSpacePos = i;
321 myCumulativeLine += myChar;
322 if ( metrics.boundingRect( myCumulativeLine ).width() >= width )
327 myList << myStringToPreviousSpace.trimmed();
328 i = myPreviousSpacePos;
329 myStringToPreviousSpace.clear();
330 myCumulativeLine.clear();
334 if ( !myCumulativeLine.trimmed().isEmpty() )
336 myList << myCumulativeLine.trimmed();
348 return mVerticalSpacing;
354 mVerticalSpacing = value;
360 return mHorizontalSpacing;
366 mHorizontalSpacing = value;
This class is the data only representation of a QgsDetailedItemWidget, designed to be used in custom ...
QString category() const
Returns the item's category.
bool isCheckable() const
Returns true if the item is checkable.
bool isEnabled() const
Returns true if the item is enabled.
QString title() const
Returns the item's title.
bool isRenderedAsWidget() const
Returns true if the item will be rendered using a widget.
QPixmap icon() const
Returns the item's icon.
QString detail() const
Returns the detailed description for the item.
bool isChecked() const
Returns true if the item is checked.
int verticalSpacing() const
void setHorizontalSpacing(int value)
void setVerticalSpacing(int value)
~QgsDetailedItemDelegate() override
QgsDetailedItemDelegate(QObject *parent=nullptr)
Constructor for QgsDetailedItemDelegate.
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Reimplement for parent class.
int horizontalSpacing() const
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Reimplement for parent class.
Scoped object for saving and restoring a QPainter object's state.