File indexing completed on 2024-05-12 15:59:57
0001 /* This file is part of the KDE project 0002 * SPDX-FileCopyrightText: 2008 Jan Hambrecht <jaham@gmx.net> 0003 * SPDX-FileCopyrightText: 2018 Boudewijn Rempt <boud@valdyas.org> 0004 * 0005 * SPDX-License-Identifier: LGPL-2.0-or-later 0006 */ 0007 0008 #include "KisResourceThumbnailPainter.h" 0009 #include "KisResourceModel.h" 0010 0011 #include <QPainter> 0012 #include <QDebug> 0013 0014 KisResourceThumbnailPainter::KisResourceThumbnailPainter(QObject *parent) 0015 : QObject(parent) 0016 , m_checkerPainter(4) 0017 { 0018 } 0019 0020 QImage KisResourceThumbnailPainter::getReadyThumbnail(const QModelIndex &index, QSize size, const QPalette& palette) const 0021 { 0022 QImage thumbLabel = QImage(size, QImage::Format_ARGB32); 0023 thumbLabel.fill(Qt::white); 0024 QPainter painter(&thumbLabel); 0025 paint(&painter, index, QRect(QPoint(0, 0), size), palette, false, false); 0026 return thumbLabel; 0027 } 0028 0029 void KisResourceThumbnailPainter::paint(QPainter *painter, const QModelIndex& index, QRect rect, const QPalette& palette, bool selected, bool addMargin) const 0030 { 0031 QImage thumbnail = index.data(Qt::UserRole + KisAbstractResourceModel::Thumbnail).value<QImage>(); 0032 QString resourceType = index.data(Qt::UserRole + KisAbstractResourceModel::ResourceType).toString(); 0033 QString name = index.data(Qt::UserRole + KisAbstractResourceModel::Name).toString(); 0034 0035 paint(painter, thumbnail, resourceType, name, rect, palette, selected, addMargin); 0036 } 0037 0038 void KisResourceThumbnailPainter::paint(QPainter *painter, QImage thumbnail, QString resourceType, QString name, QRect rect, const QPalette& palette, bool selected, bool addMargin) const 0039 { 0040 painter->save(); 0041 0042 if(addMargin) { 0043 // margin has empty space...which we want to be the color palette backround 0044 painter->fillRect(rect, palette.background()); 0045 } 0046 0047 qreal devicePixelRatioF = painter->device()->devicePixelRatioF(); 0048 0049 if (selected) { 0050 painter->fillRect(rect, palette.highlight()); 0051 } 0052 0053 QRect innerRect = addMargin ? rect.adjusted(2, 2, -2, -2) : rect; 0054 0055 thumbnail.setDevicePixelRatio(devicePixelRatioF); 0056 0057 QSize imageSize = thumbnail.size(); 0058 0059 painter->setRenderHint(QPainter::SmoothPixmapTransform, true); 0060 0061 if (resourceType == ResourceType::Gradients) { 0062 m_checkerPainter.paint(*painter, innerRect, innerRect.topLeft()); 0063 if (!thumbnail.isNull()) { 0064 thumbnail = thumbnail.scaled(innerRect.size()*devicePixelRatioF, Qt::IgnoreAspectRatio, Qt::SmoothTransformation); 0065 painter->drawImage(innerRect.topLeft(), thumbnail); 0066 } 0067 } 0068 else if (resourceType == ResourceType::Patterns) { 0069 painter->fillRect(innerRect, Qt::white); // no checkers, they are confusing with patterns. 0070 if (!thumbnail.isNull() && (imageSize.height() > innerRect.height() || imageSize.width() > innerRect.width())) { 0071 thumbnail = thumbnail.scaled(innerRect.size()*devicePixelRatioF, Qt::KeepAspectRatio, Qt::SmoothTransformation); 0072 } 0073 QBrush patternBrush(thumbnail); 0074 patternBrush.setTransform(QTransform::fromTranslate(innerRect.x(), innerRect.y())); 0075 painter->fillRect(innerRect, patternBrush); 0076 } 0077 else if (resourceType == ResourceType::Workspaces || resourceType == ResourceType::WindowLayouts) { 0078 // TODO: thumbnails for workspaces and window layouts? 0079 painter->fillRect(innerRect, Qt::white); 0080 QPen before = painter->pen(); 0081 painter->setPen(Qt::black); 0082 painter->drawText(innerRect, Qt::TextWordWrap, name.split("_").join(" ")); 0083 painter->setPen(before); 0084 } 0085 else { 0086 painter->fillRect(innerRect, Qt::white); // no checkers, they are confusing with patterns. 0087 if (!thumbnail.isNull()) { 0088 if (imageSize.height() > innerRect.height()*devicePixelRatioF || imageSize.width() > innerRect.width()*devicePixelRatioF) { 0089 thumbnail = thumbnail.scaled(innerRect.size()*devicePixelRatioF, Qt::KeepAspectRatio, Qt::SmoothTransformation); 0090 } else if(imageSize.height() < innerRect.height()*devicePixelRatioF || imageSize.width() < innerRect.width()*devicePixelRatioF) { 0091 thumbnail = thumbnail.scaled(innerRect.size()*devicePixelRatioF, Qt::KeepAspectRatio, Qt::FastTransformation); 0092 } 0093 } 0094 QPoint topleft(innerRect.topLeft()); 0095 0096 if (thumbnail.width() < innerRect.width()*devicePixelRatioF) { 0097 topleft.setX(topleft.x() + (innerRect.width() - thumbnail.width()/devicePixelRatioF) / 2); 0098 } 0099 if (thumbnail.height() < innerRect.height()*devicePixelRatioF) { 0100 topleft.setY(topleft.y() + (innerRect.height() - thumbnail.height()/devicePixelRatioF) / 2); 0101 } 0102 painter->drawImage(topleft, thumbnail); 0103 } 0104 0105 0106 painter->restore(); 0107 }