QtGui/qpainterpath.h Source File

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;
60 struct QPainterPathPrivateDeleter;
61 class QPainterPathData;
62 class QPainterPathStrokerPrivate;
63 class QPolygonF;
64 class QRegion;
65 class QVectorPath;
66 
67 class Q_GUI_EXPORT QPainterPath
68 {
69 public:
70  enum ElementType {
74  CurveToDataElement
75  };
76 
77  class Element {
78  public:
79  qreal x;
80  qreal y;
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;
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 &);
230 
231 #ifndef QT_NO_DATASTREAM
232  friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
233  friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
234 #endif
235 };
236 
237 class QPainterPathPrivate
238 {
239 public:
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 &);
251 #endif
252 private:
253  QAtomicInt ref;
255 };
256 
257 Q_DECLARE_TYPEINFO(QPainterPath::Element, Q_PRIMITIVE_TYPE);
258 
259 #ifndef QT_NO_DATASTREAM
260 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
261 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
262 #endif
263 
264 class Q_GUI_EXPORT QPainterPathStroker
265 {
266  Q_DECLARE_PRIVATE(QPainterPathStroker)
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:
296  Q_DISABLE_COPY(QPainterPathStroker)
297 
298  friend class QX11PaintEngine;
299 
300  QScopedPointer<QPainterPathStrokerPrivate> d_ptr;
301 };
302 
303 inline void QPainterPath::moveTo(qreal x, qreal y)
304 {
305  moveTo(QPointF(x, y));
306 }
307 
308 inline void QPainterPath::lineTo(qreal x, qreal y)
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 
318 inline void QPainterPath::arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle)
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 
335 inline void QPainterPath::addEllipse(qreal x, qreal y, qreal w, qreal h)
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 
350 inline void QPainterPath::addRoundedRect(qreal x, qreal y, qreal w, qreal h,
351  qreal xRadius, qreal yRadius,
353 {
354  addRoundedRect(QRectF(x, y, w, h), xRadius, yRadius, mode);
355 }
356 
357 inline void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h,
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 
375 inline void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h,
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 
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 
409 inline void QPainterPath::setElementPositionAt(int i, qreal x, qreal y)
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
428 Q_GUI_EXPORT QDebug operator<<(QDebug, const QPainterPath &);
429 #endif
430 
432 
434 
435 #endif // QPAINTERPATH_H
void translate(qreal dx, qreal dy)
const T & at(int i) const
Definition: qvector.h:350
GLdouble GLdouble GLdouble r
Definition: GLee.h:1189
GLuint GLenum matrix
Definition: GLee.h:6572
GLenum GLint ref
Definition: GLee.h:1701
unsigned int(APIENTRYP PFNGLXGETAGPOFFSETMESAPROC)(const void *pointer)
Definition: GLee.h:10762
GLenum GLint GLint y
Definition: GLee.h:876
bool isEmpty() const
Definition: qpainterpath.h:392
GLint mode
Definition: GLee.h:4479
int int int int * dy
Definition: GLee.h:10535
void lineTo(const QPointF &p)
bool isMoveTo() const
Definition: qpainterpath.h:83
#define QT_END_NAMESPACE
Definition: qglobal.h:128
GLintptr offset
Definition: GLee.h:1562
void quadTo(const QPointF &ctrlPt, const QPointF &endPt)
bool operator==(const Element &e) const
Definition: qpainterpath.h:89
T * data() const
QByteArray & operator+=(QByteArray &a, const QStringBuilder< A, B > &b)
#define QT_BEGIN_HEADER
Definition: qglobal.h:141
void addText(const QPointF &point, const QFont &f, const QString &text)
void swap(QPainterPath &other)
Definition: qpainterpath.h:103
void cubicTo(const QPointF &ctrlPt1, const QPointF &ctrlPt2, const QPointF &endPt)
GLenum GLsizei width
Definition: GLee.h:873
bool isCurveTo() const
Definition: qpainterpath.h:85
friend Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QPainterPath &)
#define inline
Definition: image.h:2490
void addRoundRect(const QRectF &rect, int xRnd, int yRnd)
SizeMode
Definition: qnamespace.h:1187
bool operator!=(const Element &e) const
Definition: qpainterpath.h:91
Definition: qdebug.h:62
void setElementPositionAt(int i, qreal x, qreal y)
Definition: qpainterpath.h:409
bool operator==(const Attribute &cA, const AttributeInstance< type > &cB)
This operator compares the two attributes and NOT their values.
Definition: node.h:577
Q_CORE_EXPORT QBitArray operator&(const QBitArray &, const QBitArray &)
QPainterPath translated(qreal dx, qreal dy) const
qreal y() const
Definition: qpoint.h:287
#define QT_BEGIN_NAMESPACE
Definition: qglobal.h:127
Definition: qfont.h:64
Q_INLINE_TEMPLATE void qSwap(QScopedPointer< T, Cleanup > &p1, QScopedPointer< T, Cleanup > &p2)
Q_DECLARE_TYPEINFO(QPainterPath::Element, Q_PRIMITIVE_TYPE)
friend Q_GUI_EXPORT QDataStream & operator<<(QDataStream &, const QPainterPath &)
PenJoinStyle
Definition: qnamespace.h:1154
GLenum GLint x
Definition: GLee.h:876
void addEllipse(const QRectF &rect)
const QByteArray operator+(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:564
bool isLineTo() const
Definition: qpainterpath.h:84
GLuint GLsizei GLsizei * length
Definition: GLee.h:1713
Q_CORE_EXPORT QTextStream & center(QTextStream &s)
bool operator!=(const QByteArray &a1, const QByteArray &a2)
Definition: qbytearray.h:533
Q_GUI_EXPORT QDataStream & operator<<(QDataStream &, const QPainterPath &)
void arcTo(const QRectF &rect, qreal startAngle, qreal arcLength)
qreal x() const
Definition: qpoint.h:282
Q_GUI_EXPORT QDataStream & operator>>(QDataStream &, QPainterPath &)
Definition: qrect.h:511
GLfloat GLfloat p
Definition: GLee.h:5416
int int int int int int h
Definition: GLee.h:10534
bool qFuzzyCompare(const QMatrix &m1, const QMatrix &m2)
Definition: qmatrix.h:172
int int int * dx
Definition: GLee.h:10535
void addRect(const QRectF &rect)
const QPainterPath::Element & elementAt(int i) const
Definition: qpainterpath.h:402
int elementCount() const
Definition: qpainterpath.h:397
const QPoint operator-(const QPoint &p1, const QPoint &p2)
Definition: qpoint.h:170
T & first()
Definition: qvector.h:260
int size() const
Definition: qvector.h:137
void arcMoveTo(const QRectF &rect, qreal angle)
Q_CORE_EXPORT QBitArray operator|(const QBitArray &, const QBitArray &)
GLubyte GLubyte GLubyte GLubyte w
Definition: GLee.h:1775
void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius, Qt::SizeMode mode=Qt::AbsoluteSize)
GLdouble GLdouble t
Definition: GLee.h:1181
FillRule
Definition: qnamespace.h:1485
qreal height() const
Definition: qrect.h:710
#define QT_END_HEADER
Definition: qglobal.h:142
GLclampf f
Definition: GLee.h:9303
PenCapStyle
Definition: qnamespace.h:1147
qreal width() const
Definition: qrect.h:707
PenStyle
Definition: qnamespace.h:1134