1// Copyright (C) 2016 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
4#ifndef QPAINTERPATH_H
5#define QPAINTERPATH_H
6
7#include <QtGui/qtguiglobal.h>
8#include <QtGui/qtransform.h>
9#include <QtCore/qglobal.h>
10#include <QtCore/qline.h>
11#include <QtCore/qlist.h>
12#include <QtCore/qrect.h>
13#include <QtCore/qshareddata.h>
14
15QT_BEGIN_NAMESPACE
16
17
18class QFont;
19class QPainterPathPrivate;
20struct QPainterPathPrivateDeleter;
21class QPainterPathStrokerPrivate;
22class QPen;
23class QPolygonF;
24class QRegion;
25class QTransform;
26class QVectorPath;
27
28class Q_GUI_EXPORT QPainterPath
29{
30public:
31 enum ElementType {
32 MoveToElement,
33 LineToElement,
34 CurveToElement,
35 CurveToDataElement
36 };
37
38 class Element {
39 public:
40 qreal x;
41 qreal y;
42 ElementType type;
43
44 bool isMoveTo() const { return type == MoveToElement; }
45 bool isLineTo() const { return type == LineToElement; }
46 bool isCurveTo() const { return type == CurveToElement; }
47
48 operator QPointF () const { return QPointF(x, y); }
49
50 bool operator==(const Element &e) const { return qFuzzyCompare(p1: x, p2: e.x)
51 && qFuzzyCompare(p1: y, p2: e.y) && type == e.type; }
52 inline bool operator!=(const Element &e) const { return !operator==(e); }
53 };
54
55 QPainterPath() noexcept;
56 explicit QPainterPath(const QPointF &startPoint);
57 QPainterPath(const QPainterPath &other);
58 QPainterPath &operator=(const QPainterPath &other);
59 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPainterPath)
60 ~QPainterPath();
61
62 inline void swap(QPainterPath &other) noexcept { d_ptr.swap(other&: other.d_ptr); }
63
64 void clear();
65 void reserve(int size);
66 int capacity() const;
67
68 void closeSubpath();
69
70 void moveTo(const QPointF &p);
71 inline void moveTo(qreal x, qreal y);
72
73 void lineTo(const QPointF &p);
74 inline void lineTo(qreal x, qreal y);
75
76 void arcMoveTo(const QRectF &rect, qreal angle);
77 inline void arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle);
78
79 void arcTo(const QRectF &rect, qreal startAngle, qreal arcLength);
80 inline void arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength);
81
82 void cubicTo(const QPointF &ctrlPt1, const QPointF &ctrlPt2, const QPointF &endPt);
83 inline void cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
84 qreal endPtx, qreal endPty);
85 void quadTo(const QPointF &ctrlPt, const QPointF &endPt);
86 inline void quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty);
87
88 QPointF currentPosition() const;
89
90 void addRect(const QRectF &rect);
91 inline void addRect(qreal x, qreal y, qreal w, qreal h);
92 void addEllipse(const QRectF &rect);
93 inline void addEllipse(qreal x, qreal y, qreal w, qreal h);
94 inline void addEllipse(const QPointF &center, qreal rx, qreal ry);
95 void addPolygon(const QPolygonF &polygon);
96 void addText(const QPointF &point, const QFont &f, const QString &text);
97 inline void addText(qreal x, qreal y, const QFont &f, const QString &text);
98 void addPath(const QPainterPath &path);
99 void addRegion(const QRegion &region);
100
101 void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
102 Qt::SizeMode mode = Qt::AbsoluteSize);
103 inline void addRoundedRect(qreal x, qreal y, qreal w, qreal h,
104 qreal xRadius, qreal yRadius,
105 Qt::SizeMode mode = Qt::AbsoluteSize);
106
107 void connectPath(const QPainterPath &path);
108
109 bool contains(const QPointF &pt) const;
110 bool contains(const QRectF &rect) const;
111 bool intersects(const QRectF &rect) const;
112
113 void translate(qreal dx, qreal dy);
114 inline void translate(const QPointF &offset);
115
116 [[nodiscard]] QPainterPath translated(qreal dx, qreal dy) const;
117 [[nodiscard]] inline QPainterPath translated(const QPointF &offset) const;
118
119 QRectF boundingRect() const;
120 QRectF controlPointRect() const;
121
122 Qt::FillRule fillRule() const;
123 void setFillRule(Qt::FillRule fillRule);
124
125 bool isEmpty() const;
126
127 [[nodiscard]] QPainterPath toReversed() const;
128
129 QList<QPolygonF> toSubpathPolygons(const QTransform &matrix = QTransform()) const;
130 QList<QPolygonF> toFillPolygons(const QTransform &matrix = QTransform()) const;
131 QPolygonF toFillPolygon(const QTransform &matrix = QTransform()) const;
132
133 int elementCount() const;
134 QPainterPath::Element elementAt(int i) const;
135 void setElementPositionAt(int i, qreal x, qreal y);
136
137 qreal length() const;
138 qreal percentAtLength(qreal t) const;
139 QPointF pointAtPercent(qreal t) const;
140 qreal angleAtPercent(qreal t) const;
141 qreal slopeAtPercent(qreal t) const;
142
143 bool intersects(const QPainterPath &p) const;
144 bool contains(const QPainterPath &p) const;
145 [[nodiscard]] QPainterPath united(const QPainterPath &r) const;
146 [[nodiscard]] QPainterPath intersected(const QPainterPath &r) const;
147 [[nodiscard]] QPainterPath subtracted(const QPainterPath &r) const;
148
149 [[nodiscard]] QPainterPath simplified() const;
150
151 bool operator==(const QPainterPath &other) const;
152 bool operator!=(const QPainterPath &other) const;
153
154 QPainterPath operator&(const QPainterPath &other) const;
155 QPainterPath operator|(const QPainterPath &other) const;
156 QPainterPath operator+(const QPainterPath &other) const;
157 QPainterPath operator-(const QPainterPath &other) const;
158 QPainterPath &operator&=(const QPainterPath &other);
159 QPainterPath &operator|=(const QPainterPath &other);
160 QPainterPath &operator+=(const QPainterPath &other);
161 QPainterPath &operator-=(const QPainterPath &other);
162
163private:
164 QExplicitlySharedDataPointer<QPainterPathPrivate> d_ptr;
165
166 inline void ensureData() { if (!d_ptr) ensureData_helper(); }
167 void ensureData_helper();
168 void detach();
169 void setDirty(bool);
170 void computeBoundingRect() const;
171 void computeControlPointRect() const;
172
173 QPainterPathPrivate *d_func() const { return d_ptr.data(); }
174
175 friend class QPainterPathStroker;
176 friend class QPainterPathStrokerPrivate;
177 friend class QTransform;
178 friend class QVectorPath;
179 friend Q_GUI_EXPORT const QVectorPath &qtVectorPathForPath(const QPainterPath &);
180
181#ifndef QT_NO_DATASTREAM
182 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
183 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
184#endif
185};
186
187Q_DECLARE_SHARED(QPainterPath)
188Q_DECLARE_TYPEINFO(QPainterPath::Element, Q_PRIMITIVE_TYPE);
189
190#ifndef QT_NO_DATASTREAM
191Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
192Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
193#endif
194
195class Q_GUI_EXPORT QPainterPathStroker
196{
197 Q_DECLARE_PRIVATE(QPainterPathStroker)
198public:
199 QPainterPathStroker();
200 explicit QPainterPathStroker(const QPen &pen);
201 ~QPainterPathStroker();
202
203 void setWidth(qreal width);
204 qreal width() const;
205
206 void setCapStyle(Qt::PenCapStyle style);
207 Qt::PenCapStyle capStyle() const;
208
209 void setJoinStyle(Qt::PenJoinStyle style);
210 Qt::PenJoinStyle joinStyle() const;
211
212 void setMiterLimit(qreal length);
213 qreal miterLimit() const;
214
215 void setCurveThreshold(qreal threshold);
216 qreal curveThreshold() const;
217
218 void setDashPattern(Qt::PenStyle);
219 void setDashPattern(const QList<qreal> &dashPattern);
220 QList<qreal> dashPattern() const;
221
222 void setDashOffset(qreal offset);
223 qreal dashOffset() const;
224
225 QPainterPath createStroke(const QPainterPath &path) const;
226
227private:
228 Q_DISABLE_COPY(QPainterPathStroker)
229
230 friend class QX11PaintEngine;
231
232 QScopedPointer<QPainterPathStrokerPrivate> d_ptr;
233};
234
235inline void QPainterPath::moveTo(qreal x, qreal y)
236{
237 moveTo(p: QPointF(x, y));
238}
239
240inline void QPainterPath::lineTo(qreal x, qreal y)
241{
242 lineTo(p: QPointF(x, y));
243}
244
245inline void QPainterPath::arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength)
246{
247 arcTo(rect: QRectF(x, y, w, h), startAngle, arcLength);
248}
249
250inline void QPainterPath::arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle)
251{
252 arcMoveTo(rect: QRectF(x, y, w, h), angle);
253}
254
255inline void QPainterPath::cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
256 qreal endPtx, qreal endPty)
257{
258 cubicTo(ctrlPt1: QPointF(ctrlPt1x, ctrlPt1y), ctrlPt2: QPointF(ctrlPt2x, ctrlPt2y),
259 endPt: QPointF(endPtx, endPty));
260}
261
262inline void QPainterPath::quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty)
263{
264 quadTo(ctrlPt: QPointF(ctrlPtx, ctrlPty), endPt: QPointF(endPtx, endPty));
265}
266
267inline void QPainterPath::addEllipse(qreal x, qreal y, qreal w, qreal h)
268{
269 addEllipse(rect: QRectF(x, y, w, h));
270}
271
272inline void QPainterPath::addEllipse(const QPointF &center, qreal rx, qreal ry)
273{
274 addEllipse(rect: QRectF(center.x() - rx, center.y() - ry, 2 * rx, 2 * ry));
275}
276
277inline void QPainterPath::addRect(qreal x, qreal y, qreal w, qreal h)
278{
279 addRect(rect: QRectF(x, y, w, h));
280}
281
282inline void QPainterPath::addRoundedRect(qreal x, qreal y, qreal w, qreal h,
283 qreal xRadius, qreal yRadius,
284 Qt::SizeMode mode)
285{
286 addRoundedRect(rect: QRectF(x, y, w, h), xRadius, yRadius, mode);
287}
288
289inline void QPainterPath::addText(qreal x, qreal y, const QFont &f, const QString &text)
290{
291 addText(point: QPointF(x, y), f, text);
292}
293
294inline void QPainterPath::translate(const QPointF &offset)
295{ translate(dx: offset.x(), dy: offset.y()); }
296
297inline QPainterPath QPainterPath::translated(const QPointF &offset) const
298{ return translated(dx: offset.x(), dy: offset.y()); }
299
300inline QPainterPath operator *(const QPainterPath &p, const QTransform &m)
301{ return m.map(p); }
302
303#ifndef QT_NO_DEBUG_STREAM
304Q_GUI_EXPORT QDebug operator<<(QDebug, const QPainterPath &);
305#endif
306
307QT_END_NAMESPACE
308
309#endif // QPAINTERPATH_H
310

source code of qtbase/src/gui/painting/qpainterpath.h