11#pragma once
22
3- #include < QPolygonF>
4- #include < QScopedPointer>
3+ #include " graphicsutils.hpp"
4+
5+ #include < QPainterPath>
56
67namespace Graphics {
78
@@ -12,37 +13,62 @@ class GeometryCache
1213 GeometryCache () = default ;
1314 ~GeometryCache () = default ;
1415
15- void setAnchorPoints (const QPolygonF &pts, const QRectF &boundingRect)
16+ void setAnchorPoints (const QPolygonF &pts) { setAnchorPoints (pts, {}, {}); }
17+
18+ void setAnchorPoints (const QPolygonF &pts, const QRectF &boundingRect, const QPainterPath &shape)
1619 {
1720 m_anchorPoints = pts;
1821 m_boundingRect = boundingRect;
19- m_changed = true ;
22+ m_shape = shape;
23+ m_boundingRectDirty = true ;
24+ m_shapeDirty = true ;
2025 }
2126
2227 QPolygonF anchorPoints () const { return m_anchorPoints; }
2328
24- QRectF boundingRect (double margin, double penWidth)
29+ QRectF boundingRect (double margin, double penWidth, double expandSize)
30+ {
31+ auto addLen = calculateExpandSize (margin, penWidth, expandSize);
32+ if (addLen != m_rectAddLen || m_boundingRectDirty) {
33+ m_boundingRectDirty = false ;
34+ m_rectAddLen = addLen;
35+ m_cacheBoundingRect = m_boundingRect.adjusted (-addLen, -addLen, addLen, addLen);
36+ }
37+ return m_cacheBoundingRect;
38+ }
39+
40+ QPainterPath shape (double margin, double penWidth, double expandSize)
2541 {
26- auto addLen = qMax (margin / 2 , penWidth);
27- if (addLen == m_lastAddLen && !m_changed) {
28- return m_lastBoundingRect;
42+ auto addLen = calculateExpandSize (margin, penWidth, expandSize);
43+ if (addLen != m_shapeAddLen || m_shapeDirty) {
44+ m_shapeDirty = false ;
45+ m_shapeAddLen = addLen;
46+ m_cachedShape = m_shape;
47+ m_cachedShape = Utils::expandAndUnitePath (m_cachedShape, addLen);
2948 }
30- m_changed = false ;
31- m_lastAddLen = addLen;
32- m_lastBoundingRect = m_boundingRect.adjusted (-addLen, -addLen, addLen, addLen);
33- return m_lastBoundingRect;
49+ return m_cachedShape;
3450 }
3551
3652 bool isValid () const { return !m_boundingRect.isNull () && !m_anchorPoints.isEmpty (); }
3753 void invalidate () { m_boundingRect = QRectF (); }
3854
3955private:
56+ double calculateExpandSize (double margin, double penWidth, double expandSize) const
57+ {
58+ return std::max ({margin * 0.5 , penWidth, expandSize});
59+ }
60+
4061 QPolygonF m_anchorPoints; // 用于交互的锚点
4162 QRectF m_boundingRect; // 边界矩形
63+ QPainterPath m_shape; // 路径
64+
65+ bool m_boundingRectDirty = true ; // 矩形是否需要重新计算
66+ double m_rectAddLen = 0 ; // 矩形扩展长度
67+ QRectF m_cacheBoundingRect; // 缓存矩形
4268
43- bool m_changed = true ;
44- double m_lastAddLen = 0 ; // 上一次的addLen
45- QRectF m_lastBoundingRect ; // 上一次的boundingRect
69+ bool m_shapeDirty = true ; // 路径是否需要重新计算
70+ double m_shapeAddLen = 0 ; // 路径扩展长度
71+ QPainterPath m_cachedShape ; // 缓存路径
4672};
4773
4874using GeometryCachePtr = QScopedPointer<GeometryCache>;
0 commit comments