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() );
49 const QStyleOptionViewItem &option,
50 const QModelIndex &index )
const
54 if ( index.data( Qt::UserRole ).userType() == qMetaTypeId<QgsDetailedItemData>() )
60 paintAsWidget( thepPainter, option, myData );
64 paintManually( thepPainter, option, myData );
72 const QStyleOptionViewItem &option,
73 const QModelIndex &index )
const
75 if ( index.data( Qt::UserRole ).userType() == qMetaTypeId<QgsDetailedItemData>() )
81 return QSize( 378, mpWidget->height() );
86 return QSize( 50, height( option, myData ) );
92 return QSize( 50, 50 );
96void QgsDetailedItemDelegate::paintManually( QPainter *thepPainter,
97 const QStyleOptionViewItem &option,
104 mpCheckBox->setChecked( data.
isChecked() );
105 mpCheckBox->setEnabled( data.
isEnabled() );
106 QPixmap myCbxPixmap( mpCheckBox->size() );
107 mpCheckBox->render( &myCbxPixmap );
113 const QFontMetrics myTitleMetrics( titleFont( option ) );
114 const QFontMetrics myDetailMetrics( detailFont( option ) );
122 if ( option.state & QStyle::State_Selected )
124 drawHighlight( option, thepPainter, height( option, data ) );
125 thepPainter->setPen( option.palette.highlightedText().color() );
129 thepPainter->setPen( option.palette.text().color() );
138 thepPainter->drawPixmap( option.rect.x(),
139 option.rect.y() + mpCheckBox->height(),
146 bool myIconFlag =
false;
147 const QPixmap myDecoPixmap = data.
icon();
148 if ( !myDecoPixmap.isNull() )
151 int iconWidth = 32, iconHeight = 32;
153 if ( myDecoPixmap.width() <= iconWidth && myDecoPixmap.height() <= iconHeight )
156 int offsetX = 0, offsetY = 0;
157 if ( myDecoPixmap.width() < iconWidth )
158 offsetX = ( iconWidth - myDecoPixmap.width() ) / 2;
159 if ( myDecoPixmap.height() < iconHeight )
160 offsetY = ( iconHeight - myDecoPixmap.height() ) / 2;
162 thepPainter->drawPixmap( myTextStartX + offsetX,
163 myTextStartY + offsetY,
169 thepPainter->drawPixmap( myTextStartX, myTextStartY, iconWidth, iconHeight, myDecoPixmap );
177 myTextStartY += myHeight / 2;
178 thepPainter->setFont( titleFont( option ) );
179 thepPainter->drawText( myTextStartX,
185 thepPainter->setFont( detailFont( option ) );
194 const QStringList myList =
195 wordWrap( data.
detail(), myDetailMetrics, option.rect.width() - myTextStartX );
196 QStringListIterator myLineWrapIterator( myList );
197 while ( myLineWrapIterator.hasNext() )
199 const QString myLine = myLineWrapIterator.next();
200 thepPainter->drawText( myTextStartX,
209 thepPainter->setFont( categoryFont( option ) );
210 thepPainter->drawText( myTextStartX,
242void QgsDetailedItemDelegate::paintAsWidget( QPainter *thepPainter,
243 const QStyleOptionViewItem &option,
249 mpWidget->resize( option.rect.width(), mpWidget->height() );
250 mpWidget->setAutoFillBackground(
true );
253 if ( option.state & QStyle::State_Selected )
255 drawHighlight( option, thepPainter, height( option, data ) );
257 const QPixmap myPixmap = mpWidget->grab();
258 thepPainter->drawPixmap( option.rect.x(),
263void QgsDetailedItemDelegate::drawHighlight(
const QStyleOptionViewItem &option,
264 QPainter *thepPainter,
267 const QColor myColor1 = option.palette.highlight().color();
268 QColor myColor2 = myColor1;
269 myColor2 = myColor2.lighter( 110 );
270 QLinearGradient myGradient( QPointF( 0, option.rect.y() ),
271 QPointF( 0, option.rect.y() + height ) );
272 myGradient.setColorAt( 0, myColor1 );
273 myGradient.setColorAt( 0.1, myColor2 );
274 myGradient.setColorAt( 0.5, myColor1 );
275 myGradient.setColorAt( 0.9, myColor2 );
276 myGradient.setColorAt( 1, myColor2 );
277 thepPainter->fillRect( option.rect, QBrush( myGradient ) );
280int QgsDetailedItemDelegate::height(
const QStyleOptionViewItem &option,
283 const QFontMetrics myTitleMetrics( titleFont( option ) );
284 const QFontMetrics myDetailMetrics( detailFont( option ) );
285 const QFontMetrics myCategoryMetrics( categoryFont( option ) );
289 const QStringList myList = wordWrap( data.
detail(),
292 myHeight += ( myList.count() + 1 ) * ( myDetailMetrics.height() -
verticalSpacing() );
300 myHeight += ( myList.count() + 1 ) * ( myCategoryMetrics.height() -
verticalSpacing() );
306QFont QgsDetailedItemDelegate::detailFont(
const QStyleOptionViewItem &option )
const
308 const QFont myFont = option.font;
312QFont QgsDetailedItemDelegate::categoryFont(
const QStyleOptionViewItem &option )
const
314 QFont myFont = option.font;
315 myFont.setBold(
true );
319QFont QgsDetailedItemDelegate::titleFont(
const QStyleOptionViewItem &option )
const
321 QFont myTitleFont = detailFont( option );
322 myTitleFont.setBold(
true );
323 myTitleFont.setPointSize( myTitleFont.pointSize() );
328QStringList QgsDetailedItemDelegate::wordWrap(
const QString &
string,
329 const QFontMetrics &metrics,
332 if (
string.isEmpty() )
333 return QStringList();
335 return QStringList() << string;
340 QString myCumulativeLine;
341 QString myStringToPreviousSpace;
342 int myPreviousSpacePos = 0;
343 for (
int i = 0; i <
string.count(); ++i )
345 const QChar myChar =
string.at( i );
346 if ( myChar == QChar(
' ' ) )
348 myStringToPreviousSpace = myCumulativeLine;
349 myPreviousSpacePos = i;
351 myCumulativeLine += myChar;
352 if ( metrics.boundingRect( myCumulativeLine ).width() >= width )
357 myList << myStringToPreviousSpace.trimmed();
358 i = myPreviousSpacePos;
359 myStringToPreviousSpace.clear();
360 myCumulativeLine.clear();
364 if ( !myCumulativeLine.trimmed().isEmpty() )
366 myList << myCumulativeLine.trimmed();
380 return mVerticalSpacing;
386 mVerticalSpacing = value;
392 return mHorizontalSpacing;
398 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.