src/gui/painting/qpainterpath.h
changeset 0 1918ee327afb
child 4 3b1da2848fc7
equal deleted inserted replaced
-1:000000000000 0:1918ee327afb
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    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     ~QPainterPath();
       
    99 
       
   100     void closeSubpath();
       
   101 
       
   102     void moveTo(const QPointF &p);
       
   103     inline void moveTo(qreal x, qreal y);
       
   104 
       
   105     void lineTo(const QPointF &p);
       
   106     inline void lineTo(qreal x, qreal y);
       
   107 
       
   108     void arcMoveTo(const QRectF &rect, qreal angle);
       
   109     inline void arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle);
       
   110 
       
   111     void arcTo(const QRectF &rect, qreal startAngle, qreal arcLength);
       
   112     inline void arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength);
       
   113 
       
   114     void cubicTo(const QPointF &ctrlPt1, const QPointF &ctrlPt2, const QPointF &endPt);
       
   115     inline void cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
       
   116                         qreal endPtx, qreal endPty);
       
   117     void quadTo(const QPointF &ctrlPt, const QPointF &endPt);
       
   118     inline void quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty);
       
   119 
       
   120     QPointF currentPosition() const;
       
   121 
       
   122     void addRect(const QRectF &rect);
       
   123     inline void addRect(qreal x, qreal y, qreal w, qreal h);
       
   124     void addEllipse(const QRectF &rect);
       
   125     inline void addEllipse(qreal x, qreal y, qreal w, qreal h);
       
   126     inline void addEllipse(const QPointF &center, qreal rx, qreal ry);
       
   127     void addPolygon(const QPolygonF &polygon);
       
   128     void addText(const QPointF &point, const QFont &f, const QString &text);
       
   129     inline void addText(qreal x, qreal y, const QFont &f, const QString &text);
       
   130     void addPath(const QPainterPath &path);
       
   131     void addRegion(const QRegion &region);
       
   132 
       
   133     void addRoundedRect(const QRectF &rect, qreal xRadius, qreal yRadius,
       
   134                         Qt::SizeMode mode = Qt::AbsoluteSize);
       
   135     inline void addRoundedRect(qreal x, qreal y, qreal w, qreal h,
       
   136                                qreal xRadius, qreal yRadius,
       
   137                                Qt::SizeMode mode = Qt::AbsoluteSize);
       
   138 
       
   139     void addRoundRect(const QRectF &rect, int xRnd, int yRnd);
       
   140     inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,
       
   141                              int xRnd, int yRnd);
       
   142     inline void addRoundRect(const QRectF &rect, int roundness);
       
   143     inline void addRoundRect(qreal x, qreal y, qreal w, qreal h,
       
   144                              int roundness);
       
   145 
       
   146     void connectPath(const QPainterPath &path);
       
   147 
       
   148     bool contains(const QPointF &pt) const;
       
   149     bool contains(const QRectF &rect) const;
       
   150     bool intersects(const QRectF &rect) const;
       
   151 
       
   152     void translate(qreal dx, qreal dy);
       
   153     inline void translate(const QPointF &offset);
       
   154 
       
   155     QPainterPath translated(qreal dx, qreal dy) const;
       
   156     inline QPainterPath translated(const QPointF &offset) const;
       
   157 
       
   158     QRectF boundingRect() const;
       
   159     QRectF controlPointRect() const;
       
   160 
       
   161     Qt::FillRule fillRule() const;
       
   162     void setFillRule(Qt::FillRule fillRule);
       
   163 
       
   164     inline bool isEmpty() const;
       
   165 
       
   166     QPainterPath toReversed() const;
       
   167     QList<QPolygonF> toSubpathPolygons(const QMatrix &matrix = QMatrix()) const;
       
   168     QList<QPolygonF> toFillPolygons(const QMatrix &matrix = QMatrix()) const;
       
   169     QPolygonF toFillPolygon(const QMatrix &matrix = QMatrix()) const;
       
   170     QList<QPolygonF> toSubpathPolygons(const QTransform &matrix) const;
       
   171     QList<QPolygonF> toFillPolygons(const QTransform &matrix) const;
       
   172     QPolygonF toFillPolygon(const QTransform &matrix) const;
       
   173 
       
   174     inline int elementCount() const;
       
   175     inline const QPainterPath::Element &elementAt(int i) const;
       
   176     inline void setElementPositionAt(int i, qreal x, qreal y);
       
   177 
       
   178     qreal   length() const;
       
   179     qreal   percentAtLength(qreal t) const;
       
   180     QPointF pointAtPercent(qreal t) const;
       
   181     qreal   angleAtPercent(qreal t) const;
       
   182     qreal   slopeAtPercent(qreal t) const;
       
   183 
       
   184     bool intersects(const QPainterPath &p) const;
       
   185     bool contains(const QPainterPath &p) const;
       
   186     QPainterPath united(const QPainterPath &r) const;
       
   187     QPainterPath intersected(const QPainterPath &r) const;
       
   188     QPainterPath subtracted(const QPainterPath &r) const;
       
   189     QPainterPath subtractedInverted(const QPainterPath &r) const;
       
   190 
       
   191     QPainterPath simplified() const;
       
   192 
       
   193     bool operator==(const QPainterPath &other) const;
       
   194     bool operator!=(const QPainterPath &other) const;
       
   195 
       
   196     QPainterPath operator&(const QPainterPath &other) const;
       
   197     QPainterPath operator|(const QPainterPath &other) const;
       
   198     QPainterPath operator+(const QPainterPath &other) const;
       
   199     QPainterPath operator-(const QPainterPath &other) const;
       
   200     QPainterPath &operator&=(const QPainterPath &other);
       
   201     QPainterPath &operator|=(const QPainterPath &other);
       
   202     QPainterPath &operator+=(const QPainterPath &other);
       
   203     QPainterPath &operator-=(const QPainterPath &other);
       
   204 
       
   205 private:
       
   206     QScopedPointer<QPainterPathPrivate, QPainterPathPrivateDeleter> d_ptr;
       
   207 
       
   208     inline void ensureData() { if (!d_ptr) ensureData_helper(); }
       
   209     void ensureData_helper();
       
   210     inline void detach();
       
   211     void detach_helper();
       
   212     void setDirty(bool);
       
   213     void computeBoundingRect() const;
       
   214     void computeControlPointRect() const;
       
   215 
       
   216     QPainterPathData *d_func() const { return reinterpret_cast<QPainterPathData *>(d_ptr.data()); }
       
   217 
       
   218     friend class QPainterPathData;
       
   219     friend class QPainterPathStroker;
       
   220     friend class QPainterPathStrokerPrivate;
       
   221     friend class QMatrix;
       
   222     friend class QTransform;
       
   223     friend class QVectorPath;
       
   224     friend Q_GUI_EXPORT const QVectorPath &qtVectorPathForPath(const QPainterPath &);
       
   225 
       
   226 #ifndef QT_NO_DATASTREAM
       
   227     friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
       
   228     friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
       
   229 #endif
       
   230 };
       
   231 
       
   232 class QPainterPathPrivate
       
   233 {
       
   234 public:
       
   235     friend class QPainterPath;
       
   236     friend class QPainterPathData;
       
   237     friend class QPainterPathStroker;
       
   238     friend class QPainterPathStrokerPrivate;
       
   239     friend class QMatrix;
       
   240     friend class QTransform;
       
   241     friend class QVectorPath;
       
   242     friend struct QPainterPathPrivateDeleter;
       
   243 #ifndef QT_NO_DATASTREAM
       
   244     friend Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
       
   245     friend Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
       
   246 #endif
       
   247 private:
       
   248     QAtomicInt ref;
       
   249     QVector<QPainterPath::Element> elements;
       
   250 };
       
   251 
       
   252 Q_DECLARE_TYPEINFO(QPainterPath::Element, Q_PRIMITIVE_TYPE);
       
   253 
       
   254 #ifndef QT_NO_DATASTREAM
       
   255 Q_GUI_EXPORT QDataStream &operator<<(QDataStream &, const QPainterPath &);
       
   256 Q_GUI_EXPORT QDataStream &operator>>(QDataStream &, QPainterPath &);
       
   257 #endif
       
   258 
       
   259 class Q_GUI_EXPORT QPainterPathStroker
       
   260 {
       
   261     Q_DECLARE_PRIVATE(QPainterPathStroker)
       
   262 public:
       
   263     QPainterPathStroker();
       
   264     ~QPainterPathStroker();
       
   265 
       
   266     void setWidth(qreal width);
       
   267     qreal width() const;
       
   268 
       
   269     void setCapStyle(Qt::PenCapStyle style);
       
   270     Qt::PenCapStyle capStyle() const;
       
   271 
       
   272     void setJoinStyle(Qt::PenJoinStyle style);
       
   273     Qt::PenJoinStyle joinStyle() const;
       
   274 
       
   275     void setMiterLimit(qreal length);
       
   276     qreal miterLimit() const;
       
   277 
       
   278     void setCurveThreshold(qreal threshold);
       
   279     qreal curveThreshold() const;
       
   280 
       
   281     void setDashPattern(Qt::PenStyle);
       
   282     void setDashPattern(const QVector<qreal> &dashPattern);
       
   283     QVector<qreal> dashPattern() const;
       
   284 
       
   285     void setDashOffset(qreal offset);
       
   286     qreal dashOffset() const;
       
   287 
       
   288     QPainterPath createStroke(const QPainterPath &path) const;
       
   289 
       
   290 private:
       
   291     friend class QX11PaintEngine;
       
   292 
       
   293     QScopedPointer<QPainterPathStrokerPrivate> d_ptr;
       
   294 };
       
   295 
       
   296 inline void QPainterPath::moveTo(qreal x, qreal y)
       
   297 {
       
   298     moveTo(QPointF(x, y));
       
   299 }
       
   300 
       
   301 inline void QPainterPath::lineTo(qreal x, qreal y)
       
   302 {
       
   303     lineTo(QPointF(x, y));
       
   304 }
       
   305 
       
   306 inline void QPainterPath::arcTo(qreal x, qreal y, qreal w, qreal h, qreal startAngle, qreal arcLength)
       
   307 {
       
   308     arcTo(QRectF(x, y, w, h), startAngle, arcLength);
       
   309 }
       
   310 
       
   311 inline void QPainterPath::arcMoveTo(qreal x, qreal y, qreal w, qreal h, qreal angle)
       
   312 {
       
   313     arcMoveTo(QRectF(x, y, w, h), angle);
       
   314 }
       
   315 
       
   316 inline void QPainterPath::cubicTo(qreal ctrlPt1x, qreal ctrlPt1y, qreal ctrlPt2x, qreal ctrlPt2y,
       
   317                                    qreal endPtx, qreal endPty)
       
   318 {
       
   319     cubicTo(QPointF(ctrlPt1x, ctrlPt1y), QPointF(ctrlPt2x, ctrlPt2y),
       
   320             QPointF(endPtx, endPty));
       
   321 }
       
   322 
       
   323 inline void QPainterPath::quadTo(qreal ctrlPtx, qreal ctrlPty, qreal endPtx, qreal endPty)
       
   324 {
       
   325     quadTo(QPointF(ctrlPtx, ctrlPty), QPointF(endPtx, endPty));
       
   326 }
       
   327 
       
   328 inline void QPainterPath::addEllipse(qreal x, qreal y, qreal w, qreal h)
       
   329 {
       
   330     addEllipse(QRectF(x, y, w, h));
       
   331 }
       
   332 
       
   333 inline void QPainterPath::addEllipse(const QPointF &center, qreal rx, qreal ry)
       
   334 {
       
   335     addEllipse(QRectF(center.x() - rx, center.y() - ry, 2 * rx, 2 * ry));
       
   336 }
       
   337 
       
   338 inline void QPainterPath::addRect(qreal x, qreal y, qreal w, qreal h)
       
   339 {
       
   340     addRect(QRectF(x, y, w, h));
       
   341 }
       
   342 
       
   343 inline void QPainterPath::addRoundedRect(qreal x, qreal y, qreal w, qreal h,
       
   344                                          qreal xRadius, qreal yRadius,
       
   345                                          Qt::SizeMode mode)
       
   346 {
       
   347     addRoundedRect(QRectF(x, y, w, h), xRadius, yRadius, mode);
       
   348 }
       
   349 
       
   350 inline void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h,
       
   351                                        int xRnd, int yRnd)
       
   352 {
       
   353     addRoundRect(QRectF(x, y, w, h), xRnd, yRnd);
       
   354 }
       
   355 
       
   356 inline void QPainterPath::addRoundRect(const QRectF &rect,
       
   357                                        int roundness)
       
   358 {
       
   359     int xRnd = roundness;
       
   360     int yRnd = roundness;
       
   361     if (rect.width() > rect.height())
       
   362         xRnd = int(roundness * rect.height()/rect.width());
       
   363     else
       
   364         yRnd = int(roundness * rect.width()/rect.height());
       
   365     addRoundRect(rect, xRnd, yRnd);
       
   366 }
       
   367 
       
   368 inline void QPainterPath::addRoundRect(qreal x, qreal y, qreal w, qreal h,
       
   369                                        int roundness)
       
   370 {
       
   371     addRoundRect(QRectF(x, y, w, h), roundness);
       
   372 }
       
   373 
       
   374 inline void QPainterPath::addText(qreal x, qreal y, const QFont &f, const QString &text)
       
   375 {
       
   376     addText(QPointF(x, y), f, text);
       
   377 }
       
   378 
       
   379 inline void QPainterPath::translate(const QPointF &offset)
       
   380 { translate(offset.x(), offset.y()); }
       
   381 
       
   382 inline QPainterPath QPainterPath::translated(const QPointF &offset) const
       
   383 { return translated(offset.x(), offset.y()); }
       
   384 
       
   385 inline bool QPainterPath::isEmpty() const
       
   386 {
       
   387     return !d_ptr || (d_ptr->elements.size() == 1 && d_ptr->elements.first().type == MoveToElement);
       
   388 }
       
   389 
       
   390 inline int QPainterPath::elementCount() const
       
   391 {
       
   392     return d_ptr ? d_ptr->elements.size() : 0;
       
   393 }
       
   394 
       
   395 inline const QPainterPath::Element &QPainterPath::elementAt(int i) const
       
   396 {
       
   397     Q_ASSERT(d_ptr);
       
   398     Q_ASSERT(i >= 0 && i < elementCount());
       
   399     return d_ptr->elements.at(i);
       
   400 }
       
   401 
       
   402 inline void QPainterPath::setElementPositionAt(int i, qreal x, qreal y)
       
   403 {
       
   404     Q_ASSERT(d_ptr);
       
   405     Q_ASSERT(i >= 0 && i < elementCount());
       
   406     detach();
       
   407     QPainterPath::Element &e = d_ptr->elements[i];
       
   408     e.x = x;
       
   409     e.y = y;
       
   410 }
       
   411 
       
   412 
       
   413 inline void QPainterPath::detach()
       
   414 {
       
   415     if (d_ptr->ref != 1)
       
   416         detach_helper();
       
   417     setDirty(true);
       
   418 }
       
   419 
       
   420 #ifndef QT_NO_DEBUG_STREAM
       
   421 Q_GUI_EXPORT QDebug operator<<(QDebug, const QPainterPath &);
       
   422 #endif
       
   423 
       
   424 QT_END_NAMESPACE
       
   425 
       
   426 QT_END_HEADER
       
   427 
       
   428 #endif // QPAINTERPATH_H