Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qquickregularpolygonshape.cpp
Go to the documentation of this file.
1// Copyright (C) 2025 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// Qt-Security score:significant reason:default
4
7#include <algorithm>
8#include <QtMath>
9#include <cstddef>
10
11QT_BEGIN_NAMESPACE
12
13QQuickRegularPolygonShapePrivate::QQuickRegularPolygonShapePrivate() = default;
14
15QQuickRegularPolygonShapePrivate::~QQuickRegularPolygonShapePrivate() = default;
16
17namespace {
18
19qreal intersect(QVector2D p, QVector2D dir1, QVector2D q, QVector2D dir2)
20{
21 const auto r = dir1.normalized();
22 const auto s = dir2.normalized();
23
24 const QVector2D pq(q.x() - p.x(), q.y() - p.y());
25 const QVector2D snv(s.y(), -s.x());
26
28}
29
30} // namespace
31
32void QQuickRegularPolygonShapePrivate::updatePoints()
33{
34 points.clear();
35
36 const qreal sliceAngle = 360.0f / sideCount;
37 const qreal a = width.valueBypassingBindings() / 2.0f; // x-radius
38 const qreal b = height.valueBypassingBindings() / 2.0f; // y-radius
39 const QVector2D center(a, b);
40
41 for (int i = 0; i < sideCount; ++i) {
42 const qreal angleToCorner = qDegreesToRadians(i * sliceAngle);
43
44 // Start at top center
45 const QVector2D xy = center + QVector2D(a * qSin(angleToCorner), -b * qCos(angleToCorner));
46
47 points.push_back(std::move(xy));
48 }
49}
50
51void QQuickRegularPolygonShapePrivate::updateBisectors()
52{
53 const auto size = points.size();
54
55 bisectors.clear();
56
57 // A list of vectors that are the bisectors of the inner angles of the polygon.
58 // This is used to calculate the intersection point of neighboring bisectors for a corner.
59 // The minimum length of the two neighboring corner bisectors intersection point is the
60 // maximum for the center of the circle that make up the corner radius.
61 for (size_t i = 0; i < size; ++i) {
62 const auto &a = points[i];
63 const auto &b = points[(i == 0 ? size : i) - 1];
64 const auto &c = points[(i + 1 == size) ? 0 : (i + 1)];
65
66 const QVector2D vAB(b.x() - a.x(), b.y() - a.y());
67 const QVector2D vAC(c.x() - a.x(), c.y() - a.y());
68 const auto bisector = (vAB + vAC).normalized();
69
70 bisectors.push_back(std::move(bisector));
71 }
72}
73
74void QQuickRegularPolygonShapePrivate::constructPolygonPath()
75{
76 auto *ppath = QQuickShapePathPrivate::get(path);
77
78 // start path
79 path->setStartX(points[0].x());
80 path->setStartY(points[0].y());
81
82 for (const auto &p : points) {
83 auto line = new QQuickPathLine(path);
84 line->setX(p.x());
85 line->setY(p.y());
86 ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
87 }
88
89 // close path
90 auto line = new QQuickPathLine(path);
91 line->setX(points[0].x());
92 line->setY(points[0].y());
93 ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
94
95 path->processPath();
96}
97
98void QQuickRegularPolygonShapePrivate::constructRoundedPolygonPath()
99{
100 auto *ppath = QQuickShapePathPrivate::get(path);
101
102 updateBisectors();
103
104 const auto size = points.size();
105 for (size_t i = 0; i < size; ++i) {
106 const size_t idxMinusOne = (i == 0 ? size : i) - 1;
107 const size_t idxPlusOne = (i + 1 == size) ? 0 : (i + 1);
108 const auto &a = points[i];
109 const auto &b = points[idxMinusOne];
110 const auto &c = points[idxPlusOne];
111 qreal r = cornerRadius;
112
113 const QVector2D vAB(b.x() - a.x(), b.y() - a.y());
114 const QVector2D vAC(c.x() - a.x(), c.y() - a.y());
115
116 // Calculate the intersection points of the two neighboring bisectors
117 const qreal tAB =
118 intersect(a, bisectors[i],
119 b, bisectors[idxMinusOne]);
120 const qreal tAC =
121 intersect(a, bisectors[i],
122 c, bisectors[idxPlusOne]);
123 const qreal tMax = std::min(tAB, tAC);
124
125 // Angle between the two vectors AB and AC as radians
126 const qreal alpha = qAcos(QVector2D::dotProduct(vAB, vAC) / (vAB.length() * vAC.length()));
127
128 // The maximum radius of the circle that can be drawn at the corner. This is another
129 // constraint that Figma uses to calculate the corner radius. The corner radius shouldn't
130 // be bigger than half of the distance between the two neighboring corners.
131 const qreal maxRadius = std::round(QVector2D(c.x() - b.x(), c.y() - b.y()).length() / 2);
132 r = std::min(r, maxRadius);
133
134 // The optimal length of the corner bisector to place the center of the circle.
135 const qreal cLength = r / (qSin(alpha / 2));
136
137 // Clamp c to the maximum value found from the intersection points of the bisectors.
138 const qreal realC = std::min(cLength, tMax);
139
140 if (realC < cLength)
141 r = realC * qSin(alpha / 2);
142
143 const qreal t = qSqrt(qPow(realC, 2) - qPow(r, 2));
144
145 const auto p1 = (vAB.normalized() * t) + QVector2D(a.x(), a.y());
146 const auto p2 = (vAC.normalized() * t) + QVector2D(a.x(), a.y());
147
148 if (i == 0) {
149 path->setStartX(p1.x());
150 path->setStartY(p1.y());
151 } else {
152 auto line = new QQuickPathLine(path);
153 line->setX(p1.x());
154 line->setY(p1.y());
155 ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
156 }
157
158 auto arc = new QQuickPathArc(path);
159 arc->setX(p2.x());
160 arc->setY(p2.y());
161 arc->setRadiusX(r);
162 arc->setRadiusY(r);
163 ppath->appendPathElement(arc, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
164 }
165
166 // Close the polygon
167 auto line = new QQuickPathLine(path);
168 line->setX(path->startX());
169 line->setY(path->startY());
170 ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
171
172 path->processPath();
173}
174
175void QQuickRegularPolygonShapePrivate::updatePath()
176{
177 QQuickShapePathPrivate::get(path)->clearPathElements(
178 QQuickPathPrivate::DeleteElementPolicy::Delete);
179
180 updatePoints();
181
182 if (qFuzzyCompare(cornerRadius, qreal(0.0)))
183 constructPolygonPath();
184 else
185 constructRoundedPolygonPath();
186}
187
188/*!
189 \qmltype RegularPolygonShape
190 \inqmlmodule QtQuick.Shapes.DesignHelpers
191 \brief A filled regular polygon with an optional border.
192 \since QtQuick 6.10
193
194 A regular polygon can be just a 2D polygon shaped stroke, a filling, or a
195 stroke with filling. The \l strokeColor, \l strokeWidth, and \l strokeStyle
196 properties specify the appearance of the outline. The \l dashPattern and
197 \l dashOffset properties specify the appearance of dashed stroke.
198
199 The area inside the stroke is painted using either a solid fill color,
200 specified using the \l fillColor property, or a gradient, defined using
201 one of the \l ShapeGradient subtypes and set using the \l gradient
202 property. If both a color and a gradient are specified, the gradient is
203 used.
204
205 To create a polygon with a stroke, set the \l sideCount property between 3
206 to 100 and the \l strokeWidth property greater than 0. The \l strokeWidth
207 property specifies the width of the polygon stroke. The default \l sideCount
208 value is 6 and the default \l strokeWidth value is 4. Setting the
209 \l strokeWidth value to a negetive value hides the border.
210
211 The \l cornerRadius property specifies whether the polygon corners are rounded.
212*/
213
214QQuickRegularPolygonShape::QQuickRegularPolygonShape(QQuickItem *parent)
215 : QQuickShape(*(new QQuickRegularPolygonShapePrivate), parent)
216{
217 Q_D(QQuickRegularPolygonShape);
218
219 setPreferredRendererType(CurveRenderer);
220
221 setWidth(200);
222 setHeight(200);
223
224 d->path = new QQuickShapePath(this);
225 d->path->setStrokeWidth(1);
226 d->path->setStrokeColor(QColorConstants::Black);
227 d->path->setFillColor(QColorConstants::White);
228
229 d->sp.append(d->path);
230 d->path->setParent(this);
231 d->extra.value().resourcesList.append(d->path);
232
233 connect(d->path, &QQuickShapePath::strokeColorChanged, this, &QQuickRegularPolygonShape::strokeColorChanged);
234 connect(d->path, &QQuickShapePath::strokeWidthChanged, this, &QQuickRegularPolygonShape::strokeWidthChanged);
235 connect(d->path, &QQuickShapePath::fillColorChanged, this, &QQuickRegularPolygonShape::fillColorChanged);
236 connect(d->path, &QQuickShapePath::joinStyleChanged, this, &QQuickRegularPolygonShape::joinStyleChanged);
237 connect(d->path, &QQuickShapePath::capStyleChanged, this, &QQuickRegularPolygonShape::capStyleChanged);
238 connect(d->path, &QQuickShapePath::strokeStyleChanged, this, &QQuickRegularPolygonShape::strokeStyleChanged);
239 connect(d->path, &QQuickShapePath::dashOffsetChanged, this, &QQuickRegularPolygonShape::dashOffsetChanged);
240 connect(d->path, &QQuickShapePath::dashPatternChanged, this, &QQuickRegularPolygonShape::dashPatternChanged);
241 connect(d->path, &QQuickShapePath::fillItemChanged, this, &QQuickRegularPolygonShape::fillItemChanged);
242 connect(d->path, &QQuickShapePath::fillGradientChanged, this, &QQuickRegularPolygonShape::fillGradientChanged);
243}
244
245QQuickRegularPolygonShape::~QQuickRegularPolygonShape() = default;
246
247/*!
248 \include shapepath.qdocinc {dashOffset-property}
249 {QtQuick.Shapes.DesignHelpers::RegularPolygonShape}
250*/
251
252qreal QQuickRegularPolygonShape::dashOffset() const
253{
254 Q_D(const QQuickRegularPolygonShape);
255 return d->path->dashOffset();
256}
257
258void QQuickRegularPolygonShape::setDashOffset(qreal offset)
259{
260 Q_D(QQuickRegularPolygonShape);
261 d->path->setDashOffset(offset);
262}
263
264/*!
265 \qmlproperty real QtQuick.Shapes.DesignHelpers::RegularPolygonShape::cornerRadius
266
267 The property property specifies whether the polygon corners are rounded.
268
269 The default value is \c 10.
270*/
271
272qreal QQuickRegularPolygonShape::cornerRadius() const
273{
274 Q_D(const QQuickRegularPolygonShape);
275 return d->cornerRadius;
276}
277
278void QQuickRegularPolygonShape::setCornerRadius(qreal radius)
279{
280 Q_D(QQuickRegularPolygonShape);
281 if (qFuzzyCompare(d->cornerRadius, radius))
282 return;
283 d->cornerRadius = radius < 0 ? 0 : radius;
284 d->updatePath();
285 emit cornerRadiusChanged();
286}
287
288/*!
289 \qmlproperty int QtQuick.Shapes.DesignHelpers::RegularPolygonShape::sideCount
290
291 The number of edges on the regular polygon. The minimum number of edges can
292 be 3.
293
294 The default value is \c 6.
295*/
296
297int QQuickRegularPolygonShape::sideCount() const
298{
299 Q_D(const QQuickRegularPolygonShape);
300 return d->sideCount;
301}
302
303void QQuickRegularPolygonShape::setSideCount(int sideCount)
304{
305 Q_D(QQuickRegularPolygonShape);
306 if (d->sideCount == sideCount)
307 return;
308 d->sideCount = std::clamp(sideCount, QQuickRegularPolygonShapePrivate::sideCountMin,
309 QQuickRegularPolygonShapePrivate::sideCountMax);
310 d->updatePath();
311 emit sideCountChanged();
312}
313
314/*!
315 \qmlproperty real QtQuick.Shapes.DesignHelpers::RegularPolygonShape::strokeWidth
316
317 This property holds the stroke width.
318
319 When set to a negative value, no stroking occurs.
320
321 The default value is \c 1.
322*/
323
324qreal QQuickRegularPolygonShape::strokeWidth() const
325{
326 Q_D(const QQuickRegularPolygonShape);
327 return d->path->strokeWidth();
328}
329
330void QQuickRegularPolygonShape::setStrokeWidth(qreal width)
331{
332 Q_D(QQuickRegularPolygonShape);
333 d->path->setStrokeWidth(width);
334 d->updatePath();
335}
336
337/*!
338 \qmlproperty color QtQuick.Shapes.DesignHelpers::RegularPolygonShape::fillColor
339
340 This property holds the fill color.
341
342 When set to \c transparent, no filling occurs.
343
344 The default value is \c "white".
345
346 \note If either \l fillGradient is set to something other than \c null, it
347 will be used instead of \c fillColor.
348*/
349
350QColor QQuickRegularPolygonShape::fillColor() const
351{
352 Q_D(const QQuickRegularPolygonShape);
353 return d->path->fillColor();
354}
355
356void QQuickRegularPolygonShape::setFillColor(const QColor &color)
357{
358 Q_D(QQuickRegularPolygonShape);
359 d->path->setFillColor(color);
360}
361
362/*!
363 \qmlproperty color QtQuick.Shapes.DesignHelpers::RegularPolygonShape::strokeColor
364
365 This property holds the stroking color.
366
367 When set to \c transparent, no stroking occurs.
368
369 The default value is \c "black".
370*/
371
372QColor QQuickRegularPolygonShape::strokeColor() const
373{
374 Q_D(const QQuickRegularPolygonShape);
375 return d->path->strokeColor();
376}
377
378void QQuickRegularPolygonShape::setStrokeColor(const QColor &color)
379{
380 Q_D(QQuickRegularPolygonShape);
381 d->path->setStrokeColor(color);
382}
383
384/*!
385 \include shapepath.qdocinc {capStyle-property}
386 {QtQuick.Shapes.DesignHelpers::RegularPolygonShape}
387
388 Since a polygon is drawn, the path forms a loop with no line end points.
389 Therefore, capStyle is only needed when strokeStyle == ShapePath.DashLine
390*/
391
392QQuickShapePath::CapStyle QQuickRegularPolygonShape::capStyle() const
393{
394 Q_D(const QQuickRegularPolygonShape);
395 return d->path->capStyle();
396}
397
398void QQuickRegularPolygonShape::setCapStyle(QQuickShapePath::CapStyle style)
399{
400 Q_D(QQuickRegularPolygonShape);
401 d->path->setCapStyle(style);
402}
403
404/*!
405 \include shapepath.qdocinc {joinStyle-property}
406 {QtQuick.Shapes.DesignHelpers::RegularPolygonShape}
407
408 The joinStyle is only meaningful if cornerRadius == 0.
409*/
410
411QQuickShapePath::JoinStyle QQuickRegularPolygonShape::joinStyle() const
412{
413 Q_D(const QQuickRegularPolygonShape);
414 return d->path->joinStyle();
415}
416
417void QQuickRegularPolygonShape::setJoinStyle(QQuickShapePath::JoinStyle style)
418{
419 Q_D(QQuickRegularPolygonShape);
420 d->path->setJoinStyle(style);
421}
422
423/*!
424 \include shapepath.qdocinc {strokeStyle-property}
425 {QtQuick.Shapes.DesignHelpers::RegularPolygonShape}
426*/
427
428QQuickShapePath::StrokeStyle QQuickRegularPolygonShape::strokeStyle() const
429{
430 Q_D(const QQuickRegularPolygonShape);
431 return d->path->strokeStyle();
432}
433
434void QQuickRegularPolygonShape::setStrokeStyle(QQuickShapePath::StrokeStyle style)
435{
436 Q_D(QQuickRegularPolygonShape);
437 d->path->setStrokeStyle(style);
438}
439
440/*!
441 \include shapepath.qdocinc {dashPattern-property}
442 {QtQuick.Shapes.DesignHelpers::RegularPolygonShape}
443*/
444
445QList<qreal> QQuickRegularPolygonShape::dashPattern() const
446{
447 Q_D(const QQuickRegularPolygonShape);
448 return d->path->dashPattern();
449}
450
451void QQuickRegularPolygonShape::setDashPattern(const QList<qreal> &array)
452{
453 Q_D(QQuickRegularPolygonShape);
454 d->path->setDashPattern(array);
455}
456
457/*!
458 \qmlproperty ShapeGradient QtQuick.Shapes.DesignHelpers::RegularPolygonShape::fillGradient
459
460 The fillGradient of the polygon fill color.
461
462 By default, no fillGradient is enabled and the value is null. In this case, the
463 fill uses a solid color based on the value of \l fillColor.
464
465 When set, \l fillColor is ignored and filling is done using one of the
466 \l ShapeGradient subtypes.
467
468 \note The \l Gradient type cannot be used here. Rather, prefer using one of
469 the advanced subtypes, like \l LinearGradient.
470*/
471QQuickShapeGradient *QQuickRegularPolygonShape::fillGradient() const
472{
473 Q_D(const QQuickRegularPolygonShape);
474 return d->path->fillGradient();
475}
476
477void QQuickRegularPolygonShape::setFillGradient(QQuickShapeGradient *fillGradient)
478{
479 Q_D(QQuickRegularPolygonShape);
480 d->path->setFillGradient(fillGradient);
481}
482
483void QQuickRegularPolygonShape::resetFillGradient()
484{
485 setFillGradient(nullptr);
486}
487
488QQuickItem *QQuickRegularPolygonShape::fillItem() const
489{
490 Q_D(const QQuickRegularPolygonShape);
491 return d->path->fillItem();
492}
493
494void QQuickRegularPolygonShape::setFillItem(QQuickItem *newFillItem)
495{
496 Q_D(QQuickRegularPolygonShape);
497 d->path->setFillItem(newFillItem);
498}
499
500QRectF QQuickRegularPolygonShape::boundingRect() const
501{
502 Q_D(const QQuickRegularPolygonShape);
503
504 QRectF bounds;
505 for (const auto path : d->sp) {
506 const auto painterPath = path->path();
507 bounds = bounds.united(painterPath.boundingRect());
508 if (path->strokeWidth() > 0 && path->strokeColor().alpha() > 0) {
509 QPainterPathStroker stroker;
510 stroker.setWidth(path->strokeWidth());
511 stroker.setJoinStyle(Qt::PenJoinStyle(path->joinStyle()));
512 stroker.setCapStyle(Qt::PenCapStyle(path->capStyle()));
513 stroker.setMiterLimit(path->miterLimit());
514 bounds = bounds.united(stroker.createStroke(painterPath).boundingRect());
515 }
516 }
517
518 return bounds;
519}
520
521void QQuickRegularPolygonShape::componentComplete()
522{
523 Q_D(QQuickRegularPolygonShape);
524
525 if (d->path)
526 d->updatePath();
527
528 QQuickShape::componentComplete();
529}
530
531void QQuickRegularPolygonShape::geometryChange(const QRectF &newGeometry, const QRectF &oldGeometry)
532{
533 Q_D(QQuickRegularPolygonShape);
534
535 QQuickShape::geometryChange(newGeometry, oldGeometry);
536
537 if (d->path && newGeometry.size() != oldGeometry.size())
538 d->updatePath();
539}
540
541QT_END_NAMESPACE
542
543#include "moc_qquickregularpolygonshape_p.cpp"
The QVector2D class represents a vector or vertex in 2D space.
Definition qvectornd.h:34
constexpr float y() const noexcept
Returns the y coordinate of this point.
Definition qvectornd.h:518
QVector2D normalized() const noexcept
Returns the normalized unit vector form of this vector.
Definition qvectornd.h:545
constexpr float x() const noexcept
Returns the x coordinate of this point.
Definition qvectornd.h:517
static constexpr float dotProduct(QVector2D v1, QVector2D v2) noexcept
Returns the dot product of v1 and v2.
Definition qvectornd.h:620