Add missing QT_{BEGIN,END}_NAMESPACE
[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     static bool isValueType(int);
165     static QQmlValueType *valueType(int idx);
166
167     static void registerValueTypes(const char *uri, int versionMajor, int versionMinor);
168 };
169
170 class Q_QML_PRIVATE_EXPORT QQmlPointFValueType : public QQmlValueTypeBase<QPointF>
171 {
172     Q_PROPERTY(qreal x READ x WRITE setX)
173     Q_PROPERTY(qreal y READ y WRITE setY)
174     Q_OBJECT
175 public:
176     QQmlPointFValueType(QObject *parent = 0);
177
178     virtual QString toString() const;
179
180     qreal x() const;
181     qreal y() const;
182     void setX(qreal);
183     void setY(qreal);
184 };
185
186 class Q_QML_PRIVATE_EXPORT QQmlPointValueType : public QQmlValueTypeBase<QPoint>
187 {
188     Q_PROPERTY(int x READ x WRITE setX)
189     Q_PROPERTY(int y READ y WRITE setY)
190     Q_OBJECT
191 public:
192     QQmlPointValueType(QObject *parent = 0);
193
194     virtual QString toString() const;
195
196     int x() const;
197     int y() const;
198     void setX(int);
199     void setY(int);
200 };
201
202 class Q_QML_PRIVATE_EXPORT QQmlSizeFValueType : public QQmlValueTypeBase<QSizeF>
203 {
204     Q_PROPERTY(qreal width READ width WRITE setWidth)
205     Q_PROPERTY(qreal height READ height WRITE setHeight)
206     Q_OBJECT
207 public:
208     QQmlSizeFValueType(QObject *parent = 0);
209
210     virtual QString toString() const;
211
212     qreal width() const;
213     qreal height() const;
214     void setWidth(qreal);
215     void setHeight(qreal);
216 };
217
218 class Q_QML_PRIVATE_EXPORT QQmlSizeValueType : public QQmlValueTypeBase<QSize>
219 {
220     Q_PROPERTY(int width READ width WRITE setWidth)
221     Q_PROPERTY(int height READ height WRITE setHeight)
222     Q_OBJECT
223 public:
224     QQmlSizeValueType(QObject *parent = 0);
225
226     virtual QString toString() const;
227
228     int width() const;
229     int height() const;
230     void setWidth(int);
231     void setHeight(int);
232 };
233
234 class Q_QML_PRIVATE_EXPORT QQmlRectFValueType : public QQmlValueTypeBase<QRectF>
235 {
236     Q_PROPERTY(qreal x READ x WRITE setX)
237     Q_PROPERTY(qreal y READ y WRITE setY)
238     Q_PROPERTY(qreal width READ width WRITE setWidth)
239     Q_PROPERTY(qreal height READ height WRITE setHeight)
240     Q_OBJECT
241 public:
242     QQmlRectFValueType(QObject *parent = 0);
243
244     virtual QString toString() const;
245
246     qreal x() const;
247     qreal y() const;
248     void setX(qreal);
249     void setY(qreal);
250     
251     qreal width() const;
252     qreal height() const;
253     void setWidth(qreal);
254     void setHeight(qreal);
255 };
256
257 class Q_QML_PRIVATE_EXPORT QQmlRectValueType : public QQmlValueTypeBase<QRect>
258 {
259     Q_PROPERTY(int x READ x WRITE setX)
260     Q_PROPERTY(int y READ y WRITE setY)
261     Q_PROPERTY(int width READ width WRITE setWidth)
262     Q_PROPERTY(int height READ height WRITE setHeight)
263     Q_OBJECT
264 public:
265     QQmlRectValueType(QObject *parent = 0);
266
267     virtual QString toString() const;
268
269     int x() const;
270     int y() const;
271     void setX(int);
272     void setY(int);
273     
274     int width() const;
275     int height() const;
276     void setWidth(int);
277     void setHeight(int);
278 };
279
280 class Q_QML_PRIVATE_EXPORT QQmlEasingValueType : public QQmlValueTypeBase<QEasingCurve>
281 {
282     Q_OBJECT
283     Q_ENUMS(Type)
284
285     Q_PROPERTY(QQmlEasingValueType::Type type READ type WRITE setType)
286     Q_PROPERTY(qreal amplitude READ amplitude WRITE setAmplitude)
287     Q_PROPERTY(qreal overshoot READ overshoot WRITE setOvershoot)
288     Q_PROPERTY(qreal period READ period WRITE setPeriod)
289     Q_PROPERTY(QVariantList bezierCurve READ bezierCurve WRITE setBezierCurve)
290 public:
291     enum Type {
292         Linear = QEasingCurve::Linear,
293         InQuad = QEasingCurve::InQuad, OutQuad = QEasingCurve::OutQuad,
294         InOutQuad = QEasingCurve::InOutQuad, OutInQuad = QEasingCurve::OutInQuad,
295         InCubic = QEasingCurve::InCubic, OutCubic = QEasingCurve::OutCubic,
296         InOutCubic = QEasingCurve::InOutCubic, OutInCubic = QEasingCurve::OutInCubic,
297         InQuart = QEasingCurve::InQuart, OutQuart = QEasingCurve::OutQuart,
298         InOutQuart = QEasingCurve::InOutQuart, OutInQuart = QEasingCurve::OutInQuart,
299         InQuint = QEasingCurve::InQuint, OutQuint = QEasingCurve::OutQuint,
300         InOutQuint = QEasingCurve::InOutQuint, OutInQuint = QEasingCurve::OutInQuint,
301         InSine = QEasingCurve::InSine, OutSine = QEasingCurve::OutSine,
302         InOutSine = QEasingCurve::InOutSine, OutInSine = QEasingCurve::OutInSine,
303         InExpo = QEasingCurve::InExpo, OutExpo = QEasingCurve::OutExpo,
304         InOutExpo = QEasingCurve::InOutExpo, OutInExpo = QEasingCurve::OutInExpo,
305         InCirc = QEasingCurve::InCirc, OutCirc = QEasingCurve::OutCirc,
306         InOutCirc = QEasingCurve::InOutCirc, OutInCirc = QEasingCurve::OutInCirc,
307         InElastic = QEasingCurve::InElastic, OutElastic = QEasingCurve::OutElastic,
308         InOutElastic = QEasingCurve::InOutElastic, OutInElastic = QEasingCurve::OutInElastic,
309         InBack = QEasingCurve::InBack, OutBack = QEasingCurve::OutBack,
310         InOutBack = QEasingCurve::InOutBack, OutInBack = QEasingCurve::OutInBack,
311         InBounce = QEasingCurve::InBounce, OutBounce = QEasingCurve::OutBounce,
312         InOutBounce = QEasingCurve::InOutBounce, OutInBounce = QEasingCurve::OutInBounce,
313         InCurve = QEasingCurve::InCurve, OutCurve = QEasingCurve::OutCurve,
314         SineCurve = QEasingCurve::SineCurve, CosineCurve = QEasingCurve::CosineCurve,
315         Bezier = QEasingCurve::BezierSpline
316     };
317
318     QQmlEasingValueType(QObject *parent = 0);
319
320     virtual QString toString() const;
321
322     Type type() const;
323     qreal amplitude() const;
324     qreal overshoot() const;
325     qreal period() const;
326     void setType(Type);
327     void setAmplitude(qreal);
328     void setOvershoot(qreal);
329     void setPeriod(qreal);
330     void setBezierCurve(const QVariantList &);
331     QVariantList bezierCurve() const;
332 };
333
334 template<typename T>
335 int qmlRegisterValueTypeEnums(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
336 {
337     QByteArray name(T::staticMetaObject.className());
338
339     QByteArray pointerName(name + '*');
340
341     QQmlPrivate::RegisterType type = {
342         0,
343
344         qRegisterNormalizedMetaType<T *>(pointerName.constData()), 0, 0, 0,
345
346         QString(),
347
348         uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
349
350         0, 0,
351
352         0, 0, 0,
353
354         0, 0,
355
356         0,
357         0
358     };
359
360     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
361 }
362
363 QT_END_NAMESPACE
364
365 #endif  // QQMLVALUETYPE_P_H