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