KDeclarative

qimageitem.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 "qimageitem.h"
9 
10 #include <QPainter>
11 
12 
13 QImageItem::QImageItem(QQuickItem *parent)
14  : QQuickPaintedItem(parent),
15  m_smooth(false),
16  m_fillMode(QImageItem::Stretch)
17 {
18  setFlag(ItemHasContents, true);
19 }
20 
21 
22 QImageItem::~QImageItem()
23 {
24 }
25 
26 void QImageItem::setImage(const QImage &image)
27 {
28  bool oldImageNull = m_image.isNull();
29  m_image = image;
30  updatePaintedRect();
31  update();
32  Q_EMIT nativeWidthChanged();
33  Q_EMIT nativeHeightChanged();
34  Q_EMIT imageChanged();
35  if (oldImageNull != m_image.isNull()) {
36  Q_EMIT nullChanged();
37  }
38 }
39 
40 QImage QImageItem::image() const
41 {
42  return m_image;
43 }
44 
45 void QImageItem::resetImage()
46 {
47  setImage(QImage());
48 }
49 
50 void QImageItem::setSmooth(const bool smooth)
51 {
52  if (smooth == m_smooth) {
53  return;
54  }
55  m_smooth = smooth;
56  update();
57 }
58 
59 bool QImageItem::smooth() const
60 {
61  return m_smooth;
62 }
63 
64 int QImageItem::nativeWidth() const
65 {
66  return m_image.size().width() / m_image.devicePixelRatio();
67 }
68 
69 int QImageItem::nativeHeight() const
70 {
71  return m_image.size().height() / m_image.devicePixelRatio();
72 }
73 
74 QImageItem::FillMode QImageItem::fillMode() const
75 {
76  return m_fillMode;
77 }
78 
79 void QImageItem::setFillMode(QImageItem::FillMode mode)
80 {
81  if (mode == m_fillMode) {
82  return;
83  }
84 
85  m_fillMode = mode;
86  updatePaintedRect();
87  update();
88  Q_EMIT fillModeChanged();
89 }
90 
91 void QImageItem::paint(QPainter *painter)
92 {
93  if (m_image.isNull()) {
94  return;
95  }
96  painter->save();
97  painter->setRenderHint(QPainter::Antialiasing, m_smooth);
99 
100  if (m_fillMode == TileVertically) {
101  painter->scale(width()/(qreal)m_image.width(), 1);
102  }
103 
104  if (m_fillMode == TileHorizontally) {
105  painter->scale(1, height()/(qreal)m_image.height());
106  }
107 
108  if (m_fillMode >= Tile) {
109  painter->drawTiledPixmap(m_paintedRect, QPixmap::fromImage(m_image));
110  } else {
111  painter->drawImage(m_paintedRect, m_image, m_image.rect());
112  }
113 
114  painter->restore();
115 }
116 
117 bool QImageItem::isNull() const
118 {
119  return m_image.isNull();
120 }
121 
122 int QImageItem::paintedWidth() const
123 {
124  if (m_image.isNull()) {
125  return 0;
126  }
127 
128  return m_paintedRect.width();
129 }
130 
131 int QImageItem::paintedHeight() const
132 {
133  if (m_image.isNull()) {
134  return 0;
135  }
136 
137  return m_paintedRect.height();
138 }
139 
140 void QImageItem::updatePaintedRect()
141 {
142 
143  if (m_image.isNull()) {
144  return;
145  }
146 
147  QRectF sourceRect = m_paintedRect;
148 
149  QRectF destRect;
150 
151  switch (m_fillMode) {
152  case PreserveAspectFit: {
153  QSizeF scaled = m_image.size();
154 
155  scaled.scale(boundingRect().size(), Qt::KeepAspectRatio);
156  destRect = QRectF(QPoint(0, 0), scaled);
157  destRect.moveCenter(boundingRect().center().toPoint());
158  break;
159  }
160  case PreserveAspectCrop: {
161  QSizeF scaled = m_image.size();
162 
163  scaled.scale(boundingRect().size(), Qt::KeepAspectRatioByExpanding);
164  destRect = QRectF(QPoint(0, 0), scaled);
165  destRect.moveCenter(boundingRect().center().toPoint());
166  break;
167  }
168  case TileVertically: {
169  destRect = boundingRect().toRect();
170  destRect.setWidth(destRect.width() / (width()/(qreal)m_image.width()));
171  break;
172  }
173  case TileHorizontally: {
174  destRect = boundingRect().toRect();
175  destRect.setHeight(destRect.height() / (height()/(qreal)m_image.height()));
176  break;
177  }
178  case Stretch:
179  case Tile:
180  default:
181  destRect = boundingRect().toRect();
182  }
183 
184  if (destRect != sourceRect) {
185  m_paintedRect = destRect.toRect();
186  Q_EMIT paintedHeightChanged();
187  Q_EMIT paintedWidthChanged();
188  }
189 }
190 
191 void QImageItem::geometryChanged(const QRectF& newGeometry, const QRectF& oldGeometry)
192 {
193  QQuickPaintedItem::geometryChanged(newGeometry, oldGeometry);
194  updatePaintedRect();
195 }
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()
QPixmap fromImage(const QImage &image, Qt::ImageConversionFlags flags)
void drawTiledPixmap(const QRectF &rectangle, const QPixmap &pixmap, const QPointF &position)
virtual void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
void setHeight(qreal height)
QTextStream & center(QTextStream &stream)
void moveCenter(const QPointF &position)
void restore()
void drawImage(const QRectF &target, const QImage &image, const QRectF &source, Qt::ImageConversionFlags flags)
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.