Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
qquickiconimage.cpp
Go to the documentation of this file.
1// Copyright (C) 2017 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 "qquickiconimage_p.h"
6
7#include <QtCore/qmath.h>
8#include <QtQuick/private/qquickimagebase_p_p.h>
9
11
13{
14 icon.entries.clear();
15}
16
18{
19 if (isThemeIcon) {
21 return true;
22 }
23
24 return QQuickImagePrivate::updateDevicePixelRatio(targetDevicePixelRatio);
25}
26
28{
29 Q_Q(QQuickIconImage);
30 // Both geometryChange() and QQuickImageBase::sourceSizeChanged()
31 // (which we connect to updateIcon() in the constructor) can be called as a result
32 // of updateIcon() changing the various sizes, so we must check that we're not recursing.
33 if (updatingIcon)
34 return;
35
36 updatingIcon = true;
37
39 // If no size is specified for theme icons, it will use the smallest available size.
40 if (size.width() <= 0)
41 size.setWidth(q->width());
42 if (size.height() <= 0)
43 size.setHeight(q->height());
44
47
48 if (entry) {
50 const QUrl entryUrl = QUrl::fromLocalFile(entry->filename);
51 url = context ? context->resolvedUrl(entryUrl) : entryUrl;
52 isThemeIcon = true;
53 } else {
54 url = source;
55 isThemeIcon = false;
56 }
57 q->load();
58
59 updatingIcon = false;
60}
61
63{
64 Q_Q(QQuickIconImage);
65 // If we start with a sourceSize of 28x28 and then set sourceSize.width to 24, the fillMode
66 // will change to PreserveAspectFit (because pixmapSize.width() > width()), which causes the
67 // pixmap to be reloaded at its original size of 28x28, which causes the fillMode to change
68 // to Pad (because pixmapSize.width() <= width()), and so on.
70 return;
71
72 updatingFillMode = true;
73
74 const QSize pixmapSize = QSize(pix.width(), pix.height()) / calculateDevicePixelRatio();
75 if (pixmapSize.width() > q->width() || pixmapSize.height() > q->height())
77 else
78 q->setFillMode(QQuickImage::Pad);
79
80 updatingFillMode = false;
81}
82
84{
85 Q_Q(const QQuickIconImage);
86 return q->window() ? q->window()->effectiveDevicePixelRatio() : qApp->devicePixelRatio();
87}
88
91{
93}
94
96{
97 Q_D(const QQuickIconImage);
98 return d->icon.iconName;
99}
100
102{
103 Q_D(QQuickIconImage);
104 if (d->icon.iconName == name)
105 return;
106
107 d->icon.entries.clear();
110 d->updateIcon();
112}
113
115{
116 Q_D(const QQuickIconImage);
117 return d->color;
118}
119
121{
122 Q_D(QQuickIconImage);
123 if (d->color == color)
124 return;
125
126 d->color = color;
128 d->updateIcon();
130}
131
133{
134 Q_D(QQuickIconImage);
135 if (d->source == source)
136 return;
137
138 d->source = source;
140 d->updateIcon();
142}
143
145{
146 Q_D(QQuickIconImage);
148 d->updateIcon();
150}
151
152void QQuickIconImage::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
153{
154 Q_D(QQuickIconImage);
155 QQuickImage::geometryChange(newGeometry, oldGeometry);
156 if (isComponentComplete() && newGeometry.size() != oldGeometry.size())
157 d->updateIcon();
158}
159
161{
162 Q_D(QQuickIconImage);
163 if (change == ItemDevicePixelRatioHasChanged)
164 d->updateIcon();
166}
167
169{
170 Q_D(QQuickIconImage);
172 d->updateFillMode();
173
174 // Don't apply the color if we're recursing (updateFillMode() can cause us to recurse).
175 if (!d->updatingFillMode && d->color.alpha() > 0) {
176 QImage image = d->pix.image();
177 if (!image.isNull()) {
180 painter.fillRect(image.rect(), d->color);
181 d->pix.setImage(image);
182 }
183 }
184}
185
187
188#include "moc_qquickiconimage_p.cpp"
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition qcolor.h:31
static Q_GUI_EXPORT QIconLoaderEngineEntry * entryForSize(const QThemeIconInfo &info, const QSize &size, int scale=1)
QThemeIconInfo loadIcon(const QString &iconName) const
static QIconLoader * instance()
\inmodule QtGui
Definition qimage.h:37
static QMetaObject::Connection connect(const typename QtPrivate::FunctionPointer< Func1 >::Object *sender, Func1 signal, const typename QtPrivate::FunctionPointer< Func2 >::Object *receiverPrivate, Func2 slot, Qt::ConnectionType type=Qt::AutoConnection)
Definition qobject_p.h:298
The QPainter class performs low-level painting on widgets and other paint devices.
Definition qpainter.h:46
void setCompositionMode(CompositionMode mode)
Sets the composition mode to the given mode.
@ CompositionMode_SourceIn
Definition qpainter.h:103
void fillRect(const QRectF &, const QBrush &)
Fills the given rectangle with the brush specified.
The QQmlContext class defines a context within a QML engine.
Definition qqmlcontext.h:25
virtual void componentComplete()=0
Invoked after the root component that caused this instantiation has completed construction.
qreal calculateDevicePixelRatio() const
bool updateDevicePixelRatio(qreal targetDevicePixelRatio) override
void setName(const QString &name)
void pixmapChange() override
void componentComplete() override
Invoked after the root component that caused this instantiation has completed construction.
void itemChange(ItemChange change, const ItemChangeData &value) override
Called when change occurs for this item.
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override
void setColor(const QColor &color)
void setSource(const QUrl &url) override
QQuickIconImage(QQuickItem *parent=nullptr)
virtual bool updateDevicePixelRatio(qreal targetDevicePixelRatio)
void itemChange(ItemChange change, const ItemChangeData &value) override
Called when change occurs for this item.
void sourceSizeChanged()
void sourceChanged(const QUrl &)
void setFillMode(FillMode)
void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override
void pixmapChange() override
The QQuickItem class provides the most basic of all visual items in \l {Qt Quick}.
Definition qquickitem.h:64
bool isComponentComplete() const
Returns true if construction of the QML component is complete; otherwise returns false.
ItemChange
Used in conjunction with QQuickItem::itemChange() to notify the item about certain types of changes.
Definition qquickitem.h:143
@ ItemDevicePixelRatioHasChanged
Definition qquickitem.h:153
\inmodule QtCore\reentrant
Definition qrect.h:483
constexpr QSizeF size() const noexcept
Returns the size of the rectangle.
Definition qrect.h:721
\inmodule QtCore
Definition qsize.h:25
constexpr int height() const noexcept
Returns the height.
Definition qsize.h:132
constexpr int width() const noexcept
Returns the width.
Definition qsize.h:129
constexpr void setWidth(int w) noexcept
Sets the width to the given width.
Definition qsize.h:135
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:127
\inmodule QtCore
Definition qurl.h:94
static QUrl fromLocalFile(const QString &localfile)
Returns a QUrl representation of localFile, interpreted as a local file.
Definition qurl.cpp:3354
Combined button and popup list for selecting options.
Definition image.cpp:4
static void * context
#define qApp
EGLOutputLayerEXT EGLint EGLAttrib value
[5]
int qCeil(T v)
Definition qmath.h:36
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint name
GLsizei GLsizei GLchar * source
GLuint entry
GLdouble GLdouble GLdouble GLdouble q
Definition qopenglext.h:259
QQmlContext * qmlContext(const QObject *obj)
Definition qqml.cpp:71
static QT_BEGIN_NAMESPACE qreal dpr(const QWindow *w)
#define emit
double qreal
Definition qtypes.h:92
QPainter painter(this)
[7]
QThemeIconEntries entries
IUIAutomationTreeWalker __RPC__deref_out_opt IUIAutomationElement ** parent
\inmodule QtQuick
Definition qquickitem.h:158