Rename Qt Quick-specific classes to QQuick*
[profile/ivi/qtdeclarative.git] / src / declarative / util / qdeclarativepath_p.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 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 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QDECLARATIVEPATH_H
43 #define QDECLARATIVEPATH_H
44
45 #include <qdeclarative.h>
46
47 #include <private/qdeclarativenullablevalue_p_p.h>
48 #include <private/qbezier_p.h>
49
50 #include <QtCore/QObject>
51 #include <QtGui/QPainterPath>
52
53 QT_BEGIN_HEADER
54
55 QT_BEGIN_NAMESPACE
56
57 QT_MODULE(Declarative)
58
59 class QDeclarativeCurve;
60 struct QDeclarativePathData
61 {
62     int index;
63     QPointF endPoint;
64     QList<QDeclarativeCurve*> curves;
65 };
66
67 class Q_AUTOTEST_EXPORT QDeclarativePathElement : public QObject
68 {
69     Q_OBJECT
70 public:
71     QDeclarativePathElement(QObject *parent=0) : QObject(parent) {}
72 Q_SIGNALS:
73     void changed();
74 };
75
76 class Q_AUTOTEST_EXPORT QDeclarativePathAttribute : public QDeclarativePathElement
77 {
78     Q_OBJECT
79
80     Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
81     Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
82 public:
83     QDeclarativePathAttribute(QObject *parent=0) : QDeclarativePathElement(parent), _value(0) {}
84
85
86     QString name() const;
87     void setName(const QString &name);
88
89     qreal value() const;
90     void setValue(qreal value);
91
92 Q_SIGNALS:
93     void nameChanged();
94     void valueChanged();
95
96 private:
97     QString _name;
98     qreal _value;
99 };
100
101 class Q_AUTOTEST_EXPORT QDeclarativeCurve : public QDeclarativePathElement
102 {
103     Q_OBJECT
104
105     Q_PROPERTY(qreal x READ x WRITE setX NOTIFY xChanged)
106     Q_PROPERTY(qreal y READ y WRITE setY NOTIFY yChanged)
107     Q_PROPERTY(qreal relativeX READ relativeX WRITE setRelativeX NOTIFY relativeXChanged)
108     Q_PROPERTY(qreal relativeY READ relativeY WRITE setRelativeY NOTIFY relativeYChanged)
109 public:
110     QDeclarativeCurve(QObject *parent=0) : QDeclarativePathElement(parent) {}
111
112     qreal x() const;
113     void setX(qreal x);
114     bool hasX();
115
116     qreal y() const;
117     void setY(qreal y);
118     bool hasY();
119
120     qreal relativeX() const;
121     void setRelativeX(qreal x);
122     bool hasRelativeX();
123
124     qreal relativeY() const;
125     void setRelativeY(qreal y);
126     bool hasRelativeY();
127
128     virtual void addToPath(QPainterPath &, const QDeclarativePathData &) {}
129
130 Q_SIGNALS:
131     void xChanged();
132     void yChanged();
133     void relativeXChanged();
134     void relativeYChanged();
135
136 private:
137     QDeclarativeNullableValue<qreal> _x;
138     QDeclarativeNullableValue<qreal> _y;
139     QDeclarativeNullableValue<qreal> _relativeX;
140     QDeclarativeNullableValue<qreal> _relativeY;
141 };
142
143 class Q_AUTOTEST_EXPORT QDeclarativePathLine : public QDeclarativeCurve
144 {
145     Q_OBJECT
146 public:
147     QDeclarativePathLine(QObject *parent=0) : QDeclarativeCurve(parent) {}
148
149     void addToPath(QPainterPath &path, const QDeclarativePathData &);
150 };
151
152 class Q_AUTOTEST_EXPORT QDeclarativePathQuad : public QDeclarativeCurve
153 {
154     Q_OBJECT
155
156     Q_PROPERTY(qreal controlX READ controlX WRITE setControlX NOTIFY controlXChanged)
157     Q_PROPERTY(qreal controlY READ controlY WRITE setControlY NOTIFY controlYChanged)
158     Q_PROPERTY(qreal relativeControlX READ relativeControlX WRITE setRelativeControlX NOTIFY relativeControlXChanged)
159     Q_PROPERTY(qreal relativeControlY READ relativeControlY WRITE setRelativeControlY NOTIFY relativeControlYChanged)
160 public:
161     QDeclarativePathQuad(QObject *parent=0) : QDeclarativeCurve(parent), _controlX(0), _controlY(0) {}
162
163     qreal controlX() const;
164     void setControlX(qreal x);
165
166     qreal controlY() const;
167     void setControlY(qreal y);
168
169     qreal relativeControlX() const;
170     void setRelativeControlX(qreal x);
171     bool hasRelativeControlX();
172
173     qreal relativeControlY() const;
174     void setRelativeControlY(qreal y);
175     bool hasRelativeControlY();
176
177     void addToPath(QPainterPath &path, const QDeclarativePathData &);
178
179 Q_SIGNALS:
180     void controlXChanged();
181     void controlYChanged();
182     void relativeControlXChanged();
183     void relativeControlYChanged();
184
185 private:
186     qreal _controlX;
187     qreal _controlY;
188     QDeclarativeNullableValue<qreal> _relativeControlX;
189     QDeclarativeNullableValue<qreal> _relativeControlY;
190 };
191
192 class Q_AUTOTEST_EXPORT QDeclarativePathCubic : public QDeclarativeCurve
193 {
194     Q_OBJECT
195
196     Q_PROPERTY(qreal control1X READ control1X WRITE setControl1X NOTIFY control1XChanged)
197     Q_PROPERTY(qreal control1Y READ control1Y WRITE setControl1Y NOTIFY control1YChanged)
198     Q_PROPERTY(qreal control2X READ control2X WRITE setControl2X NOTIFY control2XChanged)
199     Q_PROPERTY(qreal control2Y READ control2Y WRITE setControl2Y NOTIFY control2YChanged)
200     Q_PROPERTY(qreal relativeControl1X READ relativeControl1X WRITE setRelativeControl1X NOTIFY relativeControl1XChanged)
201     Q_PROPERTY(qreal relativeControl1Y READ relativeControl1Y WRITE setRelativeControl1Y NOTIFY relativeControl1YChanged)
202     Q_PROPERTY(qreal relativeControl2X READ relativeControl2X WRITE setRelativeControl2X NOTIFY relativeControl2XChanged)
203     Q_PROPERTY(qreal relativeControl2Y READ relativeControl2Y WRITE setRelativeControl2Y NOTIFY relativeControl2YChanged)
204 public:
205     QDeclarativePathCubic(QObject *parent=0) : QDeclarativeCurve(parent), _control1X(0), _control1Y(0), _control2X(0), _control2Y(0) {}
206
207     qreal control1X() const;
208     void setControl1X(qreal x);
209
210     qreal control1Y() const;
211     void setControl1Y(qreal y);
212
213     qreal control2X() const;
214     void setControl2X(qreal x);
215
216     qreal control2Y() const;
217     void setControl2Y(qreal y);
218
219     qreal relativeControl1X() const;
220     void setRelativeControl1X(qreal x);
221     bool hasRelativeControl1X();
222
223     qreal relativeControl1Y() const;
224     void setRelativeControl1Y(qreal y);
225     bool hasRelativeControl1Y();
226
227     qreal relativeControl2X() const;
228     void setRelativeControl2X(qreal x);
229     bool hasRelativeControl2X();
230
231     qreal relativeControl2Y() const;
232     void setRelativeControl2Y(qreal y);
233     bool hasRelativeControl2Y();
234
235     void addToPath(QPainterPath &path, const QDeclarativePathData &);
236
237 Q_SIGNALS:
238     void control1XChanged();
239     void control1YChanged();
240     void control2XChanged();
241     void control2YChanged();
242     void relativeControl1XChanged();
243     void relativeControl1YChanged();
244     void relativeControl2XChanged();
245     void relativeControl2YChanged();
246
247 private:
248     qreal _control1X;
249     qreal _control1Y;
250     qreal _control2X;
251     qreal _control2Y;
252     QDeclarativeNullableValue<qreal> _relativeControl1X;
253     QDeclarativeNullableValue<qreal> _relativeControl1Y;
254     QDeclarativeNullableValue<qreal> _relativeControl2X;
255     QDeclarativeNullableValue<qreal> _relativeControl2Y;
256 };
257
258 class Q_AUTOTEST_EXPORT QDeclarativePathCatmullRomCurve : public QDeclarativeCurve
259 {
260     Q_OBJECT
261 public:
262     QDeclarativePathCatmullRomCurve(QObject *parent=0) : QDeclarativeCurve(parent) {}
263
264     void addToPath(QPainterPath &path, const QDeclarativePathData &);
265 };
266
267 class Q_AUTOTEST_EXPORT QDeclarativePathArc : public QDeclarativeCurve
268 {
269     Q_OBJECT
270     Q_PROPERTY(qreal radiusX READ radiusX WRITE setRadiusX NOTIFY radiusXChanged)
271     Q_PROPERTY(qreal radiusY READ radiusY WRITE setRadiusY NOTIFY radiusYChanged)
272     Q_PROPERTY(bool useLargeArc READ useLargeArc WRITE setUseLargeArc NOTIFY useLargeArcChanged)
273     Q_PROPERTY(ArcDirection direction READ direction WRITE setDirection NOTIFY directionChanged)
274
275 public:
276     QDeclarativePathArc(QObject *parent=0)
277         : QDeclarativeCurve(parent), _radiusX(0), _radiusY(0), _useLargeArc(false), _direction(Clockwise) {}
278
279     enum ArcDirection { Clockwise, Counterclockwise };
280     Q_ENUMS(ArcDirection)
281
282     qreal radiusX() const;
283     void setRadiusX(qreal);
284
285     qreal radiusY() const;
286     void setRadiusY(qreal);
287
288     bool useLargeArc() const;
289     void setUseLargeArc(bool);
290
291     ArcDirection direction() const;
292     void setDirection(ArcDirection direction);
293
294     void addToPath(QPainterPath &path, const QDeclarativePathData &);
295
296 Q_SIGNALS:
297     void radiusXChanged();
298     void radiusYChanged();
299     void useLargeArcChanged();
300     void directionChanged();
301
302 private:
303     qreal _radiusX;
304     qreal _radiusY;
305     bool _useLargeArc;
306     ArcDirection _direction;
307 };
308
309 class Q_AUTOTEST_EXPORT QDeclarativePathSvg : public QDeclarativeCurve
310 {
311     Q_OBJECT
312     Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
313 public:
314     QDeclarativePathSvg(QObject *parent=0) : QDeclarativeCurve(parent) {}
315
316     QString path() const;
317     void setPath(const QString &path);
318
319     void addToPath(QPainterPath &path, const QDeclarativePathData &);
320
321 Q_SIGNALS:
322     void pathChanged();
323
324 private:
325     QString _path;
326 };
327
328 class Q_AUTOTEST_EXPORT QDeclarativePathPercent : public QDeclarativePathElement
329 {
330     Q_OBJECT
331     Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
332 public:
333     QDeclarativePathPercent(QObject *parent=0) : QDeclarativePathElement(parent) {}
334
335     qreal value() const;
336     void setValue(qreal value);
337
338 signals:
339     void valueChanged();
340
341 private:
342     qreal _value;
343 };
344
345 struct QDeclarativeCachedBezier
346 {
347     QDeclarativeCachedBezier() : isValid(false) {}
348     QBezier bezier;
349     int element;
350     qreal bezLength;
351     qreal currLength;
352     qreal p;
353     bool isValid;
354 };
355
356 class QDeclarativePathPrivate;
357 class Q_AUTOTEST_EXPORT QDeclarativePath : public QObject, public QDeclarativeParserStatus
358 {
359     Q_OBJECT
360
361     Q_INTERFACES(QDeclarativeParserStatus)
362     Q_PROPERTY(QDeclarativeListProperty<QDeclarativePathElement> pathElements READ pathElements)
363     Q_PROPERTY(qreal startX READ startX WRITE setStartX NOTIFY startXChanged)
364     Q_PROPERTY(qreal startY READ startY WRITE setStartY NOTIFY startYChanged)
365     Q_PROPERTY(bool closed READ isClosed NOTIFY changed)
366     Q_CLASSINFO("DefaultProperty", "pathElements")
367     Q_INTERFACES(QDeclarativeParserStatus)
368 public:
369     QDeclarativePath(QObject *parent=0);
370     ~QDeclarativePath();
371
372     QDeclarativeListProperty<QDeclarativePathElement> pathElements();
373
374     qreal startX() const;
375     void setStartX(qreal x);
376     bool hasStartX() const;
377
378     qreal startY() const;
379     void setStartY(qreal y);
380     bool hasStartY() const;
381
382     bool isClosed() const;
383     bool hasEnd() const;
384
385     QPainterPath path() const;
386     QStringList attributes() const;
387     qreal attributeAt(const QString &, qreal) const;
388     QPointF pointAt(qreal) const;
389     QPointF sequentialPointAt(qreal p, qreal *angle = 0) const;
390
391 Q_SIGNALS:
392     void changed();
393     void startXChanged();
394     void startYChanged();
395
396 protected:
397     virtual void componentComplete();
398     virtual void classBegin();
399
400 private Q_SLOTS:
401     void processPath();
402
403 private:
404     struct AttributePoint {
405         AttributePoint() : percent(0), scale(1), origpercent(0) {}
406         AttributePoint(const AttributePoint &other)
407             : percent(other.percent), scale(other.scale), origpercent(other.origpercent), values(other.values) {}
408         AttributePoint &operator=(const AttributePoint &other) {
409             percent = other.percent; scale = other.scale; origpercent = other.origpercent; values = other.values; return *this;
410         }
411         qreal percent;      //massaged percent along the painter path
412         qreal scale;
413         qreal origpercent;  //'real' percent along the painter path
414         QHash<QString, qreal> values;
415     };
416
417     void interpolate(int idx, const QString &name, qreal value);
418     void endpoint(const QString &name);
419     void createPointCache() const;
420
421     static void interpolate(QList<AttributePoint> &points, int idx, const QString &name, qreal value);
422     static void endpoint(QList<AttributePoint> &attributePoints, const QString &name);
423     static QPointF forwardsPointAt(const QPainterPath &path, const qreal &pathLength, const QList<AttributePoint> &attributePoints, QDeclarativeCachedBezier &prevBez, qreal p, qreal *angle = 0);
424     static QPointF backwardsPointAt(const QPainterPath &path, const qreal &pathLength, const QList<AttributePoint> &attributePoints, QDeclarativeCachedBezier &prevBez, qreal p, qreal *angle = 0);
425
426 private:
427     Q_DISABLE_COPY(QDeclarativePath)
428     Q_DECLARE_PRIVATE(QDeclarativePath)
429     friend class QQuickPathAnimationUpdater;
430
431 public:
432     QPainterPath createPath(const QPointF &startPoint, const QPointF &endPoint, const QStringList &attributes, qreal &pathLength, QList<AttributePoint> &attributePoints, bool *closed = 0);
433     static QPointF sequentialPointAt(const QPainterPath &path, const qreal &pathLength, const QList<AttributePoint> &attributePoints, QDeclarativeCachedBezier &prevBez, qreal p, qreal *angle = 0);
434 };
435
436 QT_END_NAMESPACE
437
438 QML_DECLARE_TYPE(QDeclarativePathElement)
439 QML_DECLARE_TYPE(QDeclarativePathAttribute)
440 QML_DECLARE_TYPE(QDeclarativeCurve)
441 QML_DECLARE_TYPE(QDeclarativePathLine)
442 QML_DECLARE_TYPE(QDeclarativePathQuad)
443 QML_DECLARE_TYPE(QDeclarativePathCubic)
444 QML_DECLARE_TYPE(QDeclarativePathCatmullRomCurve)
445 QML_DECLARE_TYPE(QDeclarativePathArc)
446 QML_DECLARE_TYPE(QDeclarativePathSvg)
447 QML_DECLARE_TYPE(QDeclarativePathPercent)
448 QML_DECLARE_TYPE(QDeclarativePath)
449
450 QT_END_HEADER
451
452 #endif // QDECLARATIVEPATH_H