1// Copyright (C) 2024 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 QQUICKSHAPECURVERENDERER_P_H
5#define QQUICKSHAPECURVERENDERER_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 for the convenience
12// of a number of Qt sources files. This header file may change from
13// version to 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_p.h>
20#include <QtQuick/private/qquadpath_p.h>
21#include <QtQuick/private/qsgcurveabstractnode_p.h>
22#include <QtQuick/private/qsggradientcache_p.h>
23#include <qsgnode.h>
24#include <qsggeometry.h>
25#include <qsgmaterial.h>
26#include <qsgrendererinterface.h>
27#include <qsgtexture.h>
28#include <QtCore/qrunnable.h>
29#include <QRunnable>
30
31#include <QtGui/private/qtriangulator_p.h>
32#include <QtQuick/private/qsgcurvefillnode_p.h>
33
34QT_BEGIN_NAMESPACE
35
36class QQuickShapeCurveRunnable;
37
38class Q_QUICKSHAPES_EXPORT QQuickShapeCurveRenderer : public QQuickAbstractPathRenderer
39{
40public:
41 QQuickShapeCurveRenderer(QQuickItem *item)
42 : m_item(item)
43 { }
44 ~QQuickShapeCurveRenderer() override;
45
46 void beginSync(int totalCount, bool *countChanged) override;
47 void setPath(int index, const QQuickPath *path) override;
48 void setPath(int index, const QPainterPath &path, QQuickShapePath::PathHints pathHints = {}) override;
49 void setStrokeColor(int index, const QColor &color) override;
50 void setStrokeWidth(int index, qreal w) override;
51 void setFillColor(int index, const QColor &color) override;
52 void setFillRule(int index, QQuickShapePath::FillRule fillRule) override;
53 void setJoinStyle(int index, QQuickShapePath::JoinStyle joinStyle, int miterLimit) override;
54 void setCapStyle(int index, QQuickShapePath::CapStyle capStyle) override;
55 void setStrokeStyle(int index, QQuickShapePath::StrokeStyle strokeStyle,
56 qreal dashOffset, const QVector<qreal> &dashPattern) override;
57 void setFillGradient(int index, QQuickShapeGradient *gradient) override;
58 void setFillTextureProvider(int index, QQuickItem *textureProviderItem) override;
59 void setFillTransform(int index, const QSGTransform &transform) override;
60 void endSync(bool async) override;
61 void setAsyncCallback(void (*)(void *), void *) override;
62 Flags flags() const override { return SupportsAsync; }
63 void handleSceneChange(QQuickWindow *window) override;
64
65 void updateNode() override;
66
67 void setRootNode(QSGNode *node);
68 void clearNodeReferences();
69
70 using NodeList = QVector<QSGCurveAbstractNode *>;
71
72 enum DirtyFlag
73 {
74 PathDirty = 0x01,
75 FillDirty = 0x02,
76 StrokeDirty = 0x04,
77 UniformsDirty = 0x08
78 };
79
80 enum DebugVisualizationOption {
81 NoDebug = 0,
82 DebugCurves = 0x01,
83 DebugWireframe = 0x02
84 };
85
86 static int debugVisualization();
87 static void setDebugVisualization(int options);
88
89private:
90 struct PathData {
91
92 bool isFillVisible() const
93 {
94 return gradientType != QGradient::NoGradient
95 || fillTextureProviderItem != nullptr
96 || fillColor.alpha() > 0;
97 }
98
99 bool isStrokeVisible() const
100 {
101 return validPenWidth && pen.color().alpha() > 0 && pen.style() != Qt::NoPen;
102 }
103
104 QGradient::Type gradientType = QGradient::NoGradient;
105 QSGGradientCache::GradientDesc gradient;
106 QSGTransform fillTransform;
107 QColor fillColor;
108 Qt::FillRule fillRule = Qt::OddEvenFill;
109 QPen pen;
110 bool validPenWidth = true;
111 int m_dirty = 0;
112 QQuickShapePath::PathHints pathHints;
113
114 QPainterPath originalPath;
115 QQuadPath path;
116 QQuadPath fillPath;
117
118 NodeList fillNodes;
119 NodeList strokeNodes;
120
121 QQuickShapeCurveRunnable *currentRunner = nullptr;
122 QQuickItem *fillTextureProviderItem = nullptr;
123 };
124
125 void setUpRunner(PathData *pathData);
126 void maybeUpdateAsyncItem();
127
128 static void processPath(PathData *pathData);
129 static NodeList addFillNodes(const QQuadPath &path);
130 static NodeList addTriangulatingStrokerNodes(const QQuadPath &path, const QPen &pen);
131 static NodeList addCurveStrokeNodes(const QQuadPath &path, const QPen &pen);
132
133 QQuickItem *m_item;
134 QSGNode *m_rootNode = nullptr;
135 QVector<PathData> m_paths;
136 void (*m_asyncCallback)(void *) = nullptr;
137 void *m_asyncCallbackData = nullptr;
138 static int debugVisualizationFlags;
139
140 friend class QQuickShapeCurveRunnable;
141};
142
143class QQuickShapeCurveRunnable : public QObject, public QRunnable
144{
145 Q_OBJECT
146
147public:
148 ~QQuickShapeCurveRunnable() override;
149 void run() override;
150
151 bool isInitialized = false;
152 bool isAsync = false;
153 bool isDone = false;
154 bool orphaned = false;
155
156 // input / output
157 QQuickShapeCurveRenderer::PathData pathData;
158
159Q_SIGNALS:
160 void done(QQuickShapeCurveRunnable *self);
161};
162
163QT_END_NAMESPACE
164
165#endif // QQUICKSHAPECURVERENDERER_P_H
166

Provided by KDAB

Privacy Policy
Start learning QML with our Intro Training
Find out more

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