KDeclarative

qpixmapitem.cpp
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(ItemHasContents, 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();
82
83 if (m_fillMode == TileVertically) {
84 painter->scale(width() / (qreal)m_pixmap.width(), 1);
85 }
86
87 if (m_fillMode == TileHorizontally) {
88 painter->scale(1, height() / (qreal)m_pixmap.height());
89 }
90
91 if (m_fillMode >= Tile) {
92 painter->drawTiledPixmap(m_paintedRect, m_pixmap);
93 } else {
94 painter->drawPixmap(m_paintedRect, m_pixmap, 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
138 destRect = QRectF(QPoint(0, 0), scaled);
139 destRect.moveCenter(boundingRect().center().toPoint());
140
141 break;
142 }
143 case PreserveAspectCrop: {
144 QSizeF scaled = m_pixmap.size();
145
147 destRect = QRectF(QPoint(0, 0), scaled);
148 destRect.moveCenter(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"
Q_EMITQ_EMIT
void drawPixmap(const QPoint &point, const QPixmap &pixmap)
void drawTiledPixmap(const QRect &rectangle, const QPixmap &pixmap, const QPoint &position)
void restore()
void save()
void scale(qreal sx, qreal sy)
void setRenderHint(RenderHint hint, bool on)
qreal devicePixelRatio() const const
int height() const const
bool isNull() const const
QRect rect() const const
QSize size() const const
int width() const const
virtual QRectF boundingRect() const const
virtual void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
QSizeF size() const const
void update()
int height() const const
int width() const const
qreal height() const const
void moveCenter(const QPointF &position)
void setHeight(qreal height)
void setWidth(qreal width)
QRect toRect() const const
qreal width() const const
int height() const const
int width() const const
void scale(const QSizeF &size, Qt::AspectRatioMode mode)
KeepAspectRatio
QTextStream & center(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Tue Mar 26 2024 11:16:59 by doxygen 1.10.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.