Coverage Report

Created: 2026-05-31 06:50

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