Kirigami2

icon.h
1 /*
2  * SPDX-FileCopyrightText: 2011 Marco Martin <[email protected]>
3  * SPDX-FileCopyrightText: 2014 Aleix Pol Gonzalez <[email protected]>
4  * SPDX-FileCopyrightText: 2020 Carson Black <[email protected]>
5  *
6  * SPDX-License-Identifier: LGPL-2.0-or-later
7  */
8 
9 #pragma once
10 
11 #include <QIcon>
12 #include <QPointer>
13 #include <QQuickItem>
14 #include <QVariant>
15 
16 class QNetworkReply;
17 
18 namespace Kirigami
19 {
20 class PlatformTheme;
21 }
22 
23 /**
24  * Class for rendering an icon in UI.
25  */
26 class Icon : public QQuickItem
27 {
28  Q_OBJECT
29 
30  /**
31  * @brief This property holds the source of this icon.
32  *
33  * The icon can be pulled from:
34  * * The Freedesktop standard icon name:
35  * @include icon/IconThemeSource.qml
36  * * The filesystem:
37  * @include icon/FilesystemSource.qml
38  * * Remote URIs:
39  * @include icon/InternetSource.qml
40  * * Custom providers:
41  * @include icon/CustomSource.qml
42  * * Your application's bundled resources:
43  * @include icon/ResourceSource.qml
44  *
45  * @note See https://doc.qt.io/qt-5/qtquickcontrols2-icons.html for how to
46  * bundle icon themes in your application to refer to them by name instead of
47  * by resource URL.
48  *
49  * @note Use `fallback` to provide a fallback theme name for icons.
50  *
51  * @note Cuttlefish is a KDE application that lets you view all the icons that
52  * you can use for your application. It offers a number of useful features such
53  * as previews of their appearance across different installed themes, previews
54  * at different sizes, and more. You might find it a useful tool when deciding
55  * on which icons to use in your application.
56  */
57  Q_PROPERTY(QVariant source READ source WRITE setSource NOTIFY sourceChanged)
58 
59  /**
60  * @brief This property holds the name of an icon from the icon theme
61  * as a fallback for when an icon set with the ``source`` property is not found.
62  *
63  * @include icon/Fallback.qml
64  * @note This will only be loaded if source is unavailable (e.g. it doesn't exist, or network issues have prevented loading).
65  */
66  Q_PROPERTY(QString fallback READ fallback WRITE setFallback NOTIFY fallbackChanged)
67 
68  /**
69  * @brief This property holds the name of an icon from the icon theme to show
70  * while the icon set in `source` is being loaded.
71  *
72  * This will only be used if the source image is a type that can have such a
73  * long loading time that showing a temporary image in its place makes sense
74  * (e.g. a remote image, or an image from an ImageProvider of the type
75  * QtQml.QQmlImageProviderBase.ImageResponse).
76  *
77  * default: ``"image-png"``
78  *
79  * @since KDE Frameworks 5.15
80  */
81  Q_PROPERTY(QString placeholder READ placeholder WRITE setPlaceholder NOTIFY placeholderChanged)
82 
83  /**
84  * @brief This property sets whether the icon will use the
85  * @link QtGui.QIcon.Mode QIcon.Active @endlink mode,
86  * resulting in a graphical effect being applied when the icon is currently active.
87  *
88  * @note This is typically used to indicate when an item is being hovered or pressed.
89  *
90  * @image html icon/active.png
91  *
92  * The color differences under the default KDE color palette, Breeze. Note
93  * that a dull highlight background is typically displayed behind active icons and
94  * it is recommended to add one if you are creating a custom component.
95  *
96  * default: ``false``
97  */
98  Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)
99 
100  /**
101  * @brief This property specifies whether the icon's `source` is valid and is being used.
102  */
103  Q_PROPERTY(bool valid READ valid NOTIFY validChanged)
104 
105  /**
106  * @brief This property sets whether the icon will use the
107  * @link QtGui.QIcon.Mode QIcon.Selected @endlink mode,
108  * resulting in a graphical effect being applied when the icon is currently selected.
109  *
110  * This is typically used to indicate when a list item is currently selected.
111  *
112  * @image html icon/selected.png
113  *
114  * The color differences under the default KDE color palette, Breeze. Note
115  * that a blue background is typically displayed behind selected elements.
116  *
117  * default: ``false``
118  */
119  Q_PROPERTY(bool selected READ selected WRITE setSelected NOTIFY selectedChanged)
120 
121  /**
122  * @brief This property sets whether this icon will be treated as a mask.
123  *
124  * When an icon is being used as a mask, all non-transparent colors are replaced
125  * with the color provided in the Icon's ::color property.
126  *
127  * default: ``false``
128  *
129  * @see ::color
130  */
131  Q_PROPERTY(bool isMask READ isMask WRITE setIsMask NOTIFY isMaskChanged)
132 
133  /**
134  * @brief This property holds the color to use when drawing the icon.
135  *
136  * This property is used only when ::isMask is set to true.
137  * If this property is not set or is `Qt::transparent`, the icon will use
138  * the text or the selected text color, depending on if ::selected is set to
139  * true.
140  *
141  * default: ``Qt::transparent``
142  * @see Qt::GlobalColor
143  */
144  Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
145 
146  /**
147  * @brief This property specifies the status of the icon.
148  *
149  * @note Image loading will not be initiated until the item is shown, so if the Icon is not visible,
150  * it can only have Null or Loading states.
151  *
152  * default: ``Status::Null``
153  *
154  * @since KDE Frameworks 5.15
155  */
156  Q_PROPERTY(Icon::Status status READ status NOTIFY statusChanged)
157 
158  /**
159  * @brief This property holds the width of the painted area in pixels.
160  *
161  * This will be smaller than or equal to the width of the area
162  * taken up by the Item itself. This can be 0.
163  *
164  * default: ``0.0``
165  *
166  * @since KDE Frameworks 5.15
167  */
168  Q_PROPERTY(qreal paintedWidth READ paintedWidth NOTIFY paintedAreaChanged)
169 
170  /**
171  * @brief This property holds the height of the painted area in pixels.
172  *
173  * This will be smaller than or equal to the height of the area
174  * taken up by the Item itself. This can be 0.
175  *
176  * default: ``0.0``
177  *
178  * @since KDE Frameworks 5.15
179  */
180  Q_PROPERTY(qreal paintedHeight READ paintedHeight NOTIFY paintedAreaChanged)
181 public:
182  /**
183  * @brief This enum indicates the current status of the icon.
184  */
185  enum Status {
186  /**
187  * @brief No icon source has been set.
188  */
189  Null = 0,
190 
191  /**
192  * @brief The icon has been loaded correctly.
193  */
195 
196  /**
197  * @brief The icon is currently being loaded.
198  */
200 
201  /**
202  * @brief There was an error while loading the icon, for instance
203  * a non existent themed name, or an invalid url.
204  */
206  };
207  Q_ENUM(Status)
208 
209  Icon(QQuickItem *parent = nullptr);
210  ~Icon() override;
211 
212  void setSource(const QVariant &source);
213  QVariant source() const;
214 
215  void setActive(bool active = true);
216  bool active() const;
217 
218  bool valid() const;
219 
220  void setSelected(bool selected = true);
221  bool selected() const;
222 
223  void setIsMask(bool mask);
224  bool isMask() const;
225 
226  void setColor(const QColor &color);
227  QColor color() const;
228 
229  QString fallback() const;
230  void setFallback(const QString &fallback);
231 
232  QString placeholder() const;
233  void setPlaceholder(const QString &placeholder);
234 
235  Status status() const;
236 
237  qreal paintedWidth() const;
238  qreal paintedHeight() const;
239 
240  QSGNode *updatePaintNode(QSGNode *node, UpdatePaintNodeData *data) override;
241 
242 Q_SIGNALS:
243  void sourceChanged();
244  void activeChanged();
245  void validChanged();
246  void selectedChanged();
247  void isMaskChanged();
248  void colorChanged();
249  void fallbackChanged(const QString &fallback);
250  void placeholderChanged(const QString &placeholder);
251  void statusChanged();
252  void paintedAreaChanged();
253 
254 protected:
255 #if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
256  void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry) override;
257 #else
258  void geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry) override;
259 #endif
260  QImage findIcon(const QSize &size);
261  void handleFinished(QNetworkReply *reply);
262  void handleRedirect(QNetworkReply *reply);
263  QIcon::Mode iconMode() const;
264  bool guessMonochrome(const QImage &img);
265  void setStatus(Status status);
266  void updatePolish() override;
267  void updatePaintedGeometry();
268  void updateIsMaskHeuristic(const QString &iconSource);
269  void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value) override;
270 
271 private:
272  Kirigami::PlatformTheme *m_theme = nullptr;
273  QPointer<QNetworkReply> m_networkReply;
274  QHash<int, bool> m_monochromeHeuristics;
275  QVariant m_source;
276  Status m_status = Null;
277  bool m_changed;
278  bool m_active;
279  bool m_selected;
280  bool m_isMask;
281  bool m_isMaskHeuristic = false;
282  QImage m_loadedImage;
283  QColor m_color = Qt::transparent;
284  QString m_fallback = QStringLiteral("unknown");
285  QString m_placeholder = QStringLiteral("image-png");
286  qreal m_paintedWidth = 0.0;
287  qreal m_paintedHeight = 0.0;
288 
289  QImage m_icon;
290 };
Q_OBJECTQ_OBJECT
Status
This enum indicates the current status of the icon.
Definition: icon.h:185
Q_PROPERTY(...)
Q_ENUM(...)
QSizeF size() const const
bool valid
This property specifies whether the icon's source is valid and is being used.
Definition: icon.h:103
QString placeholder
This property holds the name of an icon from the icon theme to show while the icon set in source is b...
Definition: icon.h:81
@ Error
There was an error while loading the icon, for instance a non existent themed name,...
Definition: icon.h:205
bool isMask
This property sets whether this icon will be treated as a mask.
Definition: icon.h:131
bool selected
This property sets whether the icon will use the QIcon.Selected mode, resulting in a graphical effec...
Definition: icon.h:119
QColor color
This property holds the color to use when drawing the icon.
Definition: icon.h:144
@ Ready
The icon has been loaded correctly.
Definition: icon.h:194
bool active
This property sets whether the icon will use the QIcon.Active mode, resulting in a graphical effect ...
Definition: icon.h:98
@ Null
No icon source has been set.
Definition: icon.h:189
virtual void geometryChanged(const QRectF &newGeometry, const QRectF &oldGeometry)
@ Loading
The icon is currently being loaded.
Definition: icon.h:199
Q_SIGNALSQ_SIGNALS
QString fallback
This property holds the name of an icon from the icon theme as a fallback for when an icon set with t...
Definition: icon.h:66
qreal paintedWidth
This property holds the width of the painted area in pixels.
Definition: icon.h:168
Icon::Status status
This property specifies the status of the icon.
Definition: icon.h:156
qreal paintedHeight
This property holds the height of the painted area in pixels.
Definition: icon.h:180
Class for rendering an icon in UI.
Definition: icon.h:26
QVariant source
This property holds the source of this icon.
Definition: icon.h:57
transparent
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Nov 30 2023 04:01:10 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.