1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtGui module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
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>
59 class QPainterPathPrivate;
60 struct QPainterPathPrivateDeleter;
61 class QPainterPathData;
62 class QPainterPathStrokerPrivate;
67 class Q_GUI_EXPORT QPainterPath
83 bool isMoveTo() const { return type == MoveToElement; }
84 bool isLineTo() const { return type == LineToElement; }
85 bool isCurveTo() const { return type == CurveToElement; }
87 operator QPointF () const { return QPointF(x, y); }
89 bool operator==(const Element &e) const { return qFuzzyCompare(x, e.x)
90 && qFuzzyCompare(y, e.y) && type == e.type; }
91 inline bool operator!=(const Element &e) const { return !operator==(e); }
95 explicit QPainterPath(const QPointF &startPoint);
96 QPainterPath(const QPainterPath &other);
97 QPainterPath &operator=(const QPainterPath &other);
98 #ifdef Q_COMPILER_RVALUE_REFS
99 inline QPainterPath &operator=(QPainterPath &&other)
100 { qSwap(d_ptr, other.d_ptr); return *this; }
103 inline void swap(QPainterPath &other) { d_ptr.swap(other.d_ptr); }
107 void moveTo(const QPointF &p);
108 inline void moveTo(qreal x, qreal y);
110 void lineTo(const QPointF &p);
111 inline void lineTo(qreal x, qreal y);
113 void arcMoveTo(const QRectF &rect, qreal angle);
114 inline void arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle);
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);
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);
125 QPointF currentPosition() const;
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 ¢er, 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 ®ion);
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);
144 void addRoundRect(const QRectF &rect, int xRnd, int yRnd);
145 inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,
147 inline void addRoundRect(const QRectF &rect, int roundness);
148 inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,
151 void connectPath(const QPainterPath &path);
153 bool contains(const QPointF &pt) const;
154 bool contains(const QRectF &rect) const;
155 bool intersects(const QRectF &rect) const;
157 void translate(qreal dx, qreal dy);
158 inline void translate(const QPointF &offset);
160 QPainterPath translated(qreal dx, qreal dy) const;
161 inline QPainterPath translated(const QPointF &offset) const;
163 QRectF boundingRect() const;
164 QRectF controlPointRect() const;
166 Qt::FillRule fillRule() const;
167 void setFillRule(Qt::FillRule fillRule);
169 inline bool isEmpty() const;
171 QPainterPath toReversed() const;
172 QList<QPolygonF> toSubpathPolygons(const QMatrix &matrix = QMatrix()) const;
173 QList<QPolygonF> toFillPolygons(const QMatrix &matrix = QMatrix()) const;
174 QPolygonF toFillPolygon(const QMatrix &matrix = QMatrix()) const;
175 QList<QPolygonF> toSubpathPolygons(const QTransform &matrix) const;
176 QList<QPolygonF> toFillPolygons(const QTransform &matrix) const;
177 QPolygonF toFillPolygon(const QTransform &matrix) const;
179 inline int elementCount() const;
180 inline const QPainterPath::Element &elementAt(int i) const;
181 inline void setElementPositionAt(int i, qreal x, qreal y);
183 qreal length() const;
184 qreal percentAtLength(qreal t) const;
185 QPointF pointAtPercent(qreal t) const;
186 qreal angleAtPercent(qreal t) const;
187 qreal slopeAtPercent(qreal t) const;
189 bool intersects(const QPainterPath &p) const;
190 bool contains(const QPainterPath &p) const;
191 QPainterPath united(const QPainterPath &r) const;
192 QPainterPath intersected(const QPainterPath &r) const;
193 QPainterPath subtracted(const QPainterPath &r) const;
194 QPainterPath subtractedInverted(const QPainterPath &r) const;
196 QPainterPath simplified() const;
198 bool operator==(const QPainterPath &other) const;
199 bool 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) const;
205 QPainterPath &operator&=(const QPainterPath &other);
206 QPainterPath &operator|=(const QPainterPath &other);
207 QPainterPath &operator+=(const QPainterPath &other);
208 QPainterPath &operator-=(const QPainterPath &other);
211 QScopedPointer<QPainterPathPrivate, QPainterPathPrivateDeleter> d_ptr;
213 inline void ensureData() { if (!d_ptr) ensureData_helper(); }
214 void ensureData_helper();
215 inline void detach();
216 void detach_helper();
218 void computeBoundingRect() const;
219 void computeControlPointRect() const;
221 QPainterPathData *d_func() const { return reinterpret_cast<QPainterPathData *>(d_ptr.data()); }
223 friend class QPainterPathData;
224 friend class QPainterPathStroker;
225 friend class QPainterPathStrokerPrivate;
226 friend class QMatrix;
227 friend class QTransform;
228 friend class QVectorPath;
229 friend Q_GUI_EXPORT const QVectorPath &qtVectorPathForPath(const QPainterPath &);
231 #ifndef QT_NO_DATASTREAM
232 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
233 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
237 class QPainterPathPrivate
240 friend class QPainterPath;
241 friend class QPainterPathData;
242 friend class QPainterPathStroker;
243 friend class QPainterPathStrokerPrivate;
244 friend class QMatrix;
245 friend class QTransform;
246 friend class QVectorPath;
247 friend struct QPainterPathPrivateDeleter;
248 #ifndef QT_NO_DATASTREAM
249 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
250 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
254 QVector<QPainterPath::Element> elements;
257 Q_DECLARE_TYPEINFO(QPainterPath::Element, Q_PRIMITIVE_TYPE);
259 #ifndef QT_NO_DATASTREAM
260 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
261 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
264 class Q_GUI_EXPORT QPainterPathStroker
266 Q_DECLARE_PRIVATE(QPainterPathStroker)
268 QPainterPathStroker();
269 ~QPainterPathStroker();
271 void setWidth(qreal width);
274 void setCapStyle(Qt::PenCapStyle style);
275 Qt::PenCapStyle capStyle() const;
277 void setJoinStyle(Qt::PenJoinStyle style);
278 Qt::PenJoinStyle joinStyle() const;
280 void setMiterLimit(qreal length);
281 qreal miterLimit() const;
283 void setCurveThreshold(qreal threshold);
284 qreal curveThreshold() const;
286 void setDashPattern(Qt::PenStyle);
287 void setDashPattern(const QVector<qreal> &dashPattern);
288 QVector<qreal> dashPattern() const;
290 void setDashOffset(qreal offset);
291 qreal dashOffset() const;
293 QPainterPath createStroke(const QPainterPath &path) const;
296 Q_DISABLE_COPY(QPainterPathStroker)
298 friend class QX11PaintEngine;
300 QScopedPointer<QPainterPathStrokerPrivate> d_ptr;
303 inline void QPainterPath::moveTo(qreal x, qreal y)
305 moveTo(QPointF(x, y));
308 inline void QPainterPath::lineTo(qreal x, qreal y)
310 lineTo(QPointF(x, y));
313 inline void QPainterPath::arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength)
315 arcTo(QRectF(x, y, w, h), startAngle, arcLength);
318 inline void QPainterPath::arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle)
320 arcMoveTo(QRectF(x, y, w, h), angle);
323 inline void QPainterPath::cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
324 qreal endPtx, qreal endPty)
326 cubicTo(QPointF(ctrlPt1x, ctrlPt1y), QPointF(ctrlPt2x, ctrlPt2y),
327 QPointF(endPtx, endPty));
330 inline void QPainterPath::quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty)
332 quadTo(QPointF(ctrlPtx, ctrlPty), QPointF(endPtx, endPty));
335 inline void QPainterPath::addEllipse(qreal x, qreal y, qreal w, qreal h)
337 addEllipse(QRectF(x, y, w, h));
340 inline void QPainterPath::addEllipse(const QPointF ¢er, qreal rx, qreal ry)
342 addEllipse(QRectF(center.x() - rx, center.y() - ry, 2 * rx, 2 * ry));
345 inline void QPainterPath::addRect(qreal x, qreal y, qreal w, qreal h)
347 addRect(QRectF(x, y, w, h));
350 inline void QPainterPath::addRoundedRect(qreal x, qreal y, qreal w, qreal h,
351 qreal xRadius, qreal yRadius,
354 addRoundedRect(QRectF(x, y, w, h), xRadius, yRadius, mode);
357 inline void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h,
360 addRoundRect(QRectF(x, y, w, h), xRnd, yRnd);
363 inline void QPainterPath::addRoundRect(const QRectF &rect,
366 int xRnd = roundness;
367 int yRnd = roundness;
368 if (rect.width() > rect.height())
369 xRnd = int(roundness * rect.height()/rect.width());
371 yRnd = int(roundness * rect.width()/rect.height());
372 addRoundRect(rect, xRnd, yRnd);
375 inline void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h,
378 addRoundRect(QRectF(x, y, w, h), roundness);
381 inline void QPainterPath::addText(qreal x, qreal y, const QFont &f, const QString &text)
383 addText(QPointF(x, y), f, text);
386 inline void QPainterPath::translate(const QPointF &offset)
387 { translate(offset.x(), offset.y()); }
389 inline QPainterPath QPainterPath::translated(const QPointF &offset) const
390 { return translated(offset.x(), offset.y()); }
392 inline bool QPainterPath::isEmpty() const
394 return !d_ptr || (d_ptr->elements.size() == 1 && d_ptr->elements.first().type == MoveToElement);
397 inline int QPainterPath::elementCount() const
399 return d_ptr ? d_ptr->elements.size() : 0;
402 inline const QPainterPath::Element &QPainterPath::elementAt(int i) const
405 Q_ASSERT(i >= 0 && i < elementCount());
406 return d_ptr->elements.at(i);
409 inline void QPainterPath::setElementPositionAt(int i, qreal x, qreal y)
412 Q_ASSERT(i >= 0 && i < elementCount());
414 QPainterPath::Element &e = d_ptr->elements[i];
420 inline void QPainterPath::detach()
427 #ifndef QT_NO_DEBUG_STREAM
428 Q_GUI_EXPORT QDebug operator<<(QDebug, const QPainterPath &);
435 #endif // QPAINTERPATH_H