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