src/declarative/graphicsitems/qdeclarativepath_p.h
changeset 30 5dc02b23752f
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 QtDeclarative 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 QDECLARATIVEPATH_H
       
    43 #define QDECLARATIVEPATH_H
       
    44 
       
    45 #include "qdeclarativeitem.h"
       
    46 
       
    47 #include <qdeclarative.h>
       
    48 
       
    49 #include <QtCore/QObject>
       
    50 #include <QtGui/QPainterPath>
       
    51 
       
    52 QT_BEGIN_HEADER
       
    53 
       
    54 QT_BEGIN_NAMESPACE
       
    55 
       
    56 QT_MODULE(Declarative)
       
    57 class Q_DECLARATIVE_EXPORT QDeclarativePathElement : public QObject
       
    58 {
       
    59     Q_OBJECT
       
    60 public:
       
    61     QDeclarativePathElement(QObject *parent=0) : QObject(parent) {}
       
    62 Q_SIGNALS:
       
    63     void changed();
       
    64 };
       
    65 
       
    66 class Q_DECLARATIVE_EXPORT QDeclarativePathAttribute : public QDeclarativePathElement
       
    67 {
       
    68     Q_OBJECT
       
    69 
       
    70     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
       
    71     Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY changed)
       
    72 public:
       
    73     QDeclarativePathAttribute(QObject *parent=0) : QDeclarativePathElement(parent), _value(0) {}
       
    74 
       
    75 
       
    76     QString name() const;
       
    77     void setName(const QString &name);
       
    78 
       
    79     qreal value() const;
       
    80     void setValue(qreal value);
       
    81 
       
    82 Q_SIGNALS:
       
    83     void nameChanged();
       
    84 
       
    85 private:
       
    86     QString _name;
       
    87     qreal _value;
       
    88 };
       
    89 
       
    90 class Q_DECLARATIVE_EXPORT QDeclarativeCurve : public QDeclarativePathElement
       
    91 {
       
    92     Q_OBJECT
       
    93 
       
    94     Q_PROPERTY(qreal x READ x WRITE setX NOTIFY changed)
       
    95     Q_PROPERTY(qreal y READ y WRITE setY NOTIFY changed)
       
    96 public:
       
    97     QDeclarativeCurve(QObject *parent=0) : QDeclarativePathElement(parent), _x(0), _y(0) {}
       
    98 
       
    99     qreal x() const;
       
   100     void setX(qreal x);
       
   101 
       
   102     qreal y() const;
       
   103     void setY(qreal y);
       
   104 
       
   105     virtual void addToPath(QPainterPath &) {}
       
   106 
       
   107 private:
       
   108     qreal _x;
       
   109     qreal _y;
       
   110 };
       
   111 
       
   112 class Q_DECLARATIVE_EXPORT QDeclarativePathLine : public QDeclarativeCurve
       
   113 {
       
   114     Q_OBJECT
       
   115 public:
       
   116     QDeclarativePathLine(QObject *parent=0) : QDeclarativeCurve(parent) {}
       
   117 
       
   118     void addToPath(QPainterPath &path);
       
   119 };
       
   120 
       
   121 class Q_DECLARATIVE_EXPORT QDeclarativePathQuad : public QDeclarativeCurve
       
   122 {
       
   123     Q_OBJECT
       
   124 
       
   125     Q_PROPERTY(qreal controlX READ controlX WRITE setControlX NOTIFY changed)
       
   126     Q_PROPERTY(qreal controlY READ controlY WRITE setControlY NOTIFY changed)
       
   127 public:
       
   128     QDeclarativePathQuad(QObject *parent=0) : QDeclarativeCurve(parent), _controlX(0), _controlY(0) {}
       
   129 
       
   130     qreal controlX() const;
       
   131     void setControlX(qreal x);
       
   132 
       
   133     qreal controlY() const;
       
   134     void setControlY(qreal y);
       
   135 
       
   136     void addToPath(QPainterPath &path);
       
   137 
       
   138 private:
       
   139     qreal _controlX;
       
   140     qreal _controlY;
       
   141 };
       
   142 
       
   143 class Q_DECLARATIVE_EXPORT QDeclarativePathCubic : public QDeclarativeCurve
       
   144 {
       
   145     Q_OBJECT
       
   146 
       
   147     Q_PROPERTY(qreal control1X READ control1X WRITE setControl1X NOTIFY changed)
       
   148     Q_PROPERTY(qreal control1Y READ control1Y WRITE setControl1Y NOTIFY changed)
       
   149     Q_PROPERTY(qreal control2X READ control2X WRITE setControl2X NOTIFY changed)
       
   150     Q_PROPERTY(qreal control2Y READ control2Y WRITE setControl2Y NOTIFY changed)
       
   151 public:
       
   152     QDeclarativePathCubic(QObject *parent=0) : QDeclarativeCurve(parent), _control1X(0), _control1Y(0), _control2X(0), _control2Y(0) {}
       
   153 
       
   154     qreal control1X() const;
       
   155     void setControl1X(qreal x);
       
   156 
       
   157     qreal control1Y() const;
       
   158     void setControl1Y(qreal y);
       
   159 
       
   160     qreal control2X() const;
       
   161     void setControl2X(qreal x);
       
   162 
       
   163     qreal control2Y() const;
       
   164     void setControl2Y(qreal y);
       
   165 
       
   166     void addToPath(QPainterPath &path);
       
   167 
       
   168 private:
       
   169     int _control1X;
       
   170     int _control1Y;
       
   171     int _control2X;
       
   172     int _control2Y;
       
   173 };
       
   174 
       
   175 class Q_DECLARATIVE_EXPORT QDeclarativePathPercent : public QDeclarativePathElement
       
   176 {
       
   177     Q_OBJECT
       
   178     Q_PROPERTY(qreal value READ value WRITE setValue)
       
   179 public:
       
   180     QDeclarativePathPercent(QObject *parent=0) : QDeclarativePathElement(parent) {}
       
   181 
       
   182     qreal value() const;
       
   183     void setValue(qreal value);
       
   184 
       
   185 private:
       
   186     qreal _value;
       
   187 };
       
   188 
       
   189 class QDeclarativePathPrivate;
       
   190 class Q_DECLARATIVE_EXPORT QDeclarativePath : public QObject, public QDeclarativeParserStatus
       
   191 {
       
   192     Q_OBJECT
       
   193 
       
   194     Q_INTERFACES(QDeclarativeParserStatus)
       
   195     Q_PROPERTY(QDeclarativeListProperty<QDeclarativePathElement> pathElements READ pathElements)
       
   196     Q_PROPERTY(qreal startX READ startX WRITE setStartX NOTIFY startXChanged)
       
   197     Q_PROPERTY(qreal startY READ startY WRITE setStartY NOTIFY startYChanged)
       
   198     Q_PROPERTY(bool closed READ isClosed NOTIFY changed)
       
   199     Q_CLASSINFO("DefaultProperty", "pathElements")
       
   200     Q_INTERFACES(QDeclarativeParserStatus)
       
   201 public:
       
   202     QDeclarativePath(QObject *parent=0);
       
   203     ~QDeclarativePath();
       
   204 
       
   205     QDeclarativeListProperty<QDeclarativePathElement> pathElements();
       
   206 
       
   207     qreal startX() const;
       
   208     void setStartX(qreal x);
       
   209 
       
   210     qreal startY() const;
       
   211     void setStartY(qreal y);
       
   212 
       
   213     bool isClosed() const;
       
   214 
       
   215     QPainterPath path() const;
       
   216     QStringList attributes() const;
       
   217     qreal attributeAt(const QString &, qreal) const;
       
   218     QPointF pointAt(qreal) const;
       
   219 
       
   220 Q_SIGNALS:
       
   221     void changed();
       
   222     void startXChanged();
       
   223     void startYChanged();
       
   224 
       
   225 protected:
       
   226     virtual void componentComplete();
       
   227     virtual void classBegin();
       
   228 
       
   229 private Q_SLOTS:
       
   230     void processPath();
       
   231 
       
   232 private:
       
   233     struct AttributePoint {
       
   234         AttributePoint() : percent(0), scale(1), origpercent(0) {}
       
   235         AttributePoint(const AttributePoint &other)
       
   236             : percent(other.percent), scale(other.scale), origpercent(other.origpercent), values(other.values) {}
       
   237         AttributePoint &operator=(const AttributePoint &other) {
       
   238             percent = other.percent; scale = other.scale; origpercent = other.origpercent; values = other.values; return *this;
       
   239         }
       
   240         qreal percent;      //massaged percent along the painter path
       
   241         qreal scale;
       
   242         qreal origpercent;  //'real' percent along the painter path
       
   243         QHash<QString, qreal> values;
       
   244     };
       
   245 
       
   246     void interpolate(int idx, const QString &name, qreal value);
       
   247     void endpoint(const QString &name);
       
   248     void createPointCache() const;
       
   249 
       
   250 private:
       
   251     Q_DISABLE_COPY(QDeclarativePath)
       
   252     Q_DECLARE_PRIVATE(QDeclarativePath)
       
   253 };
       
   254 
       
   255 QT_END_NAMESPACE
       
   256 
       
   257 QML_DECLARE_TYPE(QDeclarativePathElement)
       
   258 QML_DECLARE_TYPE(QDeclarativePathAttribute)
       
   259 QML_DECLARE_TYPE(QDeclarativeCurve)
       
   260 QML_DECLARE_TYPE(QDeclarativePathLine)
       
   261 QML_DECLARE_TYPE(QDeclarativePathQuad)
       
   262 QML_DECLARE_TYPE(QDeclarativePathCubic)
       
   263 QML_DECLARE_TYPE(QDeclarativePathPercent)
       
   264 QML_DECLARE_TYPE(QDeclarativePath)
       
   265 
       
   266 QT_END_HEADER
       
   267 
       
   268 #endif // QDECLARATIVEPATH_H