KQuickImageEditor

imageitem.cpp
1
2/*
3 * SPDX-FileCopyrightText: (C) 2011 Marco Martin <mart@kde.org>
4 * SPDX-FileCopyrightText: (C) 2020 Luca Beltrame <lbeltrame@kde.org>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-or-later
7 */
8
9#include "imageitem.h"
10
11#include <QPainter>
12
13ImageItem::ImageItem(QQuickItem *parent)
14 : QQuickPaintedItem(parent)
15 , m_smooth(false)
16 , m_fillMode(ImageItem::Stretch)
17{
18 setFlag(ItemHasContents, true);
19}
20
21void ImageItem::setImage(const QImage &image)
22{
23 bool oldImageNull = m_image.isNull();
24 m_image = image;
25 updatePaintedRect();
26 update();
27 Q_EMIT nativeWidthChanged();
28 Q_EMIT nativeHeightChanged();
29 Q_EMIT imageChanged();
30 if (oldImageNull != m_image.isNull()) {
31 Q_EMIT nullChanged();
32 }
33}
34
35QImage ImageItem::image() const
36{
37 return m_image;
38}
39
40void ImageItem::resetImage()
41{
42 setImage(QImage());
43}
44
45int ImageItem::nativeWidth() const
46{
47 return m_image.size().width() / m_image.devicePixelRatio();
48}
49
50int ImageItem::nativeHeight() const
51{
52 return m_image.size().height() / m_image.devicePixelRatio();
53}
54
55ImageItem::FillMode ImageItem::fillMode() const
56{
57 return m_fillMode;
58}
59
60void ImageItem::setFillMode(ImageItem::FillMode mode)
61{
62 if (mode == m_fillMode) {
63 return;
64 }
65
66 m_fillMode = mode;
67 updatePaintedRect();
68 update();
69 Q_EMIT fillModeChanged();
70}
71
72void ImageItem::paint(QPainter *painter)
73{
74 if (m_image.isNull()) {
75 return;
76 }
77 painter->save();
80
81 if (m_fillMode == TileVertically) {
82 painter->scale(width() / (qreal)m_image.width(), 1);
83 }
84
85 if (m_fillMode == TileHorizontally) {
86 painter->scale(1, height() / (qreal)m_image.height());
87 }
88
89 if (m_fillMode >= Tile) {
90 painter->drawTiledPixmap(m_paintedRect, QPixmap::fromImage(m_image));
91 } else {
92 painter->drawImage(m_paintedRect, m_image, m_image.rect());
93 }
94
95 painter->restore();
96}
97
98bool ImageItem::isNull() const
99{
100 return m_image.isNull();
101}
102
103int ImageItem::paintedWidth() const
104{
105 if (m_image.isNull()) {
106 return 0;
107 }
108
109 return m_paintedRect.width();
110}
111
112int ImageItem::paintedHeight() const
113{
114 if (m_image.isNull()) {
115 return 0;
116 }
117
118 return m_paintedRect.height();
119}
120
121int ImageItem::verticalPadding() const
122{
123 if (m_image.isNull()) {
124 return 0;
125 }
126
127 return (height() - m_paintedRect.height()) / 2;
128}
129
130int ImageItem::horizontalPadding() const
131{
132 if (m_image.isNull()) {
133 return 0;
134 }
135
136 return (width() - m_paintedRect.width()) / 2;
137}
138
139void ImageItem::updatePaintedRect()
140{
141 if (m_image.isNull()) {
142 return;
143 }
144
145 QRect sourceRect = m_paintedRect;
146
147 QRect destRect;
148
149 switch (m_fillMode) {
150 case PreserveAspectFit: {
151 QSize scaled = m_image.size();
152
153 scaled.scale(boundingRect().size().toSize(), Qt::KeepAspectRatio);
154 destRect = QRect(QPoint(0, 0), scaled);
155 destRect.moveCenter(boundingRect().center().toPoint());
156 break;
157 }
158 case PreserveAspectCrop: {
159 QSize scaled = m_image.size();
160
162 destRect = QRect(QPoint(0, 0), scaled);
163 destRect.moveCenter(boundingRect().center().toPoint());
164 break;
165 }
166 case TileVertically: {
167 destRect = boundingRect().toRect();
168 destRect.setWidth(destRect.width() / (width() / (qreal)m_image.width()));
169 break;
170 }
171 case TileHorizontally: {
172 destRect = boundingRect().toRect();
173 destRect.setHeight(destRect.height() / (height() / (qreal)m_image.height()));
174 break;
175 }
176 case Stretch:
177 case Tile:
178 default:
179 destRect = boundingRect().toRect();
180 }
181
182 if (destRect != sourceRect) {
183 m_paintedRect = destRect;
184 Q_EMIT paintedHeightChanged();
185 Q_EMIT paintedWidthChanged();
186 Q_EMIT verticalPaddingChanged();
187 Q_EMIT horizontalPaddingChanged();
188 }
189}
190
191void ImageItem::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
192{
193 QQuickPaintedItem::geometryChange(newGeometry, oldGeometry);
194 updatePaintedRect();
195}
196
197#include "moc_imageitem.cpp"
qreal devicePixelRatio() const const
int height() const const
bool isNull() const const
QRect rect() const const
QSize size() const const
int width() const const
Q_EMITQ_EMIT
void drawImage(const QPoint &point, const QImage &image)
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)
QPixmap fromImage(QImage &&image, Qt::ImageConversionFlags flags)
virtual QRectF boundingRect() const const
virtual void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
QSizeF size() const const
void update()
int height() const const
void moveCenter(const QPoint &position)
void setHeight(int height)
void setWidth(int width)
int width() const const
QRect toRect() const const
int height() const const
void scale(const QSize &size, Qt::AspectRatioMode mode)
int width() const const
KeepAspectRatio
QTextStream & center(QTextStream &stream)
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Fri Dec 27 2024 11:49:24 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.