47void QQuickStarShapePrivate::updatePoints()
51 const qreal rectWidth = width.valueBypassingBindings();
52 const qreal rectHeight = height.valueBypassingBindings();
54 const QVector2D center(rectWidth * 0.5, rectHeight * 0.5);
55 const QVector2D radius(rectWidth * 0.5, rectHeight * 0.5);
56 const QVector2D inner_radius = radius * std::min(std::max(ratio, 0.001), 1.0);
58 const int numPoints = pointCount * 2;
59 const qreal sliceAngle = (360.0f / numPoints);
60 for (
int i = 0; i < numPoints; ++i) {
61 const qreal angle = i * sliceAngle;
62 const auto p = arc_point(center, i % 2 == 0 ? radius : inner_radius, arc_angle(angle));
63 points.emplace_back(std::move(p));
67void QQuickStarShapePrivate::constructPolygonPath()
69 auto *ppath = QQuickShapePathPrivate::get(path);
71 path->setStartX(points[0].x());
72 path->setStartY(points[0].y());
74 for (
const auto &p : points) {
75 auto line =
new QQuickPathLine(path);
78 ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
81 auto line =
new QQuickPathLine(path);
82 line->setX(points[0].x());
83 line->setY(points[0].y());
84 ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
89void QQuickStarShapePrivate::constructRoundedPolygonPath()
91 const auto size = points.size();
93 auto *ppath = QQuickShapePathPrivate::get(path);
95 for (size_t i = 0; i < size; ++i) {
96 const auto &a = points[i];
97 const auto &b = points[(i == 0 ? size : i) - 1];
98 const auto &c = points[(i + 1 == size) ? 0 : (i + 1)];
100 const QVector2D ab = b - a;
101 const QVector2D ac = c - a;
103 const qreal alpha = angle_between_vectors(ab, ac);
104 const qreal halfAngle = std::fabs(alpha) * 0.5;
106 qreal corner_radius = cornerRadius;
108 auto edgeOffset = corner_radius / qTan(halfAngle);
109 const qreal edge = std::min(ab.length(), ac.length());
110 if (edgeOffset > edge * 0.5) {
111 edgeOffset = edge * 0.5;
112 corner_radius = edgeOffset * qTan(halfAngle);
115 const auto B = a + ab.normalized() * edgeOffset;
116 const auto C = a + ac.normalized() * edgeOffset;
119 path->setStartX(B.x());
120 path->setStartY(B.y());
122 auto line =
new QQuickPathLine(path);
125 ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
128 auto arc =
new QQuickPathArc(path);
131 arc->setRadiusX(corner_radius);
132 arc->setRadiusY(corner_radius);
133 arc->setDirection(alpha > 0 ? QQuickPathArc::ArcDirection::Counterclockwise
134 : QQuickPathArc::ArcDirection::Clockwise);
135 ppath->appendPathElement(arc, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
138 auto line =
new QQuickPathLine(path);
139 line->setX(path->startX());
140 line->setY(path->startY());
141 ppath->appendPathElement(line, QQuickPathPrivate::ProcessPathPolicy::DontProcess);
146void QQuickStarShapePrivate::updatePath()
148 QQuickShapePathPrivate::get(path)->clearPathElements(
149 QQuickPathPrivate::DeleteElementPolicy::Delete);
153 if (qFuzzyCompare(cornerRadius, 0.0))
154 constructPolygonPath();
156 constructRoundedPolygonPath();
186QQuickStarShape::QQuickStarShape(QQuickItem *parent)
187 : QQuickShape(*(
new QQuickStarShapePrivate), parent)
189 Q_D(QQuickStarShape);
191 setPreferredRendererType(CurveRenderer);
196 d->path =
new QQuickShapePath(
this);
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);
205 connect(d->path, &QQuickShapePath::strokeColorChanged,
this, &QQuickStarShape::strokeColorChanged);
206 connect(d->path, &QQuickShapePath::strokeWidthChanged,
this, &QQuickStarShape::strokeWidthChanged);
207 connect(d->path, &QQuickShapePath::fillColorChanged,
this, &QQuickStarShape::fillColorChanged);
208 connect(d->path, &QQuickShapePath::joinStyleChanged,
this, &QQuickStarShape::joinStyleChanged);
209 connect(d->path, &QQuickShapePath::capStyleChanged,
this, &QQuickStarShape::capStyleChanged);
210 connect(d->path, &QQuickShapePath::strokeStyleChanged,
this, &QQuickStarShape::strokeStyleChanged);
211 connect(d->path, &QQuickShapePath::dashOffsetChanged,
this, &QQuickStarShape::dashOffsetChanged);
212 connect(d->path, &QQuickShapePath::dashPatternChanged,
this, &QQuickStarShape::dashPatternChanged);
213 connect(d->path, &QQuickShapePath::fillItemChanged,
this, &QQuickStarShape::fillItemChanged);
214 connect(d->path, &QQuickShapePath::fillGradientChanged,
this, &QQuickStarShape::fillGradientChanged);