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 #include "qqmlvaluetype_p.h"
43 #include "qqmlmetatype_p.h"
45 #include <private/qqmlglobal_p.h>
46 #include <QtCore/qdebug.h>
50 QQmlValueTypeFactory::QQmlValueTypeFactory()
52 for (unsigned int ii = 0; ii < QVariant::UserType; ++ii)
56 QQmlValueTypeFactory::~QQmlValueTypeFactory()
58 for (unsigned int ii = 0; ii < QVariant::UserType; ++ii)
59 delete valueTypes[ii];
62 bool QQmlValueTypeFactory::isValueType(int idx)
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) {
75 void QQmlValueTypeFactory::registerValueTypes(const char *uri, int versionMajor, int versionMinor)
77 qmlRegisterValueTypeEnums<QQmlEasingValueType>(uri, versionMajor, versionMinor, "Easing");
80 QQmlValueType *QQmlValueTypeFactory::valueType(int t)
82 QQmlValueType *rv = 0;
86 rv = new QQmlPointValueType;
88 case QVariant::PointF:
89 rv = new QQmlPointFValueType;
92 rv = new QQmlSizeValueType;
95 rv = new QQmlSizeFValueType;
98 rv = new QQmlRectValueType;
100 case QVariant::RectF:
101 rv = new QQmlRectFValueType;
103 case QVariant::EasingCurve:
104 rv = new QQmlEasingValueType;
107 rv = QQml_valueTypeProvider()->createValueType(t);
111 Q_ASSERT(!rv || rv->metaObject()->propertyCount() < 32);
115 QQmlValueType::QQmlValueType(int userType, QObject *parent)
116 : QObject(parent), m_userType(userType)
121 QQmlPointFValueType::QQmlPointFValueType(QObject *parent)
122 : QQmlValueTypeBase<QPointF>(QMetaType::QPointF, parent)
126 QString QQmlPointFValueType::toString() const
128 return QString(QLatin1String("QPointF(%1, %2)")).arg(v.x()).arg(v.y());
131 qreal QQmlPointFValueType::x() const
136 qreal QQmlPointFValueType::y() const
141 void QQmlPointFValueType::setX(qreal x)
146 void QQmlPointFValueType::setY(qreal y)
152 QQmlPointValueType::QQmlPointValueType(QObject *parent)
153 : QQmlValueTypeBase<QPoint>(QMetaType::QPoint, parent)
157 QString QQmlPointValueType::toString() const
159 return QString(QLatin1String("QPoint(%1, %2)")).arg(v.x()).arg(v.y());
162 int QQmlPointValueType::x() const
167 int QQmlPointValueType::y() const
172 void QQmlPointValueType::setX(int x)
177 void QQmlPointValueType::setY(int y)
183 QQmlSizeFValueType::QQmlSizeFValueType(QObject *parent)
184 : QQmlValueTypeBase<QSizeF>(QMetaType::QSizeF, parent)
188 QString QQmlSizeFValueType::toString() const
190 return QString(QLatin1String("QSizeF(%1, %2)")).arg(v.width()).arg(v.height());
193 qreal QQmlSizeFValueType::width() const
198 qreal QQmlSizeFValueType::height() const
203 void QQmlSizeFValueType::setWidth(qreal w)
208 void QQmlSizeFValueType::setHeight(qreal h)
214 QQmlSizeValueType::QQmlSizeValueType(QObject *parent)
215 : QQmlValueTypeBase<QSize>(QMetaType::QSize, parent)
219 QString QQmlSizeValueType::toString() const
221 return QString(QLatin1String("QSize(%1, %2)")).arg(v.width()).arg(v.height());
224 int QQmlSizeValueType::width() const
229 int QQmlSizeValueType::height() const
234 void QQmlSizeValueType::setWidth(int w)
239 void QQmlSizeValueType::setHeight(int h)
245 QQmlRectFValueType::QQmlRectFValueType(QObject *parent)
246 : QQmlValueTypeBase<QRectF>(QMetaType::QRectF, parent)
250 QString QQmlRectFValueType::toString() const
252 return QString(QLatin1String("QRectF(%1, %2, %3, %4)")).arg(v.x()).arg(v.y()).arg(v.width()).arg(v.height());
255 qreal QQmlRectFValueType::x() const
260 qreal QQmlRectFValueType::y() const
265 void QQmlRectFValueType::setX(qreal x)
270 void QQmlRectFValueType::setY(qreal y)
275 qreal QQmlRectFValueType::width() const
280 qreal QQmlRectFValueType::height() const
285 void QQmlRectFValueType::setWidth(qreal w)
290 void QQmlRectFValueType::setHeight(qreal h)
296 QQmlRectValueType::QQmlRectValueType(QObject *parent)
297 : QQmlValueTypeBase<QRect>(QMetaType::QRect, parent)
301 QString QQmlRectValueType::toString() const
303 return QString(QLatin1String("QRect(%1, %2, %3, %4)")).arg(v.x()).arg(v.y()).arg(v.width()).arg(v.height());
306 int QQmlRectValueType::x() const
311 int QQmlRectValueType::y() const
316 void QQmlRectValueType::setX(int x)
321 void QQmlRectValueType::setY(int y)
326 int QQmlRectValueType::width() const
331 int QQmlRectValueType::height() const
336 void QQmlRectValueType::setWidth(int w)
341 void QQmlRectValueType::setHeight(int h)
347 QQmlEasingValueType::QQmlEasingValueType(QObject *parent)
348 : QQmlValueTypeBase<QEasingCurve>(QMetaType::QEasingCurve, parent)
352 QString QQmlEasingValueType::toString() const
354 return QString(QLatin1String("QEasingCurve(%1, %2, %3, %4)")).arg(v.type()).arg(v.amplitude()).arg(v.overshoot()).arg(v.period());
357 QQmlEasingValueType::Type QQmlEasingValueType::type() const
359 return (QQmlEasingValueType::Type)v.type();
362 qreal QQmlEasingValueType::amplitude() const
364 return v.amplitude();
367 qreal QQmlEasingValueType::overshoot() const
369 return v.overshoot();
372 qreal QQmlEasingValueType::period() const
377 void QQmlEasingValueType::setType(QQmlEasingValueType::Type type)
379 v.setType((QEasingCurve::Type)type);
382 void QQmlEasingValueType::setAmplitude(qreal amplitude)
384 v.setAmplitude(amplitude);
387 void QQmlEasingValueType::setOvershoot(qreal overshoot)
389 v.setOvershoot(overshoot);
392 void QQmlEasingValueType::setPeriod(qreal period)
397 void QQmlEasingValueType::setBezierCurve(const QVariantList &customCurveVariant)
399 if (customCurveVariant.isEmpty())
402 QVariantList variantList = customCurveVariant;
403 if ((variantList.count() % 6) == 0) {
404 bool allRealsOk = true;
406 for (int i = 0; i < variantList.count(); i++) {
408 const qreal real = variantList.at(i).toReal(&ok);
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);
423 const QPointF c1(c1x, c1y);
424 const QPointF c2(c2x, c2y);
425 const QPointF c3(c3x, c3y);
427 newEasingCurve.addCubicBezierSegment(c1, c2, c3);
434 QVariantList QQmlEasingValueType::bezierCurve() const
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());