Initial import from qtquick2.
[profile/ivi/qtdeclarative.git] / src / declarative / items / qsgtranslate.cpp
1 // Commit: ac5c099cc3c5b8c7eec7a49fdeb8a21037230350
2 /****************************************************************************
3 **
4 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
5 ** All rights reserved.
6 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 **
8 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 **
10 ** $QT_BEGIN_LICENSE:LGPL$
11 ** No Commercial Usage
12 ** This file contains pre-release code and may not be distributed.
13 ** You may use this file in accordance with the terms and conditions
14 ** contained in the Technology Preview License Agreement accompanying
15 ** this package.
16 **
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file.  Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
24 **
25 ** In addition, as a special exception, Nokia gives you certain additional
26 ** rights.  These rights are described in the Nokia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
28 **
29 ** If you have questions regarding the use of this file, please contact
30 ** Nokia at qt-info@nokia.com.
31 **
32 **
33 **
34 **
35 **
36 **
37 **
38 **
39 ** $QT_END_LICENSE$
40 **
41 ****************************************************************************/
42
43 #include "qsgtranslate_p.h"
44 #include "qsgitem_p.h"
45
46 #include <QtCore/qmath.h>
47
48 QT_BEGIN_NAMESPACE
49
50 class QSGTranslatePrivate : public QSGTransformPrivate
51 {
52 public:
53     QSGTranslatePrivate()
54     : x(0), y(0) {}
55
56     qreal x;
57     qreal y;
58 };
59
60 QSGTranslate::QSGTranslate(QObject *parent)
61 : QSGTransform(*new QSGTranslatePrivate, parent)
62 {
63 }
64
65 QSGTranslate::~QSGTranslate()
66 {
67 }
68
69 qreal QSGTranslate::x() const
70 {
71     Q_D(const QSGTranslate);
72     return d->x;
73 }
74
75 void QSGTranslate::setX(qreal x)
76 {
77     Q_D(QSGTranslate);
78     if (d->x == x)
79         return;
80     d->x = x;
81     update();
82     emit xChanged();
83 }
84
85 qreal QSGTranslate::y() const
86 {
87     Q_D(const QSGTranslate);
88     return d->y;
89 }
90 void QSGTranslate::setY(qreal y)
91 {
92     Q_D(QSGTranslate);
93     if (d->y == y)
94         return;
95     d->y = y;
96     update();
97     emit yChanged();
98 }
99
100 void QSGTranslate::applyTo(QMatrix4x4 *matrix) const
101 {
102     Q_D(const QSGTranslate);
103     matrix->translate(d->x, d->y, 0);
104 }
105
106 class QSGScalePrivate : public QSGTransformPrivate
107 {
108 public:
109     QSGScalePrivate()
110         : xScale(1), yScale(1), zScale(1) {}
111     QVector3D origin;
112     qreal xScale;
113     qreal yScale;
114     qreal zScale;
115 };
116
117 QSGScale::QSGScale(QObject *parent)
118     : QSGTransform(*new QSGScalePrivate, parent)
119 {
120 }
121
122 QSGScale::~QSGScale()
123 {
124 }
125
126 QVector3D QSGScale::origin() const
127 {
128     Q_D(const QSGScale);
129     return d->origin;
130 }
131 void QSGScale::setOrigin(const QVector3D &point)
132 {
133     Q_D(QSGScale);
134     if (d->origin == point)
135         return;
136     d->origin = point;
137     update();
138     emit originChanged();
139 }
140
141 qreal QSGScale::xScale() const
142 {
143     Q_D(const QSGScale);
144     return d->xScale;
145 }
146 void QSGScale::setXScale(qreal scale)
147 {
148     Q_D(QSGScale);
149     if (d->xScale == scale)
150         return;
151     d->xScale = scale;
152     update();
153     emit xScaleChanged();
154     emit scaleChanged();
155 }
156
157 qreal QSGScale::yScale() const
158 {
159     Q_D(const QSGScale);
160     return d->yScale;
161 }
162 void QSGScale::setYScale(qreal scale)
163 {
164     Q_D(QSGScale);
165     if (d->yScale == scale)
166         return;
167     d->yScale = scale;
168     update();
169     emit yScaleChanged();
170     emit scaleChanged();
171 }
172
173 qreal QSGScale::zScale() const
174 {
175     Q_D(const QSGScale);
176     return d->zScale;
177 }
178 void QSGScale::setZScale(qreal scale)
179 {
180     Q_D(QSGScale);
181     if (d->zScale == scale)
182         return;
183     d->zScale = scale;
184     update();
185     emit zScaleChanged();
186     emit scaleChanged();
187 }
188
189 void QSGScale::applyTo(QMatrix4x4 *matrix) const
190 {
191     Q_D(const QSGScale);
192     matrix->translate(d->origin);
193     matrix->scale(d->xScale, d->yScale, d->zScale);
194     matrix->translate(-d->origin);
195 }
196
197 class QSGRotationPrivate : public QSGTransformPrivate
198 {
199 public:
200     QSGRotationPrivate()
201         : angle(0), axis(0, 0, 1) {}
202     QVector3D origin;
203     qreal angle;
204     QVector3D axis;
205 };
206
207 QSGRotation::QSGRotation(QObject *parent)
208     : QSGTransform(*new QSGRotationPrivate, parent)
209 {
210 }
211
212 QSGRotation::~QSGRotation()
213 {
214 }
215
216 QVector3D QSGRotation::origin() const
217 {
218     Q_D(const QSGRotation);
219     return d->origin;
220 }
221
222 void QSGRotation::setOrigin(const QVector3D &point)
223 {
224     Q_D(QSGRotation);
225     if (d->origin == point)
226         return;
227     d->origin = point;
228     update();
229     emit originChanged();
230 }
231
232 qreal QSGRotation::angle() const
233 {
234     Q_D(const QSGRotation);
235     return d->angle;
236 }
237 void QSGRotation::setAngle(qreal angle)
238 {
239     Q_D(QSGRotation);
240     if (d->angle == angle)
241         return;
242     d->angle = angle;
243     update();
244     emit angleChanged();
245 }
246
247 QVector3D QSGRotation::axis() const
248 {
249     Q_D(const QSGRotation);
250     return d->axis;
251 }
252 void QSGRotation::setAxis(const QVector3D &axis)
253 {
254     Q_D(QSGRotation);
255     if (d->axis == axis)
256          return;
257     d->axis = axis;
258     update();
259     emit axisChanged();
260 }
261
262 void QSGRotation::setAxis(Qt::Axis axis)
263 {
264     switch (axis)
265     {
266     case Qt::XAxis:
267         setAxis(QVector3D(1, 0, 0));
268         break;
269     case Qt::YAxis:
270         setAxis(QVector3D(0, 1, 0));
271         break;
272     case Qt::ZAxis:
273         setAxis(QVector3D(0, 0, 1));
274         break;
275     }
276 }
277
278 struct QGraphicsRotation {
279     static inline void projectedRotate(QMatrix4x4 *matrix, qreal angle, qreal x, qreal y, qreal z)
280     {
281         matrix->projectedRotate(angle, x, y, z);
282     }
283 };
284
285 void QSGRotation::applyTo(QMatrix4x4 *matrix) const
286 {
287     Q_D(const QSGRotation);
288
289     if (d->angle == 0. || d->axis.isNull())
290         return;
291
292     matrix->translate(d->origin);
293     QGraphicsRotation::projectedRotate(matrix, d->angle, d->axis.x(), d->axis.y(), d->axis.z());
294     matrix->translate(-d->origin);
295 }
296
297 QT_END_NAMESPACE