Kirigami2

shadowedrectangle.h
1 /*
2  * SPDX-FileCopyrightText: 2020 Arjen Hiemstra <[email protected]>
3  *
4  * SPDX-License-Identifier: LGPL-2.0-or-later
5  */
6 
7 #pragma once
8 
9 #include <QQuickItem>
10 #include <memory>
11 
13 
14 /**
15  * @brief Grouped property for rectangle border.
16  */
17 class BorderGroup : public QObject
18 {
19  Q_OBJECT
20  /**
21  * @brief This property holds the border's width in pixels.
22  *
23  * default: ``0``px
24  */
25  Q_PROPERTY(qreal width READ width WRITE setWidth NOTIFY changed)
26  /**
27  * @brief This property holds the border's color.
28  *
29  * Full RGBA colors are supported.
30  *
31  * default: ``Qt::black``
32  */
33  Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY changed)
34 
35 public:
36  explicit BorderGroup(QObject *parent = nullptr);
37 
38  qreal width() const;
39  void setWidth(qreal newWidth);
40 
41  QColor color() const;
42  void setColor(const QColor &newColor);
43 
44  Q_SIGNAL void changed();
45 
46  inline bool isEnabled() const
47  {
48  return !qFuzzyIsNull(m_width);
49  }
50 
51 private:
52  qreal m_width = 0.0;
53  QColor m_color = Qt::black;
54 };
55 
56 /**
57  * @brief Grouped property for the rectangle's shadow.
58  */
59 class ShadowGroup : public QObject
60 {
61  Q_OBJECT
62  /**
63  * @brief This property holds the shadow's approximate size in pixels.
64  * @note The actual shadow size can be less than this value due to falloff.
65  *
66  * default: ``0``px
67  */
68  Q_PROPERTY(qreal size READ size WRITE setSize NOTIFY changed)
69  /**
70  * @brief This property holds the shadow's offset in pixels on the X axis.
71  *
72  * default: ``0``px
73  */
74  Q_PROPERTY(qreal xOffset READ xOffset WRITE setXOffset NOTIFY changed)
75  /**
76  * @brief This property holds the shadow's offset in pixels on the Y axis.
77  *
78  * default: ``0``px
79  */
80  Q_PROPERTY(qreal yOffset READ yOffset WRITE setYOffset NOTIFY changed)
81  /**
82  * @brief This property holds the shadow's color.
83  *
84  * Full RGBA colors are supported.
85  *
86  * default: ``Qt::black``
87  */
88  Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY changed)
89 
90 public:
91  explicit ShadowGroup(QObject *parent = nullptr);
92 
93  qreal size() const;
94  void setSize(qreal newSize);
95 
96  qreal xOffset() const;
97  void setXOffset(qreal newXOffset);
98 
99  qreal yOffset() const;
100  void setYOffset(qreal newYOffset);
101 
102  QColor color() const;
103  void setColor(const QColor &newShadowColor);
104 
105  Q_SIGNAL void changed();
106 
107 private:
108  qreal m_size = 0.0;
109  qreal m_xOffset = 0.0;
110  qreal m_yOffset = 0.0;
111  QColor m_color = Qt::black;
112 };
113 
114 /**
115  * @brief Grouped property for corner radius.
116  */
117 class CornersGroup : public QObject
118 {
119  Q_OBJECT
120  /**
121  * @brief This property holds the top-left corner's radius in pixels.
122  *
123  * Setting this to ``-1`` indicates that the value should be ignored.
124  *
125  * default: ``-1``px
126  */
127  Q_PROPERTY(qreal topLeftRadius READ topLeft WRITE setTopLeft NOTIFY changed)
128 
129  /**
130  * @brief This property holds the top-right corner's radius in pixels.
131  *
132  * Setting this to ``-1`` indicates that the value should be ignored.
133  *
134  * default: ``-1``px
135  */
136  Q_PROPERTY(qreal topRightRadius READ topRight WRITE setTopRight NOTIFY changed)
137 
138  /**
139  * @brief This property holds the bottom-left corner's radius in pixels.
140  *
141  * Setting this to ``-1`` indicates that the value should be ignored.
142  *
143  * default: ``-1``px
144  */
145  Q_PROPERTY(qreal bottomLeftRadius READ bottomLeft WRITE setBottomLeft NOTIFY changed)
146 
147  /**
148  * @brief This property holds the bottom-right corner's radius in pixels.
149  *
150  * Setting this to ``-1`` indicates that the value should be ignored.
151  *
152  * default: ``-1``px
153  */
154  Q_PROPERTY(qreal bottomRightRadius READ bottomRight WRITE setBottomRight NOTIFY changed)
155 
156 public:
157  explicit CornersGroup(QObject *parent = nullptr);
158 
159  qreal topLeft() const;
160  void setTopLeft(qreal newTopLeft);
161 
162  qreal topRight() const;
163  void setTopRight(qreal newTopRight);
164 
165  qreal bottomLeft() const;
166  void setBottomLeft(qreal newBottomLeft);
167 
168  qreal bottomRight() const;
169  void setBottomRight(qreal newBottomRight);
170 
171  Q_SIGNAL void changed();
172 
173  QVector4D toVector4D(float all) const;
174 
175 private:
176  float m_topLeft = -1.0;
177  float m_topRight = -1.0;
178  float m_bottomLeft = -1.0;
179  float m_bottomRight = -1.0;
180 };
181 
182 /**
183  * @brief A rectangle with a shadow behind it.
184  *
185  * This item will render a rectangle, with a shadow below it. The rendering is done
186  * using distance fields, which provide greatly improved performance. The shadow is
187  * rendered outside of the item's bounds, so the item's width and height are the
188  * rectangle's width and height.
189  *
190  * @since 5.69
191  * @since 2.12
192  */
194 {
195  Q_OBJECT
196  /**
197  * @brief This property holds the radii of the rectangle's corners.
198  *
199  * This is the amount of rounding to apply to all of the rectangle's
200  * corners, in pixels. Each corner can have a different radius.
201  *
202  * default: ``0``
203  *
204  * @see corners
205  */
206  Q_PROPERTY(qreal radius READ radius WRITE setRadius NOTIFY radiusChanged)
207 
208  /**
209  * @brief This property holds the rectangle's color.
210  *
211  * Full RGBA colors are supported.
212  *
213  * default: ``Qt::white``
214  */
215  Q_PROPERTY(QColor color READ color WRITE setColor NOTIFY colorChanged)
216 
217  /**
218  * @brief This property holds the border's grouped property.
219  *
220  * Example usage:
221  * @code
222  * Kirigami.ShadowedRectangle {
223  * border.width: 2
224  * border.color: Kirigami.Theme.textColor
225  * }
226  * @endcode
227  * @see BorderGroup
228  */
229  Q_PROPERTY(BorderGroup *border READ border CONSTANT)
230 
231  /**
232  * @brief This property holds the shadow's grouped property.
233  *
234  * Example usage:
235  * @code
236  * Kirigami.ShadowedRectangle {
237  * shadow.size: 20
238  * shadow.xOffset: 5
239  * shadow.yOffset: 5
240  * }
241  * @endcode
242  *
243  * @see ShadowGroup
244  */
245  Q_PROPERTY(ShadowGroup *shadow READ shadow CONSTANT)
246 
247  /**
248  * @brief This property holds the corners grouped property
249  *
250  * Note that the values from this group override \property radius for the
251  * corner they affect.
252  *
253  * Example usage:
254  * @code
255  * Kirigami.ShadowedRectangle {
256  * corners.topLeftRadius: 4
257  * corners.topRightRadius: 5
258  * corners.bottomLeftRadius: 2
259  * corners.bottomRightRadius: 10
260  * @endcode
261  *
262  * @see CornersGroup
263  */
264  Q_PROPERTY(CornersGroup *corners READ corners CONSTANT)
265 
266  /**
267  * @brief This property holds the rectangle's render mode.
268  *
269  * default: ``RenderType::Auto``
270  *
271  * @see RenderType
272  */
273  Q_PROPERTY(RenderType renderType READ renderType WRITE setRenderType CONSTANT)
274 
275  /**
276  * @brief This property tells whether software rendering is being used.
277  *
278  * default: ``false``
279  */
280  Q_PROPERTY(bool softwareRendering READ isSoftwareRendering NOTIFY softwareRenderingChanged)
281 
282 public:
283  ShadowedRectangle(QQuickItem *parent = nullptr);
284  ~ShadowedRectangle() override;
285 
286  /**
287  * @brief Available rendering types for ShadowedRectangle.
288  */
289  enum RenderType {
290 
291  /**
292  * @brief Automatically determine the optimal rendering type.
293  *
294  * This will use the highest rendering quality possible, falling back to
295  * lower quality if the hardware doesn't support it. It will use software
296  * rendering if the QtQuick scene graph is set to use software rendering.
297  */
299 
300  /**
301  * @brief Use the highest rendering quality possible, even if the hardware might
302  * not be able to handle it normally.
303  */
305 
306  /**
307  * @brief Use the lowest rendering quality, even if the hardware could handle
308  * higher quality rendering.
309  *
310  * This might result in certain effects being omitted, like shadows.
311  */
313 
314  /**
315  * @brief Always use software rendering for this rectangle.
316  *
317  * Software rendering is intended as a fallback when the QtQuick scene
318  * graph is configured to use software rendering. It will result in
319  * a number of missing features, like shadows and multiple corner radii.
320  */
321  Software
322  };
323  Q_ENUM(RenderType)
324 
325  BorderGroup *border() const;
326  ShadowGroup *shadow() const;
327  CornersGroup *corners() const;
328 
329  qreal radius() const;
330  void setRadius(qreal newRadius);
331  Q_SIGNAL void radiusChanged();
332 
333  QColor color() const;
334  void setColor(const QColor &newColor);
335  Q_SIGNAL void colorChanged();
336 
337  RenderType renderType() const;
338  void setRenderType(RenderType renderType);
339  Q_SIGNAL void renderTypeChanged();
340 
341  void componentComplete() override;
342 
343  bool isSoftwareRendering() const;
344 
345 Q_SIGNALS:
346  void softwareRenderingChanged();
347 
348 protected:
349  PaintedRectangleItem *softwareItem() const;
350  void itemChange(QQuickItem::ItemChange change, const QQuickItem::ItemChangeData &value) override;
351  QSGNode *updatePaintNode(QSGNode *node, QQuickItem::UpdatePaintNodeData *data) override;
352 
353 private:
354  void checkSoftwareItem();
355  const std::unique_ptr<BorderGroup> m_border;
356  const std::unique_ptr<ShadowGroup> m_shadow;
357  const std::unique_ptr<CornersGroup> m_corners;
358  qreal m_radius = 0.0;
359  QColor m_color = Qt::white;
360  RenderType m_renderType = RenderType::Auto;
361  PaintedRectangleItem *m_softwareItem = nullptr;
362 };
Q_OBJECTQ_OBJECT
qreal width
This property holds the border's width in pixels.
Q_PROPERTY(...)
Q_ENUM(...)
qreal yOffset
This property holds the shadow's offset in pixels on the Y axis.
QColor color
This property holds the shadow's color.
Grouped property for rectangle border.
@ LowQuality
Use the lowest rendering quality, even if the hardware could handle higher quality rendering.
Q_SIGNALQ_SIGNAL
A rectangle with a shadow behind it.
Grouped property for corner radius.
qreal size
This property holds the shadow's approximate size in pixels.
Grouped property for the rectangle's shadow.
QColor color
This property holds the border's color.
@ Auto
Automatically determine the optimal rendering type.
Q_SIGNALSQ_SIGNALS
RenderType
Available rendering types for ShadowedRectangle.
qreal xOffset
This property holds the shadow's offset in pixels on the X axis.
@ HighQuality
Use the highest rendering quality possible, even if the hardware might not be able to handle it norma...
A rectangle with a border and rounded corners, rendered through QPainter.
QObject * parent() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Sun Jan 29 2023 04:11:03 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.