Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qiconengine.cpp
Go to the documentation of this file.
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"
7#include "qpainter.h"
8
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 */
50QSize QIconEngine::actualSize(const QSize &size, QIcon::Mode /*mode*/, QIcon::State /*state*/)
51{
52 return size;
53}
54
55/*!
56 \since 5.6
57 Constructs the icon engine.
58 */
59QIconEngine::QIconEngine()
60{
61}
62
63/*!
64 \since 5.8
65 \internal
66 */
67QIconEngine::QIconEngine(const QIconEngine &)
68{
69}
70
71/*!
72 Destroys the icon engine.
73 */
74QIconEngine::~QIconEngine()
75{
76}
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*/
84QPixmap QIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
85{
86 QPixmap pm(size);
87 {
88 QPainter p(&pm);
89 paint(&p, QRect(QPoint(0,0),size), mode, state);
90 }
91 return pm;
92}
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 */
101void QIconEngine::addPixmap(const QPixmap &/*pixmap*/, QIcon::Mode /*mode*/, QIcon::State /*state*/)
102{
103}
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 */
114void QIconEngine::addFile(const QString &/*fileName*/, const QSize &/*size*/, QIcon::Mode /*mode*/, QIcon::State /*state*/)
115{
116}
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 */
197QString QIconEngine::key() const
198{
199 return QString();
200}
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 */
213bool QIconEngine::read(QDataStream &)
214{
215 return false;
216}
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 */
224bool QIconEngine::write(QDataStream &) const
225{
226 return false;
227}
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*/
237void QIconEngine::virtual_hook(int id, void *data)
238{
239 switch (id) {
240 case QIconEngine::ScaledPixmapHook: {
241 QIconEngine::ScaledPixmapArgument &arg =
242 *reinterpret_cast<QIconEngine::ScaledPixmapArgument*>(data);
243 // try to get a pixmap with the correct size, the dpr is adjusted later on
244 arg.pixmap = pixmap(arg.size * arg.scale, arg.mode, arg.state);
245 break;
246 }
247 default:
248 break;
249 }
250}
251
252/*!
253 Returns sizes of all images that are contained in the engine for the
254 specific \a mode and \a state.
255 */
256QList<QSize> QIconEngine::availableSizes(QIcon::Mode /*mode*/, QIcon::State /*state*/)
257{
258 return {};
259}
260
261/*!
262 Returns the name used to create the engine, if available.
263 */
264QString QIconEngine::iconName()
265{
266 return QString();
267}
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 */
276bool QIconEngine::isNull()
277{
278 bool isNull = false;
279 virtual_hook(QIconEngine::IsNullHook, &isNull);
280 return isNull;
281}
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*/
297QPixmap QIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale)
298{
299 ScaledPixmapArgument arg;
300 arg.size = size;
301 arg.mode = mode;
302 arg.state = state;
303 arg.scale = scale;
304 const_cast<QIconEngine *>(this)->virtual_hook(QIconEngine::ScaledPixmapHook, reinterpret_cast<void*>(&arg));
305 return arg.pixmap;
306}
307
308
309// ------- QProxyIconEngine -----
310
311void QProxyIconEngine::paint(QPainter *painter, const QRect &rect, QIcon::Mode mode, QIcon::State state)
312{
313 proxiedEngine()->paint(painter, rect, mode, state);
314}
315
316QSize QProxyIconEngine::actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state)
317{
318 return proxiedEngine()->actualSize(size, mode, state);
319}
320
321QPixmap QProxyIconEngine::pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state)
322{
323 return proxiedEngine()->pixmap(size, mode, state);
324}
325
326void QProxyIconEngine::addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state)
327{
328 proxiedEngine()->addPixmap(pixmap, mode, state);
329}
330
331void QProxyIconEngine::addFile(const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state)
332{
333 proxiedEngine()->addFile(fileName, size, mode, state);
334}
335
337{
338 return proxiedEngine()->key();
339}
340
341QIconEngine *QProxyIconEngine::clone() const
342{
343 return proxiedEngine()->clone();
344}
345
346bool QProxyIconEngine::read(QDataStream &in)
347{
348 return proxiedEngine()->read(in);
349}
350
351bool QProxyIconEngine::write(QDataStream &out) const
352{
353 return proxiedEngine()->write(out);
354}
355
356QList<QSize> QProxyIconEngine::availableSizes(QIcon::Mode mode, QIcon::State state)
357{
358 return proxiedEngine()->availableSizes(mode, state);
359}
360
362{
363 return proxiedEngine()->iconName();
364}
365
367{
368 return proxiedEngine()->isNull();
369}
370
371QPixmap QProxyIconEngine::scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale)
372{
373 return proxiedEngine()->scaledPixmap(size, mode, state, scale);
374}
375
376void QProxyIconEngine::virtual_hook(int id, void *data)
377{
378 proxiedEngine()->virtual_hook(id, data);
379}
380
381
382QT_END_NAMESPACE
QPixmap scaledPixmap(const QSize &size, QIcon::Mode mode, QIcon::State state, qreal scale) override
void addFile(const QString &fileName, const QSize &size, QIcon::Mode mode, QIcon::State state) override
Called by QIcon::addFile().
virtual QIconEngine * proxiedEngine() const =0
void virtual_hook(int id, void *data) override
Additional method to allow extending QIconEngine without adding new virtual methods (and without brea...
QPixmap pixmap(const QSize &size, QIcon::Mode mode, QIcon::State state) override
Returns the icon as a pixmap with the required size, mode, and state.
QIconEngine * clone() const override
Reimplement this method to return a clone of this icon engine.
bool read(QDataStream &in) override
Reads icon engine contents from the QDataStream in.
QList< QSize > availableSizes(QIcon::Mode mode=QIcon::Normal, QIcon::State state=QIcon::Off) override
Returns sizes of all images that are contained in the engine for the specific mode and state.
bool isNull() override
bool write(QDataStream &out) const override
Writes the contents of this engine to the QDataStream out.
QString key() const override
\variable QIconEngine::ScaledPixmapArgument::size
void addPixmap(const QPixmap &pixmap, QIcon::Mode mode, QIcon::State state) override
Called by QIcon::addPixmap().
QString iconName() override
Returns the name used to create the engine, if available.
QSize actualSize(const QSize &size, QIcon::Mode mode, QIcon::State state) override
Returns the actual size of the icon the engine provides for the requested size, mode and state.
Combined button and popup list for selecting options.