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 QtDeclarative 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 "qdeclarativevaluetype_p.h"
44 #include "qdeclarativemetatype_p.h"
45 #include <private/qfont_p.h>
47 #include <QtCore/qdebug.h>
52 int qmlRegisterValueTypeEnums(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
54 QByteArray name(T::staticMetaObject.className());
56 QByteArray pointerName(name + '*');
58 QDeclarativePrivate::RegisterType type = {
61 qRegisterMetaType<T *>(pointerName.constData()), 0, 0, 0,
65 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
77 return QDeclarativePrivate::qmlregister(QDeclarativePrivate::TypeRegistration, &type);
80 QDeclarativeValueTypeFactory::QDeclarativeValueTypeFactory()
83 for (unsigned int ii = 0; ii < (QVariant::UserType - 1); ++ii)
84 valueTypes[ii] = valueType(ii);
87 QDeclarativeValueTypeFactory::~QDeclarativeValueTypeFactory()
89 for (unsigned int ii = 0; ii < (QVariant::UserType - 1); ++ii)
90 delete valueTypes[ii];
93 bool QDeclarativeValueTypeFactory::isValueType(int idx)
95 if ((uint)idx < QVariant::UserType
96 && idx != QVariant::StringList
97 && idx != QMetaType::QObjectStar
98 && idx != QMetaType::QWidgetStar
99 && idx != QMetaType::VoidStar
100 && idx != QMetaType::QVariant) {
106 void QDeclarativeValueTypeFactory::registerBaseTypes(const char *uri, int versionMajor, int versionMinor)
108 qmlRegisterValueTypeEnums<QDeclarativeEasingValueType>(uri, versionMajor, versionMinor, "Easing");
109 qmlRegisterValueTypeEnums<QDeclarativeFontValueType>(uri, versionMajor, versionMinor, "Font");
112 void QDeclarativeValueTypeFactory::registerValueTypes()
114 registerBaseTypes("QtQuick", 2, 0);
117 QDeclarativeValueType *QDeclarativeValueTypeFactory::valueType(int t)
119 QDeclarativeValueType *rv = 0;
122 case QVariant::Point:
123 rv = new QDeclarativePointValueType;
125 case QVariant::PointF:
126 rv = new QDeclarativePointFValueType;
129 rv = new QDeclarativeSizeValueType;
131 case QVariant::SizeF:
132 rv = new QDeclarativeSizeFValueType;
135 rv = new QDeclarativeRectValueType;
137 case QVariant::RectF:
138 rv = new QDeclarativeRectFValueType;
140 case QVariant::Vector2D:
141 rv = new QDeclarativeVector2DValueType;
143 case QVariant::Vector3D:
144 rv = new QDeclarativeVector3DValueType;
146 case QVariant::Vector4D:
147 rv = new QDeclarativeVector4DValueType;
149 case QVariant::Quaternion:
150 rv = new QDeclarativeQuaternionValueType;
152 case QVariant::Matrix4x4:
153 rv = new QDeclarativeMatrix4x4ValueType;
155 case QVariant::EasingCurve:
156 rv = new QDeclarativeEasingValueType;
159 rv = new QDeclarativeFontValueType;
161 case QVariant::Color:
162 rv = new QDeclarativeColorValueType;
168 Q_ASSERT(!rv || rv->metaObject()->propertyCount() < 32);
172 QDeclarativeValueType::QDeclarativeValueType(QObject *parent)
177 #define QML_VALUETYPE_READWRITE(name, cpptype, var) \
178 QDeclarative ## name ## ValueType::QDeclarative ## name ## ValueType(QObject *parent) \
179 : QDeclarativeValueType(parent) \
182 void QDeclarative ## name ## ValueType::read(QObject *obj, int idx) \
184 void *a[] = { &var, 0 }; \
185 QMetaObject::metacall(obj, QMetaObject::ReadProperty, idx, a); \
188 void QDeclarative ## name ## ValueType::write(QObject *obj, int idx, \
189 QDeclarativePropertyPrivate::WriteFlags flags) \
192 void *a[] = { &var, 0, &status, &flags }; \
193 QMetaObject::metacall(obj, QMetaObject::WriteProperty, idx, a); \
195 bool QDeclarative ## name ## ValueType::isEqual(const QVariant &value) const \
197 return QVariant(var) == value; \
199 QVariant QDeclarative ## name ## ValueType::value() \
201 return QVariant(var); \
203 void QDeclarative ## name ## ValueType::setValue(const QVariant &value) \
205 var = qvariant_cast<cpptype>(value); \
209 QML_VALUETYPE_READWRITE(PointF, QPointF, point);
210 QML_VALUETYPE_READWRITE(Point, QPoint, point);
211 QML_VALUETYPE_READWRITE(SizeF, QSizeF, size);
212 QML_VALUETYPE_READWRITE(Size, QSize, size);
213 QML_VALUETYPE_READWRITE(RectF, QRectF, rect);
214 QML_VALUETYPE_READWRITE(Rect, QRect, rect);
215 QML_VALUETYPE_READWRITE(Vector2D, QVector2D, vector);
216 QML_VALUETYPE_READWRITE(Vector3D, QVector3D, vector);
217 QML_VALUETYPE_READWRITE(Vector4D, QVector4D, vector);
218 QML_VALUETYPE_READWRITE(Quaternion, QQuaternion, quaternion);
219 QML_VALUETYPE_READWRITE(Matrix4x4, QMatrix4x4, matrix);
220 QML_VALUETYPE_READWRITE(Easing, QEasingCurve, easing);
221 QML_VALUETYPE_READWRITE(Font, QFont, font);
222 QML_VALUETYPE_READWRITE(Color, QColor, color);
224 QString QDeclarativePointFValueType::toString() const
226 return QString(QLatin1String("QPointF(%1, %2)")).arg(point.x()).arg(point.y());
229 qreal QDeclarativePointFValueType::x() const
234 qreal QDeclarativePointFValueType::y() const
239 void QDeclarativePointFValueType::setX(qreal x)
244 void QDeclarativePointFValueType::setY(qreal y)
249 QString QDeclarativePointValueType::toString() const
251 return QString(QLatin1String("QPoint(%1, %2)")).arg(point.x()).arg(point.y());
254 int QDeclarativePointValueType::x() const
259 int QDeclarativePointValueType::y() const
264 void QDeclarativePointValueType::setX(int x)
269 void QDeclarativePointValueType::setY(int y)
274 QString QDeclarativeSizeFValueType::toString() const
276 return QString(QLatin1String("QSizeF(%1, %2)")).arg(size.width()).arg(size.height());
279 qreal QDeclarativeSizeFValueType::width() const
284 qreal QDeclarativeSizeFValueType::height() const
286 return size.height();
289 void QDeclarativeSizeFValueType::setWidth(qreal w)
294 void QDeclarativeSizeFValueType::setHeight(qreal h)
299 QString QDeclarativeSizeValueType::toString() const
301 return QString(QLatin1String("QSize(%1, %2)")).arg(size.width()).arg(size.height());
304 int QDeclarativeSizeValueType::width() const
309 int QDeclarativeSizeValueType::height() const
311 return size.height();
314 void QDeclarativeSizeValueType::setWidth(int w)
319 void QDeclarativeSizeValueType::setHeight(int h)
324 QString QDeclarativeRectFValueType::toString() const
326 return QString(QLatin1String("QRectF(%1, %2, %3, %4)")).arg(rect.x()).arg(rect.y()).arg(rect.width()).arg(rect.height());
329 qreal QDeclarativeRectFValueType::x() const
334 qreal QDeclarativeRectFValueType::y() const
339 void QDeclarativeRectFValueType::setX(qreal x)
344 void QDeclarativeRectFValueType::setY(qreal y)
349 qreal QDeclarativeRectFValueType::width() const
354 qreal QDeclarativeRectFValueType::height() const
356 return rect.height();
359 void QDeclarativeRectFValueType::setWidth(qreal w)
364 void QDeclarativeRectFValueType::setHeight(qreal h)
369 QString QDeclarativeRectValueType::toString() const
371 return QString(QLatin1String("QRect(%1, %2, %3, %4)")).arg(rect.x()).arg(rect.y()).arg(rect.width()).arg(rect.height());
374 int QDeclarativeRectValueType::x() const
379 int QDeclarativeRectValueType::y() const
384 void QDeclarativeRectValueType::setX(int x)
389 void QDeclarativeRectValueType::setY(int y)
394 int QDeclarativeRectValueType::width() const
399 int QDeclarativeRectValueType::height() const
401 return rect.height();
404 void QDeclarativeRectValueType::setWidth(int w)
409 void QDeclarativeRectValueType::setHeight(int h)
414 QString QDeclarativeVector2DValueType::toString() const
416 return QString(QLatin1String("QVector2D(%1, %2)")).arg(vector.x()).arg(vector.y());
419 qreal QDeclarativeVector2DValueType::x() const
424 qreal QDeclarativeVector2DValueType::y() const
429 void QDeclarativeVector2DValueType::setX(qreal x)
434 void QDeclarativeVector2DValueType::setY(qreal y)
439 QString QDeclarativeVector3DValueType::toString() const
441 return QString(QLatin1String("QVector3D(%1, %2, %3)")).arg(vector.x()).arg(vector.y()).arg(vector.z());
444 qreal QDeclarativeVector3DValueType::x() const
449 qreal QDeclarativeVector3DValueType::y() const
454 qreal QDeclarativeVector3DValueType::z() const
459 void QDeclarativeVector3DValueType::setX(qreal x)
464 void QDeclarativeVector3DValueType::setY(qreal y)
469 void QDeclarativeVector3DValueType::setZ(qreal z)
474 QString QDeclarativeVector4DValueType::toString() const
476 return QString(QLatin1String("QVector4D(%1, %2, %3, %4)")).arg(vector.x()).arg(vector.y()).arg(vector.z()).arg(vector.w());
479 qreal QDeclarativeVector4DValueType::x() const
484 qreal QDeclarativeVector4DValueType::y() const
489 qreal QDeclarativeVector4DValueType::z() const
494 qreal QDeclarativeVector4DValueType::w() const
499 void QDeclarativeVector4DValueType::setX(qreal x)
504 void QDeclarativeVector4DValueType::setY(qreal y)
509 void QDeclarativeVector4DValueType::setZ(qreal z)
514 void QDeclarativeVector4DValueType::setW(qreal w)
519 QString QDeclarativeQuaternionValueType::toString() const
521 return QString(QLatin1String("QQuaternion(%1, %2, %3, %4)")).arg(quaternion.scalar()).arg(quaternion.x()).arg(quaternion.y()).arg(quaternion.z());
524 qreal QDeclarativeQuaternionValueType::scalar() const
526 return quaternion.scalar();
529 qreal QDeclarativeQuaternionValueType::x() const
531 return quaternion.x();
534 qreal QDeclarativeQuaternionValueType::y() const
536 return quaternion.y();
539 qreal QDeclarativeQuaternionValueType::z() const
541 return quaternion.z();
544 void QDeclarativeQuaternionValueType::setScalar(qreal scalar)
546 quaternion.setScalar(scalar);
549 void QDeclarativeQuaternionValueType::setX(qreal x)
554 void QDeclarativeQuaternionValueType::setY(qreal y)
559 void QDeclarativeQuaternionValueType::setZ(qreal z)
564 QString QDeclarativeMatrix4x4ValueType::toString() const
566 return QString(QLatin1String("QMatrix4x4(%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12, %13, %14, %15, %16)"))
567 .arg(matrix(0, 0)).arg(matrix(0, 1)).arg(matrix(0, 2)).arg(matrix(0, 3))
568 .arg(matrix(1, 0)).arg(matrix(1, 1)).arg(matrix(1, 2)).arg(matrix(1, 3))
569 .arg(matrix(2, 0)).arg(matrix(2, 1)).arg(matrix(2, 2)).arg(matrix(2, 3))
570 .arg(matrix(3, 0)).arg(matrix(3, 1)).arg(matrix(3, 2)).arg(matrix(3, 3));
573 QString QDeclarativeEasingValueType::toString() const
575 return QString(QLatin1String("QEasingCurve(%1, %2, %3, %4)")).arg(easing.type()).arg(easing.amplitude()).arg(easing.overshoot()).arg(easing.period());
578 QDeclarativeEasingValueType::Type QDeclarativeEasingValueType::type() const
580 return (QDeclarativeEasingValueType::Type)easing.type();
583 qreal QDeclarativeEasingValueType::amplitude() const
585 return easing.amplitude();
588 qreal QDeclarativeEasingValueType::overshoot() const
590 return easing.overshoot();
593 qreal QDeclarativeEasingValueType::period() const
595 return easing.period();
598 void QDeclarativeEasingValueType::setType(QDeclarativeEasingValueType::Type type)
600 easing.setType((QEasingCurve::Type)type);
603 void QDeclarativeEasingValueType::setAmplitude(qreal amplitude)
605 easing.setAmplitude(amplitude);
608 void QDeclarativeEasingValueType::setOvershoot(qreal overshoot)
610 easing.setOvershoot(overshoot);
613 void QDeclarativeEasingValueType::setPeriod(qreal period)
615 easing.setPeriod(period);
618 void QDeclarativeEasingValueType::setBezierCurve(const QVariantList &customCurveVariant)
620 if (customCurveVariant.isEmpty())
623 QVariantList variantList = customCurveVariant;
624 if ((variantList.count() % 6) == 0) {
625 bool allRealsOk = true;
627 for (int i = 0; i < variantList.count(); i++) {
629 const qreal real = variantList.at(i).toReal(&ok);
635 QEasingCurve newEasingCurve(QEasingCurve::BezierSpline);
636 for (int i = 0; i < reals.count() / 6; i++) {
637 const qreal c1x = reals.at(i * 6);
638 const qreal c1y = reals.at(i * 6 + 1);
639 const qreal c2x = reals.at(i * 6 + 2);
640 const qreal c2y = reals.at(i * 6 + 3);
641 const qreal c3x = reals.at(i * 6 + 4);
642 const qreal c3y = reals.at(i * 6 + 5);
644 const QPointF c1(c1x, c1y);
645 const QPointF c2(c2x, c2y);
646 const QPointF c3(c3x, c3y);
648 newEasingCurve.addCubicBezierSegment(c1, c2, c3);
649 easing = newEasingCurve;
655 QVariantList QDeclarativeEasingValueType::bezierCurve() const
658 QList<QPointF> points = easing.cubicBezierSpline();
659 for (int ii = 0; ii < points.count(); ++ii)
660 rv << QVariant(points.at(ii).x()) << QVariant(points.at(ii).y());
664 void QDeclarativeFontValueType::onLoad()
666 pixelSizeSet = false;
667 pointSizeSet = false;
670 QString QDeclarativeFontValueType::toString() const
672 return QString(QLatin1String("QFont(%1)")).arg(font.toString());
675 QString QDeclarativeFontValueType::family() const
677 return font.family();
680 void QDeclarativeFontValueType::setFamily(const QString &family)
682 font.setFamily(family);
685 bool QDeclarativeFontValueType::bold() const
690 void QDeclarativeFontValueType::setBold(bool b)
695 QDeclarativeFontValueType::FontWeight QDeclarativeFontValueType::weight() const
697 return (QDeclarativeFontValueType::FontWeight)font.weight();
700 void QDeclarativeFontValueType::setWeight(QDeclarativeFontValueType::FontWeight w)
702 font.setWeight((QFont::Weight)w);
705 bool QDeclarativeFontValueType::italic() const
707 return font.italic();
710 void QDeclarativeFontValueType::setItalic(bool b)
715 bool QDeclarativeFontValueType::underline() const
717 return font.underline();
720 void QDeclarativeFontValueType::setUnderline(bool b)
722 font.setUnderline(b);
725 bool QDeclarativeFontValueType::overline() const
727 return font.overline();
730 void QDeclarativeFontValueType::setOverline(bool b)
735 bool QDeclarativeFontValueType::strikeout() const
737 return font.strikeOut();
740 void QDeclarativeFontValueType::setStrikeout(bool b)
742 font.setStrikeOut(b);
745 qreal QDeclarativeFontValueType::pointSize() const
747 if (font.pointSizeF() == -1) {
749 dpi = qt_defaultDpi();
750 return font.pixelSize() * qreal(72.) / qreal(dpi);
752 return font.pointSizeF();
755 void QDeclarativeFontValueType::setPointSize(qreal size)
758 qWarning() << "Both point size and pixel size set. Using pixel size.";
764 font.setPointSizeF(size);
766 pointSizeSet = false;
770 int QDeclarativeFontValueType::pixelSize() const
772 if (font.pixelSize() == -1) {
774 dpi = qt_defaultDpi();
775 return (font.pointSizeF() * dpi) / qreal(72.);
777 return font.pixelSize();
780 void QDeclarativeFontValueType::setPixelSize(int size)
784 qWarning() << "Both point size and pixel size set. Using pixel size.";
785 font.setPixelSize(size);
788 pixelSizeSet = false;
792 QDeclarativeFontValueType::Capitalization QDeclarativeFontValueType::capitalization() const
794 return (QDeclarativeFontValueType::Capitalization)font.capitalization();
797 void QDeclarativeFontValueType::setCapitalization(QDeclarativeFontValueType::Capitalization c)
799 font.setCapitalization((QFont::Capitalization)c);
802 qreal QDeclarativeFontValueType::letterSpacing() const
804 return font.letterSpacing();
807 void QDeclarativeFontValueType::setLetterSpacing(qreal size)
809 font.setLetterSpacing(QFont::AbsoluteSpacing, size);
812 qreal QDeclarativeFontValueType::wordSpacing() const
814 return font.wordSpacing();
817 void QDeclarativeFontValueType::setWordSpacing(qreal size)
819 font.setWordSpacing(size);
822 QString QDeclarativeColorValueType::toString() const
824 // special case - to maintain behaviour with QtQuick 1.0, we just output normal toString() value.
825 return QVariant(color).toString();
828 qreal QDeclarativeColorValueType::r() const
833 qreal QDeclarativeColorValueType::g() const
835 return color.greenF();
838 qreal QDeclarativeColorValueType::b() const
840 return color.blueF();
843 qreal QDeclarativeColorValueType::a() const
845 return color.alphaF();
848 void QDeclarativeColorValueType::setR(qreal r)
853 void QDeclarativeColorValueType::setG(qreal g)
858 void QDeclarativeColorValueType::setB(qreal b)
863 void QDeclarativeColorValueType::setA(qreal a)