File indexing completed on 2024-05-12 16:02:10

0001 /* This file is part of the KDE project
0002  * SPDX-FileCopyrightText: 2008 Jan Hambrecht <jaham@gmx.net>
0003  *
0004  * SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 #include "KoResourceItemDelegate.h"
0008 
0009 #include <resources/KoAbstractGradient.h>
0010 #include <resources/KoColorSet.h>
0011 #include <QPainter>
0012 
0013 KoResourceItemDelegate::KoResourceItemDelegate( QObject * parent )
0014     : QAbstractItemDelegate( parent ), m_checkerPainter( 4 )
0015 {
0016 }
0017 
0018 void KoResourceItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
0019 {
0020     if( ! index.isValid() )
0021         return;
0022 
0023     KoResource * resource = static_cast<KoResource*>( index.internalPointer() );
0024     if (!resource)
0025         return;
0026 
0027     painter->save();
0028 
0029     if (option.state & QStyle::State_Selected)
0030         painter->fillRect( option.rect, option.palette.highlight() );
0031 
0032     QRect innerRect = option.rect.adjusted( 2, 1, -2, -1 );
0033 
0034     KoAbstractGradient * gradient = dynamic_cast<KoAbstractGradient*>( resource );
0035     KoColorSet * palette = dynamic_cast<KoColorSet*>( resource );
0036     if (gradient) {
0037         QGradient * g = gradient->toQGradient();
0038 
0039         QLinearGradient paintGradient;
0040         paintGradient.setStops( g->stops() );
0041         paintGradient.setStart( innerRect.topLeft() );
0042         paintGradient.setFinalStop( innerRect.topRight() );
0043 
0044         m_checkerPainter.paint( *painter, innerRect );
0045         painter->fillRect( innerRect, QBrush( paintGradient ) );
0046 
0047         delete g;
0048     }
0049     else if (palette) {
0050         QImage thumbnail = index.data( Qt::DecorationRole ).value<QImage>();
0051         painter->setRenderHint(QPainter::SmoothPixmapTransform, thumbnail.width() > innerRect.width() || thumbnail.height() > innerRect.height());
0052         painter->drawImage(innerRect, thumbnail);
0053     }
0054     else {
0055         QImage thumbnail = index.data( Qt::DecorationRole ).value<QImage>();
0056 
0057         QSize imageSize = thumbnail.size();
0058 
0059         if(imageSize.height() > innerRect.height() || imageSize.width() > innerRect.width()) {
0060             qreal scaleW = static_cast<qreal>( innerRect.width() ) / static_cast<qreal>( imageSize.width() );
0061             qreal scaleH = static_cast<qreal>( innerRect.height() ) / static_cast<qreal>( imageSize.height() );
0062 
0063             qreal scale = qMin( scaleW, scaleH );
0064 
0065             int thumbW = static_cast<int>( imageSize.width() * scale );
0066             int thumbH = static_cast<int>( imageSize.height() * scale );
0067             thumbnail = thumbnail.scaled( thumbW, thumbH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
0068         }
0069         painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
0070         if (thumbnail.hasAlphaChannel()) {
0071             painter->fillRect(innerRect, Qt::white); // no checkers, they are confusing with patterns.
0072         }
0073         painter->fillRect( innerRect, QBrush(thumbnail) );
0074     }
0075     painter->restore();
0076 }
0077 
0078 QSize KoResourceItemDelegate::sizeHint( const QStyleOptionViewItem & optionItem, const QModelIndex & ) const
0079 {
0080     return optionItem.decorationSize;
0081 }