Warning, file /office/calligra/libs/widgets/KoResourceItemDelegate.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

0001 /* This file is part of the KDE project
0002  * Copyright (C) 2008 Jan Hambrecht <jaham@gmx.net>
0003  *
0004  * This library is free software; you can redistribute it and/or
0005  * modify it under the terms of the GNU Library General Public
0006  * License as published by the Free Software Foundation; either
0007  * version 2 of the License, or (at your option) any later version.
0008  *
0009  * This library is distributed in the hope that it will be useful,
0010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0012  * Library General Public License for more details.
0013  *
0014  * You should have received a copy of the GNU Library General Public License
0015  * along with this library; see the file COPYING.LIB.  If not, write to
0016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0017  * Boston, MA 02110-1301, USA.
0018  */
0019 
0020 #include "KoResourceItemDelegate.h"
0021 
0022 #include <KoAbstractGradient.h>
0023 #include <QPainter>
0024 
0025 KoResourceItemDelegate::KoResourceItemDelegate( QObject * parent )
0026     : QAbstractItemDelegate( parent ), m_checkerPainter( 4 )
0027 {
0028 }
0029 
0030 void KoResourceItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
0031 {
0032     if( ! index.isValid() )
0033         return;
0034 
0035     KoResource * resource = static_cast<KoResource*>( index.internalPointer() );
0036     if (!resource)
0037         return;
0038 
0039     painter->save();
0040 
0041     if (option.state & QStyle::State_Selected)
0042         painter->fillRect( option.rect, option.palette.highlight() );
0043 
0044     QRect innerRect = option.rect.adjusted( 2, 1, -2, -1 );
0045 
0046     KoAbstractGradient * gradient = dynamic_cast<KoAbstractGradient*>( resource );
0047     if (gradient) {
0048         QGradient * g = gradient->toQGradient();
0049 
0050         QLinearGradient paintGradient;
0051         paintGradient.setStops( g->stops() );
0052         paintGradient.setStart( innerRect.topLeft() );
0053         paintGradient.setFinalStop( innerRect.topRight() );
0054 
0055         m_checkerPainter.paint( *painter, innerRect );
0056         painter->fillRect( innerRect, QBrush( paintGradient ) );
0057 
0058         delete g;
0059     }
0060     else {
0061         QImage thumbnail = index.data( Qt::DecorationRole ).value<QImage>();
0062 
0063         QSize imageSize = thumbnail.size();
0064 
0065         if(imageSize.height() > innerRect.height() || imageSize.width() > innerRect.width()) {
0066             qreal scaleW = static_cast<qreal>( innerRect.width() ) / static_cast<qreal>( imageSize.width() );
0067             qreal scaleH = static_cast<qreal>( innerRect.height() ) / static_cast<qreal>( imageSize.height() );
0068 
0069             qreal scale = qMin( scaleW, scaleH );
0070 
0071             int thumbW = static_cast<int>( imageSize.width() * scale );
0072             int thumbH = static_cast<int>( imageSize.height() * scale );
0073             thumbnail = thumbnail.scaled( thumbW, thumbH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
0074         }
0075         painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
0076         if (thumbnail.hasAlphaChannel()) {
0077             painter->fillRect(innerRect, Qt::white); // no checkers, they are confusing with patterns.
0078         }
0079         painter->fillRect( innerRect, QBrush(thumbnail) );
0080     }
0081     painter->restore();
0082 }
0083 
0084 QSize KoResourceItemDelegate::sizeHint( const QStyleOptionViewItem & optionItem, const QModelIndex & ) const
0085 {
0086     return optionItem.decorationSize;
0087 }