Replace 'i < len-1 && func(i+1)' by 'i+1 < len && func(i+1)'
[profile/ivi/qtbase.git] / src / gui / painting / qpainterpath.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtGui module of the Qt Toolkit.
8 **
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.
17 **
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.
21 **
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.
29 **
30 ** Other Usage
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.
33 **
34 **
35 **
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
52 QT_BEGIN_HEADER
53
54 QT_BEGIN_NAMESPACE
55
56 QT_MODULE(Gui)
57
58 class QFont;
59 class QPainterPathPrivate;
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 {
71         MoveToElement,
72         LineToElement,
73         CurveToElement,
74         CurveToDataElement
75     };
76
77     class Element {
78     public:
79         qreal x;
80         qreal y;
81         ElementType type;
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,
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);
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:
211     QScopedPointer<QPainterPathPrivate, QPainterPathPrivateDeleter> d_ptr;
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;
254     QVector<QPainterPath::Element> elements;
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:
268     QPainterPathStroker();
269     ~QPainterPathStroker();
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,
352                                          Qt::SizeMode mode)
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
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
402 inline const QPainterPath::Element &QPainterPath::elementAt(int i) const
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
431 QT_END_NAMESPACE
432
433 QT_END_HEADER
434
435 #endif // QPAINTERPATH_H