1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2017 The Qt Company Ltd. |
4 | ** Contact: http://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the Qt Quick Templates 2 module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL3$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see http://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at http://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPLv3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or later as published by the Free |
28 | ** Software Foundation and appearing in the file LICENSE.GPL included in |
29 | ** the packaging of this file. Please review the following information to |
30 | ** ensure the GNU General Public License version 2.0 requirements will be |
31 | ** met: http://www.gnu.org/licenses/gpl-2.0.html. |
32 | ** |
33 | ** $QT_END_LICENSE$ |
34 | ** |
35 | ****************************************************************************/ |
36 | |
37 | #include "qquickicon_p.h" |
38 | |
39 | QT_BEGIN_NAMESPACE |
40 | |
41 | class QQuickIconPrivate : public QSharedData |
42 | { |
43 | public: |
44 | QString name; |
45 | QUrl source; |
46 | int width = 0; |
47 | int height = 0; |
48 | QColor color = Qt::transparent; |
49 | bool cache = true; |
50 | |
51 | enum ResolveProperties { |
52 | NameResolved = 0x0001, |
53 | SourceResolved = 0x0002, |
54 | WidthResolved = 0x0004, |
55 | HeightResolved = 0x0008, |
56 | ColorResolved = 0x0010, |
57 | CacheResolved = 0x0020, |
58 | AllPropertiesResolved = 0x1ffff |
59 | }; |
60 | |
61 | // This is based on QFont's resolve_mask. |
62 | int resolveMask = 0; |
63 | }; |
64 | |
65 | QQuickIcon::QQuickIcon() |
66 | : d(new QQuickIconPrivate) |
67 | { |
68 | } |
69 | |
70 | QQuickIcon::QQuickIcon(const QQuickIcon &other) |
71 | : d(other.d) |
72 | { |
73 | } |
74 | |
75 | QQuickIcon::~QQuickIcon() |
76 | { |
77 | } |
78 | |
79 | QQuickIcon &QQuickIcon::operator=(const QQuickIcon &other) |
80 | { |
81 | d = other.d; |
82 | return *this; |
83 | } |
84 | |
85 | bool QQuickIcon::operator==(const QQuickIcon &other) const |
86 | { |
87 | return d == other.d || (d->name == other.d->name |
88 | && d->source == other.d->source |
89 | && d->width == other.d->width |
90 | && d->height == other.d->height |
91 | && d->color == other.d->color |
92 | && d->cache == other.d->cache); |
93 | } |
94 | |
95 | bool QQuickIcon::operator!=(const QQuickIcon &other) const |
96 | { |
97 | return !(*this == other); |
98 | } |
99 | |
100 | bool QQuickIcon::isEmpty() const |
101 | { |
102 | return d->name.isEmpty() && d->source.isEmpty(); |
103 | } |
104 | |
105 | QString QQuickIcon::name() const |
106 | { |
107 | return d->name; |
108 | } |
109 | |
110 | void QQuickIcon::setName(const QString &name) |
111 | { |
112 | if ((d->resolveMask & QQuickIconPrivate::NameResolved) && d->name == name) |
113 | return; |
114 | |
115 | d.detach(); |
116 | d->name = name; |
117 | d->resolveMask |= QQuickIconPrivate::NameResolved; |
118 | } |
119 | |
120 | void QQuickIcon::resetName() |
121 | { |
122 | d.detach(); |
123 | d->name = QString(); |
124 | d->resolveMask &= ~QQuickIconPrivate::NameResolved; |
125 | } |
126 | |
127 | QUrl QQuickIcon::source() const |
128 | { |
129 | return d->source; |
130 | } |
131 | |
132 | void QQuickIcon::setSource(const QUrl &source) |
133 | { |
134 | if ((d->resolveMask & QQuickIconPrivate::SourceResolved) && d->source == source) |
135 | return; |
136 | |
137 | d.detach(); |
138 | d->source = source; |
139 | d->resolveMask |= QQuickIconPrivate::SourceResolved; |
140 | } |
141 | |
142 | void QQuickIcon::resetSource() |
143 | { |
144 | d.detach(); |
145 | d->source = QString(); |
146 | d->resolveMask &= ~QQuickIconPrivate::SourceResolved; |
147 | } |
148 | |
149 | int QQuickIcon::width() const |
150 | { |
151 | return d->width; |
152 | } |
153 | |
154 | void QQuickIcon::setWidth(int width) |
155 | { |
156 | if ((d->resolveMask & QQuickIconPrivate::WidthResolved) && d->width == width) |
157 | return; |
158 | |
159 | d.detach(); |
160 | d->width = width; |
161 | d->resolveMask |= QQuickIconPrivate::WidthResolved; |
162 | } |
163 | |
164 | void QQuickIcon::resetWidth() |
165 | { |
166 | d.detach(); |
167 | d->width = 0; |
168 | d->resolveMask &= ~QQuickIconPrivate::WidthResolved; |
169 | } |
170 | |
171 | int QQuickIcon::height() const |
172 | { |
173 | return d->height; |
174 | } |
175 | |
176 | void QQuickIcon::setHeight(int height) |
177 | { |
178 | if ((d->resolveMask & QQuickIconPrivate::HeightResolved) && d->height == height) |
179 | return; |
180 | |
181 | d.detach(); |
182 | d->height = height; |
183 | d->resolveMask |= QQuickIconPrivate::HeightResolved; |
184 | } |
185 | |
186 | void QQuickIcon::resetHeight() |
187 | { |
188 | d.detach(); |
189 | d->height = 0; |
190 | d->resolveMask &= ~QQuickIconPrivate::HeightResolved; |
191 | } |
192 | |
193 | QColor QQuickIcon::color() const |
194 | { |
195 | return d->color; |
196 | } |
197 | |
198 | void QQuickIcon::setColor(const QColor &color) |
199 | { |
200 | if ((d->resolveMask & QQuickIconPrivate::ColorResolved) && d->color == color) |
201 | return; |
202 | |
203 | d.detach(); |
204 | d->color = color; |
205 | d->resolveMask |= QQuickIconPrivate::ColorResolved; |
206 | } |
207 | |
208 | void QQuickIcon::resetColor() |
209 | { |
210 | d.detach(); |
211 | d->color = Qt::transparent; |
212 | d->resolveMask &= ~QQuickIconPrivate::ColorResolved; |
213 | } |
214 | |
215 | bool QQuickIcon::cache() const |
216 | { |
217 | return d->cache; |
218 | } |
219 | |
220 | void QQuickIcon::setCache(bool cache) |
221 | { |
222 | if ((d->resolveMask & QQuickIconPrivate::CacheResolved) && d->cache == cache) |
223 | return; |
224 | |
225 | d.detach(); |
226 | d->cache = cache; |
227 | d->resolveMask |= QQuickIconPrivate::CacheResolved; |
228 | } |
229 | |
230 | void QQuickIcon::resetCache() |
231 | { |
232 | d.detach(); |
233 | d->cache = true; |
234 | d->resolveMask &= ~QQuickIconPrivate::CacheResolved; |
235 | } |
236 | |
237 | QQuickIcon QQuickIcon::resolve(const QQuickIcon &other) const |
238 | { |
239 | QQuickIcon resolved = *this; |
240 | resolved.d.detach(); |
241 | |
242 | if (!(d->resolveMask & QQuickIconPrivate::NameResolved)) |
243 | resolved.d->name = other.d->name; |
244 | |
245 | if (!(d->resolveMask & QQuickIconPrivate::SourceResolved)) |
246 | resolved.d->source = other.d->source; |
247 | |
248 | if (!(d->resolveMask & QQuickIconPrivate::WidthResolved)) |
249 | resolved.d->width = other.d->width; |
250 | |
251 | if (!(d->resolveMask & QQuickIconPrivate::HeightResolved)) |
252 | resolved.d->height = other.d->height; |
253 | |
254 | if (!(d->resolveMask & QQuickIconPrivate::ColorResolved)) |
255 | resolved.d->color = other.d->color; |
256 | |
257 | if (!(d->resolveMask & QQuickIconPrivate::CacheResolved)) |
258 | resolved.d->cache = other.d->cache; |
259 | |
260 | return resolved; |
261 | } |
262 | |
263 | QT_END_NAMESPACE |
264 | |