KDeclarative

qpixmapitem.cpp
1 /*
2  SPDX-FileCopyrightText: 2011 Marco Martin <[email protected]>
3  SPDX-FileCopyrightText: 2015 Luca Beltrame <[email protected]>
4 
5  SPDX-License-Identifier: LGPL-2.0-or-later
6 */
7 
8 #include "qpixmapitem.h"
9 
10 #include <QPainter>
11 
12 
13 QPixmapItem::QPixmapItem(QQuickItem *parent)
14  : QQuickPaintedItem(parent),
15  m_smooth(false),
16  m_fillMode(QPixmapItem::Stretch)
17 {
18  setFlag(ItemHasContents, true);
19 
20 }
21 
22 
23 QPixmapItem::~QPixmapItem()
24 {
25 }
26 
27 void QPixmapItem::setPixmap(const QPixmap &pixmap)
28 {
29  bool oldPixmapNull = m_pixmap.isNull();
30  m_pixmap = pixmap;
31  updatePaintedRect();
32  update();
33  Q_EMIT nativeWidthChanged();
34  Q_EMIT nativeHeightChanged();
35  Q_EMIT pixmapChanged();
36  if (oldPixmapNull != m_pixmap.isNull()) {
37  Q_EMIT nullChanged();
38  }
39 }
40 
41 QPixmap QPixmapItem::pixmap() const
42 {
43  return m_pixmap;
44 }
45 
46 void QPixmapItem::resetPixmap()
47 {
48  setPixmap(QPixmap());
49 }
50 
51 void QPixmapItem::setSmooth(const bool smooth)
52 {
53  if (smooth == m_smooth) {
54  return;
55  }
56  m_smooth = smooth;
57  update();
58 }
59 
60 bool QPixmapItem::smooth() const
61 {
62  return m_smooth;
63 }
64 
65 int QPixmapItem::nativeWidth() const
66 {
67  return m_pixmap.size().width() / m_pixmap.devicePixelRatio();
68 }
69 
70 int QPixmapItem::nativeHeight() const
71 {
72  return m_pixmap.size().height() / m_pixmap.devicePixelRatio();
73 }
74 
75 QPixmapItem::FillMode QPixmapItem::fillMode() const
76 {
77  return m_fillMode;
78 }
79 
80 void QPixmapItem::setFillMode(QPixmapItem::FillMode mode)
81 {
82  if (mode == m_fillMode) {
83  return;
84  }
85 
86  m_fillMode = mode;
87  updatePaintedRect();
88  update();
89  Q_EMIT fillModeChanged();
90 
91 }
92 
93 void QPixmapItem::paint(QPainter *painter)
94 {
95  if (m_pixmap.isNull()) {
96  return;
97  }
98  painter->save();
99  painter->setRenderHint(QPainter::Antialiasing, m_smooth);
101 
102  if (m_fillMode == TileVertically) {
103  painter->scale(width()/(qreal)m_pixmap.width(), 1);
104  }
105 
106  if (m_fillMode == TileHorizontally) {
107  painter->scale(1, height()/(qreal)m_pixmap.height());
108  }
109 
110  if (m_fillMode >= Tile) {
111  painter->drawTiledPixmap(m_paintedRect, m_pixmap);
112  } else {
113  painter->drawPixmap(m_paintedRect, m_pixmap, m_pixmap.rect());
114  }
115 
116  painter->restore();
117 }
118 
119 bool QPixmapItem::isNull() const
120 {
121  return m_pixmap.isNull();
122 }
123 
124 
125 int QPixmapItem::paintedWidth() const
126 {
127  if (m_pixmap.isNull()) {
128  return 0;
129  }
130 
131  return m_paintedRect.width();
132 }
133 
134 int QPixmapItem::paintedHeight() const
135 {
136  if (m_pixmap.isNull()) {
137  return 0;
138  }
139 
140  return m_paintedRect.height();
141 }
142 
143 void QPixmapItem::updatePaintedRect()
144 {
145 
146  if (m_pixmap.isNull()) {
147  return;
148  }
149 
150  QRectF sourceRect = m_paintedRect;
151 
152  QRectF destRect;
153 
154  switch (m_fillMode) {
155  case PreserveAspectFit: {
156  QSizeF scaled = m_pixmap.size();
157 
158  scaled.scale(boundingRect().size(), Qt::KeepAspectRatio);
159  destRect = QRectF(QPoint(0, 0), scaled);
160  destRect.moveCenter(boundingRect().center().toPoint());
161 
162  break;
163  }
164  case PreserveAspectCrop: {
165  QSizeF scaled = m_pixmap.size();
166 
167  scaled.scale(boundingRect().size(), Qt::KeepAspectRatioByExpanding);
168  destRect = QRectF(QPoint(0, 0), scaled);
169  destRect.moveCenter(boundingRect().center().toPoint());
170  break;
171  }
172  case TileVertically: {
173  destRect = boundingRect().toRect();
174  destRect.setWidth(destRect.width() / (width()/(qreal)m_pixmap.width()));
175  break;
176  }
177  case TileHorizontally: {
178  destRect = boundingRect().toRect();
179  destRect.setHeight(destRect.height() / (height()/(qreal)m_pixmap.height()));
180  break;
181  }
182  case Stretch:
183  case Tile:
184  default:
185  destRect = boundingRect().toRect();
186  }
187 
188  if (destRect != sourceRect) {
189  m_paintedRect = destRect.toRect();
190  Q_EMIT paintedHeightChanged();
191  Q_EMIT paintedWidthChanged();
192  }
193 }
194 
195 void QPixmapItem::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry)
196 {
197  QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
198  updatePaintedRect();
199 }
200 
QRect toRect() const const
void scale(qreal width, qreal height, Qt::AspectRatioMode mode)
void setRenderHint(QPainter::RenderHint hint, bool on)
void scale(qreal sx, qreal sy)
void save()
void drawTiledPixmap(const QRectF &rectangle, const QPixmap &pixmap, const QPointF &position)
virtual void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
void setHeight(qreal height)
void drawPixmap(const QRectF &target, const QPixmap &pixmap, const QRectF &source)
QTextStream & center(QTextStream &stream)
void moveCenter(const QPointF &position)
void restore()
qreal width() const const
void setWidth(qreal width)
KeepAspectRatio
void update(Part *part, const QByteArray &data, qint64 dataSize)
qreal height() const const
Q_EMITQ_EMIT
This file is part of the KDE documentation.
Documentation copyright © 1996-2021 The KDE developers.
Generated on Mon Jan 25 2021 22:44:28 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.