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 <QtCore/qglobal.h>
46#include <QtCore/qrect.h>
47#include <QtCore/qline.h>
48#include <QtCore/qvector.h>
49#include <QtCore/qscopedpointer.h>
50
51QT_BEGIN_NAMESPACE
52
53
54class QFont;
55class QPainterPathPrivate;
56struct QPainterPathPrivateDeleter;
57class QPainterPathData;
58class QPainterPathStrokerPrivate;
59class QPen;
60class QPolygonF;
61class QRegion;
62class QVectorPath;
63
64class Q_GUI_EXPORT QPainterPath
65{
66public:
67 enum ElementType {
68 MoveToElement,
69 LineToElement,
70 CurveToElement,
71 CurveToDataElement
72 };
73
74 class Element {
75 public:
76 qreal x;
77 qreal y;
78 ElementType type;
79
80 bool isMoveTo() const { return type == MoveToElement; }
81 bool isLineTo() const { return type == LineToElement; }
82 bool isCurveTo() const { return type == CurveToElement; }
83
84 operator QPointF () const { return QPointF(x, y); }
85
86 bool operator==(const Element &e) const { return qFuzzyCompare(x, e.x)
87 && qFuzzyCompare(y, e.y) && type == e.type; }
88 inline bool operator!=(const Element &e) const { return !operator==(e); }
89 };
90
91 QPainterPath() noexcept;
92 explicit QPainterPath(const QPointF &startPoint);
93 QPainterPath(const QPainterPath &other);
94 QPainterPath &operator=(const QPainterPath &other);
95 inline QPainterPath &operator=(QPainterPath &&other) noexcept
96 { qSwap(d_ptr, other.d_ptr); return *this; }
97 ~QPainterPath();
98
99 inline void swap(QPainterPath &other) noexcept { d_ptr.swap(other.d_ptr); }
100
101 void clear();
102 void reserve(int size);
103 int capacity() const;
104
105 void closeSubpath();
106
107 void moveTo(const QPointF &p);
108 inline void moveTo(qreal x, qreal y);
109
110 void lineTo(const QPointF &p);
111 inline void lineTo(qreal x, qreal y);
112
113 void arcMoveTo(const QRectF &rect, qreal angle);
114 inline void arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle);
115
116 void arcTo(const QRectF &rect, qreal startAngle, qreal arcLength);
117 inline void arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength);
118
119 void cubicTo(const QPointF &ctrlPt1, const QPointF &ctrlPt2, const QPointF &endPt);
120 inline void cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
121 qreal endPtx, qreal endPty);
122 void quadTo(const QPointF &ctrlPt, const QPointF &endPt);
123 inline void quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty);
124
125 QPointF currentPosition() const;
126
127 void addRect(const QRectF &rect);
128 inline void addRect(qreal x, qreal y, qreal w, qreal h);
129 void addEllipse(const QRectF &rect);
130 inline void addEllipse(qreal x, qreal y, qreal w, qreal h);
131 inline void addEllipse(const QPointF &center, qreal rx, qreal ry);
132 void addPolygon(const QPolygonF &polygon);
133 void addText(const QPointF &point, const QFont &f, const QString &text);
134 inline void addText(qreal x, qreal y, const QFont &f, const QString &text);
135 void addPath(const QPainterPath &path);
136 void addRegion(const QRegion &region);
137
138 void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
139 Qt::SizeMode mode = Qt::AbsoluteSize);
140 inline void addRoundedRect(qreal x, qreal y, qreal w, qreal h,
141 qreal xRadius, qreal yRadius,
142 Qt::SizeMode mode = Qt::AbsoluteSize);
143
144#if QT_DEPRECATED_SINCE(5, 13)
145 QT_DEPRECATED_X("Use addRoundedRect(..., Qt::RelativeSize) instead")
146 void addRoundRect(const QRectF &rect, int xRnd, int yRnd);
147 QT_DEPRECATED_X("Use addRoundedRect(..., Qt::RelativeSize) instead")
148 void addRoundRect(qreal x, qreal y, qreal w, qreal h,
149 int xRnd, int yRnd);
150 QT_DEPRECATED_X("Use addRoundedRect(..., Qt::RelativeSize) instead")
151 void addRoundRect(const QRectF &rect, int roundness);
152 QT_DEPRECATED_X("Use addRoundedRect(..., Qt::RelativeSize) instead")
153 void addRoundRect(qreal x, qreal y, qreal w, qreal h,
154 int roundness);
155#endif
156
157 void connectPath(const QPainterPath &path);
158
159 bool contains(const QPointF &pt) const;
160 bool contains(const QRectF &rect) const;
161 bool intersects(const QRectF &rect) const;
162
163 void translate(qreal dx, qreal dy);
164 inline void translate(const QPointF &offset);
165
166 Q_REQUIRED_RESULT QPainterPath translated(qreal dx, qreal dy) const;
167 Q_REQUIRED_RESULT inline QPainterPath translated(const QPointF &offset) const;
168
169 QRectF boundingRect() const;
170 QRectF controlPointRect() const;
171
172 Qt::FillRule fillRule() const;
173 void setFillRule(Qt::FillRule fillRule);
174
175 bool isEmpty() const;
176
177 Q_REQUIRED_RESULT QPainterPath toReversed() const;
178 QList<QPolygonF> toSubpathPolygons(const QMatrix &matrix = QMatrix()) const;
179 QList<QPolygonF> toFillPolygons(const QMatrix &matrix = QMatrix()) const;
180 QPolygonF toFillPolygon(const QMatrix &matrix = QMatrix()) const;
181 QList<QPolygonF> toSubpathPolygons(const QTransform &matrix) const;
182 QList<QPolygonF> toFillPolygons(const QTransform &matrix) const;
183 QPolygonF toFillPolygon(const QTransform &matrix) const;
184
185 int elementCount() const;
186 QPainterPath::Element elementAt(int i) const;
187 void setElementPositionAt(int i, qreal x, qreal y);
188
189 qreal length() const;
190 qreal percentAtLength(qreal t) const;
191 QPointF pointAtPercent(qreal t) const;
192 qreal angleAtPercent(qreal t) const;
193 qreal slopeAtPercent(qreal t) const;
194
195 bool intersects(const QPainterPath &p) const;
196 bool contains(const QPainterPath &p) const;
197 Q_REQUIRED_RESULT QPainterPath united(const QPainterPath &r) const;
198 Q_REQUIRED_RESULT QPainterPath intersected(const QPainterPath &r) const;
199 Q_REQUIRED_RESULT QPainterPath subtracted(const QPainterPath &r) const;
200#if QT_DEPRECATED_SINCE(5, 13)
201 QT_DEPRECATED_X("Use r.subtracted() instead")
202 Q_REQUIRED_RESULT QPainterPath subtractedInverted(const QPainterPath &r) const;
203#endif
204
205 Q_REQUIRED_RESULT QPainterPath simplified() const;
206
207 bool operator==(const QPainterPath &other) const;
208 bool operator!=(const QPainterPath &other) const;
209
210 QPainterPath operator&(const QPainterPath &other) const;
211 QPainterPath operator|(const QPainterPath &other) const;
212 QPainterPath operator+(const QPainterPath &other) const;
213 QPainterPath operator-(const QPainterPath &other) const;
214 QPainterPath &operator&=(const QPainterPath &other);
215 QPainterPath &operator|=(const QPainterPath &other);
216 QPainterPath &operator+=(const QPainterPath &other);
217 QPainterPath &operator-=(const QPainterPath &other);
218
219private:
220 QScopedPointer<QPainterPathPrivate, QPainterPathPrivateDeleter> d_ptr;
221
222 inline void ensureData() { if (!d_ptr) ensureData_helper(); }
223 void ensureData_helper();
224 void detach();
225 void detach_helper();
226 void setDirty(bool);
227 void computeBoundingRect() const;
228 void computeControlPointRect() const;
229
230 QPainterPathData *d_func() const { return reinterpret_cast<QPainterPathData *>(d_ptr.data()); }
231
232 friend class QPainterPathData;
233 friend class QPainterPathStroker;
234 friend class QPainterPathStrokerPrivate;
235 friend class QMatrix;
236 friend class QTransform;
237 friend class QVectorPath;
238 friend Q_GUI_EXPORT const QVectorPath &qtVectorPathForPath(const QPainterPath &);
239
240#ifndef QT_NO_DATASTREAM
241 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
242 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
243#endif
244};
245
246Q_DECLARE_SHARED_NOT_MOVABLE_UNTIL_QT6(QPainterPath)
247Q_DECLARE_TYPEINFO(QPainterPath::Element, Q_PRIMITIVE_TYPE);
248
249#ifndef QT_NO_DATASTREAM
250Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
251Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
252#endif
253
254class Q_GUI_EXPORT QPainterPathStroker
255{
256 Q_DECLARE_PRIVATE(QPainterPathStroker)
257public:
258 QPainterPathStroker();
259 explicit QPainterPathStroker(const QPen &pen);
260 ~QPainterPathStroker();
261
262 void setWidth(qreal width);
263 qreal width() const;
264
265 void setCapStyle(Qt::PenCapStyle style);
266 Qt::PenCapStyle capStyle() const;
267
268 void setJoinStyle(Qt::PenJoinStyle style);
269 Qt::PenJoinStyle joinStyle() const;
270
271 void setMiterLimit(qreal length);
272 qreal miterLimit() const;
273
274 void setCurveThreshold(qreal threshold);
275 qreal curveThreshold() const;
276
277 void setDashPattern(Qt::PenStyle);
278 void setDashPattern(const QVector<qreal> &dashPattern);
279 QVector<qreal> dashPattern() const;
280
281 void setDashOffset(qreal offset);
282 qreal dashOffset() const;
283
284 QPainterPath createStroke(const QPainterPath &path) const;
285
286private:
287 Q_DISABLE_COPY(QPainterPathStroker)
288
289 friend class QX11PaintEngine;
290
291 QScopedPointer<QPainterPathStrokerPrivate> d_ptr;
292};
293
294inline void QPainterPath::moveTo(qreal x, qreal y)
295{
296 moveTo(QPointF(x, y));
297}
298
299inline void QPainterPath::lineTo(qreal x, qreal y)
300{
301 lineTo(QPointF(x, y));
302}
303
304inline void QPainterPath::arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength)
305{
306 arcTo(QRectF(x, y, w, h), startAngle, arcLength);
307}
308
309inline void QPainterPath::arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle)
310{
311 arcMoveTo(QRectF(x, y, w, h), angle);
312}
313
314inline void QPainterPath::cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
315 qreal endPtx, qreal endPty)
316{
317 cubicTo(QPointF(ctrlPt1x, ctrlPt1y), QPointF(ctrlPt2x, ctrlPt2y),
318 QPointF(endPtx, endPty));
319}
320
321inline void QPainterPath::quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty)
322{
323 quadTo(QPointF(ctrlPtx, ctrlPty), QPointF(endPtx, endPty));
324}
325
326inline void QPainterPath::addEllipse(qreal x, qreal y, qreal w, qreal h)
327{
328 addEllipse(QRectF(x, y, w, h));
329}
330
331inline void QPainterPath::addEllipse(const QPointF &center, qreal rx, qreal ry)
332{
333 addEllipse(QRectF(center.x() - rx, center.y() - ry, 2 * rx, 2 * ry));
334}
335
336inline void QPainterPath::addRect(qreal x, qreal y, qreal w, qreal h)
337{
338 addRect(QRectF(x, y, w, h));
339}
340
341inline void QPainterPath::addRoundedRect(qreal x, qreal y, qreal w, qreal h,
342 qreal xRadius, qreal yRadius,
343 Qt::SizeMode mode)
344{
345 addRoundedRect(QRectF(x, y, w, h), xRadius, yRadius, mode);
346}
347
348inline void QPainterPath::addText(qreal x, qreal y, const QFont &f, const QString &text)
349{
350 addText(QPointF(x, y), f, text);
351}
352
353inline void QPainterPath::translate(const QPointF &offset)
354{ translate(offset.x(), offset.y()); }
355
356inline QPainterPath QPainterPath::translated(const QPointF &offset) const
357{ return translated(offset.x(), offset.y()); }
358
359
360#ifndef QT_NO_DEBUG_STREAM
361Q_GUI_EXPORT QDebug operator<<(QDebug, const QPainterPath &);
362#endif
363
364QT_END_NAMESPACE
365
366#endif // QPAINTERPATH_H
367