Implement strict mode for qmldir modules
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlvaluetype_p.h
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 #ifndef QQMLVALUETYPE_P_H
43 #define QQMLVALUETYPE_P_H
44
45 //
46 //  W A R N I N G
47 //  -------------
48 //
49 // This file is not part of the Qt API.  It exists purely as an
50 // implementation detail.  This header file may change from version to
51 // version without notice, or even be removed.
52 //
53 // We mean it.
54 //
55
56 #include "qqml.h"
57 #include "qqmlproperty.h"
58 #include "qqmlproperty_p.h"
59 #include "qqmlnullablevalue_p_p.h"
60
61 #include <QtCore/qobject.h>
62 #include <QtCore/qrect.h>
63 #include <QtCore/qeasingcurve.h>
64 #include <QtCore/qvariant.h>
65
66 QT_BEGIN_NAMESPACE
67
68 class Q_QML_PRIVATE_EXPORT QQmlValueType : public QObject
69 {
70     Q_OBJECT
71 public:
72     QQmlValueType(int userType, QObject *parent = 0);
73     virtual void read(QObject *, int) = 0;
74     virtual void readVariantValue(QObject *, int, QVariant *) = 0;
75     virtual void write(QObject *, int, QQmlPropertyPrivate::WriteFlags flags) = 0;
76     virtual void writeVariantValue(QObject *, int, QQmlPropertyPrivate::WriteFlags, QVariant *) = 0;
77     virtual QVariant value() = 0;
78     virtual void setValue(const QVariant &) = 0;
79
80     virtual QString toString() const = 0;
81     virtual bool isEqual(const QVariant &value) const = 0;
82
83     virtual void onLoad() {}
84
85     inline int userType() const
86     {
87         return m_userType;
88     }
89
90 protected:
91     inline void readProperty(QObject *obj, int idx, void *p)
92     {
93         void *a[] = { p, 0 };
94         QMetaObject::metacall(obj, QMetaObject::ReadProperty, idx, a);
95         onLoad();
96     }
97
98     inline void writeProperty(QObject *obj, int idx, QQmlPropertyPrivate::WriteFlags flags, void *p)
99     {
100         int status = -1;
101         void *a[] = { p, 0, &status, &flags };
102         QMetaObject::metacall(obj, QMetaObject::WriteProperty, idx, a);
103     }
104
105 private:
106     int m_userType;
107 };
108
109 template <typename T>
110 class QQmlValueTypeBase : public QQmlValueType
111 {
112 public:
113     typedef T ValueType;
114
115     QQmlValueTypeBase(int userType, QObject *parent)
116         : QQmlValueType(userType, parent)
117     {
118     }
119
120     virtual void read(QObject *obj, int idx)
121     {
122         readProperty(obj, idx, &v);
123     }
124
125     virtual void readVariantValue(QObject *obj, int idx, QVariant *into)
126     {
127         // important: must not change the userType of the variant
128         readProperty(obj, idx, into);
129     }
130
131     virtual void write(QObject *obj, int idx, QQmlPropertyPrivate::WriteFlags flags)
132     {
133         writeProperty(obj, idx, flags, &v);
134     }
135
136     virtual void writeVariantValue(QObject *obj, int idx, QQmlPropertyPrivate::WriteFlags flags, QVariant *from)
137     {
138         writeProperty(obj, idx, flags, from);
139     }
140
141     virtual QVariant value()
142     {
143         return QVariant::fromValue(v);
144     }
145
146     virtual void setValue(const QVariant &value)
147     {
148         v = qvariant_cast<T>(value);
149         onLoad();
150     }
151
152     virtual bool isEqual(const QVariant &other) const
153     {
154         return QVariant::fromValue(v) == other;
155     }
156
157 protected:
158     ValueType v;
159 };
160
161 class Q_QML_PRIVATE_EXPORT QQmlValueTypeFactory
162 {
163 public:
164     QQmlValueTypeFactory();
165     ~QQmlValueTypeFactory();
166     static bool isValueType(int);
167     static QQmlValueType *valueType(int);
168
169     static void registerValueTypes(const char *uri, int versionMajor, int versionMinor);
170
171     QQmlValueType *operator[](int idx) const {
172         if (idx >= (int)QVariant::UserType)
173             return 0;
174
175         QQmlValueType *rv = valueTypes[idx];
176         if (!rv) {
177             // Table update is not thread-safe, but the potential for leaks is
178             // so small that the cost of protection is unwarranted
179             if ((rv = valueType(idx))) {
180                 valueTypes[idx] = rv;
181             }
182         }
183         return rv;
184     }
185
186 private:
187     mutable QQmlValueType *valueTypes[QVariant::UserType];
188 };
189
190 class Q_QML_PRIVATE_EXPORT QQmlPointFValueType : public QQmlValueTypeBase<QPointF>
191 {
192     Q_PROPERTY(qreal x READ x WRITE setX)
193     Q_PROPERTY(qreal y READ y WRITE setY)
194     Q_OBJECT
195 public:
196     QQmlPointFValueType(QObject *parent = 0);
197
198     virtual QString toString() const;
199
200     qreal x() const;
201     qreal y() const;
202     void setX(qreal);
203     void setY(qreal);
204 };
205
206 class Q_QML_PRIVATE_EXPORT QQmlPointValueType : public QQmlValueTypeBase<QPoint>
207 {
208     Q_PROPERTY(int x READ x WRITE setX)
209     Q_PROPERTY(int y READ y WRITE setY)
210     Q_OBJECT
211 public:
212     QQmlPointValueType(QObject *parent = 0);
213
214     virtual QString toString() const;
215
216     int x() const;
217     int y() const;
218     void setX(int);
219     void setY(int);
220 };
221
222 class Q_QML_PRIVATE_EXPORT QQmlSizeFValueType : public QQmlValueTypeBase<QSizeF>
223 {
224     Q_PROPERTY(qreal width READ width WRITE setWidth)
225     Q_PROPERTY(qreal height READ height WRITE setHeight)
226     Q_OBJECT
227 public:
228     QQmlSizeFValueType(QObject *parent = 0);
229
230     virtual QString toString() const;
231
232     qreal width() const;
233     qreal height() const;
234     void setWidth(qreal);
235     void setHeight(qreal);
236 };
237
238 class Q_QML_PRIVATE_EXPORT QQmlSizeValueType : public QQmlValueTypeBase<QSize>
239 {
240     Q_PROPERTY(int width READ width WRITE setWidth)
241     Q_PROPERTY(int height READ height WRITE setHeight)
242     Q_OBJECT
243 public:
244     QQmlSizeValueType(QObject *parent = 0);
245
246     virtual QString toString() const;
247
248     int width() const;
249     int height() const;
250     void setWidth(int);
251     void setHeight(int);
252 };
253
254 class Q_QML_PRIVATE_EXPORT QQmlRectFValueType : public QQmlValueTypeBase<QRectF>
255 {
256     Q_PROPERTY(qreal x READ x WRITE setX)
257     Q_PROPERTY(qreal y READ y WRITE setY)
258     Q_PROPERTY(qreal width READ width WRITE setWidth)
259     Q_PROPERTY(qreal height READ height WRITE setHeight)
260     Q_OBJECT
261 public:
262     QQmlRectFValueType(QObject *parent = 0);
263
264     virtual QString toString() const;
265
266     qreal x() const;
267     qreal y() const;
268     void setX(qreal);
269     void setY(qreal);
270     
271     qreal width() const;
272     qreal height() const;
273     void setWidth(qreal);
274     void setHeight(qreal);
275 };
276
277 class Q_QML_PRIVATE_EXPORT QQmlRectValueType : public QQmlValueTypeBase<QRect>
278 {
279     Q_PROPERTY(int x READ x WRITE setX)
280     Q_PROPERTY(int y READ y WRITE setY)
281     Q_PROPERTY(int width READ width WRITE setWidth)
282     Q_PROPERTY(int height READ height WRITE setHeight)
283     Q_OBJECT
284 public:
285     QQmlRectValueType(QObject *parent = 0);
286
287     virtual QString toString() const;
288
289     int x() const;
290     int y() const;
291     void setX(int);
292     void setY(int);
293     
294     int width() const;
295     int height() const;
296     void setWidth(int);
297     void setHeight(int);
298 };
299
300 class Q_QML_PRIVATE_EXPORT QQmlEasingValueType : public QQmlValueTypeBase<QEasingCurve>
301 {
302     Q_OBJECT
303     Q_ENUMS(Type)
304
305     Q_PROPERTY(QQmlEasingValueType::Type type READ type WRITE setType)
306     Q_PROPERTY(qreal amplitude READ amplitude WRITE setAmplitude)
307     Q_PROPERTY(qreal overshoot READ overshoot WRITE setOvershoot)
308     Q_PROPERTY(qreal period READ period WRITE setPeriod)
309     Q_PROPERTY(QVariantList bezierCurve READ bezierCurve WRITE setBezierCurve)
310 public:
311     enum Type {
312         Linear = QEasingCurve::Linear,
313         InQuad = QEasingCurve::InQuad, OutQuad = QEasingCurve::OutQuad,
314         InOutQuad = QEasingCurve::InOutQuad, OutInQuad = QEasingCurve::OutInQuad,
315         InCubic = QEasingCurve::InCubic, OutCubic = QEasingCurve::OutCubic,
316         InOutCubic = QEasingCurve::InOutCubic, OutInCubic = QEasingCurve::OutInCubic,
317         InQuart = QEasingCurve::InQuart, OutQuart = QEasingCurve::OutQuart,
318         InOutQuart = QEasingCurve::InOutQuart, OutInQuart = QEasingCurve::OutInQuart,
319         InQuint = QEasingCurve::InQuint, OutQuint = QEasingCurve::OutQuint,
320         InOutQuint = QEasingCurve::InOutQuint, OutInQuint = QEasingCurve::OutInQuint,
321         InSine = QEasingCurve::InSine, OutSine = QEasingCurve::OutSine,
322         InOutSine = QEasingCurve::InOutSine, OutInSine = QEasingCurve::OutInSine,
323         InExpo = QEasingCurve::InExpo, OutExpo = QEasingCurve::OutExpo,
324         InOutExpo = QEasingCurve::InOutExpo, OutInExpo = QEasingCurve::OutInExpo,
325         InCirc = QEasingCurve::InCirc, OutCirc = QEasingCurve::OutCirc,
326         InOutCirc = QEasingCurve::InOutCirc, OutInCirc = QEasingCurve::OutInCirc,
327         InElastic = QEasingCurve::InElastic, OutElastic = QEasingCurve::OutElastic,
328         InOutElastic = QEasingCurve::InOutElastic, OutInElastic = QEasingCurve::OutInElastic,
329         InBack = QEasingCurve::InBack, OutBack = QEasingCurve::OutBack,
330         InOutBack = QEasingCurve::InOutBack, OutInBack = QEasingCurve::OutInBack,
331         InBounce = QEasingCurve::InBounce, OutBounce = QEasingCurve::OutBounce,
332         InOutBounce = QEasingCurve::InOutBounce, OutInBounce = QEasingCurve::OutInBounce,
333         InCurve = QEasingCurve::InCurve, OutCurve = QEasingCurve::OutCurve,
334         SineCurve = QEasingCurve::SineCurve, CosineCurve = QEasingCurve::CosineCurve,
335         Bezier = QEasingCurve::BezierSpline
336     };
337
338     QQmlEasingValueType(QObject *parent = 0);
339
340     virtual QString toString() const;
341
342     Type type() const;
343     qreal amplitude() const;
344     qreal overshoot() const;
345     qreal period() const;
346     void setType(Type);
347     void setAmplitude(qreal);
348     void setOvershoot(qreal);
349     void setPeriod(qreal);
350     void setBezierCurve(const QVariantList &);
351     QVariantList bezierCurve() const;
352 };
353
354 template<typename T>
355 int qmlRegisterValueTypeEnums(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
356 {
357     QByteArray name(T::staticMetaObject.className());
358
359     QByteArray pointerName(name + '*');
360
361     QQmlPrivate::RegisterType type = {
362         0,
363
364         qRegisterNormalizedMetaType<T *>(pointerName.constData()), 0, 0, 0,
365
366         QString(),
367
368         uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
369
370         0, 0,
371
372         0, 0, 0,
373
374         0, 0,
375
376         0,
377         0
378     };
379
380     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
381 }
382
383 QT_END_NAMESPACE
384
385 #endif  // QQMLVALUETYPE_P_H