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 &option,
48 const QModelIndex &index )
const 58 paintAsWidget( thepPainter, option, myData );
62 paintManually( thepPainter, option, myData );
65 thepPainter->restore();
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 QFontMetrics myTitleMetrics( titleFont( option ) );
113 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 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 ) );
194 wordWrap( data.
detail(), myDetailMetrics, option.rect.width() - myTextStartX );
195 QStringListIterator myLineWrapIterator( myList );
196 while ( myLineWrapIterator.hasNext() )
198 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 QPixmap myPixmap = QPixmap::grabWidget( mpWidget );
257 thepPainter->drawPixmap( option.rect.x(),
262 void QgsDetailedItemDelegate::drawHighlight(
const QStyleOptionViewItem &option,
263 QPainter *thepPainter,
266 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 QFontMetrics myTitleMetrics( titleFont( option ) );
283 QFontMetrics myDetailMetrics( detailFont( option ) );
284 QFontMetrics myCategoryMetrics( categoryFont( option ) );
288 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 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 QChar myChar =
string.at( i );
345 if ( myChar == QChar(
' ' ) )
347 myStringToPreviousSpace = myCumulativeLine;
348 myPreviousSpacePos = i;
350 myCumulativeLine += myChar;
351 if ( metrics.width( myCumulativeLine ) >= 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;
QgsDetailedItemDelegate(QObject *parent=nullptr)
Constructor for QgsDetailedItemDelegate.
This class is the data only representation of a QgsDetailedItemWidget, designed to be used in custom ...
bool isRenderedAsWidget() const
void setVerticalSpacing(int value)
int verticalSpacing() const
QSize sizeHint(const QStyleOptionViewItem &option, const QModelIndex &index) const override
Reimplement for parent class.
void paint(QPainter *painter, const QStyleOptionViewItem &option, const QModelIndex &index) const override
Reimplement for parent class.
void setHorizontalSpacing(int value)
~QgsDetailedItemDelegate() override
int horizontalSpacing() const