Implement strict mode for qmldir modules
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlvaluetype.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 QtQml 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 "qqmlvaluetype_p.h"
43 #include "qqmlmetatype_p.h"
44
45 #include <private/qqmlglobal_p.h>
46 #include <QtCore/qdebug.h>
47
48 QT_BEGIN_NAMESPACE
49
50 QQmlValueTypeFactory::QQmlValueTypeFactory()
51 {
52     for (unsigned int ii = 0; ii < QVariant::UserType; ++ii)
53         valueTypes[ii] = 0;
54 }
55
56 QQmlValueTypeFactory::~QQmlValueTypeFactory()
57 {
58     for (unsigned int ii = 0; ii < QVariant::UserType; ++ii)
59         delete valueTypes[ii];
60 }
61
62 bool QQmlValueTypeFactory::isValueType(int idx)
63 {
64     if ((uint)idx < QVariant::UserType
65             && idx != QVariant::StringList
66             && idx != QMetaType::QObjectStar
67             && idx != QMetaType::QWidgetStar
68             && idx != QMetaType::VoidStar
69             && idx != QMetaType::QVariant) {
70         return true;
71     }
72     return false;
73 }
74
75 void QQmlValueTypeFactory::registerValueTypes(const char *uri, int versionMajor, int versionMinor)
76 {
77     qmlRegisterValueTypeEnums<QQmlEasingValueType>(uri, versionMajor, versionMinor, "Easing");
78 }
79
80 QQmlValueType *QQmlValueTypeFactory::valueType(int t)
81 {
82     QQmlValueType *rv = 0;
83
84     switch (t) {
85     case QVariant::Point:
86         rv = new QQmlPointValueType;
87         break;
88     case QVariant::PointF:
89         rv = new QQmlPointFValueType;
90         break;
91     case QVariant::Size:
92         rv = new QQmlSizeValueType;
93         break;
94     case QVariant::SizeF:
95         rv = new QQmlSizeFValueType;
96         break;
97     case QVariant::Rect:
98         rv = new QQmlRectValueType;
99         break;
100     case QVariant::RectF:
101         rv = new QQmlRectFValueType;
102         break;
103     case QVariant::EasingCurve:
104         rv = new QQmlEasingValueType;
105         break;
106     default:
107         rv = QQml_valueTypeProvider()->createValueType(t);
108         break;
109     }
110
111     Q_ASSERT(!rv || rv->metaObject()->propertyCount() < 32);
112     return rv;
113 }
114
115 QQmlValueType::QQmlValueType(int userType, QObject *parent)
116 : QObject(parent), m_userType(userType)
117 {
118 }
119
120
121 QQmlPointFValueType::QQmlPointFValueType(QObject *parent)
122     : QQmlValueTypeBase<QPointF>(QMetaType::QPointF, parent)
123 {
124 }
125
126 QString QQmlPointFValueType::toString() const
127 {
128     return QString(QLatin1String("QPointF(%1, %2)")).arg(v.x()).arg(v.y());
129 }
130
131 qreal QQmlPointFValueType::x() const
132 {
133     return v.x();
134 }
135
136 qreal QQmlPointFValueType::y() const
137 {
138     return v.y();
139 }
140
141 void QQmlPointFValueType::setX(qreal x)
142 {
143     v.setX(x);
144 }
145
146 void QQmlPointFValueType::setY(qreal y)
147 {
148     v.setY(y);
149 }
150
151
152 QQmlPointValueType::QQmlPointValueType(QObject *parent)
153     : QQmlValueTypeBase<QPoint>(QMetaType::QPoint, parent)
154 {
155 }
156
157 QString QQmlPointValueType::toString() const
158 {
159     return QString(QLatin1String("QPoint(%1, %2)")).arg(v.x()).arg(v.y());
160 }
161
162 int QQmlPointValueType::x() const
163 {
164     return v.x();
165 }
166
167 int QQmlPointValueType::y() const
168 {
169     return v.y();
170 }
171
172 void QQmlPointValueType::setX(int x)
173 {
174     v.setX(x);
175 }
176
177 void QQmlPointValueType::setY(int y)
178 {
179     v.setY(y);
180 }
181
182
183 QQmlSizeFValueType::QQmlSizeFValueType(QObject *parent)
184     : QQmlValueTypeBase<QSizeF>(QMetaType::QSizeF, parent)
185 {
186 }
187
188 QString QQmlSizeFValueType::toString() const
189 {
190     return QString(QLatin1String("QSizeF(%1, %2)")).arg(v.width()).arg(v.height());
191 }
192
193 qreal QQmlSizeFValueType::width() const
194 {
195     return v.width();
196 }
197
198 qreal QQmlSizeFValueType::height() const
199 {
200     return v.height();
201 }
202
203 void QQmlSizeFValueType::setWidth(qreal w)
204 {
205     v.setWidth(w);
206 }
207
208 void QQmlSizeFValueType::setHeight(qreal h)
209 {
210     v.setHeight(h);
211 }
212
213
214 QQmlSizeValueType::QQmlSizeValueType(QObject *parent)
215     : QQmlValueTypeBase<QSize>(QMetaType::QSize, parent)
216 {
217 }
218
219 QString QQmlSizeValueType::toString() const
220 {
221     return QString(QLatin1String("QSize(%1, %2)")).arg(v.width()).arg(v.height());
222 }
223
224 int QQmlSizeValueType::width() const
225 {
226     return v.width();
227 }
228
229 int QQmlSizeValueType::height() const
230 {
231     return v.height();
232 }
233
234 void QQmlSizeValueType::setWidth(int w)
235 {
236     v.setWidth(w);
237 }
238
239 void QQmlSizeValueType::setHeight(int h)
240 {
241     v.setHeight(h);
242 }
243
244
245 QQmlRectFValueType::QQmlRectFValueType(QObject *parent)
246     : QQmlValueTypeBase<QRectF>(QMetaType::QRectF, parent)
247 {
248 }
249
250 QString QQmlRectFValueType::toString() const
251 {
252     return QString(QLatin1String("QRectF(%1, %2, %3, %4)")).arg(v.x()).arg(v.y()).arg(v.width()).arg(v.height());
253 }
254
255 qreal QQmlRectFValueType::x() const
256 {
257     return v.x();
258 }
259
260 qreal QQmlRectFValueType::y() const
261 {
262     return v.y();
263 }
264
265 void QQmlRectFValueType::setX(qreal x)
266 {
267     v.moveLeft(x);
268 }
269
270 void QQmlRectFValueType::setY(qreal y)
271 {
272     v.moveTop(y);
273 }
274
275 qreal QQmlRectFValueType::width() const
276 {
277     return v.width();
278 }
279
280 qreal QQmlRectFValueType::height() const
281 {
282     return v.height();
283 }
284
285 void QQmlRectFValueType::setWidth(qreal w)
286 {
287     v.setWidth(w);
288 }
289
290 void QQmlRectFValueType::setHeight(qreal h)
291 {
292     v.setHeight(h);
293 }
294
295
296 QQmlRectValueType::QQmlRectValueType(QObject *parent)
297     : QQmlValueTypeBase<QRect>(QMetaType::QRect, parent)
298 {
299 }
300
301 QString QQmlRectValueType::toString() const
302 {
303     return QString(QLatin1String("QRect(%1, %2, %3, %4)")).arg(v.x()).arg(v.y()).arg(v.width()).arg(v.height());
304 }
305
306 int QQmlRectValueType::x() const
307 {
308     return v.x();
309 }
310
311 int QQmlRectValueType::y() const
312 {
313     return v.y();
314 }
315
316 void QQmlRectValueType::setX(int x)
317 {
318     v.moveLeft(x);
319 }
320
321 void QQmlRectValueType::setY(int y)
322 {
323     v.moveTop(y);
324 }
325
326 int QQmlRectValueType::width() const
327 {
328     return v.width();
329 }
330
331 int QQmlRectValueType::height() const
332 {
333     return v.height();
334 }
335
336 void QQmlRectValueType::setWidth(int w)
337 {
338     v.setWidth(w);
339 }
340
341 void QQmlRectValueType::setHeight(int h)
342 {
343     v.setHeight(h);
344 }
345
346
347 QQmlEasingValueType::QQmlEasingValueType(QObject *parent)
348     : QQmlValueTypeBase<QEasingCurve>(QMetaType::QEasingCurve, parent)
349 {
350 }
351
352 QString QQmlEasingValueType::toString() const
353 {
354     return QString(QLatin1String("QEasingCurve(%1, %2, %3, %4)")).arg(v.type()).arg(v.amplitude()).arg(v.overshoot()).arg(v.period());
355 }
356
357 QQmlEasingValueType::Type QQmlEasingValueType::type() const
358 {
359     return (QQmlEasingValueType::Type)v.type();
360 }
361
362 qreal QQmlEasingValueType::amplitude() const
363 {
364     return v.amplitude();
365 }
366
367 qreal QQmlEasingValueType::overshoot() const
368 {
369     return v.overshoot();
370 }
371
372 qreal QQmlEasingValueType::period() const
373 {
374     return v.period();
375 }
376
377 void QQmlEasingValueType::setType(QQmlEasingValueType::Type type)
378 {
379     v.setType((QEasingCurve::Type)type);
380 }
381
382 void QQmlEasingValueType::setAmplitude(qreal amplitude)
383 {
384     v.setAmplitude(amplitude);
385 }
386
387 void QQmlEasingValueType::setOvershoot(qreal overshoot)
388 {
389     v.setOvershoot(overshoot);
390 }
391
392 void QQmlEasingValueType::setPeriod(qreal period)
393 {
394     v.setPeriod(period);
395 }
396
397 void QQmlEasingValueType::setBezierCurve(const QVariantList &customCurveVariant)
398 {
399     if (customCurveVariant.isEmpty())
400         return;
401
402     QVariantList variantList = customCurveVariant;
403     if ((variantList.count() % 6) == 0) {
404         bool allRealsOk = true;
405         QList<qreal> reals;
406         for (int i = 0; i < variantList.count(); i++) {
407             bool ok;
408             const qreal real = variantList.at(i).toReal(&ok);
409             reals.append(real);
410             if (!ok)
411                 allRealsOk = false;
412         }
413         if (allRealsOk) {
414             QEasingCurve newEasingCurve(QEasingCurve::BezierSpline);
415             for (int i = 0; i < reals.count() / 6; i++) {
416                 const qreal c1x = reals.at(i * 6);
417                 const qreal c1y = reals.at(i * 6 + 1);
418                 const qreal c2x = reals.at(i * 6 + 2);
419                 const qreal c2y = reals.at(i * 6 + 3);
420                 const qreal c3x = reals.at(i * 6 + 4);
421                 const qreal c3y = reals.at(i * 6 + 5);
422
423                 const QPointF c1(c1x, c1y);
424                 const QPointF c2(c2x, c2y);
425                 const QPointF c3(c3x, c3y);
426
427                 newEasingCurve.addCubicBezierSegment(c1, c2, c3);
428                 v = newEasingCurve;
429             }
430         }
431     }
432 }
433
434 QVariantList QQmlEasingValueType::bezierCurve() const
435 {
436     QVariantList rv;
437     QVector<QPointF> points = v.toCubicSpline();
438     for (int ii = 0; ii < points.count(); ++ii)
439         rv << QVariant(points.at(ii).x()) << QVariant(points.at(ii).y());
440     return rv;
441 }
442
443 QT_END_NAMESPACE