1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the demonstration applications of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:BSD$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** BSD License Usage
18** Alternatively, you may use this file under the terms of the BSD license
19** as follows:
20**
21** "Redistribution and use in source and binary forms, with or without
22** modification, are permitted provided that the following conditions are
23** met:
24** * Redistributions of source code must retain the above copyright
25** notice, this list of conditions and the following disclaimer.
26** * Redistributions in binary form must reproduce the above copyright
27** notice, this list of conditions and the following disclaimer in
28** the documentation and/or other materials provided with the
29** distribution.
30** * Neither the name of The Qt Company Ltd nor the names of its
31** contributors may be used to endorse or promote products derived
32** from this software without specific prior written permission.
33**
34**
35** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
36** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
37** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
38** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
39** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
40** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
41** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
42** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
43** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
44** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
45** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
46**
47** $QT_END_LICENSE$
48**
49****************************************************************************/
50
51#include "beziercurve.h"
52
53#include <QtQuick/qsgnode.h>
54#include <QtQuick/qsgflatcolormaterial.h>
55
56//! [1]
57BezierCurve::BezierCurve(QQuickItem *parent)
58 : QQuickItem(parent)
59 , m_p1(0, 0)
60 , m_p2(1, 0)
61 , m_p3(0, 1)
62 , m_p4(1, 1)
63 , m_segmentCount(32)
64{
65 setFlag(flag: ItemHasContents, enabled: true);
66}
67//! [1]
68
69//! [2]
70BezierCurve::~BezierCurve()
71{
72}
73//! [2]
74
75//! [3]
76void BezierCurve::setP1(const QPointF &p)
77{
78 if (p == m_p1)
79 return;
80
81 m_p1 = p;
82 emit p1Changed(p);
83 update();
84}
85//! [3]
86
87void BezierCurve::setP2(const QPointF &p)
88{
89 if (p == m_p2)
90 return;
91
92 m_p2 = p;
93 emit p2Changed(p);
94 update();
95}
96
97void BezierCurve::setP3(const QPointF &p)
98{
99 if (p == m_p3)
100 return;
101
102 m_p3 = p;
103 emit p3Changed(p);
104 update();
105}
106
107void BezierCurve::setP4(const QPointF &p)
108{
109 if (p == m_p4)
110 return;
111
112 m_p4 = p;
113 emit p4Changed(p);
114 update();
115}
116
117void BezierCurve::setSegmentCount(int count)
118{
119 if (m_segmentCount == count)
120 return;
121
122 m_segmentCount = count;
123 emit segmentCountChanged(count);
124 update();
125}
126
127//! [4]
128QSGNode *BezierCurve::updatePaintNode(QSGNode *oldNode, UpdatePaintNodeData *)
129{
130 QSGGeometryNode *node = nullptr;
131 QSGGeometry *geometry = nullptr;
132
133 if (!oldNode) {
134 node = new QSGGeometryNode;
135//! [4] //! [5]
136 geometry = new QSGGeometry(QSGGeometry::defaultAttributes_Point2D(), m_segmentCount);
137 geometry->setLineWidth(2);
138 geometry->setDrawingMode(QSGGeometry::DrawLineStrip);
139 node->setGeometry(geometry);
140 node->setFlag(QSGNode::OwnsGeometry);
141//! [5] //! [6]
142 QSGFlatColorMaterial *material = new QSGFlatColorMaterial;
143 material->setColor(QColor(255, 0, 0));
144 node->setMaterial(material);
145 node->setFlag(QSGNode::OwnsMaterial);
146//! [6] //! [7]
147 } else {
148 node = static_cast<QSGGeometryNode *>(oldNode);
149 geometry = node->geometry();
150 geometry->allocate(vertexCount: m_segmentCount);
151 }
152//! [7]
153
154//! [8]
155 QSizeF itemSize = size();
156 QSGGeometry::Point2D *vertices = geometry->vertexDataAsPoint2D();
157 for (int i = 0; i < m_segmentCount; ++i) {
158 qreal t = i / qreal(m_segmentCount - 1);
159 qreal invt = 1 - t;
160
161 QPointF pos = invt * invt * invt * m_p1
162 + 3 * invt * invt * t * m_p2
163 + 3 * invt * t * t * m_p3
164 + t * t * t * m_p4;
165
166 float x = pos.x() * itemSize.width();
167 float y = pos.y() * itemSize.height();
168
169 vertices[i].set(nx: x, ny: y);
170 }
171 node->markDirty(bits: QSGNode::DirtyGeometry);
172//! [8]
173
174
175//! [9]
176 return node;
177}
178//! [9]
179
180

source code of qtdeclarative/examples/quick/scenegraph/customgeometry/beziercurve.cpp