Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qpainterpath.h
Go to the documentation of this file.
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
10#include <QtCore/qglobal.h>
11#include <QtCore/qline.h>
12#include <QtCore/qlist.h>
13#include <QtCore/qpoint.h>
14#include <QtCore/qrect.h>
15
17
18
19class QFont;
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
51 {
52 return type == e.type
53 && qFuzzyCompare(QPointF(*this), QPointF(e));
54 }
55 inline bool operator!=(const Element &e) const { return !operator==(e); }
56 };
57
58 QPainterPath() noexcept;
59 explicit QPainterPath(const QPointF &startPoint);
60 QPainterPath(const QPainterPath &other);
61 QPainterPath &operator=(const QPainterPath &other);
62 QPainterPath(QPainterPath &&other) noexcept
63 : d_ptr(std::exchange(other.d_ptr, nullptr))
64 {}
65 QT_MOVE_ASSIGNMENT_OPERATOR_IMPL_VIA_PURE_SWAP(QPainterPath)
66 ~QPainterPath();
67
68 inline void swap(QPainterPath &other) noexcept { qt_ptr_swap(d_ptr, other.d_ptr); }
69
70 void clear();
71 void reserve(int size);
72 int capacity() const;
73
74 void closeSubpath();
75
76 void moveTo(const QPointF &p);
77 inline void moveTo(qreal x, qreal y);
78
79 void lineTo(const QPointF &p);
80 inline void lineTo(qreal x, qreal y);
81
82 void arcMoveTo(const QRectF &rect, qreal angle);
83 inline void arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle);
84
85 void arcTo(const QRectF &rect, qreal startAngle, qreal arcLength);
86 inline void arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength);
87
88 void cubicTo(const QPointF &ctrlPt1, const QPointF &ctrlPt2, const QPointF &endPt);
89 inline void cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
90 qreal endPtx, qreal endPty);
91 void quadTo(const QPointF &ctrlPt, const QPointF &endPt);
92 inline void quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty);
93
94 QPointF currentPosition() const;
95
96 void addRect(const QRectF &rect);
97 inline void addRect(qreal x, qreal y, qreal w, qreal h);
98 void addEllipse(const QRectF &rect);
99 inline void addEllipse(qreal x, qreal y, qreal w, qreal h);
100 inline void addEllipse(const QPointF &center, qreal rx, qreal ry);
101 void addPolygon(const QPolygonF &polygon);
102 void addText(const QPointF &point, const QFont &f, const QString &text);
103 inline void addText(qreal x, qreal y, const QFont &f, const QString &text);
104 void addPath(const QPainterPath &path);
105 void addRegion(const QRegion &region);
106
107 void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
108 Qt::SizeMode mode = Qt::AbsoluteSize);
109 inline void addRoundedRect(qreal x, qreal y, qreal w, qreal h,
110 qreal xRadius, qreal yRadius,
111 Qt::SizeMode mode = Qt::AbsoluteSize);
112
113 void connectPath(const QPainterPath &path);
114
115 bool contains(const QPointF &pt) const;
116 bool contains(const QRectF &rect) const;
117 bool intersects(const QRectF &rect) const;
118
119 void translate(qreal dx, qreal dy);
120 inline void translate(const QPointF &offset);
121
122 [[nodiscard]] QPainterPath translated(qreal dx, qreal dy) const;
123 [[nodiscard]] inline QPainterPath translated(const QPointF &offset) const;
124
125 QRectF boundingRect() const;
126 QRectF controlPointRect() const;
127
128 Qt::FillRule fillRule() const;
129 void setFillRule(Qt::FillRule fillRule);
130
131 bool isEmpty() const;
132
133 [[nodiscard]] QPainterPath toReversed() const;
134
135 QList<QPolygonF> toSubpathPolygons(const QTransform &matrix = QTransform()) const;
136 QList<QPolygonF> toFillPolygons(const QTransform &matrix = QTransform()) const;
137 QPolygonF toFillPolygon(const QTransform &matrix = QTransform()) const;
138
139 int elementCount() const;
140 QPainterPath::Element elementAt(int i) const;
141 void setElementPositionAt(int i, qreal x, qreal y);
142
143 bool isCachingEnabled() const;
144 void setCachingEnabled(bool enabled);
145 qreal length() const;
146 qreal percentAtLength(qreal len) const;
147 QPointF pointAtPercent(qreal t) const;
148 qreal angleAtPercent(qreal t) const;
149 qreal slopeAtPercent(qreal t) const;
150 [[nodiscard]] QPainterPath trimmed(qreal fromFraction, qreal toFraction, qreal offset = 0) const;
151
152 bool intersects(const QPainterPath &p) const;
153 bool contains(const QPainterPath &p) const;
154 [[nodiscard]] QPainterPath united(const QPainterPath &r) const;
155 [[nodiscard]] QPainterPath intersected(const QPainterPath &r) const;
156 [[nodiscard]] QPainterPath subtracted(const QPainterPath &r) const;
157
158 [[nodiscard]] QPainterPath simplified() const;
159
160 bool operator==(const QPainterPath &other) const;
161 bool operator!=(const QPainterPath &other) const;
162
163 QPainterPath operator&(const QPainterPath &other) const;
164 QPainterPath operator|(const QPainterPath &other) const;
165 QPainterPath operator+(const QPainterPath &other) const;
166 QPainterPath operator-(const QPainterPath &other) const;
167 QPainterPath &operator&=(const QPainterPath &other);
168 QPainterPath &operator|=(const QPainterPath &other);
169 QPainterPath &operator+=(const QPainterPath &other);
170 QPainterPath &operator-=(const QPainterPath &other);
171
172private:
173 QPainterPathPrivate *d_ptr;
174
175 inline void ensureData() { if (!d_ptr) ensureData_helper(); }
176 void ensureData_helper();
177 void setDirty(bool);
178 void computeBoundingRect() const;
179 void computeControlPointRect() const;
180
181 QPainterPathPrivate *d_func() const { return d_ptr; }
182
183 friend class QPainterPathStroker;
184 friend class QPainterPathStrokerPrivate;
185 friend class QPainterPathPrivate;
186 friend class QTransform;
187 friend class QVectorPath;
188 friend Q_GUI_EXPORT const QVectorPath &qtVectorPathForPath(const QPainterPath &);
189
190#ifndef QT_NO_DATASTREAM
191 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
192 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
193#endif
194};
195
197Q_DECLARE_TYPEINFO(QPainterPath::Element, Q_PRIMITIVE_TYPE);
198
199#ifndef QT_NO_DATASTREAM
200Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
201Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
202#endif
203
204class Q_GUI_EXPORT QPainterPathStroker
205{
206 Q_DECLARE_PRIVATE(QPainterPathStroker)
207public:
208 QPainterPathStroker();
209 explicit QPainterPathStroker(const QPen &pen);
210 ~QPainterPathStroker();
211
212 void setWidth(qreal width);
213 qreal width() const;
214
215 void setCapStyle(Qt::PenCapStyle style);
216 Qt::PenCapStyle capStyle() const;
217
218 void setJoinStyle(Qt::PenJoinStyle style);
219 Qt::PenJoinStyle joinStyle() const;
220
221 void setMiterLimit(qreal length);
222 qreal miterLimit() const;
223
224 void setCurveThreshold(qreal threshold);
225 qreal curveThreshold() const;
226
228 void setDashPattern(const QList<qreal> &dashPattern);
229 QList<qreal> dashPattern() const;
230
231 void setDashOffset(qreal offset);
232 qreal dashOffset() const;
233
234 QPainterPath createStroke(const QPainterPath &path) const;
235
236private:
237 Q_DISABLE_COPY(QPainterPathStroker)
238
239 friend class QX11PaintEngine;
240
241 QScopedPointer<QPainterPathStrokerPrivate> d_ptr;
242};
243
244inline void QPainterPath::moveTo(qreal x, qreal y)
245{
246 moveTo(QPointF(x, y));
247}
248
249inline void QPainterPath::lineTo(qreal x, qreal y)
250{
251 lineTo(QPointF(x, y));
252}
253
254inline void QPainterPath::arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength)
255{
256 arcTo(QRectF(x, y, w, h), startAngle, arcLength);
257}
258
259inline void QPainterPath::arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle)
260{
261 arcMoveTo(QRectF(x, y, w, h), angle);
262}
263
264inline void QPainterPath::cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
265 qreal endPtx, qreal endPty)
266{
267 cubicTo(QPointF(ctrlPt1x, ctrlPt1y), QPointF(ctrlPt2x, ctrlPt2y),
268 QPointF(endPtx, endPty));
269}
270
271inline void QPainterPath::quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty)
272{
273 quadTo(QPointF(ctrlPtx, ctrlPty), QPointF(endPtx, endPty));
274}
275
276inline void QPainterPath::addEllipse(qreal x, qreal y, qreal w, qreal h)
277{
278 addEllipse(QRectF(x, y, w, h));
279}
280
281inline void QPainterPath::addEllipse(const QPointF &center, qreal rx, qreal ry)
282{
283 addEllipse(QRectF(center.x() - rx, center.y() - ry, 2 * rx, 2 * ry));
284}
285
286inline void QPainterPath::addRect(qreal x, qreal y, qreal w, qreal h)
287{
288 addRect(QRectF(x, y, w, h));
289}
290
291inline void QPainterPath::addRoundedRect(qreal x, qreal y, qreal w, qreal h,
292 qreal xRadius, qreal yRadius,
293 Qt::SizeMode mode)
294{
295 addRoundedRect(QRectF(x, y, w, h), xRadius, yRadius, mode);
296}
297
298inline void QPainterPath::addText(qreal x, qreal y, const QFont &f, const QString &text)
299{
300 addText(QPointF(x, y), f, text);
301}
302
303inline void QPainterPath::translate(const QPointF &offset)
304{ translate(offset.x(), offset.y()); }
305
306inline QPainterPath QPainterPath::translated(const QPointF &offset) const
307{ return translated(offset.x(), offset.y()); }
308
309inline QPainterPath operator *(const QPainterPath &p, const QTransform &m)
310{ return m.map(p); }
311
312#ifndef QT_NO_DEBUG_STREAM
313Q_GUI_EXPORT QDebug operator<<(QDebug, const QPainterPath &);
314#endif
315
316QT_END_NAMESPACE
317
318#endif // QPAINTERPATH_H
\inmodule QtCore\reentrant
Definition qdatastream.h:50
QPaintEngine * paintEngine() const override
Definition qpicture.cpp:385
void setDpiY(int dpi)
Definition qpicture.cpp:384
int metric(PaintDeviceMetric m) const override
Definition qpicture.cpp:386
void setDpiX(int dpi)
Definition qpicture.cpp:383
The QPainterPathStroker class is used to generate fillable outlines for a given painter path.
void setCurveThreshold(qreal threshold)
Specifies the curve flattening threshold, controlling the granularity with which the generated outlin...
Qt::PenJoinStyle joinStyle() const
Returns the join style of the generated outlines.
QList< qreal > dashPattern() const
Returns the dash pattern for the generated outlines.
QPainterPathStroker(const QPen &pen)
Creates a new stroker based on pen.
void setDashPattern(Qt::PenStyle)
Sets the dash pattern for the generated outlines to style.
qreal miterLimit() const
Returns the miter limit for the generated outlines.
qreal curveThreshold() const
Returns the curve flattening threshold for the generated outlines.
Qt::PenCapStyle capStyle() const
Returns the cap style of the generated outlines.
void setDashPattern(const QList< qreal > &dashPattern)
This is an overloaded member function, provided for convenience. It differs from the above function o...
qreal width() const
Returns the width of the generated outlines.
void setDashOffset(qreal offset)
Sets the dash offset for the generated outlines to offset.
void setCapStyle(Qt::PenCapStyle style)
Sets the cap style of the generated outlines to style.
void setWidth(qreal width)
Sets the width of the generated outline painter path to width.
QPainterPath createStroke(const QPainterPath &path) const
Generates a new path that is a fillable area representing the outline of the given path.
void setJoinStyle(Qt::PenJoinStyle style)
Sets the join style of the generated outlines to style.
qreal dashOffset() const
Returns the dash offset for the generated outlines.
void setMiterLimit(qreal length)
Sets the miter limit of the generated outlines to limit.
\inmodule QtGui
friend class QPaintEngine
Definition qpainter.h:437
The QPolygonF class provides a list of points using floating point precision.
Definition qpolygon.h:96
Combined button and popup list for selecting options.
QDataStream & operator>>(QDataStream &s, QKeyCombination &combination)
Q_CORE_EXPORT QDebug operator<<(QDebug debug, QDir::Filters filters)
Definition qdir.cpp:2582
QPainterPath operator*(const QPainterPath &p, const QTransform &m)
static const quint16 mfhdr_maj
Definition qpicture.cpp:95
QT_BEGIN_NAMESPACE void qt_format_text(const QFont &fnt, const QRectF &_r, int tf, int alignment, const QTextOption *opt, const QString &str, QRectF *brect, int tabstops, int *, int tabarraylen, QPainter *painter)
static const quint16 mfhdr_min
Definition qpicture.cpp:96
const char * qt_mfhdr_tag
Definition qpicture.cpp:94