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 QQmlValueTypeFactory();
165 ~QQmlValueTypeFactory();
166 static bool isValueType(int);
167 static QQmlValueType *valueType(int);
169 static void registerValueTypes(const char *uri, int versionMajor, int versionMinor);
171 QQmlValueType *operator[](int idx) const {
172 if (idx >= (int)QVariant::UserType)
175 QQmlValueType *rv = valueTypes[idx];
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;
187 mutable QQmlValueType *valueTypes[QVariant::UserType];
190 class Q_QML_PRIVATE_EXPORT QQmlPointFValueType : public QQmlValueTypeBase<QPointF>
192 Q_PROPERTY(qreal x READ x WRITE setX)
193 Q_PROPERTY(qreal y READ y WRITE setY)
196 QQmlPointFValueType(QObject *parent = 0);
198 virtual QString toString() const;
206 class Q_QML_PRIVATE_EXPORT QQmlPointValueType : public QQmlValueTypeBase<QPoint>
208 Q_PROPERTY(int x READ x WRITE setX)
209 Q_PROPERTY(int y READ y WRITE setY)
212 QQmlPointValueType(QObject *parent = 0);
214 virtual QString toString() const;
222 class Q_QML_PRIVATE_EXPORT QQmlSizeFValueType : public QQmlValueTypeBase<QSizeF>
224 Q_PROPERTY(qreal width READ width WRITE setWidth)
225 Q_PROPERTY(qreal height READ height WRITE setHeight)
228 QQmlSizeFValueType(QObject *parent = 0);
230 virtual QString toString() const;
233 qreal height() const;
234 void setWidth(qreal);
235 void setHeight(qreal);
238 class Q_QML_PRIVATE_EXPORT QQmlSizeValueType : public QQmlValueTypeBase<QSize>
240 Q_PROPERTY(int width READ width WRITE setWidth)
241 Q_PROPERTY(int height READ height WRITE setHeight)
244 QQmlSizeValueType(QObject *parent = 0);
246 virtual QString toString() const;
254 class Q_QML_PRIVATE_EXPORT QQmlRectFValueType : public QQmlValueTypeBase<QRectF>
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)
262 QQmlRectFValueType(QObject *parent = 0);
264 virtual QString toString() const;
272 qreal height() const;
273 void setWidth(qreal);
274 void setHeight(qreal);
277 class Q_QML_PRIVATE_EXPORT QQmlRectValueType : public QQmlValueTypeBase<QRect>
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)
285 QQmlRectValueType(QObject *parent = 0);
287 virtual QString toString() const;
300 class Q_QML_PRIVATE_EXPORT QQmlEasingValueType : public QQmlValueTypeBase<QEasingCurve>
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)
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
338 QQmlEasingValueType(QObject *parent = 0);
340 virtual QString toString() const;
343 qreal amplitude() const;
344 qreal overshoot() const;
345 qreal period() const;
347 void setAmplitude(qreal);
348 void setOvershoot(qreal);
349 void setPeriod(qreal);
350 void setBezierCurve(const QVariantList &);
351 QVariantList bezierCurve() const;
355 int qmlRegisterValueTypeEnums(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
357 QByteArray name(T::staticMetaObject.className());
359 QByteArray pointerName(name + '*');
361 QQmlPrivate::RegisterType type = {
364 qRegisterNormalizedMetaType<T *>(pointerName.constData()), 0, 0, 0,
368 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
380 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
385 #endif // QQMLVALUETYPE_P_H