1// Copyright (C) 2017 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qquickiconimage_p.h"
5#include "qquickiconimage_p_p.h"
6
7#include <QtCore/qmath.h>
8#include <QtQuick/private/qquickimagebase_p_p.h>
9
10QT_BEGIN_NAMESPACE
11
12QQuickIconImagePrivate::~QQuickIconImagePrivate()
13{
14 icon.entries.clear();
15}
16
17bool QQuickIconImagePrivate::updateDevicePixelRatio(qreal targetDevicePixelRatio)
18{
19 if (isThemeIcon) {
20 devicePixelRatio = effectiveDevicePixelRatio();
21 return true;
22 }
23
24 return QQuickImagePrivate::updateDevicePixelRatio(targetDevicePixelRatio);
25}
26
27void QQuickIconImage::load()
28{
29 Q_D(QQuickIconImage);
30 // Both geometryChange() and QQuickImageBase::sourceSizeChanged()
31 // (which we connect to load() in the constructor) can be called as a result
32 // of load() changing the various sizes, so we must check that we're not recursing.
33 if (d->updatingIcon)
34 return;
35
36 d->updatingIcon = true;
37
38 QSize size = d->sourcesize;
39 // If no size is specified for theme icons, it will use the smallest available size.
40 if (size.width() <= 0)
41 size.setWidth(width());
42 if (size.height() <= 0)
43 size.setHeight(height());
44
45 const qreal dpr = d->effectiveDevicePixelRatio();
46
47 if (const auto *entry = QIconLoaderEngine::entryForSize(info: d->icon, size: size * dpr, scale: qCeil(v: dpr))) {
48 // First, try to load an icon from the theme (index.theme).
49 QQmlContext *context = qmlContext(this);
50 const QUrl entryUrl = QUrl::fromLocalFile(localfile: entry->filename);
51 d->url = context ? context->resolvedUrl(entryUrl) : entryUrl;
52 d->isThemeIcon = true;
53 } else if (d->source.isEmpty()) {
54 // Then, try loading it through a platform icon engine.
55 std::unique_ptr<QIconEngine> iconEngine(QIconLoader::instance()->iconEngine(iconName: d->icon.iconName));
56 if (iconEngine && !iconEngine->isNull()) {
57 // ### TODO that's the best we can do for now to select different pixmaps based on the
58 // QuickItem's state. QQuickIconImage cannot know about the state of the control that
59 // uses it without adding more properties that are then synced up with the control.
60 const QIcon::Mode mode = isEnabled() ? QIcon::Normal : QIcon::Disabled;
61 const QImage image = iconEngine->scaledPixmap(size, mode, state: QIcon::Off, scale: dpr).toImage();
62 d->devicePixelRatio = image.devicePixelRatio();
63 d->setImage(image);
64 }
65 } else {
66 // Either we failed to load a named icon or the source was set instead; use that.
67 d->url = d->source;
68 d->isThemeIcon = false;
69 }
70 if (!d->url.isEmpty())
71 QQuickImage::load();
72
73 d->updatingIcon = false;
74}
75
76void QQuickIconImagePrivate::updateFillMode()
77{
78 Q_Q(QQuickIconImage);
79 // If we start with a sourceSize of 28x28 and then set sourceSize.width to 24, the fillMode
80 // will change to PreserveAspectFit (because pixmapSize.width() > width()), which causes the
81 // pixmap to be reloaded at its original size of 28x28, which causes the fillMode to change
82 // to Pad (because pixmapSize.width() <= width()), and so on.
83 if (updatingFillMode)
84 return;
85
86 updatingFillMode = true;
87
88 const QSize pixmapSize = QSize(currentPix->width(), currentPix->height()) / effectiveDevicePixelRatio();
89 if (pixmapSize.width() > q->width() || pixmapSize.height() > q->height())
90 q->setFillMode(QQuickImage::PreserveAspectFit);
91 else
92 q->setFillMode(QQuickImage::Pad);
93
94 updatingFillMode = false;
95}
96
97QQuickIconImage::QQuickIconImage(QQuickItem *parent)
98 : QQuickImage(*(new QQuickIconImagePrivate), parent)
99{
100 setFillMode(Pad);
101}
102
103QString QQuickIconImage::name() const
104{
105 Q_D(const QQuickIconImage);
106 return d->icon.iconName;
107}
108
109void QQuickIconImage::setName(const QString &name)
110{
111 Q_D(QQuickIconImage);
112 if (d->icon.iconName == name)
113 return;
114
115 d->icon.entries.clear();
116 d->icon = QIconLoader::instance()->loadIcon(iconName: name);
117 if (d->icon.iconName.isEmpty())
118 d->icon.iconName = name;
119 if (isComponentComplete())
120 load();
121 emit nameChanged();
122}
123
124QColor QQuickIconImage::color() const
125{
126 Q_D(const QQuickIconImage);
127 return d->color;
128}
129
130void QQuickIconImage::setColor(const QColor &color)
131{
132 Q_D(QQuickIconImage);
133 if (d->color == color)
134 return;
135
136 d->color = color;
137 if (isComponentComplete())
138 load();
139 emit colorChanged();
140}
141
142void QQuickIconImage::setSource(const QUrl &source)
143{
144 Q_D(QQuickIconImage);
145 if (d->source == source)
146 return;
147
148 d->source = source;
149 if (isComponentComplete())
150 load();
151 emit sourceChanged(source);
152}
153
154void QQuickIconImage::snapPositionTo(QPointF pos)
155{
156 // Ensure that we are placed on an integer position relative to the owning control. We assume
157 // that there is exactly one intermediate parent item between us and the control (in practice,
158 // the contentItem). The idea is to enable the app, by placing the controls at integer
159 // positions, to avoid the image being smoothed over pixel boundaries in the basic, DPR=1 case.
160 QPointF offset;
161 if (parentItem())
162 offset = parentItem()->position();
163 QPointF offsetPos(std::round(x: offset.x() + pos.x()), std::round(x: offset.y() + pos.y()));
164 setPosition(offsetPos - offset);
165}
166
167void QQuickIconImage::componentComplete()
168{
169 QQuickImage::componentComplete();
170 load();
171}
172
173void QQuickIconImage::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
174{
175 QQuickImage::geometryChange(newGeometry, oldGeometry);
176 if (isComponentComplete() && newGeometry.size() != oldGeometry.size())
177 load();
178}
179
180void QQuickIconImage::pixmapChange()
181{
182 Q_D(QQuickIconImage);
183 QQuickImage::pixmapChange();
184 d->updateFillMode();
185
186 // Don't apply the color if we're recursing (updateFillMode() can cause us to recurse).
187 if (!d->updatingFillMode && d->color.alpha() > 0) {
188 QImage image = d->currentPix->image();
189 if (!image.isNull()) {
190 QPainter painter(&image);
191 painter.setCompositionMode(QPainter::CompositionMode_SourceIn);
192 painter.fillRect(image.rect(), color: d->color);
193 d->currentPix->setImage(image);
194 }
195 }
196}
197
198QT_END_NAMESPACE
199
200#include "moc_qquickiconimage_p.cpp"
201

source code of qtdeclarative/src/quickcontrolsimpl/qquickiconimage.cpp