1 | // Copyright (C) 2016 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 "qiconengine.h" |
5 | #include "qiconengine_p.h" |
6 | #include "qpainter.h" |
7 | |
8 | QT_BEGIN_NAMESPACE |
9 | |
10 | /*! |
11 | \class QIconEngine |
12 | |
13 | \brief The QIconEngine class provides an abstract base class for QIcon renderers. |
14 | |
15 | \ingroup painting |
16 | \inmodule QtGui |
17 | |
18 | An icon engine provides the rendering functions for a QIcon. Each icon has a |
19 | corresponding icon engine that is responsible for drawing the icon with a |
20 | requested size, mode and state. |
21 | |
22 | The icon is rendered by the paint() function, and the icon can additionally be |
23 | obtained as a pixmap with the pixmap() function (the default implementation |
24 | simply uses paint() to achieve this). The addPixmap() function can be used to |
25 | add new pixmaps to the icon engine, and is used by QIcon to add specialized |
26 | custom pixmaps. |
27 | |
28 | The paint(), pixmap(), and addPixmap() functions are all virtual, and can |
29 | therefore be reimplemented in subclasses of QIconEngine. |
30 | |
31 | \sa QIconEnginePlugin |
32 | |
33 | */ |
34 | |
35 | /*! |
36 | \fn virtual void QIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) = 0; |
37 | |
38 | Uses the given \a painter to paint the icon with the required \a mode and |
39 | \a state into the rectangle \a rect. |
40 | */ |
41 | |
42 | /*! Returns the actual size of the icon the engine provides for the |
43 | requested \a size, \a mode and \a state. The default implementation |
44 | returns the given \a size. |
45 | |
46 | The returned size is in device-independent pixels (This |
47 | is relevant for high-dpi pixmaps). |
48 | */ |
49 | QSize QIconEngine::actualSize(const QSize &size, QIcon::Mode /*mode*/, QIcon::State /*state*/) |
50 | { |
51 | return size; |
52 | } |
53 | |
54 | /*! |
55 | \since 5.6 |
56 | Constructs the icon engine. |
57 | */ |
58 | QIconEngine::QIconEngine() |
59 | { |
60 | } |
61 | |
62 | /*! |
63 | \since 5.8 |
64 | \internal |
65 | */ |
66 | QIconEngine::QIconEngine(const QIconEngine &) |
67 | { |
68 | } |
69 | |
70 | /*! |
71 | Destroys the icon engine. |
72 | */ |
73 | QIconEngine::~QIconEngine() |
74 | { |
75 | } |
76 | |
77 | |
78 | /*! |
79 | Returns the icon as a pixmap with the required \a size, \a mode, |
80 | and \a state. The default implementation creates a new pixmap and |
81 | calls paint() to fill it. |
82 | */ |
83 | QPixmap QIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) |
84 | { |
85 | QPixmap pm(size); |
86 | { |
87 | QPainter p(&pm); |
88 | paint(painter: &p, rect: QRect(QPoint(0,0),size), mode, state); |
89 | } |
90 | return pm; |
91 | } |
92 | |
93 | /*! |
94 | Called by QIcon::addPixmap(). Adds a specialized \a pixmap for the given |
95 | \a mode and \a state. The default pixmap-based engine stores any supplied |
96 | pixmaps, and it uses them instead of scaled pixmaps if the size of a pixmap |
97 | matches the size of icon requested. Custom icon engines that implement |
98 | scalable vector formats are free to ignores any extra pixmaps. |
99 | */ |
100 | void QIconEngine::addPixmap(const QPixmap &/*pixmap*/, QIcon::Mode /*mode*/, QIcon::State /*state*/) |
101 | { |
102 | } |
103 | |
104 | |
105 | /*! Called by QIcon::addFile(). Adds a specialized pixmap from the |
106 | file with the given \a fileName, \a size, \a mode and \a state. The |
107 | default pixmap-based engine stores any supplied file names, and it |
108 | loads the pixmaps on demand instead of using scaled pixmaps if the |
109 | size of a pixmap matches the size of icon requested. Custom icon |
110 | engines that implement scalable vector formats are free to ignores |
111 | any extra files. |
112 | */ |
113 | void QIconEngine::addFile(const QString &/*fileName*/, const QSize &/*size*/, QIcon::Mode /*mode*/, QIcon::State /*state*/) |
114 | { |
115 | } |
116 | |
117 | |
118 | /*! |
119 | \enum QIconEngine::IconEngineHook |
120 | |
121 | These enum values are used for virtual_hook() to allow additional |
122 | queries to icon engine without breaking binary compatibility. |
123 | |
124 | \value IsNullHook Allow to query if this engine represents a null |
125 | icon. The \a data argument of the virtual_hook() is a pointer to a |
126 | bool that can be set to true if the icon is null. This enum value |
127 | was added in Qt 5.7. |
128 | |
129 | \value ScaledPixmapHook Provides a way to get a pixmap that is scaled |
130 | according to the given scale (typically equal to the \l {High |
131 | DPI}{device pixel ratio}). The \a data argument of the virtual_hook() |
132 | function is a \l ScaledPixmapArgument pointer that contains both the input and |
133 | output arguments. This enum value was added in Qt 5.9. |
134 | |
135 | \sa virtual_hook() |
136 | */ |
137 | |
138 | /*! |
139 | \class QIconEngine::ScaledPixmapArgument |
140 | \since 5.9 |
141 | |
142 | \inmodule QtGui |
143 | |
144 | This struct represents arguments to the virtual_hook() function when |
145 | the \a id parameter is QIconEngine::ScaledPixmapHook. |
146 | |
147 | The struct provides a way for icons created via \l QIcon::fromTheme() |
148 | to return pixmaps that are designed for the current \l {High |
149 | DPI}{device pixel ratio}. The scale for such an icon is specified |
150 | using the \l {Icon Theme Specification - Directory Layout}{Scale directory key} |
151 | in the appropriate \c index.theme file. |
152 | |
153 | Icons created via other approaches will return the same result as a call to |
154 | \l pixmap() would, and continue to benefit from Qt's \l {High Resolution |
155 | Versions of Images}{"@nx" high DPI syntax}. |
156 | |
157 | \sa virtual_hook(), QIconEngine::IconEngineHook, {High DPI Icons} |
158 | */ |
159 | |
160 | /*! |
161 | \variable QIconEngine::ScaledPixmapArgument::size |
162 | \brief The requested size of the pixmap. |
163 | */ |
164 | |
165 | /*! |
166 | \variable QIconEngine::ScaledPixmapArgument::mode |
167 | \brief The requested mode of the pixmap. |
168 | |
169 | \sa QIcon::Mode |
170 | */ |
171 | |
172 | /*! |
173 | \variable QIconEngine::ScaledPixmapArgument::state |
174 | \brief The requested state of the pixmap. |
175 | |
176 | \sa QIcon::State |
177 | */ |
178 | |
179 | /*! |
180 | \variable QIconEngine::ScaledPixmapArgument::scale |
181 | \brief The requested scale of the pixmap. |
182 | */ |
183 | |
184 | /*! |
185 | \variable QIconEngine::ScaledPixmapArgument::pixmap |
186 | |
187 | \brief The pixmap that is the best match for the given \l size, \l mode, \l |
188 | state, and \l scale. This is an output parameter that is set after calling |
189 | \l virtual_hook(). |
190 | */ |
191 | |
192 | |
193 | /*! |
194 | Returns a key that identifies this icon engine. |
195 | */ |
196 | QString QIconEngine::key() const |
197 | { |
198 | return QString(); |
199 | } |
200 | |
201 | /*! \fn QIconEngine *QIconEngine::clone() const |
202 | |
203 | Reimplement this method to return a clone of this icon engine. |
204 | */ |
205 | |
206 | /*! |
207 | Reads icon engine contents from the QDataStream \a in. Returns |
208 | true if the contents were read; otherwise returns \c false. |
209 | |
210 | QIconEngine's default implementation always return false. |
211 | */ |
212 | bool QIconEngine::read(QDataStream &) |
213 | { |
214 | return false; |
215 | } |
216 | |
217 | /*! |
218 | Writes the contents of this engine to the QDataStream \a out. |
219 | Returns \c true if the contents were written; otherwise returns \c false. |
220 | |
221 | QIconEngine's default implementation always return false. |
222 | */ |
223 | bool QIconEngine::write(QDataStream &) const |
224 | { |
225 | return false; |
226 | } |
227 | |
228 | /*! |
229 | Additional method to allow extending QIconEngine without |
230 | adding new virtual methods (and without breaking binary compatibility). |
231 | The actual action and format of \a data depends on \a id argument |
232 | which is in fact a constant from IconEngineHook enum. |
233 | |
234 | \sa IconEngineHook |
235 | */ |
236 | void QIconEngine::virtual_hook(int id, void *data) |
237 | { |
238 | switch (id) { |
239 | case QIconEngine::ScaledPixmapHook: { |
240 | QIconEngine::ScaledPixmapArgument &arg = |
241 | *reinterpret_cast<QIconEngine::ScaledPixmapArgument*>(data); |
242 | // try to get a pixmap with the correct size, the dpr is adjusted later on |
243 | arg.pixmap = pixmap(size: arg.size * arg.scale, mode: arg.mode, state: arg.state); |
244 | break; |
245 | } |
246 | default: |
247 | break; |
248 | } |
249 | } |
250 | |
251 | /*! |
252 | Returns sizes of all images that are contained in the engine for the |
253 | specific \a mode and \a state. |
254 | */ |
255 | QList<QSize> QIconEngine::availableSizes(QIcon::Mode /*mode*/, QIcon::State /*state*/) |
256 | { |
257 | return {}; |
258 | } |
259 | |
260 | /*! |
261 | Returns the name used to create the engine, if available. |
262 | */ |
263 | QString QIconEngine::iconName() |
264 | { |
265 | return QString(); |
266 | } |
267 | |
268 | /*! |
269 | \since 5.7 |
270 | |
271 | Returns true if this icon engine represent a null QIcon. |
272 | |
273 | \include qiconengine-virtualhookhelper.qdocinc |
274 | */ |
275 | bool QIconEngine::isNull() |
276 | { |
277 | bool isNull = false; |
278 | virtual_hook(id: QIconEngine::IsNullHook, data: &isNull); |
279 | return isNull; |
280 | } |
281 | |
282 | /*! |
283 | \since 5.9 |
284 | |
285 | Returns a pixmap for the given \a size, \a mode, \a state and \a scale. |
286 | |
287 | The \a scale argument is typically equal to the \l {High DPI} |
288 | {device pixel ratio} of the display. The size is given in device-independent pixels. |
289 | |
290 | \include qiconengine-virtualhookhelper.qdocinc |
291 | |
292 | \note Some engines may cast \a scale to an integer. |
293 | |
294 | \sa ScaledPixmapArgument |
295 | */ |
296 | QPixmap QIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale) |
297 | { |
298 | ScaledPixmapArgument arg; |
299 | arg.size = size; |
300 | arg.mode = mode; |
301 | arg.state = state; |
302 | arg.scale = scale; |
303 | const_cast<QIconEngine *>(this)->virtual_hook(id: QIconEngine::ScaledPixmapHook, data: reinterpret_cast<void*>(&arg)); |
304 | return arg.pixmap; |
305 | } |
306 | |
307 | |
308 | // ------- QProxyIconEngine ----- |
309 | |
310 | void QProxyIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state) |
311 | { |
312 | proxiedEngine()->paint(painter, rect, mode, state); |
313 | } |
314 | |
315 | QSize QProxyIconEngine::actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state) |
316 | { |
317 | return proxiedEngine()->actualSize(size, mode, state); |
318 | } |
319 | |
320 | QPixmap QProxyIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) |
321 | { |
322 | return proxiedEngine()->pixmap(size, mode, state); |
323 | } |
324 | |
325 | void QProxyIconEngine::addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state) |
326 | { |
327 | proxiedEngine()->addPixmap(pixmap, mode, state); |
328 | } |
329 | |
330 | void QProxyIconEngine::addFile(const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state) |
331 | { |
332 | proxiedEngine()->addFile(fileName, size, mode, state); |
333 | } |
334 | |
335 | QString QProxyIconEngine::key() const |
336 | { |
337 | return proxiedEngine()->key(); |
338 | } |
339 | |
340 | QIconEngine *QProxyIconEngine::clone() const |
341 | { |
342 | return proxiedEngine()->clone(); |
343 | } |
344 | |
345 | bool QProxyIconEngine::read(QDataStream &in) |
346 | { |
347 | return proxiedEngine()->read(in); |
348 | } |
349 | |
350 | bool QProxyIconEngine::write(QDataStream &out) const |
351 | { |
352 | return proxiedEngine()->write(out); |
353 | } |
354 | |
355 | QList<QSize> QProxyIconEngine::availableSizes(QIcon::Mode mode, QIcon::State state) |
356 | { |
357 | return proxiedEngine()->availableSizes(mode, state); |
358 | } |
359 | |
360 | QString QProxyIconEngine::iconName() |
361 | { |
362 | return proxiedEngine()->iconName(); |
363 | } |
364 | |
365 | bool QProxyIconEngine::isNull() |
366 | { |
367 | return proxiedEngine()->isNull(); |
368 | } |
369 | |
370 | QPixmap QProxyIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale) |
371 | { |
372 | return proxiedEngine()->scaledPixmap(size, mode, state, scale); |
373 | } |
374 | |
375 | void QProxyIconEngine::virtual_hook(int id, void *data) |
376 | { |
377 | proxiedEngine()->virtual_hook(id, data); |
378 | } |
379 | |
380 | |
381 | QT_END_NAMESPACE |
382 | |