Remove "All rights reserved" line from license headers.
[profile/ivi/qtdeclarative.git] / src / quick / util / qdeclarativepathinterpolator.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdeclarativepathinterpolator_p.h"
43
44 #include "qdeclarativepath_p.h"
45
46 QT_BEGIN_NAMESPACE
47
48 /*!
49     \qmlclass PathInterpolator QQuickPathInterpolator
50     \inqmlmodule QtQuick 2
51     \brief The PathInterpolator element provides a way to manually animate along a path.
52
53     PathInterpolator provides \c x, \c y, and \c angle information for a particular \c progress
54     along a path.
55
56     In the following example, we animate a green rectangle along a bezier path.
57
58     \snippet doc/src/snippets/declarative/pathinterpolator.qml 0
59 */
60
61 QDeclarativePathInterpolator::QDeclarativePathInterpolator(QObject *parent) :
62     QObject(parent), _path(0), _x(0), _y(0), _angle(0), _progress(0)
63 {
64 }
65
66 /*!
67     \qmlproperty Path QtQuick2::PathInterpolator::path
68     This property holds the path to interpolate.
69
70     For more information on defining a path see the \l Path documentation.
71 */
72 QDeclarativePath *QDeclarativePathInterpolator::path() const
73 {
74     return _path;
75 }
76
77 void QDeclarativePathInterpolator::setPath(QDeclarativePath *path)
78 {
79     if (_path == path)
80         return;
81     if (_path)
82         disconnect(_path, SIGNAL(changed()), this, SLOT(_q_pathUpdated()));
83     _path = path;
84     connect(_path, SIGNAL(changed()), this, SLOT(_q_pathUpdated()));
85     emit pathChanged();
86 }
87
88 /*!
89     \qmlproperty real QtQuick2::PathInterpolator::progress
90     This property holds the current progress along the path.
91
92     Typical usage of PathInterpolator is to set the progress
93     (often via a NumberAnimation), and read the corresponding
94     values for x, y, and angle (often via bindings to these values).
95
96     Progress ranges from 0.0 to 1.0.
97 */
98 qreal QDeclarativePathInterpolator::progress() const
99 {
100     return _progress;
101 }
102
103 void QDeclarativePathInterpolator::setProgress(qreal progress)
104 {
105     if (progress == _progress)
106         return;
107     _progress = progress;
108     emit progressChanged();
109     _q_pathUpdated();
110 }
111
112 /*!
113     \qmlproperty real QtQuick2::PathInterpolator::x
114     \qmlproperty real QtQuick2::PathInterpolator::y
115
116     These properties hold the position of the path at \l progress.
117 */
118 qreal QDeclarativePathInterpolator::x() const
119 {
120     return _x;
121 }
122
123 qreal QDeclarativePathInterpolator::y() const
124 {
125     return _y;
126 }
127
128 /*!
129     \qmlproperty real QtQuick2::PathInterpolator::angle
130
131     This property holds the angle of the path tangent at \l progress.
132
133     Angles are reported clockwise, with zero degrees at the 3 o'clock position.
134 */
135 qreal QDeclarativePathInterpolator::angle() const
136 {
137     return _angle;
138 }
139
140 void QDeclarativePathInterpolator::_q_pathUpdated()
141 {
142     if (! _path)
143         return;
144
145     qreal angle = 0;
146     const QPointF pt = _path->sequentialPointAt(_progress, &angle);
147
148     if (_x != pt.x()) {
149         _x = pt.x();
150         emit xChanged();
151     }
152
153     if (_y != pt.y()) {
154         _y = pt.y();
155         emit yChanged();
156     }
157
158     //convert to clockwise
159     angle = qreal(360) - angle;
160     if (qFuzzyCompare(angle, qreal(360)))
161         angle = qreal(0);
162
163     if (angle != _angle) {
164         _angle = angle;
165         emit angleChanged();
166     }
167 }
168
169 QT_END_NAMESPACE