source: trunk/src/gui/painting/qpainterpath.h@ 478

Last change on this file since 478 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 13.1 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information (qt-info@nokia.com)
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at qt-sales@nokia.com.
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
51QT_BEGIN_HEADER
52
53QT_BEGIN_NAMESPACE
54
55QT_MODULE(Gui)
56
57class QFont;
58class QPainterPathPrivate;
59class QPainterPathData;
60class QPainterPathStrokerPrivate;
61class QPolygonF;
62class QRegion;
63class QVectorPath;
64
65class Q_GUI_EXPORT QPainterPath
66{
67public:
68 enum ElementType {
69 MoveToElement,
70 LineToElement,
71 CurveToElement,
72 CurveToDataElement
73 };
74
75 class Element {
76 public:
77 qreal x;
78 qreal y;
79 ElementType type;
80
81 bool isMoveTo() const { return type == MoveToElement; }
82 bool isLineTo() const { return type == LineToElement; }
83 bool isCurveTo() const { return type == CurveToElement; }
84
85 operator QPointF () const { return QPointF(x, y); }
86
87 bool operator==(const Element &e) const { return qFuzzyCompare(x, e.x)
88 && qFuzzyCompare(y, e.y) && type == e.type; }
89 inline bool operator!=(const Element &e) const { return !operator==(e); }
90 };
91
92 QPainterPath();
93 explicit QPainterPath(const QPointF &startPoint);
94 QPainterPath(const QPainterPath &other);
95 QPainterPath &operator=(const QPainterPath &other);
96 ~QPainterPath();
97
98 void closeSubpath();
99
100 void moveTo(const QPointF &p);
101 inline void moveTo(qreal x, qreal y);
102
103 void lineTo(const QPointF &p);
104 inline void lineTo(qreal x, qreal y);
105
106 void arcMoveTo(const QRectF &rect, qreal angle);
107 inline void arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle);
108
109 void arcTo(const QRectF &rect, qreal startAngle, qreal arcLength);
110 inline void arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength);
111
112 void cubicTo(const QPointF &ctrlPt1, const QPointF &ctrlPt2, const QPointF &endPt);
113 inline void cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
114 qreal endPtx, qreal endPty);
115 void quadTo(const QPointF &ctrlPt, const QPointF &endPt);
116 inline void quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty);
117
118 QPointF currentPosition() const;
119
120 void addRect(const QRectF &rect);
121 inline void addRect(qreal x, qreal y, qreal w, qreal h);
122 void addEllipse(const QRectF &rect);
123 inline void addEllipse(qreal x, qreal y, qreal w, qreal h);
124 inline void addEllipse(const QPointF &center, qreal rx, qreal ry);
125 void addPolygon(const QPolygonF &polygon);
126 void addText(const QPointF &point, const QFont &f, const QString &text);
127 inline void addText(qreal x, qreal y, const QFont &f, const QString &text);
128 void addPath(const QPainterPath &path);
129 void addRegion(const QRegion &region);
130
131 void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
132 Qt::SizeMode mode = Qt::AbsoluteSize);
133 inline void addRoundedRect(qreal x, qreal y, qreal w, qreal h,
134 qreal xRadius, qreal yRadius,
135 Qt::SizeMode mode = Qt::AbsoluteSize);
136
137 void addRoundRect(const QRectF &rect, int xRnd, int yRnd);
138 inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,
139 int xRnd, int yRnd);
140 inline void addRoundRect(const QRectF &rect, int roundness);
141 inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,
142 int roundness);
143
144 void connectPath(const QPainterPath &path);
145
146 bool contains(const QPointF &pt) const;
147 bool contains(const QRectF &rect) const;
148 bool intersects(const QRectF &rect) const;
149
150 QRectF boundingRect() const;
151 QRectF controlPointRect() const;
152
153 Qt::FillRule fillRule() const;
154 void setFillRule(Qt::FillRule fillRule);
155
156 inline bool isEmpty() const;
157
158 QPainterPath toReversed() const;
159 QList<QPolygonF> toSubpathPolygons(const QMatrix &matrix = QMatrix()) const;
160 QList<QPolygonF> toFillPolygons(const QMatrix &matrix = QMatrix()) const;
161 QPolygonF toFillPolygon(const QMatrix &matrix = QMatrix()) const;
162 QList<QPolygonF> toSubpathPolygons(const QTransform &matrix) const;
163 QList<QPolygonF> toFillPolygons(const QTransform &matrix) const;
164 QPolygonF toFillPolygon(const QTransform &matrix) const;
165
166 inline int elementCount() const;
167 inline const QPainterPath::Element &elementAt(int i) const;
168 inline void setElementPositionAt(int i, qreal x, qreal y);
169
170 qreal length() const;
171 qreal percentAtLength(qreal t) const;
172 QPointF pointAtPercent(qreal t) const;
173 qreal angleAtPercent(qreal t) const;
174 qreal slopeAtPercent(qreal t) const;
175
176 bool intersects(const QPainterPath &p) const;
177 bool contains(const QPainterPath &p) const;
178 QPainterPath united(const QPainterPath &r) const;
179 QPainterPath intersected(const QPainterPath &r) const;
180 QPainterPath subtracted(const QPainterPath &r) const;
181 QPainterPath subtractedInverted(const QPainterPath &r) const;
182
183 QPainterPath simplified() const;
184
185 bool operator==(const QPainterPath &other) const;
186 bool operator!=(const QPainterPath &other) const;
187
188 QPainterPath operator&(const QPainterPath &other) const;
189 QPainterPath operator|(const QPainterPath &other) const;
190 QPainterPath operator+(const QPainterPath &other) const;
191 QPainterPath operator-(const QPainterPath &other) const;
192 QPainterPath &operator&=(const QPainterPath &other);
193 QPainterPath &operator|=(const QPainterPath &other);
194 QPainterPath &operator+=(const QPainterPath &other);
195 QPainterPath &operator-=(const QPainterPath &other);
196
197private:
198 QPainterPathPrivate *d_ptr;
199
200 inline void ensureData() { if (!d_ptr) ensureData_helper(); }
201 void ensureData_helper();
202 inline void detach();
203 void detach_helper();
204 void setDirty(bool);
205 void computeBoundingRect() const;
206 void computeControlPointRect() const;
207
208 QPainterPathData *d_func() const { return reinterpret_cast<QPainterPathData *>(d_ptr); }
209
210 friend class QPainterPathData;
211 friend class QPainterPathStroker;
212 friend class QPainterPathStrokerPrivate;
213 friend class QMatrix;
214 friend class QTransform;
215 friend Q_GUI_EXPORT const QVectorPath &qtVectorPathForPath(const QPainterPath &);
216
217#ifndef QT_NO_DATASTREAM
218 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
219 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
220#endif
221};
222
223class QPainterPathPrivate
224{
225public:
226 friend class QPainterPath;
227 friend class QPainterPathData;
228 friend class QPainterPathStroker;
229 friend class QPainterPathStrokerPrivate;
230 friend class QMatrix;
231 friend class QTransform;
232#ifndef QT_NO_DATASTREAM
233 friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
234 friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
235#endif
236private:
237 QAtomicInt ref;
238 QVector<QPainterPath::Element> elements;
239};
240
241Q_DECLARE_TYPEINFO(QPainterPath::Element, Q_PRIMITIVE_TYPE);
242
243#ifndef QT_NO_DATASTREAM
244Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
245Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
246#endif
247
248class Q_GUI_EXPORT QPainterPathStroker
249{
250 Q_DECLARE_PRIVATE(QPainterPathStroker)
251public:
252 QPainterPathStroker();
253 ~QPainterPathStroker();
254
255 void setWidth(qreal width);
256 qreal width() const;
257
258 void setCapStyle(Qt::PenCapStyle style);
259 Qt::PenCapStyle capStyle() const;
260
261 void setJoinStyle(Qt::PenJoinStyle style);
262 Qt::PenJoinStyle joinStyle() const;
263
264 void setMiterLimit(qreal length);
265 qreal miterLimit() const;
266
267 void setCurveThreshold(qreal threshold);
268 qreal curveThreshold() const;
269
270 void setDashPattern(Qt::PenStyle);
271 void setDashPattern(const QVector<qreal> &dashPattern);
272 QVector<qreal> dashPattern() const;
273
274 void setDashOffset(qreal offset);
275 qreal dashOffset() const;
276
277 QPainterPath createStroke(const QPainterPath &path) const;
278
279private:
280 friend class QX11PaintEngine;
281
282 QPainterPathStrokerPrivate *d_ptr;
283};
284
285inline void QPainterPath::moveTo(qreal x, qreal y)
286{
287 moveTo(QPointF(x, y));
288}
289
290inline void QPainterPath::lineTo(qreal x, qreal y)
291{
292 lineTo(QPointF(x, y));
293}
294
295inline void QPainterPath::arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLenght)
296{
297 arcTo(QRectF(x, y, w, h), startAngle, arcLenght);
298}
299
300inline void QPainterPath::arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle)
301{
302 arcMoveTo(QRectF(x, y, w, h), angle);
303}
304
305inline void QPainterPath::cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
306 qreal endPtx, qreal endPty)
307{
308 cubicTo(QPointF(ctrlPt1x, ctrlPt1y), QPointF(ctrlPt2x, ctrlPt2y),
309 QPointF(endPtx, endPty));
310}
311
312inline void QPainterPath::quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty)
313{
314 quadTo(QPointF(ctrlPtx, ctrlPty), QPointF(endPtx, endPty));
315}
316
317inline void QPainterPath::addEllipse(qreal x, qreal y, qreal w, qreal h)
318{
319 addEllipse(QRectF(x, y, w, h));
320}
321
322inline void QPainterPath::addEllipse(const QPointF &center, qreal rx, qreal ry)
323{
324 addEllipse(QRectF(center.x() - rx, center.y() - ry, 2 * rx, 2 * ry));
325}
326
327inline void QPainterPath::addRect(qreal x, qreal y, qreal w, qreal h)
328{
329 addRect(QRectF(x, y, w, h));
330}
331
332inline void QPainterPath::addRoundedRect(qreal x, qreal y, qreal w, qreal h,
333 qreal xRadius, qreal yRadius,
334 Qt::SizeMode mode)
335{
336 addRoundedRect(QRectF(x, y, w, h), xRadius, yRadius, mode);
337}
338
339inline void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h,
340 int xRnd, int yRnd)
341{
342 addRoundRect(QRectF(x, y, w, h), xRnd, yRnd);
343}
344
345inline void QPainterPath::addRoundRect(const QRectF &rect,
346 int roundness)
347{
348 int xRnd = roundness;
349 int yRnd = roundness;
350 if (rect.width() > rect.height())
351 xRnd = int(roundness * rect.height()/rect.width());
352 else
353 yRnd = int(roundness * rect.width()/rect.height());
354 addRoundRect(rect, xRnd, yRnd);
355}
356
357inline void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h,
358 int roundness)
359{
360 addRoundRect(QRectF(x, y, w, h), roundness);
361}
362
363inline void QPainterPath::addText(qreal x, qreal y, const QFont &f, const QString &text)
364{
365 addText(QPointF(x, y), f, text);
366}
367
368inline bool QPainterPath::isEmpty() const
369{
370 return !d_ptr || (d_ptr->elements.size() == 1 && d_ptr->elements.first().type == MoveToElement);
371}
372
373inline int QPainterPath::elementCount() const
374{
375 return d_ptr ? d_ptr->elements.size() : 0;
376}
377
378inline const QPainterPath::Element &QPainterPath::elementAt(int i) const
379{
380 Q_ASSERT(d_ptr);
381 Q_ASSERT(i >= 0 && i < elementCount());
382 return d_ptr->elements.at(i);
383}
384
385inline void QPainterPath::setElementPositionAt(int i, qreal x, qreal y)
386{
387 Q_ASSERT(d_ptr);
388 Q_ASSERT(i >= 0 && i < elementCount());
389 detach();
390 QPainterPath::Element &e = d_ptr->elements[i];
391 e.x = x;
392 e.y = y;
393}
394
395
396inline void QPainterPath::detach()
397{
398 if (d_ptr->ref != 1)
399 detach_helper();
400 setDirty(true);
401}
402
403#ifndef QT_NO_DEBUG_STREAM
404Q_GUI_EXPORT QDebug operator<<(QDebug, const QPainterPath &);
405#endif
406
407QT_END_NAMESPACE
408
409QT_END_HEADER
410
411#endif // QPAINTERPATH_H
Note: See TracBrowser for help on using the repository browser.