1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtGui module of the Qt Toolkit.
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 Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
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 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #ifndef QPAINTERPATH_H
43 #define QPAINTERPATH_H
45 #include <QtGui/qmatrix.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>
58 class QPainterPathPrivate;
59 struct QPainterPathPrivateDeleter;
60 class QPainterPathData;
61 class QPainterPathStrokerPrivate;
66 class Q_GUI_EXPORT QPainterPath
82 bool isMoveTo() const { return type == MoveToElement; }
83 bool isLineTo() const { return type == LineToElement; }
84 bool isCurveTo() const { return type == CurveToElement; }
86 operator QPointF () const { return QPointF(x, y); }
88 bool operator==(const Element &e) const { return qFuzzyCompare(x, e.x)
89 && qFuzzyCompare(y, e.y) && type == e.type; }
90 inline bool operator!=(const Element &e) const { return !operator==(e); }
94 explicit QPainterPath(const QPointF &startPoint);
95 QPainterPath(const QPainterPath &other);
96 QPainterPath &operator=(const QPainterPath &other);
97 #ifdef Q_COMPILER_RVALUE_REFS
98 inline QPainterPath &operator=(QPainterPath &&other)
99 { qSwap(d_ptr, other.d_ptr); return *this; }
102 inline void swap(QPainterPath &other) { d_ptr.swap(other.d_ptr); }
106 void moveTo(const QPointF &p);
107 inline void moveTo(qreal x, qreal y);
109 void lineTo(const QPointF &p);
110 inline void lineTo(qreal x, qreal y);
112 void arcMoveTo(const QRectF &rect, qreal angle);
113 inline void arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle);
115 void arcTo(const QRectF &rect, qreal startAngle, qreal arcLength);
116 inline void arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength);
118 void cubicTo(const QPointF &ctrlPt1, const QPointF &ctrlPt2, const QPointF &endPt);
119 inline void cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
120 qreal endPtx, qreal endPty);
121 void quadTo(const QPointF &ctrlPt, const QPointF &endPt);
122 inline void quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty);
124 QPointF currentPosition() const;
126 void addRect(const QRectF &rect);
127 inline void addRect(qreal x, qreal y, qreal w, qreal h);
128 void addEllipse(const QRectF &rect);
129 inline void addEllipse(qreal x, qreal y, qreal w, qreal h);
130 inline void addEllipse(const QPointF ¢er, qreal rx, qreal ry);
131 void addPolygon(const QPolygonF &polygon);
132 void addText(const QPointF &point, const QFont &f, const QString &text);
133 inline void addText(qreal x, qreal y, const QFont &f, const QString &text);
134 void addPath(const QPainterPath &path);
135 void addRegion(const QRegion ®ion);
137 void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
138 Qt::SizeMode mode = Qt::AbsoluteSize);
139 inline void addRoundedRect(qreal x, qreal y, qreal w, qreal h,
140 qreal xRadius, qreal yRadius,
141 Qt::SizeMode mode = Qt::AbsoluteSize);
143 void addRoundRect(const QRectF &rect, int xRnd, int yRnd);
144 inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,
146 inline void addRoundRect(const QRectF &rect, int roundness);
147 inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,
150 void connectPath(const QPainterPath &path);
152 bool contains(const QPointF &pt) const;
153 bool contains(const QRectF &rect) const;
154 bool intersects(const QRectF &rect) const;
156 void translate(qreal dx, qreal dy);
157 inline void translate(const QPointF &offset);
159 QPainterPath translated(qreal dx, qreal dy) const;
160 inline QPainterPath translated(const QPointF &offset) const;
162 QRectF boundingRect() const;
163 QRectF controlPointRect() const;
165 Qt::FillRule fillRule() const;
166 void setFillRule(Qt::FillRule fillRule);
168 bool isEmpty() const;
170 QPainterPath toReversed() const;
171 QList<QPolygonF> toSubpathPolygons(const QMatrix &matrix = QMatrix()) const;
172 QList<QPolygonF> toFillPolygons(const QMatrix &matrix = QMatrix()) const;
173 QPolygonF toFillPolygon(const QMatrix &matrix = QMatrix()) const;
174 QList<QPolygonF> toSubpathPolygons(const QTransform &matrix) const;
175 QList<QPolygonF> toFillPolygons(const QTransform &matrix) const;
176 QPolygonF toFillPolygon(const QTransform &matrix) const;
178 int elementCount() const;
179 QPainterPath::Element elementAt(int i) const;
180 void setElementPositionAt(int i, qreal x, qreal y);
182 qreal length() const;
183 qreal percentAtLength(qreal t) const;
184 QPointF pointAtPercent(qreal t) const;
185 qreal angleAtPercent(qreal t) const;
186 qreal slopeAtPercent(qreal t) const;
188 bool intersects(const QPainterPath &p) const;
189 bool contains(const QPainterPath &p) const;
190 QPainterPath united(const QPainterPath &r) const;
191 QPainterPath intersected(const QPainterPath &r) const;
192 QPainterPath subtracted(const QPainterPath &r) const;
193 QPainterPath subtractedInverted(const QPainterPath &r) const;
195 QPainterPath simplified() const;
197 bool operator==(const QPainterPath &other) const;
198 bool operator!=(const QPainterPath &other) const;
200 QPainterPath operator&(const QPainterPath &other) const;
201 QPainterPath operator|(const QPainterPath &other) const;
202 QPainterPath operator+(const QPainterPath &other) const;
203 QPainterPath operator-(const QPainterPath &other) const;
204 QPainterPath &operator&=(const QPainterPath &other);
205 QPainterPath &operator|=(const QPainterPath &other);
206 QPainterPath &operator+=(const QPainterPath &other);
207 QPainterPath &operator-=(const QPainterPath &other);
210 QScopedPointer<QPainterPathPrivate, QPainterPathPrivateDeleter> d_ptr;
212 inline void ensureData() { if (!d_ptr) ensureData_helper(); }
213 void ensureData_helper();
215 void detach_helper();
217 void computeBoundingRect() const;
218 void computeControlPointRect() const;
220 QPainterPathData *d_func() const { return reinterpret_cast<QPainterPathData *>(d_ptr.data()); }
222 friend class QPainterPathData;
223 friend class QPainterPathStroker;
224 friend class QPainterPathStrokerPrivate;
225 friend class QMatrix;
226 friend class QTransform;
227 friend class QVectorPath;
228 friend Q_GUI_EXPORT const QVectorPath &qtVectorPathForPath(const QPainterPath &);
230 #ifndef QT_NO_DATASTREAM
231 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
232 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
236 Q_DECLARE_TYPEINFO(QPainterPath::Element, Q_PRIMITIVE_TYPE);
238 #ifndef QT_NO_DATASTREAM
239 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
240 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
243 class Q_GUI_EXPORT QPainterPathStroker
245 Q_DECLARE_PRIVATE(QPainterPathStroker)
247 QPainterPathStroker();
248 ~QPainterPathStroker();
250 void setWidth(qreal width);
253 void setCapStyle(Qt::PenCapStyle style);
254 Qt::PenCapStyle capStyle() const;
256 void setJoinStyle(Qt::PenJoinStyle style);
257 Qt::PenJoinStyle joinStyle() const;
259 void setMiterLimit(qreal length);
260 qreal miterLimit() const;
262 void setCurveThreshold(qreal threshold);
263 qreal curveThreshold() const;
265 void setDashPattern(Qt::PenStyle);
266 void setDashPattern(const QVector<qreal> &dashPattern);
267 QVector<qreal> dashPattern() const;
269 void setDashOffset(qreal offset);
270 qreal dashOffset() const;
272 QPainterPath createStroke(const QPainterPath &path) const;
275 Q_DISABLE_COPY(QPainterPathStroker)
277 friend class QX11PaintEngine;
279 QScopedPointer<QPainterPathStrokerPrivate> d_ptr;
282 inline void QPainterPath::moveTo(qreal x, qreal y)
284 moveTo(QPointF(x, y));
287 inline void QPainterPath::lineTo(qreal x, qreal y)
289 lineTo(QPointF(x, y));
292 inline void QPainterPath::arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength)
294 arcTo(QRectF(x, y, w, h), startAngle, arcLength);
297 inline void QPainterPath::arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle)
299 arcMoveTo(QRectF(x, y, w, h), angle);
302 inline void QPainterPath::cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
303 qreal endPtx, qreal endPty)
305 cubicTo(QPointF(ctrlPt1x, ctrlPt1y), QPointF(ctrlPt2x, ctrlPt2y),
306 QPointF(endPtx, endPty));
309 inline void QPainterPath::quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty)
311 quadTo(QPointF(ctrlPtx, ctrlPty), QPointF(endPtx, endPty));
314 inline void QPainterPath::addEllipse(qreal x, qreal y, qreal w, qreal h)
316 addEllipse(QRectF(x, y, w, h));
319 inline void QPainterPath::addEllipse(const QPointF ¢er, qreal rx, qreal ry)
321 addEllipse(QRectF(center.x() - rx, center.y() - ry, 2 * rx, 2 * ry));
324 inline void QPainterPath::addRect(qreal x, qreal y, qreal w, qreal h)
326 addRect(QRectF(x, y, w, h));
329 inline void QPainterPath::addRoundedRect(qreal x, qreal y, qreal w, qreal h,
330 qreal xRadius, qreal yRadius,
333 addRoundedRect(QRectF(x, y, w, h), xRadius, yRadius, mode);
336 inline void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h,
339 addRoundRect(QRectF(x, y, w, h), xRnd, yRnd);
342 inline void QPainterPath::addRoundRect(const QRectF &rect,
345 int xRnd = roundness;
346 int yRnd = roundness;
347 if (rect.width() > rect.height())
348 xRnd = int(roundness * rect.height()/rect.width());
350 yRnd = int(roundness * rect.width()/rect.height());
351 addRoundRect(rect, xRnd, yRnd);
354 inline void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h,
357 addRoundRect(QRectF(x, y, w, h), roundness);
360 inline void QPainterPath::addText(qreal x, qreal y, const QFont &f, const QString &text)
362 addText(QPointF(x, y), f, text);
365 inline void QPainterPath::translate(const QPointF &offset)
366 { translate(offset.x(), offset.y()); }
368 inline QPainterPath QPainterPath::translated(const QPointF &offset) const
369 { return translated(offset.x(), offset.y()); }
372 #ifndef QT_NO_DEBUG_STREAM
373 Q_GUI_EXPORT QDebug operator<<(QDebug, const QPainterPath &);
380 #endif // QPAINTERPATH_H