diff --git a/libs/resourcewidgets/KisResourceItemDelegate.cpp b/libs/resourcewidgets/KisResourceItemDelegate.cpp index aace214206..b96641dc7c 100644 --- a/libs/resourcewidgets/KisResourceItemDelegate.cpp +++ b/libs/resourcewidgets/KisResourceItemDelegate.cpp @@ -1,87 +1,88 @@ /* This file is part of the KDE project * Copyright (C) 2008 Jan Hambrecht * Copyright (C) 2018 Boudewijn Rempt * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public License * along with this library; see the file COPYING.LIB. If not, write to * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301, USA. */ #include "KisResourceItemDelegate.h" #include #include #include "KisResourceModel.h" KisResourceItemDelegate::KisResourceItemDelegate(QObject *parent) : QAbstractItemDelegate(parent) , m_checkerPainter(4) { } void KisResourceItemDelegate::paint(QPainter *painter, const QStyleOptionViewItem & option, const QModelIndex &index) const { if (!index.isValid()) return; painter->save(); if (option.state & QStyle::State_Selected) { painter->fillRect(option.rect, option.palette.highlight()); } QRect innerRect = option.rect.adjusted(2, 1, -2, -1); QImage thumbnail = index.data(Qt::DecorationRole).value(); QSize imageSize = thumbnail.size(); - if (imageSize.height() > innerRect.height() || imageSize.width() > innerRect.width()) { - qreal scaleW = static_cast(innerRect.width()) / static_cast(imageSize.width()); - qreal scaleH = static_cast(innerRect.height()) / static_cast(imageSize.height()); - - qreal scale = qMin(scaleW, scaleH); - - int thumbW = static_cast(imageSize.width() * scale); - int thumbH = static_cast(imageSize.height() * scale); - thumbnail = thumbnail.scaled(thumbW, thumbH, Qt::KeepAspectRatio, Qt::SmoothTransformation); - } - painter->setRenderHint(QPainter::SmoothPixmapTransform, true); QString resourceType = index.data(Qt::UserRole + KisResourceModel::ResourceType).toString(); // XXX: don't use a hardcoded string here to identify the resource type if (resourceType == ResourceType::Gradients) { m_checkerPainter.paint(*painter, innerRect); + thumbnail = thumbnail.scaled(innerRect.width(), innerRect.height(), Qt::IgnoreAspectRatio, Qt::SmoothTransformation); + painter->drawImage(innerRect.topLeft(), thumbnail); + } + else if (resourceType == ResourceType::Patterns) { + painter->fillRect(innerRect, Qt::white); // no checkers, they are confusing with patterns. + if (imageSize.height() > innerRect.height() || imageSize.width() > innerRect.width()) { + thumbnail = thumbnail.scaled(innerRect.width(), innerRect.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation); + } + painter->fillRect(innerRect, QBrush(thumbnail)); } else { painter->fillRect(innerRect, Qt::white); // no checkers, they are confusing with patterns. + if (imageSize.height() > innerRect.height() || imageSize.width() > innerRect.width()) { + thumbnail = thumbnail.scaled(innerRect.width(), innerRect.height(), Qt::KeepAspectRatio, Qt::SmoothTransformation); + } + QPoint topleft(innerRect.topLeft()); + + if (thumbnail.width() < innerRect.width()) { + topleft.setX(topleft.x() + (innerRect.width() - thumbnail.width()) / 2); + } + if (thumbnail.height() < innerRect.height()) { + topleft.setY(topleft.y() + (innerRect.height() - thumbnail.height()) / 2); + } + painter->drawImage(topleft, thumbnail); } - QPoint topleft(innerRect.topLeft()); - - if (thumbnail.width() < innerRect.width()) { - topleft.setX(topleft.x() + (innerRect.width() - thumbnail.width()) / 2); - } - if (thumbnail.height() < innerRect.height()) { - topleft.setY(topleft.y() + (innerRect.height() - thumbnail.height()) / 2); - } - painter->drawImage(topleft, thumbnail); painter->restore(); } QSize KisResourceItemDelegate::sizeHint(const QStyleOptionViewItem &optionItem, const QModelIndex &) const { return optionItem.decorationSize; }