Coverage Report

Created: 2026-02-10 07:39

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
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
0
{
51
0
    return size;
52
0
}
53
54
/*!
55
    \since 5.6
56
    Constructs the icon engine.
57
 */
58
QIconEngine::QIconEngine()
59
0
{
60
0
}
61
62
/*!
63
    \since 5.8
64
    \internal
65
 */
66
QIconEngine::QIconEngine(const QIconEngine &)
67
0
{
68
0
}
69
70
/*!
71
  Destroys the icon engine.
72
 */
73
QIconEngine::~QIconEngine()
74
0
{
75
0
}
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
0
{
85
0
    QPixmap pm(size);
86
0
    {
87
0
        QPainter p(&pm);
88
0
        paint(&p, QRect(QPoint(0,0),size), mode, state);
89
0
    }
90
0
    return pm;
91
0
}
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
0
{
102
0
}
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
0
{
115
0
}
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
0
{
198
0
    return QString();
199
0
}
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
0
{
214
0
    return false;
215
0
}
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
0
{
225
0
    return false;
226
0
}
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
0
{
238
0
    switch (id) {
239
0
    case QIconEngine::ScaledPixmapHook: {
240
0
        QIconEngine::ScaledPixmapArgument &arg =
241
0
            *reinterpret_cast<QIconEngine::ScaledPixmapArgument*>(data);
242
        // try to get a pixmap with the correct size, the dpr is adjusted later on
243
0
        arg.pixmap = pixmap(arg.size * arg.scale, arg.mode, arg.state);
244
0
        break;
245
0
    }
246
0
    default:
247
0
        break;
248
0
    }
249
0
}
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
0
{
257
0
    return {};
258
0
}
259
260
/*!
261
    Returns the name used to create the engine, if available.
262
 */
263
QString QIconEngine::iconName()
264
0
{
265
0
    return QString();
266
0
}
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
0
{
277
0
    bool isNull = false;
278
0
    virtual_hook(QIconEngine::IsNullHook, &isNull);
279
0
    return isNull;
280
0
}
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
0
{
298
0
    ScaledPixmapArgument arg;
299
0
    arg.size = size;
300
0
    arg.mode = mode;
301
0
    arg.state = state;
302
0
    arg.scale = scale;
303
0
    const_cast<QIconEngine *>(this)->virtual_hook(QIconEngine::ScaledPixmapHook, reinterpret_cast<void*>(&arg));
304
0
    return arg.pixmap;
305
0
}
306
307
308
// ------- QProxyIconEngine -----
309
310
void QProxyIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
311
0
{
312
0
    proxiedEngine()->paint(painter, rect, mode, state);
313
0
}
314
315
QSize QProxyIconEngine::actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state)
316
0
{
317
0
    return proxiedEngine()->actualSize(size, mode, state);
318
0
}
319
320
QPixmap QProxyIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
321
0
{
322
0
    return proxiedEngine()->pixmap(size, mode, state);
323
0
}
324
325
void QProxyIconEngine::addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state)
326
0
{
327
0
    proxiedEngine()->addPixmap(pixmap, mode, state);
328
0
}
329
330
void QProxyIconEngine::addFile(const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state)
331
0
{
332
0
    proxiedEngine()->addFile(fileName, size, mode, state);
333
0
}
334
335
QString QProxyIconEngine::key() const
336
0
{
337
0
    return proxiedEngine()->key();
338
0
}
339
340
QIconEngine *QProxyIconEngine::clone() const
341
0
{
342
0
    return proxiedEngine()->clone();
343
0
}
344
345
bool QProxyIconEngine::read(QDataStream &in)
346
0
{
347
0
    return proxiedEngine()->read(in);
348
0
}
349
350
bool QProxyIconEngine::write(QDataStream &out) const
351
0
{
352
0
    return proxiedEngine()->write(out);
353
0
}
354
355
QList<QSize> QProxyIconEngine::availableSizes(QIcon::Mode mode, QIcon::State state)
356
0
{
357
0
    return proxiedEngine()->availableSizes(mode, state);
358
0
}
359
360
QString QProxyIconEngine::iconName()
361
0
{
362
0
    return proxiedEngine()->iconName();
363
0
}
364
365
bool QProxyIconEngine::isNull()
366
0
{
367
0
    return proxiedEngine()->isNull();
368
0
}
369
370
QPixmap QProxyIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale)
371
0
{
372
0
    return proxiedEngine()->scaledPixmap(size, mode, state, scale);
373
0
}
374
375
void QProxyIconEngine::virtual_hook(int id, void *data)
376
0
{
377
0
    proxiedEngine()->virtual_hook(id, data);
378
0
}
379
380
381
QT_END_NAMESPACE