24#include <QFontMetrics>
25#include <QStyleOptionViewItem>
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
53 if ( index.data( Qt::UserRole ).userType() == qMetaTypeId<QgsDetailedItemData>() )
59 paintAsWidget( thepPainter, option, myData );
63 paintManually( thepPainter, option, myData );
71 const QStyleOptionViewItem &option,
72 const QModelIndex &index )
const
74 if ( index.data( Qt::UserRole ).userType() == qMetaTypeId<QgsDetailedItemData>() )
80 return QSize( 378, mpWidget->height() );
85 return QSize( 50, height( option, myData ) );
91 return QSize( 50, 50 );
95void 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,
241void 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(),
262void 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 ) );
279int 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() );
305QFont QgsDetailedItemDelegate::detailFont(
const QStyleOptionViewItem &option )
const
307 const QFont myFont = option.font;
311QFont QgsDetailedItemDelegate::categoryFont(
const QStyleOptionViewItem &option )
const
313 QFont myFont = option.font;
314 myFont.setBold(
true );
318QFont QgsDetailedItemDelegate::titleFont(
const QStyleOptionViewItem &option )
const
320 QFont myTitleFont = detailFont( option );
321 myTitleFont.setBold(
true );
322 myTitleFont.setPointSize( myTitleFont.pointSize() );
327QStringList 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;
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.