qpainterpath.h

Go to the documentation of this file.
00001 /****************************************************************************
00002 **
00003 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
00004 ** All rights reserved.
00005 ** Contact: Nokia Corporation (qt-info@nokia.com)
00006 **
00007 ** This file is part of the QtGui module of the Qt Toolkit.
00008 **
00009 ** $QT_BEGIN_LICENSE:LGPL$
00010 ** Commercial Usage
00011 ** Licensees holding valid Qt Commercial licenses may use this file in
00012 ** accordance with the Qt Commercial License Agreement provided with the
00013 ** Software or, alternatively, in accordance with the terms contained in
00014 ** a written agreement between you and Nokia.
00015 **
00016 ** GNU Lesser General Public License Usage
00017 ** Alternatively, this file may be used under the terms of the GNU Lesser
00018 ** General Public License version 2.1 as published by the Free Software
00019 ** Foundation and appearing in the file LICENSE.LGPL included in the
00020 ** packaging of this file.  Please review the following information to
00021 ** ensure the GNU Lesser General Public License version 2.1 requirements
00022 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
00023 **
00024 ** In addition, as a special exception, Nokia gives you certain additional
00025 ** rights.  These rights are described in the Nokia Qt LGPL Exception
00026 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this module.
00027 **
00028 ** GNU General Public License Usage
00029 ** Alternatively, this file may be used under the terms of the GNU
00030 ** General Public License version 3.0 as published by the Free Software
00031 ** Foundation and appearing in the file LICENSE.GPL included in the
00032 ** packaging of this file.  Please review the following information to
00033 ** ensure the GNU General Public License version 3.0 requirements will be
00034 ** met: http://www.gnu.org/copyleft/gpl.html.
00035 **
00036 ** If you have questions regarding the use of this file, please contact
00037 ** Nokia at qt-info@nokia.com.
00038 ** $QT_END_LICENSE$
00039 **
00040 ****************************************************************************/
00041 
00042 #ifndef QPAINTERPATH_H
00043 #define QPAINTERPATH_H
00044 
00045 #include <QtGui/qmatrix.h>
00046 #include <QtCore/qglobal.h>
00047 #include <QtCore/qrect.h>
00048 #include <QtCore/qline.h>
00049 #include <QtCore/qvector.h>
00050 #include <QtCore/qscopedpointer.h>
00051 
00052 QT_BEGIN_HEADER
00053 
00054 QT_BEGIN_NAMESPACE
00055 
00056 QT_MODULE(Gui)
00057 
00058 class QFont;
00059 class QPainterPathPrivate;
00060 struct QPainterPathPrivateDeleter;
00061 class QPainterPathData;
00062 class QPainterPathStrokerPrivate;
00063 class QPolygonF;
00064 class QRegion;
00065 class QVectorPath;
00066 
00067 class Q_GUI_EXPORT QPainterPath
00068 {
00069 public:
00070     enum ElementType {
00071         MoveToElement,
00072         LineToElement,
00073         CurveToElement,
00074         CurveToDataElement
00075     };
00076 
00077     class Element {
00078     public:
00079         qreal x;
00080         qreal y;
00081         ElementType type;
00082 
00083         bool isMoveTo() const { return type == MoveToElement; }
00084         bool isLineTo() const { return type == LineToElement; }
00085         bool isCurveTo() const { return type == CurveToElement; }
00086 
00087         operator QPointF () const { return QPointF(x, y); }
00088 
00089         bool operator==(const Element &e) const { return qFuzzyCompare(x, e.x)
00090             && qFuzzyCompare(y, e.y) && type == e.type; }
00091         inline bool operator!=(const Element &e) const { return !operator==(e); }
00092     };
00093 
00094     QPainterPath();
00095     explicit QPainterPath(const QPointF &startPoint);
00096     QPainterPath(const QPainterPath &other);
00097     QPainterPath &operator=(const QPainterPath &other);
00098     ~QPainterPath();
00099 
00100     void closeSubpath();
00101 
00102     void moveTo(const QPointF &p);
00103     inline void moveTo(qreal x, qreal y);
00104 
00105     void lineTo(const QPointF &p);
00106     inline void lineTo(qreal x, qreal y);
00107 
00108     void arcMoveTo(const QRectF &rect, qreal angle);
00109     inline void arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle);
00110 
00111     void arcTo(const QRectF &rect, qreal startAngle, qreal arcLength);
00112     inline void arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength);
00113 
00114     void cubicTo(const QPointF &ctrlPt1, const QPointF &ctrlPt2, const QPointF &endPt);
00115     inline void cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
00116                         qreal endPtx, qreal endPty);
00117     void quadTo(const QPointF &ctrlPt, const QPointF &endPt);
00118     inline void quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty);
00119 
00120     QPointF currentPosition() const;
00121 
00122     void addRect(const QRectF &rect);
00123     inline void addRect(qreal x, qreal y, qreal w, qreal h);
00124     void addEllipse(const QRectF &rect);
00125     inline void addEllipse(qreal x, qreal y, qreal w, qreal h);
00126     inline void addEllipse(const QPointF &center, qreal rx, qreal ry);
00127     void addPolygon(const QPolygonF &polygon);
00128     void addText(const QPointF &point, const QFont &f, const QString &text);
00129     inline void addText(qreal x, qreal y, const QFont &f, const QString &text);
00130     void addPath(const QPainterPath &path);
00131     void addRegion(const QRegion &region);
00132 
00133     void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
00134                         Qt::SizeMode mode = Qt::AbsoluteSize);
00135     inline void addRoundedRect(qreal x, qreal y, qreal w, qreal h,
00136                                qreal xRadius, qreal yRadius,
00137                                Qt::SizeMode mode = Qt::AbsoluteSize);
00138 
00139     void addRoundRect(const QRectF &rect, int xRnd, int yRnd);
00140     inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,
00141                              int xRnd, int yRnd);
00142     inline void addRoundRect(const QRectF &rect, int roundness);
00143     inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,
00144                              int roundness);
00145 
00146     void connectPath(const QPainterPath &path);
00147 
00148     bool contains(const QPointF &pt) const;
00149     bool contains(const QRectF &rect) const;
00150     bool intersects(const QRectF &rect) const;
00151 
00152     void translate(qreal dx, qreal dy);
00153     inline void translate(const QPointF &offset);
00154 
00155     QPainterPath translated(qreal dx, qreal dy) const;
00156     inline QPainterPath translated(const QPointF &offset) const;
00157 
00158     QRectF boundingRect() const;
00159     QRectF controlPointRect() const;
00160 
00161     Qt::FillRule fillRule() const;
00162     void setFillRule(Qt::FillRule fillRule);
00163 
00164     inline bool isEmpty() const;
00165 
00166     QPainterPath toReversed() const;
00167     QList<QPolygonF> toSubpathPolygons(const QMatrix &matrix = QMatrix()) const;
00168     QList<QPolygonF> toFillPolygons(const QMatrix &matrix = QMatrix()) const;
00169     QPolygonF toFillPolygon(const QMatrix &matrix = QMatrix()) const;
00170     QList<QPolygonF> toSubpathPolygons(const QTransform &matrix) const;
00171     QList<QPolygonF> toFillPolygons(const QTransform &matrix) const;
00172     QPolygonF toFillPolygon(const QTransform &matrix) const;
00173 
00174     inline int elementCount() const;
00175     inline const QPainterPath::Element &elementAt(int i) const;
00176     inline void setElementPositionAt(int i, qreal x, qreal y);
00177 
00178     qreal   length() const;
00179     qreal   percentAtLength(qreal t) const;
00180     QPointF pointAtPercent(qreal t) const;
00181     qreal   angleAtPercent(qreal t) const;
00182     qreal   slopeAtPercent(qreal t) const;
00183 
00184     bool intersects(const QPainterPath &p) const;
00185     bool contains(const QPainterPath &p) const;
00186     QPainterPath united(const QPainterPath &r) const;
00187     QPainterPath intersected(const QPainterPath &r) const;
00188     QPainterPath subtracted(const QPainterPath &r) const;
00189     QPainterPath subtractedInverted(const QPainterPath &r) const;
00190 
00191     QPainterPath simplified() const;
00192 
00193     bool operator==(const QPainterPath &other) const;
00194     bool operator!=(const QPainterPath &other) const;
00195 
00196     QPainterPath operator&(const QPainterPath &other) const;
00197     QPainterPath operator|(const QPainterPath &other) const;
00198     QPainterPath operator+(const QPainterPath &other) const;
00199     QPainterPath operator-(const QPainterPath &other) const;
00200     QPainterPath &operator&=(const QPainterPath &other);
00201     QPainterPath &operator|=(const QPainterPath &other);
00202     QPainterPath &operator+=(const QPainterPath &other);
00203     QPainterPath &operator-=(const QPainterPath &other);
00204 
00205 private:
00206     QScopedPointer<QPainterPathPrivate, QPainterPathPrivateDeleter> d_ptr;
00207 
00208     inline void ensureData() { if (!d_ptr) ensureData_helper(); }
00209     void ensureData_helper();
00210     inline void detach();
00211     void detach_helper();
00212     void setDirty(bool);
00213     void computeBoundingRect() const;
00214     void computeControlPointRect() const;
00215 
00216     QPainterPathData *d_func() const { return reinterpret_cast<QPainterPathData *>(d_ptr.data()); }
00217 
00218     friend class QPainterPathData;
00219     friend class QPainterPathStroker;
00220     friend class QPainterPathStrokerPrivate;
00221     friend class QMatrix;
00222     friend class QTransform;
00223     friend class QVectorPath;
00224     friend Q_GUI_EXPORT const QVectorPath &qtVectorPathForPath(const QPainterPath &);
00225 
00226 #ifndef QT_NO_DATASTREAM
00227     friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
00228     friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
00229 #endif
00230 };
00231 
00232 class QPainterPathPrivate
00233 {
00234 public:
00235     friend class QPainterPath;
00236     friend class QPainterPathData;
00237     friend class QPainterPathStroker;
00238     friend class QPainterPathStrokerPrivate;
00239     friend class QMatrix;
00240     friend class QTransform;
00241     friend class QVectorPath;
00242     friend struct QPainterPathPrivateDeleter;
00243 #ifndef QT_NO_DATASTREAM
00244     friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
00245     friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
00246 #endif
00247 private:
00248     QAtomicInt ref;
00249     QVector<QPainterPath::Element> elements;
00250 };
00251 
00252 Q_DECLARE_TYPEINFO(QPainterPath::Element, Q_PRIMITIVE_TYPE);
00253 
00254 #ifndef QT_NO_DATASTREAM
00255 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
00256 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
00257 #endif
00258 
00259 class Q_GUI_EXPORT QPainterPathStroker
00260 {
00261     Q_DECLARE_PRIVATE(QPainterPathStroker)
00262 public:
00263     QPainterPathStroker();
00264     ~QPainterPathStroker();
00265 
00266     void setWidth(qreal width);
00267     qreal width() const;
00268 
00269     void setCapStyle(Qt::PenCapStyle style);
00270     Qt::PenCapStyle capStyle() const;
00271 
00272     void setJoinStyle(Qt::PenJoinStyle style);
00273     Qt::PenJoinStyle joinStyle() const;
00274 
00275     void setMiterLimit(qreal length);
00276     qreal miterLimit() const;
00277 
00278     void setCurveThreshold(qreal threshold);
00279     qreal curveThreshold() const;
00280 
00281     void setDashPattern(Qt::PenStyle);
00282     void setDashPattern(const QVector<qreal> &dashPattern);
00283     QVector<qreal> dashPattern() const;
00284 
00285     void setDashOffset(qreal offset);
00286     qreal dashOffset() const;
00287 
00288     QPainterPath createStroke(const QPainterPath &path) const;
00289 
00290 private:
00291     Q_DISABLE_COPY(QPainterPathStroker)
00292 
00293     friend class QX11PaintEngine;
00294 
00295     QScopedPointer<QPainterPathStrokerPrivate> d_ptr;
00296 };
00297 
00298 inline void QPainterPath::moveTo(qreal x, qreal y)
00299 {
00300     moveTo(QPointF(x, y));
00301 }
00302 
00303 inline void QPainterPath::lineTo(qreal x, qreal y)
00304 {
00305     lineTo(QPointF(x, y));
00306 }
00307 
00308 inline void QPainterPath::arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength)
00309 {
00310     arcTo(QRectF(x, y, w, h), startAngle, arcLength);
00311 }
00312 
00313 inline void QPainterPath::arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle)
00314 {
00315     arcMoveTo(QRectF(x, y, w, h), angle);
00316 }
00317 
00318 inline void QPainterPath::cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
00319                                    qreal endPtx, qreal endPty)
00320 {
00321     cubicTo(QPointF(ctrlPt1x, ctrlPt1y), QPointF(ctrlPt2x, ctrlPt2y),
00322             QPointF(endPtx, endPty));
00323 }
00324 
00325 inline void QPainterPath::quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty)
00326 {
00327     quadTo(QPointF(ctrlPtx, ctrlPty), QPointF(endPtx, endPty));
00328 }
00329 
00330 inline void QPainterPath::addEllipse(qreal x, qreal y, qreal w, qreal h)
00331 {
00332     addEllipse(QRectF(x, y, w, h));
00333 }
00334 
00335 inline void QPainterPath::addEllipse(const QPointF &center, qreal rx, qreal ry)
00336 {
00337     addEllipse(QRectF(center.x() - rx, center.y() - ry, 2 * rx, 2 * ry));
00338 }
00339 
00340 inline void QPainterPath::addRect(qreal x, qreal y, qreal w, qreal h)
00341 {
00342     addRect(QRectF(x, y, w, h));
00343 }
00344 
00345 inline void QPainterPath::addRoundedRect(qreal x, qreal y, qreal w, qreal h,
00346                                          qreal xRadius, qreal yRadius,
00347                                          Qt::SizeMode mode)
00348 {
00349     addRoundedRect(QRectF(x, y, w, h), xRadius, yRadius, mode);
00350 }
00351 
00352 inline void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h,
00353                                        int xRnd, int yRnd)
00354 {
00355     addRoundRect(QRectF(x, y, w, h), xRnd, yRnd);
00356 }
00357 
00358 inline void QPainterPath::addRoundRect(const QRectF &rect,
00359                                        int roundness)
00360 {
00361     int xRnd = roundness;
00362     int yRnd = roundness;
00363     if (rect.width() > rect.height())
00364         xRnd = int(roundness * rect.height()/rect.width());
00365     else
00366         yRnd = int(roundness * rect.width()/rect.height());
00367     addRoundRect(rect, xRnd, yRnd);
00368 }
00369 
00370 inline void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h,
00371                                        int roundness)
00372 {
00373     addRoundRect(QRectF(x, y, w, h), roundness);
00374 }
00375 
00376 inline void QPainterPath::addText(qreal x, qreal y, const QFont &f, const QString &text)
00377 {
00378     addText(QPointF(x, y), f, text);
00379 }
00380 
00381 inline void QPainterPath::translate(const QPointF &offset)
00382 { translate(offset.x(), offset.y()); }
00383 
00384 inline QPainterPath QPainterPath::translated(const QPointF &offset) const
00385 { return translated(offset.x(), offset.y()); }
00386 
00387 inline bool QPainterPath::isEmpty() const
00388 {
00389     return !d_ptr || (d_ptr->elements.size() == 1 && d_ptr->elements.first().type == MoveToElement);
00390 }
00391 
00392 inline int QPainterPath::elementCount() const
00393 {
00394     return d_ptr ? d_ptr->elements.size() : 0;
00395 }
00396 
00397 inline const QPainterPath::Element &QPainterPath::elementAt(int i) const
00398 {
00399     Q_ASSERT(d_ptr);
00400     Q_ASSERT(i >= 0 && i < elementCount());
00401     return d_ptr->elements.at(i);
00402 }
00403 
00404 inline void QPainterPath::setElementPositionAt(int i, qreal x, qreal y)
00405 {
00406     Q_ASSERT(d_ptr);
00407     Q_ASSERT(i >= 0 && i < elementCount());
00408     detach();
00409     QPainterPath::Element &e = d_ptr->elements[i];
00410     e.x = x;
00411     e.y = y;
00412 }
00413 
00414 
00415 inline void QPainterPath::detach()
00416 {
00417     if (d_ptr->ref != 1)
00418         detach_helper();
00419     setDirty(true);
00420 }
00421 
00422 #ifndef QT_NO_DEBUG_STREAM
00423 Q_GUI_EXPORT QDebug operator<<(QDebug, const QPainterPath &);
00424 #endif
00425 
00426 QT_END_NAMESPACE
00427 
00428 QT_END_HEADER
00429 
00430 #endif // QPAINTERPATH_H