46void QQuickStarShapePrivate::updatePoints()
50 const qreal rectWidth = width.valueBypassingBindings();
51 const qreal rectHeight = height.valueBypassingBindings();
53 const QVector2D center(rectWidth * 0.5, rectHeight * 0.5);
54 const QVector2D radius(rectWidth * 0.5, rectHeight * 0.5);
55 const QVector2D inner_radius = radius * std::min(std::max(ratio, 0.001), 1.0);
57 const int numPoints = pointCount * 2;
58 const qreal sliceAngle = (360.0f / numPoints);
59 for (
int i = 0; i < numPoints; ++i) {
60 const qreal angle = i * sliceAngle;
61 const auto p = arc_point(center, i % 2 == 0 ? radius : inner_radius, arc_angle(angle));
62 points.emplace_back(std::move(p));
66void QQuickStarShapePrivate::constructPolygonPath()
68 auto *ppath = QQuickShapePathPrivate::get(path);
70 path->setStartX(points[0].x());
71 path->setStartY(points[0].y());
73 for (
const auto &p : points) {
74 auto line =
new QQuickPathLine(path);
77 ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
80 auto line =
new QQuickPathLine(path);
81 line->setX(points[0].x());
82 line->setY(points[0].y());
83 ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
88void QQuickStarShapePrivate::constructRoundedPolygonPath()
90 const auto size = points.size();
92 auto *ppath = QQuickShapePathPrivate::get(path);
94 for (size_t i = 0; i < size; ++i) {
95 const auto &a = points[i];
96 const auto &b = points[(i == 0 ? size : i) - 1];
97 const auto &c = points[(i + 1 == size) ? 0 : (i + 1)];
99 const QVector2D ab = b - a;
100 const QVector2D ac = c - a;
102 const qreal alpha = angle_between_vectors(ab, ac);
103 const qreal halfAngle = std::fabs(alpha) * 0.5;
105 qreal corner_radius = cornerRadius;
107 auto edgeOffset = corner_radius / qTan(halfAngle);
108 const qreal edge = std::min(ab.length(), ac.length());
109 if (edgeOffset > edge * 0.5) {
110 edgeOffset = edge * 0.5;
111 corner_radius = edgeOffset * qTan(halfAngle);
114 const auto B = a + ab.normalized() * edgeOffset;
115 const auto C = a + ac.normalized() * edgeOffset;
118 path->setStartX(B.x());
119 path->setStartY(B.y());
121 auto line =
new QQuickPathLine(path);
124 ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
127 auto arc =
new QQuickPathArc(path);
130 arc->setRadiusX(corner_radius);
131 arc->setRadiusY(corner_radius);
132 arc->setDirection(alpha > 0 ? QQuickPathArc::ArcDirection::Counterclockwise
133 : QQuickPathArc::ArcDirection::Clockwise);
134 ppath->appendPathElement(arc, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
137 auto line =
new QQuickPathLine(path);
138 line->setX(path->startX());
139 line->setY(path->startY());
140 ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
145void QQuickStarShapePrivate::updatePath()
147 QQuickShapePathPrivate::get(path)->clearPathElements(
148 QQuickPathPrivate::DeleteElementPolicy::Delete);
152 if (qFuzzyCompare(cornerRadius, 0.0))
153 constructPolygonPath();
155 constructRoundedPolygonPath();
185QQuickStarShape::QQuickStarShape(QQuickItem *parent)
186 : QQuickShape(*(
new QQuickStarShapePrivate), parent)
188 Q_D(QQuickStarShape);
190 setPreferredRendererType(CurveRenderer);
195 d->path =
new QQuickShapePath(
this);
196 d->path->setAsynchronous(
true);
197 d->path->setStrokeWidth(1);
198 d->path->setStrokeColor(QColorConstants::Black);
199 d->path->setFillColor(QColorConstants::White);
201 d->sp.append(d->path);
202 d->path->setParent(
this);
203 d->extra.value().resourcesList.append(d->path);
219void QQuickStarShape::setDashOffset(qreal offset)
221 Q_D(QQuickStarShape);
222 if (qFuzzyCompare(d->path->dashOffset(), offset))
224 d->path->setDashOffset(offset);
225 emit dashOffsetChanged();
243void QQuickStarShape::setCornerRadius(qreal radius)
245 Q_D(QQuickStarShape);
246 if (qFuzzyCompare(d->cornerRadius, radius))
248 d->cornerRadius = radius;
250 emit cornerRadiusChanged();
318void QQuickStarShape::setStrokeWidth(qreal width)
320 Q_D(QQuickStarShape);
321 if (qFuzzyCompare(d->path->strokeWidth(), width))
323 d->path->setStrokeWidth(width);
324 emit strokeWidthChanged();