File indexing completed on 2023-05-30 10:45:26
0001 /* 0002 SPDX-FileCopyrightText: 2011 Peter Hedlund <peter.hedlund@kdemail.net> 0003 SPDX-License-Identifier: GPL-2.0-or-later 0004 */ 0005 0006 #include "kwqpixmapitem.h" 0007 0008 #include <QPainter> 0009 0010 KWQPixmapItem::KWQPixmapItem(const QPixmap &pixmap, QGraphicsItem *parentItem) : QGraphicsPixmapItem(pixmap, parentItem) 0011 { 0012 setCacheMode(NoCache); 0013 setTransformationMode(Qt::SmoothTransformation); 0014 } 0015 0016 0017 QRectF KWQPixmapItem::boundingRect() const 0018 { 0019 return m_imageRect; 0020 } 0021 0022 0023 void KWQPixmapItem::setImageRect(const QRect& rect) 0024 { 0025 m_imageRect = rect; 0026 } 0027 0028 0029 void KWQPixmapItem::paint(QPainter *painter, const QStyleOptionGraphicsItem* options, QWidget* widget) 0030 { 0031 Q_UNUSED(options); 0032 Q_UNUSED(widget); 0033 QRect viewRect = painter->combinedTransform().mapRect(boundingRect().toRect()); 0034 QPixmap pm = pixmap().scaled(viewRect.size(), Qt::KeepAspectRatio, Qt::SmoothTransformation); 0035 QSize pixmapSize = pm.size(); 0036 pixmapSize.scale(QSizeF(boundingRect().width(), boundingRect().height()).toSize(), Qt::KeepAspectRatio); 0037 painter->drawPixmap((boundingRect().width() - pixmapSize.width()) / 2, (boundingRect().height() - pixmapSize.height()) / 2, pixmapSize.width(), pixmapSize.height(), pm); 0038 }