KDStateMachineEditor API Documentation 2.1
Loading...
Searching...
No Matches
qopengl2pexvertexarray.cpp
Go to the documentation of this file.
1/****************************************************************************
2**
3** Copyright (C) 2013 Digia Plc and/or its subsidiary(-ies).
4** Contact: http://www.qt-project.org/legal
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
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 Digia. For licensing terms and
14** conditions see http://qt.digia.com/licensing. For further information
15** use the contact form at http://qt.digia.com/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 2.1 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 2.1 requirements
23** will be met: https://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24**
25** In addition, as a special exception, Digia gives you certain additional
26** rights. These rights are described in the Digia Qt LGPL Exception
27** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28**
29** GNU General Public License Usage
30** Alternatively, this file may be used under the terms of the GNU
31** General Public License version 3.0 as published by the Free Software
32** Foundation and appearing in the file LICENSE.GPL included in the
33** packaging of this file. Please review the following information to
34** ensure the GNU General Public License version 3.0 requirements will be
35** met: https://www.gnu.org/copyleft/gpl.html.
36**
37**
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
43
44#include <private/qbezier_p.h>
45
46QT_BEGIN_NAMESPACE
47
49{
50 vertexArray.reset();
51 vertexArrayStops.reset();
52 boundingRectDirty = true;
53}
54
55
57{
58 if (boundingRectDirty) {
59 return QOpenGLRect(0.0, 0.0, 0.0, 0.0);
60 }
61
62 return QOpenGLRect(minX, minY, maxX, maxY);
63}
64
65void QOpenGL2PEXVertexArray::addClosingLine(int index)
66{
67 const QPointF point(vertexArray.at(index));
68 if (point != QPointF(vertexArray.last())) {
69 vertexArray.add(QOpenGLPoint{point});
70 }
71}
72
73void QOpenGL2PEXVertexArray::addCentroid(const QVectorPath &path, int subPathIndex)
74{
75 const auto *const points = reinterpret_cast<const QPointF *>(path.points());
76 const QPainterPath::ElementType *const elements = path.elements();
77
78 QPointF sum = points[subPathIndex];
79 int count = 1;
80
81 for (int i = subPathIndex + 1; i < path.elementCount() && (!elements || elements[i] != QPainterPath::MoveToElement); ++i) {
82 sum += points[i];
83 ++count;
84 }
85
86 const QPointF centroid = sum / qreal(count);
87 vertexArray.add(QOpenGLPoint{centroid});
88}
89
90void QOpenGL2PEXVertexArray::addPath(const QVectorPath &path, GLfloat curveInverseScale, bool outline) // NOLINT(readability-function-cognitive-complexity)
91{
92 const auto* const points = reinterpret_cast<const QPointF*>(path.points());
93 const QPainterPath::ElementType* const elements = path.elements();
94
95 if (boundingRectDirty) {
96 minX = maxX = static_cast<float>(points[0].x());
97 minY = maxY = static_cast<float>(points[0].y());
98 boundingRectDirty = false;
99 }
100
101 if (!outline && !path.isConvex())
102 addCentroid(path, 0);
103
104 int lastMoveTo = static_cast<int>(vertexArray.size());
105 vertexArray.add(QOpenGLPoint{points[0]}); // The first element is always a moveTo
106
107 do {
108 if (!elements) {
109// qDebug("QVectorPath has no elements");
110 // If the path has a null elements pointer, the elements implicitly
111 // start with a moveTo (already added) and continue with lineTos:
112 for (int i=1; i<path.elementCount(); ++i) {
113 lineToArray(points[i]);
114 }
115
116 break;
117 }
118// qDebug("QVectorPath has element types");
119
120 for (int i=1; i<path.elementCount(); ++i) {
121 switch (elements[i]) {
122 case QPainterPath::MoveToElement:
123 if (!outline)
124 addClosingLine(lastMoveTo);
125// qDebug("element[%d] is a MoveToElement", i);
126 vertexArrayStops.add(static_cast<int>(vertexArray.size()));
127 if (!outline) {
128 if (!path.isConvex()) {
129 addCentroid(path, i);
130 }
131 lastMoveTo = static_cast<int>(vertexArray.size());
132 }
133 lineToArray(points[i]); // Add the moveTo as a new vertex
134 break;
135 case QPainterPath::LineToElement:
136// qDebug("element[%d] is a LineToElement", i);
137 lineToArray(points[i]);
138 break;
139 case QPainterPath::CurveToElement: {
140 const QBezier b = QBezier::fromPoints(*(static_cast<const QPointF *>(points) + i - 1),
141 points[i],
142 points[i+1],
143 points[i+2]);
144 const QRectF bounds = b.bounds();
145 // threshold based on same algorithm as in qtriangulatingstroker.cpp
146 // NOLINTNEXTLINE
147 int threshold = qMin<float>(64, qMax(bounds.width(), bounds.height()) * 3.14F / (curveInverseScale * 6));
148 threshold = std::max(threshold, 3);
149 const qreal one_over_threshold_minus_1 = qreal(1) / (threshold - 1);
150 for (int t=0; t<threshold; ++t) {
151 const QPointF pt = b.pointAt(t * one_over_threshold_minus_1);
152 lineToArray(pt);
153 }
154 i += 2;
155 break; }
156 default:
157 break;
158 }
159 }
160 } while (0);
161
162 if (!outline)
163 addClosingLine(lastMoveTo);
164 vertexArrayStops.add(static_cast<int>(vertexArray.size()));
165}
166
168{
169 const auto x = static_cast<float>(point.x());
170 const auto y = static_cast<float>(point.y());
171 vertexArray.add(QOpenGLPoint{x, y});
172
173 if (x > maxX)
174 maxX = x;
175 else if (x < minX)
176 minX = x;
177 if (y > maxY)
178 maxY = y;
179 else if (y < minY)
180 minY = y;
181}
182
183QT_END_NAMESPACE
void addPath(const QVectorPath &path, GLfloat curveInverseScale, bool outline=true)

© Klarälvdalens Datakonsult AB (KDAB)
"The Qt, C++ and OpenGL Experts"
https://www.kdab.com/
KDStateMachineEditor
Create Qt State Machine metacode using a graphical user interface
https://github.com/KDAB/KDStateMachineEditor
Generated on Tue Jul 15 2025 15:21:47 for KDStateMachineEditor API Documentation by doxygen 1.9.8