1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QDECLARATIVEPATH_H
43 #define QDECLARATIVEPATH_H
45 #include <qdeclarative.h>
47 #include <private/qdeclarativenullablevalue_p_p.h>
48 #include <private/qbezier_p.h>
50 #include <QtCore/QObject>
51 #include <QtGui/QPainterPath>
57 class QDeclarativeCurve;
58 struct QDeclarativePathData
62 QList<QDeclarativeCurve*> curves;
65 class Q_AUTOTEST_EXPORT QDeclarativePathElement : public QObject
69 QDeclarativePathElement(QObject *parent=0) : QObject(parent) {}
74 class Q_AUTOTEST_EXPORT QDeclarativePathAttribute : public QDeclarativePathElement
78 Q_PROPERTY(QString name READ name WRITE setName NOTIFY nameChanged)
79 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
81 QDeclarativePathAttribute(QObject *parent=0) : QDeclarativePathElement(parent), _value(0) {}
85 void setName(const QString &name);
88 void setValue(qreal value);
99 class Q_AUTOTEST_EXPORT QDeclarativeCurve : public QDeclarativePathElement
103 Q_PROPERTY(qreal x READ x WRITE setX NOTIFY xChanged)
104 Q_PROPERTY(qreal y READ y WRITE setY NOTIFY yChanged)
105 Q_PROPERTY(qreal relativeX READ relativeX WRITE setRelativeX NOTIFY relativeXChanged)
106 Q_PROPERTY(qreal relativeY READ relativeY WRITE setRelativeY NOTIFY relativeYChanged)
108 QDeclarativeCurve(QObject *parent=0) : QDeclarativePathElement(parent) {}
118 qreal relativeX() const;
119 void setRelativeX(qreal x);
122 qreal relativeY() const;
123 void setRelativeY(qreal y);
126 virtual void addToPath(QPainterPath &, const QDeclarativePathData &) {}
131 void relativeXChanged();
132 void relativeYChanged();
135 QDeclarativeNullableValue<qreal> _x;
136 QDeclarativeNullableValue<qreal> _y;
137 QDeclarativeNullableValue<qreal> _relativeX;
138 QDeclarativeNullableValue<qreal> _relativeY;
141 class Q_AUTOTEST_EXPORT QDeclarativePathLine : public QDeclarativeCurve
145 QDeclarativePathLine(QObject *parent=0) : QDeclarativeCurve(parent) {}
147 void addToPath(QPainterPath &path, const QDeclarativePathData &);
150 class Q_AUTOTEST_EXPORT QDeclarativePathQuad : public QDeclarativeCurve
154 Q_PROPERTY(qreal controlX READ controlX WRITE setControlX NOTIFY controlXChanged)
155 Q_PROPERTY(qreal controlY READ controlY WRITE setControlY NOTIFY controlYChanged)
156 Q_PROPERTY(qreal relativeControlX READ relativeControlX WRITE setRelativeControlX NOTIFY relativeControlXChanged)
157 Q_PROPERTY(qreal relativeControlY READ relativeControlY WRITE setRelativeControlY NOTIFY relativeControlYChanged)
159 QDeclarativePathQuad(QObject *parent=0) : QDeclarativeCurve(parent), _controlX(0), _controlY(0) {}
161 qreal controlX() const;
162 void setControlX(qreal x);
164 qreal controlY() const;
165 void setControlY(qreal y);
167 qreal relativeControlX() const;
168 void setRelativeControlX(qreal x);
169 bool hasRelativeControlX();
171 qreal relativeControlY() const;
172 void setRelativeControlY(qreal y);
173 bool hasRelativeControlY();
175 void addToPath(QPainterPath &path, const QDeclarativePathData &);
178 void controlXChanged();
179 void controlYChanged();
180 void relativeControlXChanged();
181 void relativeControlYChanged();
186 QDeclarativeNullableValue<qreal> _relativeControlX;
187 QDeclarativeNullableValue<qreal> _relativeControlY;
190 class Q_AUTOTEST_EXPORT QDeclarativePathCubic : public QDeclarativeCurve
194 Q_PROPERTY(qreal control1X READ control1X WRITE setControl1X NOTIFY control1XChanged)
195 Q_PROPERTY(qreal control1Y READ control1Y WRITE setControl1Y NOTIFY control1YChanged)
196 Q_PROPERTY(qreal control2X READ control2X WRITE setControl2X NOTIFY control2XChanged)
197 Q_PROPERTY(qreal control2Y READ control2Y WRITE setControl2Y NOTIFY control2YChanged)
198 Q_PROPERTY(qreal relativeControl1X READ relativeControl1X WRITE setRelativeControl1X NOTIFY relativeControl1XChanged)
199 Q_PROPERTY(qreal relativeControl1Y READ relativeControl1Y WRITE setRelativeControl1Y NOTIFY relativeControl1YChanged)
200 Q_PROPERTY(qreal relativeControl2X READ relativeControl2X WRITE setRelativeControl2X NOTIFY relativeControl2XChanged)
201 Q_PROPERTY(qreal relativeControl2Y READ relativeControl2Y WRITE setRelativeControl2Y NOTIFY relativeControl2YChanged)
203 QDeclarativePathCubic(QObject *parent=0) : QDeclarativeCurve(parent), _control1X(0), _control1Y(0), _control2X(0), _control2Y(0) {}
205 qreal control1X() const;
206 void setControl1X(qreal x);
208 qreal control1Y() const;
209 void setControl1Y(qreal y);
211 qreal control2X() const;
212 void setControl2X(qreal x);
214 qreal control2Y() const;
215 void setControl2Y(qreal y);
217 qreal relativeControl1X() const;
218 void setRelativeControl1X(qreal x);
219 bool hasRelativeControl1X();
221 qreal relativeControl1Y() const;
222 void setRelativeControl1Y(qreal y);
223 bool hasRelativeControl1Y();
225 qreal relativeControl2X() const;
226 void setRelativeControl2X(qreal x);
227 bool hasRelativeControl2X();
229 qreal relativeControl2Y() const;
230 void setRelativeControl2Y(qreal y);
231 bool hasRelativeControl2Y();
233 void addToPath(QPainterPath &path, const QDeclarativePathData &);
236 void control1XChanged();
237 void control1YChanged();
238 void control2XChanged();
239 void control2YChanged();
240 void relativeControl1XChanged();
241 void relativeControl1YChanged();
242 void relativeControl2XChanged();
243 void relativeControl2YChanged();
250 QDeclarativeNullableValue<qreal> _relativeControl1X;
251 QDeclarativeNullableValue<qreal> _relativeControl1Y;
252 QDeclarativeNullableValue<qreal> _relativeControl2X;
253 QDeclarativeNullableValue<qreal> _relativeControl2Y;
256 class Q_AUTOTEST_EXPORT QDeclarativePathCatmullRomCurve : public QDeclarativeCurve
260 QDeclarativePathCatmullRomCurve(QObject *parent=0) : QDeclarativeCurve(parent) {}
262 void addToPath(QPainterPath &path, const QDeclarativePathData &);
265 class Q_AUTOTEST_EXPORT QDeclarativePathArc : public QDeclarativeCurve
268 Q_PROPERTY(qreal radiusX READ radiusX WRITE setRadiusX NOTIFY radiusXChanged)
269 Q_PROPERTY(qreal radiusY READ radiusY WRITE setRadiusY NOTIFY radiusYChanged)
270 Q_PROPERTY(bool useLargeArc READ useLargeArc WRITE setUseLargeArc NOTIFY useLargeArcChanged)
271 Q_PROPERTY(ArcDirection direction READ direction WRITE setDirection NOTIFY directionChanged)
274 QDeclarativePathArc(QObject *parent=0)
275 : QDeclarativeCurve(parent), _radiusX(0), _radiusY(0), _useLargeArc(false), _direction(Clockwise) {}
277 enum ArcDirection { Clockwise, Counterclockwise };
278 Q_ENUMS(ArcDirection)
280 qreal radiusX() const;
281 void setRadiusX(qreal);
283 qreal radiusY() const;
284 void setRadiusY(qreal);
286 bool useLargeArc() const;
287 void setUseLargeArc(bool);
289 ArcDirection direction() const;
290 void setDirection(ArcDirection direction);
292 void addToPath(QPainterPath &path, const QDeclarativePathData &);
295 void radiusXChanged();
296 void radiusYChanged();
297 void useLargeArcChanged();
298 void directionChanged();
304 ArcDirection _direction;
307 class Q_AUTOTEST_EXPORT QDeclarativePathSvg : public QDeclarativeCurve
310 Q_PROPERTY(QString path READ path WRITE setPath NOTIFY pathChanged)
312 QDeclarativePathSvg(QObject *parent=0) : QDeclarativeCurve(parent) {}
314 QString path() const;
315 void setPath(const QString &path);
317 void addToPath(QPainterPath &path, const QDeclarativePathData &);
326 class Q_AUTOTEST_EXPORT QDeclarativePathPercent : public QDeclarativePathElement
329 Q_PROPERTY(qreal value READ value WRITE setValue NOTIFY valueChanged)
331 QDeclarativePathPercent(QObject *parent=0) : QDeclarativePathElement(parent) {}
334 void setValue(qreal value);
343 struct QDeclarativeCachedBezier
345 QDeclarativeCachedBezier() : isValid(false) {}
354 class QDeclarativePathPrivate;
355 class Q_AUTOTEST_EXPORT QDeclarativePath : public QObject, public QDeclarativeParserStatus
359 Q_INTERFACES(QDeclarativeParserStatus)
360 Q_PROPERTY(QDeclarativeListProperty<QDeclarativePathElement> pathElements READ pathElements)
361 Q_PROPERTY(qreal startX READ startX WRITE setStartX NOTIFY startXChanged)
362 Q_PROPERTY(qreal startY READ startY WRITE setStartY NOTIFY startYChanged)
363 Q_PROPERTY(bool closed READ isClosed NOTIFY changed)
364 Q_CLASSINFO("DefaultProperty", "pathElements")
365 Q_INTERFACES(QDeclarativeParserStatus)
367 QDeclarativePath(QObject *parent=0);
370 QDeclarativeListProperty<QDeclarativePathElement> pathElements();
372 qreal startX() const;
373 void setStartX(qreal x);
374 bool hasStartX() const;
376 qreal startY() const;
377 void setStartY(qreal y);
378 bool hasStartY() const;
380 bool isClosed() const;
383 QPainterPath path() const;
384 QStringList attributes() const;
385 qreal attributeAt(const QString &, qreal) const;
386 QPointF pointAt(qreal) const;
387 QPointF sequentialPointAt(qreal p, qreal *angle = 0) const;
391 void startXChanged();
392 void startYChanged();
395 virtual void componentComplete();
396 virtual void classBegin();
402 struct AttributePoint {
403 AttributePoint() : percent(0), scale(1), origpercent(0) {}
404 AttributePoint(const AttributePoint &other)
405 : percent(other.percent), scale(other.scale), origpercent(other.origpercent), values(other.values) {}
406 AttributePoint &operator=(const AttributePoint &other) {
407 percent = other.percent; scale = other.scale; origpercent = other.origpercent; values = other.values; return *this;
409 qreal percent; //massaged percent along the painter path
411 qreal origpercent; //'real' percent along the painter path
412 QHash<QString, qreal> values;
415 void interpolate(int idx, const QString &name, qreal value);
416 void endpoint(const QString &name);
417 void createPointCache() const;
419 static void interpolate(QList<AttributePoint> &points, int idx, const QString &name, qreal value);
420 static void endpoint(QList<AttributePoint> &attributePoints, const QString &name);
421 static QPointF forwardsPointAt(const QPainterPath &path, const qreal &pathLength, const QList<AttributePoint> &attributePoints, QDeclarativeCachedBezier &prevBez, qreal p, qreal *angle = 0);
422 static QPointF backwardsPointAt(const QPainterPath &path, const qreal &pathLength, const QList<AttributePoint> &attributePoints, QDeclarativeCachedBezier &prevBez, qreal p, qreal *angle = 0);
425 Q_DISABLE_COPY(QDeclarativePath)
426 Q_DECLARE_PRIVATE(QDeclarativePath)
427 friend class QQuickPathAnimationUpdater;
430 QPainterPath createPath(const QPointF &startPoint, const QPointF &endPoint, const QStringList &attributes, qreal &pathLength, QList<AttributePoint> &attributePoints, bool *closed = 0);
431 static QPointF sequentialPointAt(const QPainterPath &path, const qreal &pathLength, const QList<AttributePoint> &attributePoints, QDeclarativeCachedBezier &prevBez, qreal p, qreal *angle = 0);
436 QML_DECLARE_TYPE(QDeclarativePathElement)
437 QML_DECLARE_TYPE(QDeclarativePathAttribute)
438 QML_DECLARE_TYPE(QDeclarativeCurve)
439 QML_DECLARE_TYPE(QDeclarativePathLine)
440 QML_DECLARE_TYPE(QDeclarativePathQuad)
441 QML_DECLARE_TYPE(QDeclarativePathCubic)
442 QML_DECLARE_TYPE(QDeclarativePathCatmullRomCurve)
443 QML_DECLARE_TYPE(QDeclarativePathArc)
444 QML_DECLARE_TYPE(QDeclarativePathSvg)
445 QML_DECLARE_TYPE(QDeclarativePathPercent)
446 QML_DECLARE_TYPE(QDeclarativePath)
450 #endif // QDECLARATIVEPATH_H