Qt
Internal/Contributor docs for the Qt SDK. <b>Note:</b> These are NOT official API docs; those are found <a href='https://doc.qt.io/'>here</a>.
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
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 if (source.isEmpty()) {
54 std::unique_ptr<QIconEngine> iconEngine(QIconLoader::instance()->iconEngine(icon.iconName));
55 if (iconEngine && !iconEngine->isNull()) {
56 // ### TODO that's the best we can do for now to select different pixmaps based on the
57 // QuickItem's state. QQuickIconImage cannot know about the state of the control that
58 // uses it without adding more properties that are then synced up with the control.
59 const QIcon::Mode mode = q->isEnabled() ? QIcon::Normal : QIcon::Disabled;
60 const QImage image = iconEngine->scaledPixmap(size, mode, QIcon::Off, dpr).toImage();
62 }
63 } else {
64 url = source;
65 isThemeIcon = false;
66 }
67 if (!url.isEmpty())
68 q->load();
69
70 updatingIcon = false;
71}
72
74{
75 Q_Q(QQuickIconImage);
76 // If we start with a sourceSize of 28x28 and then set sourceSize.width to 24, the fillMode
77 // will change to PreserveAspectFit (because pixmapSize.width() > width()), which causes the
78 // pixmap to be reloaded at its original size of 28x28, which causes the fillMode to change
79 // to Pad (because pixmapSize.width() <= width()), and so on.
81 return;
82
83 updatingFillMode = true;
84
86 if (pixmapSize.width() > q->width() || pixmapSize.height() > q->height())
88 else
89 q->setFillMode(QQuickImage::Pad);
90
91 updatingFillMode = false;
92}
93
95{
96 Q_Q(const QQuickIconImage);
97 return q->window() ? q->window()->effectiveDevicePixelRatio() : qApp->devicePixelRatio();
98}
99
105
107{
108 Q_D(const QQuickIconImage);
109 return d->icon.iconName;
110}
111
113{
114 Q_D(QQuickIconImage);
115 if (d->icon.iconName == name)
116 return;
117
118 d->icon.entries.clear();
119 d->icon = QIconLoader::instance()->loadIcon(name);
120 if (d->icon.iconName.isEmpty())
121 d->icon.iconName = name;
123 d->updateIcon();
125}
126
128{
129 Q_D(const QQuickIconImage);
130 return d->color;
131}
132
134{
135 Q_D(QQuickIconImage);
136 if (d->color == color)
137 return;
138
139 d->color = color;
141 d->updateIcon();
143}
144
146{
147 Q_D(QQuickIconImage);
148 if (d->source == source)
149 return;
150
151 d->source = source;
153 d->updateIcon();
155}
156
164
165void QQuickIconImage::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
166{
167 Q_D(QQuickIconImage);
168 QQuickImage::geometryChange(newGeometry, oldGeometry);
169 if (isComponentComplete() && newGeometry.size() != oldGeometry.size())
170 d->updateIcon();
171}
172
174{
175 Q_D(QQuickIconImage);
176 if (change == ItemDevicePixelRatioHasChanged)
177 d->updateIcon();
179}
180
182{
183 Q_D(QQuickIconImage);
185 d->updateFillMode();
186
187 // Don't apply the color if we're recursing (updateFillMode() can cause us to recurse).
188 if (!d->updatingFillMode && d->color.alpha() > 0) {
189 QImage image = d->currentPix->image();
190 if (!image.isNull()) {
193 painter.fillRect(image.rect(), d->color);
194 d->currentPix->setImage(image);
195 }
196 }
197}
198
200
201#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)
static QIconLoader * instance()
Mode
This enum type describes the mode for which a pixmap is intended to be used.
Definition qicon.h:22
@ Disabled
Definition qicon.h:22
@ Normal
Definition qicon.h:22
@ Off
Definition qicon.h:23
\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:299
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 setImage(const QImage &img)
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:63
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:144
@ ItemDevicePixelRatioHasChanged
Definition qquickitem.h:154
\inmodule QtCore\reentrant
Definition qrect.h:484
constexpr QSizeF size() const noexcept
Returns the size of the rectangle.
Definition qrect.h:735
\inmodule QtCore
Definition qsize.h:25
constexpr void setWidth(int w) noexcept
Sets the width to the given width.
Definition qsize.h:136
\macro QT_RESTRICTED_CAST_FROM_ASCII
Definition qstring.h:129
\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:3368
bool isEmpty() const
Returns true if the URL has no data; otherwise returns false.
Definition qurl.cpp:1896
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 mode
GLenum GLuint GLintptr GLsizeiptr size
[1]
GLuint color
[2]
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:75
static QT_BEGIN_NAMESPACE qreal dpr(const QWindow *w)
#define emit
double qreal
Definition qtypes.h:187
QPainter painter(this)
[7]
QThemeIconEntries entries
\inmodule QtQuick
Definition qquickitem.h:159