Qt 4.8
qpainterpath.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
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 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.
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 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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #ifndef QPAINTERPATH_H
43 #define QPAINTERPATH_H
44 
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>
51 
53 
55 
56 QT_MODULE(Gui)
57 
58 class QFont;
61 class QPainterPathData;
63 class QPolygonF;
64 class QRegion;
65 class QVectorPath;
66 
68 {
69 public:
70  enum ElementType {
74  CurveToDataElement
75  };
76 
77  class Element {
78  public:
82 
83  bool isMoveTo() const { return type == MoveToElement; }
84  bool isLineTo() const { return type == LineToElement; }
85  bool isCurveTo() const { return type == CurveToElement; }
86 
87  operator QPointF () const { return QPointF(x, y); }
88 
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); }
92  };
93 
94  QPainterPath();
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; }
101 #endif
102  ~QPainterPath();
103  inline void swap(QPainterPath &other) { d_ptr.swap(other.d_ptr); }
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,
140  inline void addRoundedRect(qreal x, qreal y, qreal w, qreal h,
141  qreal xRadius, qreal yRadius,
143 
144  void addRoundRect(const QRectF &rect, int xRnd, int yRnd);
145  inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,
146  int xRnd, int yRnd);
147  inline void addRoundRect(const QRectF &rect, int roundness);
148  inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,
149  int roundness);
150 
151  void connectPath(const QPainterPath &path);
152 
153  bool contains(const QPointF &pt) const;
154  bool contains(const QRectF &rect) const;
155  bool intersects(const QRectF &rect) const;
156 
157  void translate(qreal dx, qreal dy);
158  inline void translate(const QPointF &offset);
159 
160  QPainterPath translated(qreal dx, qreal dy) const;
161  inline QPainterPath translated(const QPointF &offset) const;
162 
163  QRectF boundingRect() const;
164  QRectF controlPointRect() const;
165 
166  Qt::FillRule fillRule() const;
167  void setFillRule(Qt::FillRule fillRule);
168 
169  inline bool isEmpty() const;
170 
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;
178 
179  inline int elementCount() const;
180  inline const QPainterPath::Element &elementAt(int i) const;
181  inline void setElementPositionAt(int i, qreal x, qreal y);
182 
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;
188 
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;
195 
196  QPainterPath simplified() const;
197 
198  bool operator==(const QPainterPath &other) const;
199  bool operator!=(const QPainterPath &other) const;
200 
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);
209 
210 private:
212 
213  inline void ensureData() { if (!d_ptr) ensureData_helper(); }
214  void ensureData_helper();
215  inline void detach();
216  void detach_helper();
217  void setDirty(bool);
218  void computeBoundingRect() const;
219  void computeControlPointRect() const;
220 
221  QPainterPathData *d_func() const { return reinterpret_cast<QPainterPathData *>(d_ptr.data()); }
222 
223  friend class QPainterPathData;
224  friend class QPainterPathStroker;
226  friend class QMatrix;
227  friend class QTransform;
228  friend class QVectorPath;
230 
231 #ifndef QT_NO_DATASTREAM
234 #endif
235 };
236 
238 {
239 public:
240  friend class QPainterPath;
241  friend class QPainterPathData;
242  friend class QPainterPathStroker;
244  friend class QMatrix;
245  friend class QTransform;
246  friend class QVectorPath;
248 #ifndef QT_NO_DATASTREAM
251 #endif
252 private:
255 };
256 
258 
259 #ifndef QT_NO_DATASTREAM
262 #endif
263 
265 {
267 public:
270 
271  void setWidth(qreal width);
272  qreal width() const;
273 
274  void setCapStyle(Qt::PenCapStyle style);
275  Qt::PenCapStyle capStyle() const;
276 
277  void setJoinStyle(Qt::PenJoinStyle style);
278  Qt::PenJoinStyle joinStyle() const;
279 
280  void setMiterLimit(qreal length);
281  qreal miterLimit() const;
282 
283  void setCurveThreshold(qreal threshold);
284  qreal curveThreshold() const;
285 
286  void setDashPattern(Qt::PenStyle);
287  void setDashPattern(const QVector<qreal> &dashPattern);
288  QVector<qreal> dashPattern() const;
289 
290  void setDashOffset(qreal offset);
291  qreal dashOffset() const;
292 
293  QPainterPath createStroke(const QPainterPath &path) const;
294 
295 private:
297 
299 
301 };
302 
303 inline void QPainterPath::moveTo(qreal x, qreal y)
304 {
305  moveTo(QPointF(x, y));
306 }
307 
309 {
310  lineTo(QPointF(x, y));
311 }
312 
313 inline void QPainterPath::arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength)
314 {
315  arcTo(QRectF(x, y, w, h), startAngle, arcLength);
316 }
317 
319 {
320  arcMoveTo(QRectF(x, y, w, h), angle);
321 }
322 
323 inline void QPainterPath::cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
324  qreal endPtx, qreal endPty)
325 {
326  cubicTo(QPointF(ctrlPt1x, ctrlPt1y), QPointF(ctrlPt2x, ctrlPt2y),
327  QPointF(endPtx, endPty));
328 }
329 
330 inline void QPainterPath::quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty)
331 {
332  quadTo(QPointF(ctrlPtx, ctrlPty), QPointF(endPtx, endPty));
333 }
334 
336 {
337  addEllipse(QRectF(x, y, w, h));
338 }
339 
340 inline void QPainterPath::addEllipse(const QPointF &center, qreal rx, qreal ry)
341 {
342  addEllipse(QRectF(center.x() - rx, center.y() - ry, 2 * rx, 2 * ry));
343 }
344 
345 inline void QPainterPath::addRect(qreal x, qreal y, qreal w, qreal h)
346 {
347  addRect(QRectF(x, y, w, h));
348 }
349 
351  qreal xRadius, qreal yRadius,
352  Qt::SizeMode mode)
353 {
354  addRoundedRect(QRectF(x, y, w, h), xRadius, yRadius, mode);
355 }
356 
358  int xRnd, int yRnd)
359 {
360  addRoundRect(QRectF(x, y, w, h), xRnd, yRnd);
361 }
362 
363 inline void QPainterPath::addRoundRect(const QRectF &rect,
364  int roundness)
365 {
366  int xRnd = roundness;
367  int yRnd = roundness;
368  if (rect.width() > rect.height())
369  xRnd = int(roundness * rect.height()/rect.width());
370  else
371  yRnd = int(roundness * rect.width()/rect.height());
372  addRoundRect(rect, xRnd, yRnd);
373 }
374 
376  int roundness)
377 {
378  addRoundRect(QRectF(x, y, w, h), roundness);
379 }
380 
381 inline void QPainterPath::addText(qreal x, qreal y, const QFont &f, const QString &text)
382 {
383  addText(QPointF(x, y), f, text);
384 }
385 
386 inline void QPainterPath::translate(const QPointF &offset)
387 { translate(offset.x(), offset.y()); }
388 
389 inline QPainterPath QPainterPath::translated(const QPointF &offset) const
390 { return translated(offset.x(), offset.y()); }
391 
392 inline bool QPainterPath::isEmpty() const
393 {
394  return !d_ptr || (d_ptr->elements.size() == 1 && d_ptr->elements.first().type == MoveToElement);
395 }
396 
397 inline int QPainterPath::elementCount() const
398 {
399  return d_ptr ? d_ptr->elements.size() : 0;
400 }
401 
403 {
404  Q_ASSERT(d_ptr);
405  Q_ASSERT(i >= 0 && i < elementCount());
406  return d_ptr->elements.at(i);
407 }
408 
410 {
411  Q_ASSERT(d_ptr);
412  Q_ASSERT(i >= 0 && i < elementCount());
413  detach();
414  QPainterPath::Element &e = d_ptr->elements[i];
415  e.x = x;
416  e.y = y;
417 }
418 
419 
420 inline void QPainterPath::detach()
421 {
422  if (d_ptr->ref != 1)
423  detach_helper();
424  setDirty(true);
425 }
426 
427 #ifndef QT_NO_DEBUG_STREAM
429 #endif
430 
432 
434 
435 #endif // QPAINTERPATH_H
ElementType type
the type of element
Definition: qpainterpath.h:81
The QDebug class provides an output stream for debugging information.
Definition: qdebug.h:62
The QPainterPath::Element class specifies the position and type of a subpath.
Definition: qpainterpath.h:77
bool isEmpty() const
Returns true if either there are no elements in this path, or if the only element is a MoveToElement;...
Definition: qpainterpath.h:392
ElementType
This enum describes the types of elements used to connect vertices in subpaths.
Definition: qpainterpath.h:70
void addRoundRect(const QRectF &rect, int xRnd, int yRnd)
Adds a rectangle r with rounded corners to the path.
timeval operator-(const timeval &t1, const timeval &t2)
Definition: qcore_unix_p.h:133
double qreal
Definition: qglobal.h:1193
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
void translate(qreal dx, qreal dy)
Translates all elements in the path by ({dx}, {dy}).
#define QT_MODULE(x)
Definition: qglobal.h:2783
T * data() const
Returns the value of the pointer referenced by this object.
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
The QMatrix class specifies 2D transformations of a coordinate system.
Definition: qmatrix.h:61
The QAtomicInt class provides platform-independent atomic operations on integers. ...
Definition: qatomic.h:55
The QPainterPath class provides a container for painting operations, enabling graphical shapes to be ...
Definition: qpainterpath.h:67
#define Q_GUI_EXPORT
Definition: qglobal.h:1450
void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode=Qt::AbsoluteSize)
Adds the given rectangle rect with rounded corners to the path.
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
FillRule
Definition: qnamespace.h:1485
bool isLineTo() const
Returns true if the element is a line, otherwise returns false.
Definition: qpainterpath.h:84
static Q_DECL_CONSTEXPR bool qFuzzyCompare(double p1, double p2)
Definition: qglobal.h:2030
#define Q_DISABLE_COPY(Class)
Disables the use of copy constructors and assignment operators for the given Class.
Definition: qglobal.h:2523
bool operator!=(QBool b1, bool b2)
Definition: qglobal.h:2026
const QVectorPath & qtVectorPathForPath(const QPainterPath &path)
bool operator==(const Element &e) const
Returns true if this element is equal to other; otherwise returns false.
Definition: qpainterpath.h:89
bool isCurveTo() const
Returns true if the element is a curve, otherwise returns false.
Definition: qpainterpath.h:85
qreal y
the y coordinate of the element&#39;s position.
Definition: qpainterpath.h:80
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
void arcMoveTo(const QRectF &rect, qreal angle)
Creates a move to that lies on the arc that occupies the given rectangle at angle.
const QPainterPath::Element & elementAt(int i) const
Returns the element at the given index in the painter path.
Definition: qpainterpath.h:402
PenStyle
Definition: qnamespace.h:1134
qreal x() const
Returns the x-coordinate of this point.
Definition: qpoint.h:282
PenCapStyle
Definition: qnamespace.h:1147
static const QRectF boundingRect(const QPointF *points, int pointCount)
void lineTo(const QPointF &p)
Adds a straight line from the current position to the given endPoint.
void setElementPositionAt(int i, qreal x, qreal y)
Sets the x and y coordinate of the element at index index to x and y.
Definition: qpainterpath.h:409
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
void ensureData()
Definition: qpainterpath.h:213
static bool isEmpty(const char *str)
QPainterPathData * d_func() const
Definition: qpainterpath.h:221
void swap(QPainterPath &other)
Swaps painter path other with this painter path.
Definition: qpainterpath.h:103
qreal height() const
Returns the height of the rectangle.
Definition: qrect.h:710
Q_DECLARE_TYPEINFO(QPainterPath::Element, Q_PRIMITIVE_TYPE)
The QPolygonF class provides a vector of points using floating point precision.
Definition: qpolygon.h:134
void addRect(const QRectF &rect)
Adds the given rectangle to this path as a closed subpath.
bool operator!=(const Element &e) const
Returns true if this element is not equal to other; otherwise returns false.
Definition: qpainterpath.h:91
qreal width() const
Returns the width of the rectangle.
Definition: qrect.h:707
The QRegion class specifies a clip region for a painter.
Definition: qregion.h:68
The QPainterPathStroker class is used to generate fillable outlines for a given painter path...
Definition: qpainterpath.h:264
PenJoinStyle
Definition: qnamespace.h:1154
void qSwap(T &value1, T &value2)
Definition: qglobal.h:2181
Q_CORE_EXPORT QTextStream & center(QTextStream &s)
Q_GUI_EXPORT QDataStream & operator<<(QDataStream &, const QPainterPath &)
qreal angle(const QPointF &p1, const QPointF &p2)
Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QPainterPath &)
The QFont class specifies a font used for drawing text.
Definition: qfont.h:64
Q_CORE_EXPORT QBitArray operator &(const QBitArray &, const QBitArray &)
void arcTo(const QRectF &rect, qreal startAngle, qreal arcLength)
Creates an arc that occupies the given rectangle, beginning at the specified startAngle and extending...
void addText(const QPointF &point, const QFont &f, const QString &text)
Adds the given text to this path as a set of closed subpaths created from the font supplied...
bool isMoveTo() const
Returns true if the element is moving the current position, otherwise returns false.
Definition: qpainterpath.h:83
QVector< QPainterPath::Element > elements
Definition: qpainterpath.h:254
void quadTo(const QPointF &ctrlPt, const QPointF &endPt)
Adds a quadratic Bezier curve between the current position and the given endPoint with the control po...
qreal x
the x coordinate of the element&#39;s position.
Definition: qpainterpath.h:79
qreal y() const
Returns the y-coordinate of this point.
Definition: qpoint.h:287
void cubicTo(const QPointF &ctrlPt1, const QPointF &ctrlPt2, const QPointF &endPt)
Adds a cubic Bezier curve between the current position and the given endPoint using the control point...
#define Q_DECLARE_PRIVATE(Class)
Definition: qglobal.h:2467
int elementCount() const
Returns the number of path elements in the painter path.
Definition: qpainterpath.h:397
static void setCapStyle(int cap_style, GC gc)
The QDataStream class provides serialization of binary data to a QIODevice.
Definition: qdatastream.h:71
Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &)
SizeMode
Definition: qnamespace.h:1187
timeval & operator+=(timeval &t1, const timeval &t2)
Definition: qcore_unix_p.h:120
#define class
#define QT_END_HEADER
Definition: qglobal.h:137
bool operator==(QBool b1, bool b2)
Definition: qglobal.h:2023
void addEllipse(const QRectF &rect)
Creates an ellipse within the specified boundingRectangle and adds it to the painter path as a closed...
#define text
Definition: qobjectdefs.h:80
QScopedPointer< QPainterPathPrivate, QPainterPathPrivateDeleter > d_ptr
Definition: qpainterpath.h:211
The QTransform class specifies 2D transformations of a coordinate system.
Definition: qtransform.h:65
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
QPainterPath translated(qreal dx, qreal dy) const
Returns a copy of the path that is translated by ({dx}, {dy}).
timeval operator+(const timeval &t1, const timeval &t2)
Definition: qcore_unix_p.h:126