1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
|
// Copyright (C) 2025 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
#include "qquickstarshape_p.h"
#include "qquickstarshape_p_p.h"
#include <algorithm>
#include <QtMath>
#include <cstddef>
QT_BEGIN_NAMESPACE
namespace {
inline int wrapIndex(int index, int size)
{
return (index + size) % size;
}
inline qreal arc_angle(qreal angle)
{
return angle - 90;
}
inline QVector2D arc_point(QVector2D center, QVector2D radius, qreal angle)
{
return QVector2D(center.x() + radius.x() * qCos(qDegreesToRadians(angle)),
center.y() + radius.y() * qSin(qDegreesToRadians(angle)));
}
inline qreal cross(QVector2D a, QVector2D b)
{
return a.x() * b.y() - a.y() * b.x();
}
qreal angle_between_vectors(QVector2D a, QVector2D b)
{
const QVector2D uA = a.normalized();
const QVector2D uB = b.normalized();
const qreal angle = qAtan2(cross(uA, uB), QVector2D::dotProduct(uA, uB));
if (std::fabs(angle) < FLT_EPSILON)
return 0.0f;
return angle;
}
} // namespace
QQuickStarShapePrivate::QQuickStarShapePrivate() = default;
QQuickStarShapePrivate::~QQuickStarShapePrivate() = default;
void QQuickStarShapePrivate::updatePoints()
{
points.clear();
const qreal rectWidth = width.valueBypassingBindings();
const qreal rectHeight = height.valueBypassingBindings();
const QVector2D center(rectWidth * 0.5, rectHeight * 0.5);
const QVector2D radius(rectWidth * 0.5, rectHeight * 0.5);
const QVector2D inner_radius = radius * std::min(std::max(ratio, 0.001), 1.0);
const int numPoints = pointCount * 2;
const qreal sliceAngle = (360.0f / numPoints);
for (int i = 0; i < numPoints; ++i) {
const qreal angle = i * sliceAngle;
const auto p = arc_point(center, i % 2 == 0 ? radius : inner_radius, arc_angle(angle));
points.emplace_back(std::move(p));
}
}
void QQuickStarShapePrivate::constructPolygonPath()
{
auto *ppath = QQuickShapePathPrivate::get(path);
path->setStartX(points[0].x());
path->setStartY(points[0].y());
for (const auto &p : points) {
auto line = new QQuickPathLine(path);
line->setX(p.x());
line->setY(p.y());
ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
}
auto line = new QQuickPathLine(path);
line->setX(points[0].x());
line->setY(points[0].y());
ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
path->processPath();
}
void QQuickStarShapePrivate::constructRoundedPolygonPath()
{
const auto size = points.size();
auto *ppath = QQuickShapePathPrivate::get(path);
for (size_t i = 0; i < size; ++i) {
const auto &a = points[wrapIndex(i, size)];
const auto &b = points[wrapIndex(i - 1, size)];
const auto &c = points[wrapIndex(i + 1, size)];
const QVector2D ab = b - a;
const QVector2D ac = c - a;
const qreal alpha = angle_between_vectors(ab, ac);
const qreal halfAngle = std::fabs(alpha) * 0.5;
qreal corner_radius = cornerRadius;
auto edgeOffset = corner_radius / qTan(halfAngle);
const qreal edge = std::min(ab.length(), ac.length());
if (edgeOffset > edge * 0.5) {
edgeOffset = edge * 0.5;
corner_radius = edgeOffset * qTan(halfAngle);
}
const auto B = a + ab.normalized() * edgeOffset;
const auto C = a + ac.normalized() * edgeOffset;
if (i == 0) {
path->setStartX(B.x());
path->setStartY(B.y());
} else {
auto line = new QQuickPathLine(path);
line->setX(B.x());
line->setY(B.y());
ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
}
auto arc = new QQuickPathArc(path);
arc->setX(C.x());
arc->setY(C.y());
arc->setRadiusX(corner_radius);
arc->setRadiusY(corner_radius);
arc->setDirection(alpha > 0 ? QQuickPathArc::ArcDirection::Counterclockwise
: QQuickPathArc::ArcDirection::Clockwise);
ppath->appendPathElement(arc, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
}
auto line = new QQuickPathLine(path);
line->setX(path->startX());
line->setY(path->startY());
ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
path->processPath();
}
void QQuickStarShapePrivate::updatePath()
{
QQuickShapePathPrivate::get(path)->clearPathElements(
QQuickPathPrivate::DeleteElementPolicy::Delete);
updatePoints();
if (qFuzzyCompare(cornerRadius, 0.0))
constructPolygonPath();
else
constructRoundedPolygonPath();
}
/*!
\qmltype StarShape
\inqmlmodule QtQuick.Shapes.DesignHelpers
\brief A filled star-shaped polygon with an optional border.
\since QtQuick 6.10
A star can be a star shaped stroke, a filling, or a stroke with filling.
The \l strokeColor, \l strokeWidth, and \l strokeStyle properties specify
the appearance of the outline. The \l dashPattern and \l dashOffset
properties specify the appearance of dashed stroke.
Set the \l pointCount property between 3 and 60 to specify the number of
points of the star. Set the \l ratio between 0.1 and 1 to specify the
distance of the inner points of the star from the center.
The area inside the stroke is painted using either a solid fill color, specified using the
\l fillColor property, or a gradient, defined using one of the \l ShapeGradient subtypes and set
using the \l gradient property. If both a color and a gradient are specified, the gradient is
used.
To create a star with a stroke, set the \l strokeWidth property to a value greater than 0. The
\l strokeWidth property specifies the width of the polygon stroke. The default \l pointCount
value is 6 and the default \l strokeWidth value is 4. Setting the \l strokeWidth value to a
negative value hides the border.
The \l cornerRadius property specifies whether the star corners are rounded.
*/
QQuickStarShape::QQuickStarShape(QQuickItem *parent)
: QQuickShape(*(new QQuickStarShapePrivate), parent)
{
Q_D(QQuickStarShape);
setPreferredRendererType(CurveRenderer);
setWidth(200);
setHeight(200);
d->path = new QQuickShapePath(this);
d->path->setAsynchronous(true);
d->path->setStrokeWidth(1);
d->path->setStrokeColor(QColorConstants::Black);
d->path->setFillColor(QColorConstants::White);
d->sp.append(d->path);
d->path->setParent(this);
d->extra.value().resourcesList.append(d->path);
}
QQuickStarShape::~QQuickStarShape() = default;
/*!
\include shapepath.qdocinc {dashOffset-property}
{QtQuick.Shapes.DesignHelpers::StarShape}
*/
qreal QQuickStarShape::dashOffset() const
{
Q_D(const QQuickStarShape);
return d->path->dashOffset();
}
void QQuickStarShape::setDashOffset(qreal offset)
{
Q_D(QQuickStarShape);
if (qFuzzyCompare(d->path->dashOffset(), offset))
return;
d->path->setDashOffset(offset);
emit dashOffsetChanged();
}
/*!
\qmlproperty real QtQuick.Shapes.DesignHelpers::StarShape::cornerRadius
The property controls the rounding of both the star's outer points and
inner points.
The default value is \c 10.
*/
qreal QQuickStarShape::cornerRadius() const
{
Q_D(const QQuickStarShape);
return d->cornerRadius;
}
void QQuickStarShape::setCornerRadius(qreal radius)
{
Q_D(QQuickStarShape);
if (qFuzzyCompare(d->cornerRadius, radius))
return;
d->cornerRadius = radius;
d->updatePath();
emit cornerRadiusChanged();
}
/*!
\qmlproperty real QtQuick.Shapes.DesignHelpers::StarShape::ratio
The property defines the distance of the inner points of the star from the
center.
The default value is \c 0.5.
*/
qreal QQuickStarShape::ratio() const
{
Q_D(const QQuickStarShape);
return d->ratio;
}
void QQuickStarShape::setRatio(qreal ratio)
{
Q_D(QQuickStarShape);
if (qFuzzyCompare(d->ratio, ratio))
return;
d->ratio = ratio;
d->updatePath();
emit ratioChanged();
}
/*!
\qmlproperty int QtQuick.Shapes.DesignHelpers::StarShape::pointCount
The property defines the total number of points the star has.
The default value is \c 6.
*/
int QQuickStarShape::pointCount() const
{
Q_D(const QQuickStarShape);
return d->pointCount;
}
void QQuickStarShape::setPointCount(int count)
{
Q_D(QQuickStarShape);
if (d->pointCount == count)
return;
d->pointCount = count;
d->updatePath();
emit pointCountChanged();
}
/*!
\qmlproperty real QtQuick.Shapes.DesignHelpers::StarShape::strokeWidth
This property holds the stroke width.
When set to a negative value, no stroking occurs.
The default value is \c 1.
*/
qreal QQuickStarShape::strokeWidth() const
{
Q_D(const QQuickStarShape);
return d->path->strokeWidth();
}
void QQuickStarShape::setStrokeWidth(qreal width)
{
Q_D(QQuickStarShape);
if (qFuzzyCompare(d->path->strokeWidth(), width))
return;
d->path->setStrokeWidth(width);
emit strokeWidthChanged();
}
/*!
\qmlproperty color QtQuick.Shapes.DesignHelpers::StarShape::fillColor
This property holds the fill color.
When set to \c transparent, no filling occurs.
The default value is \c "white".
\note If either \l fillGradient is set to something other than \c null, it
will be used instead of \c fillColor.
*/
QColor QQuickStarShape::fillColor() const
{
Q_D(const QQuickStarShape);
return d->path->fillColor();
}
void QQuickStarShape::setFillColor(const QColor &color)
{
Q_D(QQuickStarShape);
d->path->setFillColor(color);
d->updatePath();
emit fillColorChanged();
}
/*!
\qmlproperty color QtQuick.Shapes.DesignHelpers::StarShape::strokeColor
This property holds the stroking color.
When set to \c transparent, no stroking occurs.
The default value is \c "black".
*/
QColor QQuickStarShape::strokeColor() const
{
Q_D(const QQuickStarShape);
return d->path->strokeColor();
}
void QQuickStarShape::setStrokeColor(const QColor &color)
{
Q_D(QQuickStarShape);
d->path->setStrokeColor(color);
emit strokeColorChanged();
}
/*!
\include shapepath.qdocinc {capStyle-property}
{QtQuick.Shapes.DesignHelpers::StarShape}
Since a star is drawn, the path forms a loop with no line end points.
Therefore, capStyle is only needed when strokeStyle == ShapePath.DashLine
*/
QQuickShapePath::CapStyle QQuickStarShape::capStyle() const
{
Q_D(const QQuickStarShape);
return d->path->capStyle();
}
void QQuickStarShape::setCapStyle(QQuickShapePath::CapStyle style)
{
Q_D(QQuickStarShape);
if (d->path->capStyle() == style)
return;
d->path->setCapStyle(style);
emit capStyleChanged();
}
/*!
\include shapepath.qdocinc {joinStyle-property}
{QtQuick.Shapes.DesignHelpers::StarShape}
The joinStyle is only meaningful if cornerRadius == 0.
*/
QQuickShapePath::JoinStyle QQuickStarShape::joinStyle() const
{
Q_D(const QQuickStarShape);
return d->path->joinStyle();
}
void QQuickStarShape::setJoinStyle(QQuickShapePath::JoinStyle style)
{
Q_D(QQuickStarShape);
if (d->path->joinStyle() == style)
return;
d->path->setJoinStyle(style);
emit joinStyleChanged();
}
/*!
\include shapepath.qdocinc {strokeStyle-property}
{QtQuick.Shapes.DesignHelpers::StarShape}
*/
QQuickShapePath::StrokeStyle QQuickStarShape::strokeStyle() const
{
Q_D(const QQuickStarShape);
return d->path->strokeStyle();
}
void QQuickStarShape::setStrokeStyle(QQuickShapePath::StrokeStyle style)
{
Q_D(QQuickStarShape);
if (d->path->strokeStyle() == style)
return;
d->path->setStrokeStyle(style);
emit strokeStyleChanged();
}
/*!
\include shapepath.qdocinc {dashPattern-property}
{QtQuick.Shapes.DesignHelpers::StarShape}
*/
QVector<qreal> QQuickStarShape::dashPattern() const
{
Q_D(const QQuickStarShape);
return d->path->dashPattern();
}
void QQuickStarShape::setDashPattern(const QVector<qreal> &array)
{
Q_D(QQuickStarShape);
d->path->setDashPattern(array);
emit dashPatternChanged();
}
/*!
\qmlproperty ShapeGradient QtQuick.Shapes.DesignHelpers::StarShape::fillGradient
The fillGradient of the star fill color.
By default, no fillGradient is enabled and the value is null. In this case, the
fill uses a solid color based on the value of \l fillColor.
When set, \l fillColor is ignored and filling is done using one of the
\l ShapeGradient subtypes.
\note The \l Gradient type cannot be used here. Rather, prefer using one of
the advanced subtypes, like \l LinearGradient.
*/
QQuickShapeGradient *QQuickStarShape::fillGradient() const
{
Q_D(const QQuickStarShape);
return d->path->fillGradient();
}
void QQuickStarShape::setFillGradient(QQuickShapeGradient *fillGradient)
{
Q_D(QQuickStarShape);
d->path->setFillGradient(fillGradient);
emit gradientChanged();
}
void QQuickStarShape::resetFillGradient()
{
setFillGradient(nullptr);
}
void QQuickStarShape::itemChange(ItemChange change, const ItemChangeData &value)
{
Q_D(QQuickStarShape);
if (d->path)
d->updatePath();
QQuickItem::itemChange(change, value);
}
QT_END_NAMESPACE
#include "moc_qquickstarshape_p.cpp"
|