src/gui/painting/qpainterpath_p.h
changeset 0 1918ee327afb
child 3 41300fa6a67c
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_P_H
       
    43 #define QPAINTERPATH_P_H
       
    44 
       
    45 //
       
    46 //  W A R N I N G
       
    47 //  -------------
       
    48 //
       
    49 // This file is not part of the Qt API.  It exists for the convenience
       
    50 // of other Qt classes.  This header file may change from version to
       
    51 // version without notice, or even be removed.
       
    52 //
       
    53 // We mean it.
       
    54 //
       
    55 
       
    56 #include "QtGui/qpainterpath.h"
       
    57 #include "QtGui/qregion.h"
       
    58 #include "QtCore/qlist.h"
       
    59 #include "QtCore/qvarlengtharray.h"
       
    60 
       
    61 #include <qdebug.h>
       
    62 
       
    63 #include <private/qvectorpath_p.h>
       
    64 #include <private/qstroker_p.h>
       
    65 
       
    66 QT_BEGIN_NAMESPACE
       
    67 
       
    68 class QPainterPathStrokerPrivate
       
    69 {
       
    70 public:
       
    71     QPainterPathStrokerPrivate();
       
    72 
       
    73     QStroker stroker;
       
    74     QVector<qfixed> dashPattern;
       
    75     qreal dashOffset;
       
    76 };
       
    77 
       
    78 class QPolygonF;
       
    79 class QVectorPathConverter;
       
    80 
       
    81 class QVectorPathConverter
       
    82 {
       
    83 public:
       
    84     QVectorPathConverter(const QVector<QPainterPath::Element> &path, uint fillRule)
       
    85         : pathData(path, fillRule),
       
    86           path(pathData.points.data(), path.size(),
       
    87                pathData.elements.data(), pathData.flags) {}
       
    88 
       
    89     const QVectorPath &vectorPath() {
       
    90         return path;
       
    91     }
       
    92 
       
    93     struct QVectorPathData {
       
    94         QVectorPathData(const QVector<QPainterPath::Element> &path, uint fillRule)
       
    95             : elements(path.size()),
       
    96               points(path.size() * 2),
       
    97               flags(0)
       
    98         {
       
    99             int ptsPos = 0;
       
   100             for (int i=0; i<path.size(); ++i) {
       
   101                 const QPainterPath::Element &e = path.at(i);
       
   102                 elements[i] = e.type;
       
   103                 points[ptsPos++] = e.x;
       
   104                 points[ptsPos++] = e.y;
       
   105                 if (e.type == QPainterPath::CurveToElement)
       
   106                     flags |= QVectorPath::CurvedShapeHint;
       
   107             }
       
   108 
       
   109             if (fillRule == Qt::WindingFill)
       
   110                 flags |= QVectorPath::WindingFill;
       
   111             else
       
   112                 flags |= QVectorPath::OddEvenFill;
       
   113 
       
   114         }
       
   115         QVarLengthArray<QPainterPath::ElementType> elements;
       
   116         QVarLengthArray<qreal> points;
       
   117         uint flags;
       
   118     };
       
   119 
       
   120     QVectorPathData pathData;
       
   121     QVectorPath path;
       
   122 
       
   123 private:
       
   124     Q_DISABLE_COPY(QVectorPathConverter)
       
   125 };
       
   126 
       
   127 class QPainterPathData : public QPainterPathPrivate
       
   128 {
       
   129 public:
       
   130     QPainterPathData() :
       
   131         cStart(0), fillRule(Qt::OddEvenFill),
       
   132         dirtyBounds(false), dirtyControlBounds(false),
       
   133         pathConverter(0)
       
   134     {
       
   135         ref = 1;
       
   136         require_moveTo = false;
       
   137     }
       
   138 
       
   139     QPainterPathData(const QPainterPathData &other) :
       
   140         QPainterPathPrivate(), cStart(other.cStart), fillRule(other.fillRule),
       
   141         dirtyBounds(other.dirtyBounds), bounds(other.bounds),
       
   142         dirtyControlBounds(other.dirtyControlBounds),
       
   143         controlBounds(other.controlBounds),
       
   144         pathConverter(0)
       
   145     {
       
   146         ref = 1;
       
   147         require_moveTo = false;
       
   148         elements = other.elements;
       
   149     }
       
   150 
       
   151     ~QPainterPathData() {
       
   152         delete pathConverter;
       
   153     }
       
   154 
       
   155     inline bool isClosed() const;
       
   156     inline void close();
       
   157     inline void maybeMoveTo();
       
   158 
       
   159     const QVectorPath &vectorPath() {
       
   160         if (!pathConverter)
       
   161             pathConverter = new QVectorPathConverter(elements, fillRule);
       
   162         return pathConverter->path;
       
   163     }
       
   164 
       
   165     int cStart;
       
   166     Qt::FillRule fillRule;
       
   167 
       
   168     bool require_moveTo;
       
   169 
       
   170     bool   dirtyBounds;
       
   171     QRectF bounds;
       
   172     bool   dirtyControlBounds;
       
   173     QRectF controlBounds;
       
   174 
       
   175     QVectorPathConverter *pathConverter;
       
   176 };
       
   177 
       
   178 
       
   179 inline const QPainterPath QVectorPath::convertToPainterPath() const
       
   180 {
       
   181         QPainterPath path;
       
   182         path.ensureData();
       
   183         QPainterPathData *data = path.d_func();
       
   184         data->elements.reserve(m_count);
       
   185         int index = 0;
       
   186         data->elements[0].x = m_points[index++];
       
   187         data->elements[0].y = m_points[index++];
       
   188 
       
   189         if (m_elements) {
       
   190             data->elements[0].type = m_elements[0];
       
   191             for (int i=1; i<m_count; ++i) {
       
   192                 QPainterPath::Element element;
       
   193                 element.x = m_points[index++];
       
   194                 element.y = m_points[index++];
       
   195                 element.type = m_elements[i];
       
   196                 data->elements << element;
       
   197             }
       
   198         } else {
       
   199             data->elements[0].type = QPainterPath::MoveToElement;
       
   200             for (int i=1; i<m_count; ++i) {
       
   201                 QPainterPath::Element element;
       
   202                 element.x = m_points[index++];
       
   203                 element.y = m_points[index++];
       
   204                 element.type = QPainterPath::LineToElement;
       
   205                 data->elements << element;
       
   206             }
       
   207         }
       
   208 
       
   209         if (m_hints & OddEvenFill)
       
   210             data->fillRule = Qt::OddEvenFill;
       
   211         else
       
   212             data->fillRule = Qt::WindingFill;
       
   213         return path;
       
   214 }
       
   215 
       
   216 void Q_GUI_EXPORT qt_find_ellipse_coords(const QRectF &r, qreal angle, qreal length,
       
   217                                          QPointF* startPoint, QPointF *endPoint);
       
   218 
       
   219 inline bool QPainterPathData::isClosed() const
       
   220 {
       
   221     const QPainterPath::Element &first = elements.at(cStart);
       
   222     const QPainterPath::Element &last = elements.last();
       
   223     return first.x == last.x && first.y == last.y;
       
   224 }
       
   225 
       
   226 inline void QPainterPathData::close()
       
   227 {
       
   228     Q_ASSERT(ref == 1);
       
   229     require_moveTo = true;
       
   230     const QPainterPath::Element &first = elements.at(cStart);
       
   231     QPainterPath::Element &last = elements.last();
       
   232     if (first.x != last.x || first.y != last.y) {
       
   233         if (qFuzzyCompare(first.x, last.x) && qFuzzyCompare(first.y, last.y)) {
       
   234             last.x = first.x;
       
   235             last.y = first.y;
       
   236         } else {
       
   237             QPainterPath::Element e = { first.x, first.y, QPainterPath::LineToElement };
       
   238             elements << e;
       
   239         }
       
   240     }
       
   241 }
       
   242 
       
   243 inline void QPainterPathData::maybeMoveTo()
       
   244 {
       
   245     if (require_moveTo) {
       
   246         QPainterPath::Element e = elements.last();
       
   247         e.type = QPainterPath::MoveToElement;
       
   248         elements.append(e);
       
   249         require_moveTo = false;
       
   250     }
       
   251 }
       
   252 
       
   253 #define KAPPA 0.5522847498
       
   254 
       
   255 
       
   256 QT_END_NAMESPACE
       
   257 
       
   258 #endif // QPAINTERPATH_P_H