1/****************************************************************************
2**
3** Copyright (C) 2016 The Qt Company Ltd.
4** Contact: https://www.qt.io/licensing/
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial License Usage
10** Licensees holding valid commercial Qt licenses may use this file in
11** accordance with the commercial license agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and The Qt Company. For licensing terms
14** and conditions see https://www.qt.io/terms-conditions. For further
15** information use the contact form at https://www.qt.io/contact-us.
16**
17** GNU Lesser General Public License Usage
18** Alternatively, this file may be used under the terms of the GNU Lesser
19** General Public License version 3 as published by the Free Software
20** Foundation and appearing in the file LICENSE.LGPL3 included in the
21** packaging of this file. Please review the following information to
22** ensure the GNU Lesser General Public License version 3 requirements
23** will be met: https://www.gnu.org/licenses/lgpl-3.0.html.
24**
25** GNU General Public License Usage
26** Alternatively, this file may be used under the terms of the GNU
27** General Public License version 2.0 or (at your option) the GNU General
28** Public license version 3 or any later version approved by the KDE Free
29** Qt Foundation. The licenses are as published by the Free Software
30** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3
31** included in the packaging of this file. Please review the following
32** information to ensure the GNU General Public License requirements will
33** be met: https://www.gnu.org/licenses/gpl-2.0.html and
34** https://www.gnu.org/licenses/gpl-3.0.html.
35**
36** $QT_END_LICENSE$
37**
38****************************************************************************/
39
40#ifndef QPAINTERPATH_H
41#define QPAINTERPATH_H
42
43#include <QtGui/qtguiglobal.h>
44#include <QtGui/qmatrix.h>
45#include <QtGui/qtransform.h>
46#include <QtCore/qglobal.h>
47#include <QtCore/qrect.h>
48#include <QtCore/qline.h>
49#include <QtCore/qvector.h>
50#include <QtCore/qscopedpointer.h>
51
52QT_BEGIN_NAMESPACE
53
54
55class QFont;
56class QPainterPathPrivate;
57struct QPainterPathPrivateDeleter;
58class QPainterPathData;
59class QPainterPathStrokerPrivate;
60class QPen;
61class QPolygonF;
62class QRegion;
63class QVectorPath;
64
65class Q_GUI_EXPORT QPainterPath
66{
67public:
68 enum ElementType {
69 MoveToElement,
70 LineToElement,
71 CurveToElement,
72 CurveToDataElement
73 };
74
75 class Element {
76 public:
77 qreal x;
78 qreal y;
79 ElementType type;
80
81 bool isMoveTo() const { return type == MoveToElement; }
82 bool isLineTo() const { return type == LineToElement; }
83 bool isCurveTo() const { return type == CurveToElement; }
84
85 operator QPointF () const { return QPointF(x, y); }
86
87 bool operator==(const Element &e) const { return qFuzzyCompare(p1: x, p2: e.x)
88 && qFuzzyCompare(p1: y, p2: e.y) && type == e.type; }
89 inline bool operator!=(const Element &e) const { return !operator==(e); }
90 };
91
92 QPainterPath() noexcept;
93 explicit QPainterPath(const QPointF &startPoint);
94 QPainterPath(const QPainterPath &other);
95 QPainterPath &operator=(const QPainterPath &other);
96 inline QPainterPath &operator=(QPainterPath &&other) noexcept
97 { qSwap(value1&: d_ptr, value2&: other.d_ptr); return *this; }
98 ~QPainterPath();
99
100 inline void swap(QPainterPath &other) noexcept { d_ptr.swap(other&: other.d_ptr); }
101
102 void clear();
103 void reserve(int size);
104 int capacity() const;
105
106 void closeSubpath();
107
108 void moveTo(const QPointF &p);
109 inline void moveTo(qreal x, qreal y);
110
111 void lineTo(const QPointF &p);
112 inline void lineTo(qreal x, qreal y);
113
114 void arcMoveTo(const QRectF &rect, qreal angle);
115 inline void arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle);
116
117 void arcTo(const QRectF &rect, qreal startAngle, qreal arcLength);
118 inline void arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength);
119
120 void cubicTo(const QPointF &ctrlPt1, const QPointF &ctrlPt2, const QPointF &endPt);
121 inline void cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
122 qreal endPtx, qreal endPty);
123 void quadTo(const QPointF &ctrlPt, const QPointF &endPt);
124 inline void quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty);
125
126 QPointF currentPosition() const;
127
128 void addRect(const QRectF &rect);
129 inline void addRect(qreal x, qreal y, qreal w, qreal h);
130 void addEllipse(const QRectF &rect);
131 inline void addEllipse(qreal x, qreal y, qreal w, qreal h);
132 inline void addEllipse(const QPointF &center, qreal rx, qreal ry);
133 void addPolygon(const QPolygonF &polygon);
134 void addText(const QPointF &point, const QFont &f, const QString &text);
135 inline void addText(qreal x, qreal y, const QFont &f, const QString &text);
136 void addPath(const QPainterPath &path);
137 void addRegion(const QRegion &region);
138
139 void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
140 Qt::SizeMode mode = Qt::AbsoluteSize);
141 inline void addRoundedRect(qreal x, qreal y, qreal w, qreal h,
142 qreal xRadius, qreal yRadius,
143 Qt::SizeMode mode = Qt::AbsoluteSize);
144
145#if QT_DEPRECATED_SINCE(5, 13)
146 QT_DEPRECATED_X("Use addRoundedRect(..., Qt::RelativeSize) instead")
147 void addRoundRect(const QRectF &rect, int xRnd, int yRnd);
148 QT_DEPRECATED_X("Use addRoundedRect(..., Qt::RelativeSize) instead")
149 void addRoundRect(qreal x, qreal y, qreal w, qreal h,
150 int xRnd, int yRnd);
151 QT_DEPRECATED_X("Use addRoundedRect(..., Qt::RelativeSize) instead")
152 void addRoundRect(const QRectF &rect, int roundness);
153 QT_DEPRECATED_X("Use addRoundedRect(..., Qt::RelativeSize) instead")
154 void addRoundRect(qreal x, qreal y, qreal w, qreal h,
155 int roundness);
156#endif
157
158 void connectPath(const QPainterPath &path);
159
160 bool contains(const QPointF &pt) const;
161 bool contains(const QRectF &rect) const;
162 bool intersects(const QRectF &rect) const;
163
164 void translate(qreal dx, qreal dy);
165 inline void translate(const QPointF &offset);
166
167 Q_REQUIRED_RESULT QPainterPath translated(qreal dx, qreal dy) const;
168 Q_REQUIRED_RESULT inline QPainterPath translated(const QPointF &offset) const;
169
170 QRectF boundingRect() const;
171 QRectF controlPointRect() const;
172
173 Qt::FillRule fillRule() const;
174 void setFillRule(Qt::FillRule fillRule);
175
176 bool isEmpty() const;
177
178 Q_REQUIRED_RESULT QPainterPath toReversed() const;
179
180#if QT_DEPRECATED_SINCE(5, 15)
181 QT_DEPRECATED_X("Use toSubpathPolygons(const QTransform &)")
182 QList<QPolygonF> toSubpathPolygons(const QMatrix &matrix) const;
183 QT_DEPRECATED_X("Use toFillPolygons(const QTransform &")
184 QList<QPolygonF> toFillPolygons(const QMatrix &matrix) const;
185 QT_DEPRECATED_X("Use toFillPolygon(const QTransform &)")
186 QPolygonF toFillPolygon(const QMatrix &matrix) const;
187#endif // QT_DEPRECATED_SINCE(5, 15)
188 QList<QPolygonF> toSubpathPolygons(const QTransform &matrix = QTransform()) const;
189 QList<QPolygonF> toFillPolygons(const QTransform &matrix = QTransform()) const;
190 QPolygonF toFillPolygon(const QTransform &matrix = QTransform()) const;
191
192 int elementCount() const;
193 QPainterPath::Element elementAt(int i) const;
194 void setElementPositionAt(int i, qreal x, qreal y);
195
196 qreal length() const;
197 qreal percentAtLength(qreal t) const;
198 QPointF pointAtPercent(qreal t) const;
199 qreal angleAtPercent(qreal t) const;
200 qreal slopeAtPercent(qreal t) const;
201
202 bool intersects(const QPainterPath &p) const;
203 bool contains(const QPainterPath &p) const;
204 Q_REQUIRED_RESULT QPainterPath united(const QPainterPath &r) const;
205 Q_REQUIRED_RESULT QPainterPath intersected(const QPainterPath &r) const;
206 Q_REQUIRED_RESULT QPainterPath subtracted(const QPainterPath &r) const;
207#if QT_DEPRECATED_SINCE(5, 13)
208 QT_DEPRECATED_X("Use r.subtracted() instead")
209 Q_REQUIRED_RESULT QPainterPath subtractedInverted(const QPainterPath &r) const;
210#endif
211
212 Q_REQUIRED_RESULT QPainterPath simplified() const;
213
214 bool operator==(const QPainterPath &other) const;
215 bool operator!=(const QPainterPath &other) const;
216
217 QPainterPath operator&(const QPainterPath &other) const;
218 QPainterPath operator|(const QPainterPath &other) const;
219 QPainterPath operator+(const QPainterPath &other) const;
220 QPainterPath operator-(const QPainterPath &other) const;
221 QPainterPath &operator&=(const QPainterPath &other);
222 QPainterPath &operator|=(const QPainterPath &other);
223 QPainterPath &operator+=(const QPainterPath &other);
224 QPainterPath &operator-=(const QPainterPath &other);
225
226private:
227 QScopedPointer<QPainterPathPrivate, QPainterPathPrivateDeleter> d_ptr;
228
229 inline void ensureData() { if (!d_ptr) ensureData_helper(); }
230 void ensureData_helper();
231 void detach();
232 void detach_helper();
233 void setDirty(bool);
234 void computeBoundingRect() const;
235 void computeControlPointRect() const;
236
237 QPainterPathData *d_func() const { return reinterpret_cast<QPainterPathData *>(d_ptr.data()); }
238
239 friend class QPainterPathData;
240 friend class QPainterPathStroker;
241 friend class QPainterPathStrokerPrivate;
242 friend class QMatrix;
243 friend class QTransform;
244 friend class QVectorPath;
245 friend Q_GUI_EXPORT const QVectorPath &qtVectorPathForPath(const QPainterPath &);
246
247#ifndef QT_NO_DATASTREAM
248 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
249 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
250#endif
251};
252
253Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QPainterPath)
254Q_DECLARE_TYPEINFO(QPainterPath::Element, Q_PRIMITIVE_TYPE);
255
256#ifndef QT_NO_DATASTREAM
257Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
258Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
259#endif
260
261class Q_GUI_EXPORT QPainterPathStroker
262{
263 Q_DECLARE_PRIVATE(QPainterPathStroker)
264public:
265 QPainterPathStroker();
266 explicit QPainterPathStroker(const QPen &pen);
267 ~QPainterPathStroker();
268
269 void setWidth(qreal width);
270 qreal width() const;
271
272 void setCapStyle(Qt::PenCapStyle style);
273 Qt::PenCapStyle capStyle() const;
274
275 void setJoinStyle(Qt::PenJoinStyle style);
276 Qt::PenJoinStyle joinStyle() const;
277
278 void setMiterLimit(qreal length);
279 qreal miterLimit() const;
280
281 void setCurveThreshold(qreal threshold);
282 qreal curveThreshold() const;
283
284 void setDashPattern(Qt::PenStyle);
285 void setDashPattern(const QVector<qreal> &dashPattern);
286 QVector<qreal> dashPattern() const;
287
288 void setDashOffset(qreal offset);
289 qreal dashOffset() const;
290
291 QPainterPath createStroke(const QPainterPath &path) const;
292
293private:
294 Q_DISABLE_COPY(QPainterPathStroker)
295
296 friend class QX11PaintEngine;
297
298 QScopedPointer<QPainterPathStrokerPrivate> d_ptr;
299};
300
301inline void QPainterPath::moveTo(qreal x, qreal y)
302{
303 moveTo(p: QPointF(x, y));
304}
305
306inline void QPainterPath::lineTo(qreal x, qreal y)
307{
308 lineTo(p: QPointF(x, y));
309}
310
311inline void QPainterPath::arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength)
312{
313 arcTo(rect: QRectF(x, y, w, h), startAngle, arcLength);
314}
315
316inline void QPainterPath::arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle)
317{
318 arcMoveTo(rect: QRectF(x, y, w, h), angle);
319}
320
321inline void QPainterPath::cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
322 qreal endPtx, qreal endPty)
323{
324 cubicTo(ctrlPt1: QPointF(ctrlPt1x, ctrlPt1y), ctrlPt2: QPointF(ctrlPt2x, ctrlPt2y),
325 endPt: QPointF(endPtx, endPty));
326}
327
328inline void QPainterPath::quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty)
329{
330 quadTo(ctrlPt: QPointF(ctrlPtx, ctrlPty), endPt: QPointF(endPtx, endPty));
331}
332
333inline void QPainterPath::addEllipse(qreal x, qreal y, qreal w, qreal h)
334{
335 addEllipse(rect: QRectF(x, y, w, h));
336}
337
338inline void QPainterPath::addEllipse(const QPointF &center, qreal rx, qreal ry)
339{
340 addEllipse(rect: QRectF(center.x() - rx, center.y() - ry, 2 * rx, 2 * ry));
341}
342
343inline void QPainterPath::addRect(qreal x, qreal y, qreal w, qreal h)
344{
345 addRect(rect: QRectF(x, y, w, h));
346}
347
348inline void QPainterPath::addRoundedRect(qreal x, qreal y, qreal w, qreal h,
349 qreal xRadius, qreal yRadius,
350 Qt::SizeMode mode)
351{
352 addRoundedRect(rect: QRectF(x, y, w, h), xRadius, yRadius, mode);
353}
354
355inline void QPainterPath::addText(qreal x, qreal y, const QFont &f, const QString &text)
356{
357 addText(point: QPointF(x, y), f, text);
358}
359
360inline void QPainterPath::translate(const QPointF &offset)
361{ translate(dx: offset.x(), dy: offset.y()); }
362
363inline QPainterPath QPainterPath::translated(const QPointF &offset) const
364{ return translated(dx: offset.x(), dy: offset.y()); }
365
366inline QPainterPath operator *(const QPainterPath &p, const QTransform &m)
367{ return m.map(p); }
368
369#ifndef QT_NO_DEBUG_STREAM
370Q_GUI_EXPORT QDebug operator<<(QDebug, const QPainterPath &);
371#endif
372
373QT_END_NAMESPACE
374
375#endif // QPAINTERPATH_H
376

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