Krita Source Code Documentation
Loading...
Searching...
No Matches
KoResourceItemDelegate.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 * SPDX-FileCopyrightText: 2008 Jan Hambrecht <jaham@gmx.net>
3 *
4 * SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6
7#include "KoResourceItemDelegate.h"
8
11#include <QPainter>
12
13KoResourceItemDelegate::KoResourceItemDelegate( QObject * parent )
14 : QAbstractItemDelegate( parent ), m_checkerPainter( 4 )
15{
16}
17
18void KoResourceItemDelegate::paint( QPainter * painter, const QStyleOptionViewItem & option, const QModelIndex & index ) const
19{
20 if( ! index.isValid() )
21 return;
22
23 KoResource * resource = static_cast<KoResource*>( index.internalPointer() );
24 if (!resource)
25 return;
26
27 painter->save();
28
29 if (option.state & QStyle::State_Selected)
30 painter->fillRect( option.rect, option.palette.highlight() );
31
32 QRect innerRect = option.rect.adjusted( 2, 1, -2, -1 );
33
34 KoAbstractGradient * gradient = dynamic_cast<KoAbstractGradient*>( resource );
35 KoColorSet * palette = dynamic_cast<KoColorSet*>( resource );
36 if (gradient) {
37 QGradient * g = gradient->toQGradient();
38
39 QLinearGradient paintGradient;
40 paintGradient.setStops( g->stops() );
41 paintGradient.setStart( innerRect.topLeft() );
42 paintGradient.setFinalStop( innerRect.topRight() );
43
44 m_checkerPainter.paint( *painter, innerRect );
45 painter->fillRect( innerRect, QBrush( paintGradient ) );
46
47 delete g;
48 }
49 else if (palette) {
50 QImage thumbnail = index.data( Qt::DecorationRole ).value<QImage>();
51 painter->setRenderHint(QPainter::SmoothPixmapTransform, thumbnail.width() > innerRect.width() || thumbnail.height() > innerRect.height());
52 painter->drawImage(innerRect, thumbnail);
53 }
54 else {
55 QImage thumbnail = index.data( Qt::DecorationRole ).value<QImage>();
56
57 QSize imageSize = thumbnail.size();
58
59 if(imageSize.height() > innerRect.height() || imageSize.width() > innerRect.width()) {
60 qreal scaleW = static_cast<qreal>( innerRect.width() ) / static_cast<qreal>( imageSize.width() );
61 qreal scaleH = static_cast<qreal>( innerRect.height() ) / static_cast<qreal>( imageSize.height() );
62
63 qreal scale = qMin( scaleW, scaleH );
64
65 int thumbW = static_cast<int>( imageSize.width() * scale );
66 int thumbH = static_cast<int>( imageSize.height() * scale );
67 thumbnail = thumbnail.scaled( thumbW, thumbH, Qt::IgnoreAspectRatio, Qt::SmoothTransformation );
68 }
69 painter->setRenderHint(QPainter::SmoothPixmapTransform, true);
70 if (thumbnail.hasAlphaChannel()) {
71 painter->fillRect(innerRect, Qt::white); // no checkers, they are confusing with patterns.
72 }
73 painter->fillRect( innerRect, QBrush(thumbnail) );
74 }
75 painter->restore();
76}
77
78QSize KoResourceItemDelegate::sizeHint( const QStyleOptionViewItem & optionItem, const QModelIndex & ) const
79{
80 return optionItem.decorationSize;
81}
ChildIterator< value_type, is_const > parent(const ChildIterator< value_type, is_const > &it)
Definition KisForest.h:327
rgba palette[MAX_PALETTE]
Definition palette.c:35
virtual QGradient * toQGradient() const