1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #ifndef QQMLVALUETYPE_P_H
43 #define QQMLVALUETYPE_P_H
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.
57 #include "qqmlproperty.h"
58 #include "qqmlproperty_p.h"
59 #include "qqmlnullablevalue_p_p.h"
61 #include <QtCore/qobject.h>
62 #include <QtCore/qrect.h>
63 #include <QtCore/qeasingcurve.h>
64 #include <QtCore/qvariant.h>
68 class Q_QML_PRIVATE_EXPORT QQmlValueType : public QObject
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;
80 virtual QString toString() const = 0;
81 virtual bool isEqual(const QVariant &value) const = 0;
83 virtual void onLoad() {}
85 inline int userType() const
91 inline void readProperty(QObject *obj, int idx, void *p)
94 QMetaObject::metacall(obj, QMetaObject::ReadProperty, idx, a);
98 inline void writeProperty(QObject *obj, int idx, QQmlPropertyPrivate::WriteFlags flags, void *p)
101 void *a[] = { p, 0, &status, &flags };
102 QMetaObject::metacall(obj, QMetaObject::WriteProperty, idx, a);
109 template <typename T>
110 class QQmlValueTypeBase : public QQmlValueType
115 QQmlValueTypeBase(int userType, QObject *parent)
116 : QQmlValueType(userType, parent)
120 virtual void read(QObject *obj, int idx)
122 readProperty(obj, idx, &v);
125 virtual void readVariantValue(QObject *obj, int idx, QVariant *into)
127 // important: must not change the userType of the variant
128 readProperty(obj, idx, into);
131 virtual void write(QObject *obj, int idx, QQmlPropertyPrivate::WriteFlags flags)
133 writeProperty(obj, idx, flags, &v);
136 virtual void writeVariantValue(QObject *obj, int idx, QQmlPropertyPrivate::WriteFlags flags, QVariant *from)
138 writeProperty(obj, idx, flags, from);
141 virtual QVariant value()
143 return QVariant::fromValue(v);
146 virtual void setValue(const QVariant &value)
148 v = qvariant_cast<T>(value);
152 virtual bool isEqual(const QVariant &other) const
154 return QVariant::fromValue(v) == other;
161 class Q_QML_PRIVATE_EXPORT QQmlValueTypeFactory
164 static bool isValueType(int);
165 static QQmlValueType *valueType(int idx);
167 static void registerValueTypes(const char *uri, int versionMajor, int versionMinor);
170 class Q_QML_PRIVATE_EXPORT QQmlPointFValueType : public QQmlValueTypeBase<QPointF>
172 Q_PROPERTY(qreal x READ x WRITE setX)
173 Q_PROPERTY(qreal y READ y WRITE setY)
176 QQmlPointFValueType(QObject *parent = 0);
178 virtual QString toString() const;
186 class Q_QML_PRIVATE_EXPORT QQmlPointValueType : public QQmlValueTypeBase<QPoint>
188 Q_PROPERTY(int x READ x WRITE setX)
189 Q_PROPERTY(int y READ y WRITE setY)
192 QQmlPointValueType(QObject *parent = 0);
194 virtual QString toString() const;
202 class Q_QML_PRIVATE_EXPORT QQmlSizeFValueType : public QQmlValueTypeBase<QSizeF>
204 Q_PROPERTY(qreal width READ width WRITE setWidth)
205 Q_PROPERTY(qreal height READ height WRITE setHeight)
208 QQmlSizeFValueType(QObject *parent = 0);
210 virtual QString toString() const;
213 qreal height() const;
214 void setWidth(qreal);
215 void setHeight(qreal);
218 class Q_QML_PRIVATE_EXPORT QQmlSizeValueType : public QQmlValueTypeBase<QSize>
220 Q_PROPERTY(int width READ width WRITE setWidth)
221 Q_PROPERTY(int height READ height WRITE setHeight)
224 QQmlSizeValueType(QObject *parent = 0);
226 virtual QString toString() const;
234 class Q_QML_PRIVATE_EXPORT QQmlRectFValueType : public QQmlValueTypeBase<QRectF>
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)
242 QQmlRectFValueType(QObject *parent = 0);
244 virtual QString toString() const;
252 qreal height() const;
253 void setWidth(qreal);
254 void setHeight(qreal);
257 class Q_QML_PRIVATE_EXPORT QQmlRectValueType : public QQmlValueTypeBase<QRect>
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)
265 QQmlRectValueType(QObject *parent = 0);
267 virtual QString toString() const;
280 class Q_QML_PRIVATE_EXPORT QQmlEasingValueType : public QQmlValueTypeBase<QEasingCurve>
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)
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
318 QQmlEasingValueType(QObject *parent = 0);
320 virtual QString toString() const;
323 qreal amplitude() const;
324 qreal overshoot() const;
325 qreal period() const;
327 void setAmplitude(qreal);
328 void setOvershoot(qreal);
329 void setPeriod(qreal);
330 void setBezierCurve(const QVariantList &);
331 QVariantList bezierCurve() const;
335 int qmlRegisterValueTypeEnums(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
337 QByteArray name(T::staticMetaObject.className());
339 QByteArray pointerName(name + '*');
341 QQmlPrivate::RegisterType type = {
344 qRegisterNormalizedMetaType<T *>(pointerName.constData()), 0, 0, 0,
348 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
360 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
365 #endif // QQMLVALUETYPE_P_H