887159f9bccce3b80f3b034b5eedc82d1060c628
[profile/ivi/qtdeclarative.git] / src / quick / items / qquicktranslate.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: http://www.qt-project.org/
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 #include "qquicktranslate_p.h"
43 #include "qquickitem_p.h"
44
45 #include <QtCore/qmath.h>
46
47 QT_BEGIN_NAMESPACE
48
49 class QQuickTranslatePrivate : public QQuickTransformPrivate
50 {
51 public:
52     QQuickTranslatePrivate()
53     : x(0), y(0) {}
54
55     qreal x;
56     qreal y;
57 };
58
59 /*!
60     Constructs an empty QQuickTranslate object with the given \a parent.
61 */
62 QQuickTranslate::QQuickTranslate(QObject *parent)
63 : QQuickTransform(*new QQuickTranslatePrivate, parent)
64 {
65 }
66
67 /*!
68     Destroys the graphics scale.
69 */
70 QQuickTranslate::~QQuickTranslate()
71 {
72 }
73
74 /*!
75     \property QQuickTranslate::x
76     \brief the horizontal translation.
77
78     The translation can be any real number; the default value is 0.0.
79
80     \sa y
81 */
82 qreal QQuickTranslate::x() const
83 {
84     Q_D(const QQuickTranslate);
85     return d->x;
86 }
87
88 void QQuickTranslate::setX(qreal x)
89 {
90     Q_D(QQuickTranslate);
91     if (d->x == x)
92         return;
93     d->x = x;
94     update();
95     emit xChanged();
96 }
97
98 /*!
99     \property QQuickTranslate::y
100     \brief the vertical translation.
101
102     The translation can be any real number; the default value is 0.0.
103
104     \sa x
105 */
106 qreal QQuickTranslate::y() const
107 {
108     Q_D(const QQuickTranslate);
109     return d->y;
110 }
111 void QQuickTranslate::setY(qreal y)
112 {
113     Q_D(QQuickTranslate);
114     if (d->y == y)
115         return;
116     d->y = y;
117     update();
118     emit yChanged();
119 }
120
121 void QQuickTranslate::applyTo(QMatrix4x4 *matrix) const
122 {
123     Q_D(const QQuickTranslate);
124     matrix->translate(d->x, d->y, 0);
125 }
126
127 class QQuickScalePrivate : public QQuickTransformPrivate
128 {
129 public:
130     QQuickScalePrivate()
131         : xScale(1), yScale(1), zScale(1) {}
132     QVector3D origin;
133     qreal xScale;
134     qreal yScale;
135     qreal zScale;
136 };
137
138 QQuickScale::QQuickScale(QObject *parent)
139     : QQuickTransform(*new QQuickScalePrivate, parent)
140 {
141 }
142
143 QQuickScale::~QQuickScale()
144 {
145 }
146
147 QVector3D QQuickScale::origin() const
148 {
149     Q_D(const QQuickScale);
150     return d->origin;
151 }
152 void QQuickScale::setOrigin(const QVector3D &point)
153 {
154     Q_D(QQuickScale);
155     if (d->origin == point)
156         return;
157     d->origin = point;
158     update();
159     emit originChanged();
160 }
161
162 qreal QQuickScale::xScale() const
163 {
164     Q_D(const QQuickScale);
165     return d->xScale;
166 }
167 void QQuickScale::setXScale(qreal scale)
168 {
169     Q_D(QQuickScale);
170     if (d->xScale == scale)
171         return;
172     d->xScale = scale;
173     update();
174     emit xScaleChanged();
175     emit scaleChanged();
176 }
177
178 qreal QQuickScale::yScale() const
179 {
180     Q_D(const QQuickScale);
181     return d->yScale;
182 }
183 void QQuickScale::setYScale(qreal scale)
184 {
185     Q_D(QQuickScale);
186     if (d->yScale == scale)
187         return;
188     d->yScale = scale;
189     update();
190     emit yScaleChanged();
191     emit scaleChanged();
192 }
193
194 qreal QQuickScale::zScale() const
195 {
196     Q_D(const QQuickScale);
197     return d->zScale;
198 }
199 void QQuickScale::setZScale(qreal scale)
200 {
201     Q_D(QQuickScale);
202     if (d->zScale == scale)
203         return;
204     d->zScale = scale;
205     update();
206     emit zScaleChanged();
207     emit scaleChanged();
208 }
209
210 void QQuickScale::applyTo(QMatrix4x4 *matrix) const
211 {
212     Q_D(const QQuickScale);
213     matrix->translate(d->origin);
214     matrix->scale(d->xScale, d->yScale, d->zScale);
215     matrix->translate(-d->origin);
216 }
217
218 class QQuickRotationPrivate : public QQuickTransformPrivate
219 {
220 public:
221     QQuickRotationPrivate()
222         : angle(0), axis(0, 0, 1) {}
223     QVector3D origin;
224     qreal angle;
225     QVector3D axis;
226 };
227
228 QQuickRotation::QQuickRotation(QObject *parent)
229     : QQuickTransform(*new QQuickRotationPrivate, parent)
230 {
231 }
232
233 QQuickRotation::~QQuickRotation()
234 {
235 }
236
237 QVector3D QQuickRotation::origin() const
238 {
239     Q_D(const QQuickRotation);
240     return d->origin;
241 }
242
243 void QQuickRotation::setOrigin(const QVector3D &point)
244 {
245     Q_D(QQuickRotation);
246     if (d->origin == point)
247         return;
248     d->origin = point;
249     update();
250     emit originChanged();
251 }
252
253 qreal QQuickRotation::angle() const
254 {
255     Q_D(const QQuickRotation);
256     return d->angle;
257 }
258 void QQuickRotation::setAngle(qreal angle)
259 {
260     Q_D(QQuickRotation);
261     if (d->angle == angle)
262         return;
263     d->angle = angle;
264     update();
265     emit angleChanged();
266 }
267
268 QVector3D QQuickRotation::axis() const
269 {
270     Q_D(const QQuickRotation);
271     return d->axis;
272 }
273 void QQuickRotation::setAxis(const QVector3D &axis)
274 {
275     Q_D(QQuickRotation);
276     if (d->axis == axis)
277          return;
278     d->axis = axis;
279     update();
280     emit axisChanged();
281 }
282
283 void QQuickRotation::setAxis(Qt::Axis axis)
284 {
285     switch (axis)
286     {
287     case Qt::XAxis:
288         setAxis(QVector3D(1, 0, 0));
289         break;
290     case Qt::YAxis:
291         setAxis(QVector3D(0, 1, 0));
292         break;
293     case Qt::ZAxis:
294         setAxis(QVector3D(0, 0, 1));
295         break;
296     }
297 }
298
299 class QGraphicsRotation {
300 public:
301     static inline void projectedRotate(QMatrix4x4 *matrix, qreal angle, qreal x, qreal y, qreal z)
302     {
303         matrix->projectedRotate(angle, x, y, z);
304     }
305 };
306
307 void QQuickRotation::applyTo(QMatrix4x4 *matrix) const
308 {
309     Q_D(const QQuickRotation);
310
311     if (d->angle == 0. || d->axis.isNull())
312         return;
313
314     matrix->translate(d->origin);
315     QGraphicsRotation::projectedRotate(matrix, d->angle, d->axis.x(), d->axis.y(), d->axis.z());
316     matrix->translate(-d->origin);
317 }
318
319 QT_END_NAMESPACE