1// Copyright (C) 2016 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#ifndef QQUICKSHAPE_P_P_H
5#define QQUICKSHAPE_P_P_H
6
7//
8// W A R N I N G
9// -------------
10//
11// This file is not part of the Qt API. It exists purely as an
12// implementation detail. This header file may change from version to
13// version without notice, or even be removed.
14//
15// We mean it.
16//
17
18#include <QtQuickShapes/private/qquickshapesglobal_p.h>
19#include <QtQuickShapes/private/qquickshape_p.h>
20#include <private/qquickitem_p.h>
21#include <private/qsgtransform_p.h>
22#include <QPainterPath>
23#include <QColor>
24#include <QBrush>
25#include <QElapsedTimer>
26#if QT_CONFIG(opengl)
27# include <private/qopenglcontext_p.h>
28#endif
29QT_BEGIN_NAMESPACE
30
31class QSGPlainTexture;
32class QRhi;
33
34class QQuickAbstractPathRenderer
35{
36public:
37 enum Flag {
38 SupportsAsync = 0x01
39 };
40 Q_DECLARE_FLAGS(Flags, Flag)
41 enum FillGradientType { NoGradient = 0, LinearGradient, RadialGradient, ConicalGradient };
42
43 virtual ~QQuickAbstractPathRenderer() { }
44
45 // Gui thread
46 virtual void beginSync(int totalCount, bool *countChanged) = 0;
47 virtual void endSync(bool async) = 0;
48 virtual void setAsyncCallback(void (*)(void *), void *) { }
49 virtual Flags flags() const { return {}; }
50 virtual void setPath(int index, const QQuickPath *path);
51 virtual void setPath(int index, const QPainterPath &path, QQuickShapePath::PathHints pathHints = {}) = 0;
52 virtual void setStrokeColor(int index, const QColor &color) = 0;
53 virtual void setStrokeWidth(int index, qreal w) = 0;
54 virtual void setFillColor(int index, const QColor &color) = 0;
55 virtual void setFillRule(int index, QQuickShapePath::FillRule fillRule) = 0;
56 virtual void setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit) = 0;
57 virtual void setCapStyle(int index, QQuickShapePath::CapStyle capStyle) = 0;
58 virtual void setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle,
59 qreal dashOffset, const QVector<qreal> &dashPattern) = 0;
60 virtual void setFillGradient(int index, QQuickShapeGradient *gradient) = 0;
61 virtual void setFillTextureProvider(int index, QQuickItem *textureProviderItem) = 0;
62 virtual void setFillTransform(int index, const QSGTransform &transform) = 0;
63 virtual void setTriangulationScale(qreal) { }
64 virtual void handleSceneChange(QQuickWindow *window) = 0;
65
66 // Render thread, with gui blocked
67 virtual void updateNode() = 0;
68};
69
70Q_DECLARE_OPERATORS_FOR_FLAGS(QQuickAbstractPathRenderer::Flags)
71
72inline void QQuickAbstractPathRenderer::setPath(int index, const QQuickPath *path)
73{
74 QQuickShapePath::PathHints hints;
75 QPainterPath newPath = path ? path->path() : QPainterPath();
76 if (const auto *shapePath = qobject_cast<const QQuickShapePath *>(object: path)) {
77 hints = shapePath->pathHints();
78 if (shapePath->hasTrim()) {
79 const QQuickShapeTrim *trim = const_cast<QQuickShapePath *>(shapePath)->trim();
80 newPath = path->path().trimmed(fromFraction: trim->start(), toFraction: trim->end(), offset: trim->offset());
81 }
82 }
83 setPath(index, path: newPath, pathHints: hints);
84}
85
86struct QQuickShapeStrokeFillParams
87{
88 QQuickShapeStrokeFillParams();
89
90 QColor strokeColor;
91 qreal strokeWidth;
92 QColor fillColor;
93 QQuickShapePath::FillRule fillRule;
94 QQuickShapePath::JoinStyle joinStyle;
95 int miterLimit;
96 QQuickShapePath::CapStyle capStyle;
97 QQuickShapePath::StrokeStyle strokeStyle;
98 qreal dashOffset;
99 QVector<qreal> dashPattern;
100 QQuickShapeGradient *fillGradient;
101 QSGTransform fillTransform;
102 QQuickItem *fillItem;
103 QQuickShapeTrim *trim;
104};
105
106class Q_QUICKSHAPES_EXPORT QQuickShapePathPrivate : public QQuickPathPrivate
107{
108 Q_DECLARE_PUBLIC(QQuickShapePath)
109
110public:
111 enum Dirty {
112 DirtyPath = 0x01,
113 DirtyStrokeColor = 0x02,
114 DirtyStrokeWidth = 0x04,
115 DirtyFillColor = 0x08,
116 DirtyFillRule = 0x10,
117 DirtyStyle = 0x20,
118 DirtyDash = 0x40,
119 DirtyFillGradient = 0x80,
120 DirtyFillTransform = 0x100,
121 DirtyFillItem = 0x200,
122 DirtyTrim = 0x400,
123
124 DirtyAll = 0x7FF
125 };
126
127 QQuickShapePathPrivate();
128
129 void _q_pathChanged();
130 void _q_fillGradientChanged();
131 void _q_fillItemDestroyed();
132
133 void handleSceneChange();
134
135 void writeToDebugStream(QDebug &debug) const override;
136
137 static QQuickShapePathPrivate *get(QQuickShapePath *p) { return p->d_func(); }
138
139 int dirty;
140 QQuickShapeStrokeFillParams sfp;
141 QQuickShapePath::PathHints pathHints;
142};
143
144class Q_QUICKSHAPES_EXPORT QQuickShapePrivate : public QQuickItemPrivate
145{
146 Q_DECLARE_PUBLIC(QQuickShape)
147
148public:
149 QQuickShapePrivate();
150 ~QQuickShapePrivate();
151
152 void init();
153
154 QQuickShape::RendererType selectRendererType();
155 void createRenderer();
156 QSGNode *createNode();
157 void sync();
158
159 void _q_shapePathChanged();
160 void setStatus(QQuickShape::Status newStatus);
161 void handleSceneChange(QQuickWindow *w);
162
163 static QQuickShapePrivate *get(QQuickShape *item) { return item->d_func(); }
164
165 static void asyncShapeReady(void *data);
166
167 qreal getImplicitWidth() const override;
168 qreal getImplicitHeight() const override;
169
170 int effectRefCount;
171 QVector<QQuickShapePath *> sp;
172 QElapsedTimer syncTimer;
173 QQuickAbstractPathRenderer *renderer = nullptr;
174 int syncTimingTotalDirty = 0;
175 int syncTimeCounter = 0;
176 QQuickShape::Status status = QQuickShape::Null;
177 QQuickShape::RendererType rendererType = QQuickShape::UnknownRenderer;
178 QQuickShape::RendererType preferredType = QQuickShape::UnknownRenderer;
179 QQuickShape::ContainsMode containsMode = QQuickShape::BoundingRectContains;
180 QQuickShape::FillMode fillMode = QQuickShape::NoResize;
181 QQuickShape::HAlignment horizontalAlignment = QQuickShape::AlignLeft;
182 QQuickShape::VAlignment verticalAlignment = QQuickShape::AlignTop;
183
184 bool spChanged = false;
185 bool rendererChanged = false;
186 bool async = false;
187 bool enableVendorExts = false;
188 bool syncTimingActive = false;
189 qreal triangulationScale = 1.0;
190};
191
192QT_END_NAMESPACE
193
194#endif
195

source code of qtdeclarative/src/quickshapes/qquickshape_p_p.h