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 "qquickicon_p.h"
5
6#include <private/qqmlcontextdata_p.h>
7#include <private/qqmldata_p.h>
8
9QT_BEGIN_NAMESPACE
10
11class QQuickIconPrivate : public QSharedData
12{
13public:
14 // This is based on QFont's resolve_mask.
15 enum ResolveProperties {
16 NameResolved = 0x0001,
17 SourceResolved = 0x0002,
18 WidthResolved = 0x0004,
19 HeightResolved = 0x0008,
20 ColorResolved = 0x0010,
21 CacheResolved = 0x0020,
22 AllPropertiesResolved = 0x1ffff
23 };
24 int resolveMask = 0;
25
26 QString name;
27 QUrl source;
28 QUrl resolvedSource;
29 int width = 0;
30 int height = 0;
31 QColor color = Qt::transparent;
32 bool cache = true;
33};
34
35QQuickIcon::QQuickIcon()
36 : d(new QQuickIconPrivate)
37{
38}
39
40QQuickIcon::QQuickIcon(const QQuickIcon &other)
41 : d(other.d)
42{
43}
44
45QQuickIcon::~QQuickIcon()
46{
47}
48
49QQuickIcon &QQuickIcon::operator=(const QQuickIcon &other)
50{
51 d = other.d;
52 return *this;
53}
54
55bool QQuickIcon::operator==(const QQuickIcon &other) const
56{
57 return d == other.d || (d->name == other.d->name
58 && d->source == other.d->source
59 && d->resolvedSource == other.d->resolvedSource
60 && d->width == other.d->width
61 && d->height == other.d->height
62 && d->color == other.d->color
63 && d->cache == other.d->cache);
64}
65
66bool QQuickIcon::operator!=(const QQuickIcon &other) const
67{
68 return !(*this == other);
69}
70
71bool QQuickIcon::isEmpty() const
72{
73 return d->name.isEmpty() && d->source.isEmpty();
74}
75
76QString QQuickIcon::name() const
77{
78 return d->name;
79}
80
81void QQuickIcon::setName(const QString &name)
82{
83 if ((d->resolveMask & QQuickIconPrivate::NameResolved) && d->name == name)
84 return;
85
86 d.detach();
87 d->name = name;
88 d->resolveMask |= QQuickIconPrivate::NameResolved;
89}
90
91void QQuickIcon::resetName()
92{
93 d.detach();
94 d->name = QString();
95 d->resolveMask &= ~QQuickIconPrivate::NameResolved;
96}
97
98QUrl QQuickIcon::source() const
99{
100 return d->source;
101}
102
103void QQuickIcon::setSource(const QUrl &source)
104{
105 if ((d->resolveMask & QQuickIconPrivate::SourceResolved) && d->source == source)
106 return;
107
108 d.detach();
109 d->source = source;
110 d->resolvedSource.clear();
111 d->resolveMask |= QQuickIconPrivate::SourceResolved;
112}
113
114void QQuickIcon::resetSource()
115{
116 d.detach();
117 d->source = QString();
118 d->resolvedSource.clear();
119 d->resolveMask &= ~QQuickIconPrivate::SourceResolved;
120}
121
122QUrl QQuickIcon::resolvedSource() const
123{
124 return d->resolvedSource.isEmpty() ? d->source : d->resolvedSource;
125}
126
127// must be called by the property owner (e.g. Button) prior to emitting changed signal.
128void QQuickIcon::ensureRelativeSourceResolved(const QObject *owner)
129{
130 if (d->source.isEmpty())
131 return;
132 if (!d->resolvedSource.isEmpty())
133 return; // already resolved relative to (possibly) different owner
134 const QQmlData *data = QQmlData::get(object: owner);
135 if (!data || !data->outerContext)
136 return;
137 d.detach();
138 d->resolvedSource = data->outerContext->resolvedUrl(d->source);
139}
140
141int QQuickIcon::width() const
142{
143 return d->width;
144}
145
146void QQuickIcon::setWidth(int width)
147{
148 if ((d->resolveMask & QQuickIconPrivate::WidthResolved) && d->width == width)
149 return;
150
151 d.detach();
152 d->width = width;
153 d->resolveMask |= QQuickIconPrivate::WidthResolved;
154}
155
156void QQuickIcon::resetWidth()
157{
158 d.detach();
159 d->width = 0;
160 d->resolveMask &= ~QQuickIconPrivate::WidthResolved;
161}
162
163int QQuickIcon::height() const
164{
165 return d->height;
166}
167
168void QQuickIcon::setHeight(int height)
169{
170 if ((d->resolveMask & QQuickIconPrivate::HeightResolved) && d->height == height)
171 return;
172
173 d.detach();
174 d->height = height;
175 d->resolveMask |= QQuickIconPrivate::HeightResolved;
176}
177
178void QQuickIcon::resetHeight()
179{
180 d.detach();
181 d->height = 0;
182 d->resolveMask &= ~QQuickIconPrivate::HeightResolved;
183}
184
185QColor QQuickIcon::color() const
186{
187 return d->color;
188}
189
190void QQuickIcon::setColor(const QColor &color)
191{
192 if ((d->resolveMask & QQuickIconPrivate::ColorResolved) && d->color == color)
193 return;
194
195 d.detach();
196 d->color = color;
197 d->resolveMask |= QQuickIconPrivate::ColorResolved;
198}
199
200void QQuickIcon::resetColor()
201{
202 d.detach();
203 d->color = Qt::transparent;
204 d->resolveMask &= ~QQuickIconPrivate::ColorResolved;
205}
206
207bool QQuickIcon::cache() const
208{
209 return d->cache;
210}
211
212void QQuickIcon::setCache(bool cache)
213{
214 if ((d->resolveMask & QQuickIconPrivate::CacheResolved) && d->cache == cache)
215 return;
216
217 d.detach();
218 d->cache = cache;
219 d->resolveMask |= QQuickIconPrivate::CacheResolved;
220}
221
222void QQuickIcon::resetCache()
223{
224 d.detach();
225 d->cache = true;
226 d->resolveMask &= ~QQuickIconPrivate::CacheResolved;
227}
228
229QQuickIcon QQuickIcon::resolve(const QQuickIcon &other) const
230{
231 QQuickIcon resolved = *this;
232 resolved.d.detach();
233
234 if (!(d->resolveMask & QQuickIconPrivate::NameResolved))
235 resolved.d->name = other.d->name;
236
237 if (!(d->resolveMask & QQuickIconPrivate::SourceResolved)) {
238 resolved.d->source = other.d->source;
239 resolved.d->resolvedSource = other.d->resolvedSource;
240 }
241
242 if (!(d->resolveMask & QQuickIconPrivate::WidthResolved))
243 resolved.d->width = other.d->width;
244
245 if (!(d->resolveMask & QQuickIconPrivate::HeightResolved))
246 resolved.d->height = other.d->height;
247
248 if (!(d->resolveMask & QQuickIconPrivate::ColorResolved))
249 resolved.d->color = other.d->color;
250
251 if (!(d->resolveMask & QQuickIconPrivate::CacheResolved))
252 resolved.d->cache = other.d->cache;
253
254 return resolved;
255}
256
257QT_END_NAMESPACE
258
259#include "moc_qquickicon_p.cpp"
260

source code of qtdeclarative/src/quicktemplates/qquickicon.cpp