1/*
2 SPDX-FileCopyrightText: 2011 Marco Martin <mart@kde.org>
3 SPDX-FileCopyrightText: 2015 Luca Beltrame <lbeltrame@kde.org>
4
5 SPDX-License-Identifier: LGPL-2.0-or-later
6*/
7
8#include "qpixmapitem.h"
9
10#include <QPainter>
11
12QPixmapItem::QPixmapItem(QQuickItem *parent)
13 : QQuickPaintedItem(parent)
14 , m_fillMode(QPixmapItem::Stretch)
15{
16 setFlag(flag: ItemHasContents, enabled: true);
17}
18
19QPixmapItem::~QPixmapItem()
20{
21}
22
23void QPixmapItem::setPixmap(const QPixmap &pixmap)
24{
25 bool oldPixmapNull = m_pixmap.isNull();
26 m_pixmap = pixmap;
27 updatePaintedRect();
28 update();
29 Q_EMIT nativeWidthChanged();
30 Q_EMIT nativeHeightChanged();
31 Q_EMIT pixmapChanged();
32 if (oldPixmapNull != m_pixmap.isNull()) {
33 Q_EMIT nullChanged();
34 }
35}
36
37QPixmap QPixmapItem::pixmap() const
38{
39 return m_pixmap;
40}
41
42void QPixmapItem::resetPixmap()
43{
44 setPixmap(QPixmap());
45}
46
47int QPixmapItem::nativeWidth() const
48{
49 return m_pixmap.size().width() / m_pixmap.devicePixelRatio();
50}
51
52int QPixmapItem::nativeHeight() const
53{
54 return m_pixmap.size().height() / m_pixmap.devicePixelRatio();
55}
56
57QPixmapItem::FillMode QPixmapItem::fillMode() const
58{
59 return m_fillMode;
60}
61
62void QPixmapItem::setFillMode(QPixmapItem::FillMode mode)
63{
64 if (mode == m_fillMode) {
65 return;
66 }
67
68 m_fillMode = mode;
69 updatePaintedRect();
70 update();
71 Q_EMIT fillModeChanged();
72}
73
74void QPixmapItem::paint(QPainter *painter)
75{
76 if (m_pixmap.isNull()) {
77 return;
78 }
79 painter->save();
80 painter->setRenderHint(hint: QPainter::Antialiasing, on: smooth());
81 painter->setRenderHint(hint: QPainter::SmoothPixmapTransform, on: smooth());
82
83 if (m_fillMode == TileVertically) {
84 painter->scale(sx: width() / (qreal)m_pixmap.width(), sy: 1);
85 }
86
87 if (m_fillMode == TileHorizontally) {
88 painter->scale(sx: 1, sy: height() / (qreal)m_pixmap.height());
89 }
90
91 if (m_fillMode >= Tile) {
92 painter->drawTiledPixmap(rect: m_paintedRect, pm: m_pixmap);
93 } else {
94 painter->drawPixmap(targetRect: m_paintedRect, pixmap: m_pixmap, sourceRect: m_pixmap.rect());
95 }
96
97 painter->restore();
98}
99
100bool QPixmapItem::isNull() const
101{
102 return m_pixmap.isNull();
103}
104
105int QPixmapItem::paintedWidth() const
106{
107 if (m_pixmap.isNull()) {
108 return 0;
109 }
110
111 return m_paintedRect.width();
112}
113
114int QPixmapItem::paintedHeight() const
115{
116 if (m_pixmap.isNull()) {
117 return 0;
118 }
119
120 return m_paintedRect.height();
121}
122
123void QPixmapItem::updatePaintedRect()
124{
125 if (m_pixmap.isNull()) {
126 return;
127 }
128
129 QRectF sourceRect = m_paintedRect;
130
131 QRectF destRect;
132
133 switch (m_fillMode) {
134 case PreserveAspectFit: {
135 QSizeF scaled = m_pixmap.size();
136
137 scaled.scale(s: boundingRect().size(), mode: Qt::KeepAspectRatio);
138 destRect = QRectF(QPoint(0, 0), scaled);
139 destRect.moveCenter(p: boundingRect().center().toPoint());
140
141 break;
142 }
143 case PreserveAspectCrop: {
144 QSizeF scaled = m_pixmap.size();
145
146 scaled.scale(s: boundingRect().size(), mode: Qt::KeepAspectRatioByExpanding);
147 destRect = QRectF(QPoint(0, 0), scaled);
148 destRect.moveCenter(p: boundingRect().center().toPoint());
149 break;
150 }
151 case TileVertically: {
152 destRect = boundingRect().toRect();
153 destRect.setWidth(destRect.width() / (width() / (qreal)m_pixmap.width()));
154 break;
155 }
156 case TileHorizontally: {
157 destRect = boundingRect().toRect();
158 destRect.setHeight(destRect.height() / (height() / (qreal)m_pixmap.height()));
159 break;
160 }
161 case Stretch:
162 case Tile:
163 default:
164 destRect = boundingRect().toRect();
165 }
166
167 if (destRect != sourceRect) {
168 m_paintedRect = destRect.toRect();
169 Q_EMIT paintedHeightChanged();
170 Q_EMIT paintedWidthChanged();
171 }
172}
173
174void QPixmapItem::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
175{
176 QQuickPaintedItem::geometryChange(newGeometry, oldGeometry);
177 updatePaintedRect();
178}
179
180#include "moc_qpixmapitem.cpp"
181

source code of kdeclarative/src/qmlcontrols/kquickcontrolsaddons/qpixmapitem.cpp