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

KDE's Doxygen guidelines are available online.