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 <private/qquickvaluetypes_p.h>
44 #include <qtquickglobal.h>
45 #include <private/qqmlvaluetype_p.h>
46 #include <private/qfont_p.h>
51 namespace QQuickValueTypes {
52 void registerValueTypes()
54 QQmlValueTypeFactory::registerValueTypes("QtQuick", 2, 0);
55 qmlRegisterValueTypeEnums<QQuickFontValueType>("QtQuick", 2, 0, "Font");
59 QQuickColorValueType::QQuickColorValueType(QObject *parent)
60 : QQmlValueTypeBase<QColor>(QMetaType::QColor, parent)
64 QString QQuickColorValueType::toString() const
66 // to maintain behaviour with QtQuick 1.0, we just output normal toString() value.
67 return QVariant(v).toString();
70 qreal QQuickColorValueType::r() const
75 qreal QQuickColorValueType::g() const
80 qreal QQuickColorValueType::b() const
85 qreal QQuickColorValueType::a() const
90 void QQuickColorValueType::setR(qreal r)
95 void QQuickColorValueType::setG(qreal g)
100 void QQuickColorValueType::setB(qreal b)
105 void QQuickColorValueType::setA(qreal a)
111 QQuickVector2DValueType::QQuickVector2DValueType(QObject *parent)
112 : QQmlValueTypeBase<QVector2D>(QMetaType::QVector2D, parent)
116 QString QQuickVector2DValueType::toString() const
118 return QString(QLatin1String("QVector2D(%1, %2)")).arg(v.x()).arg(v.y());
121 bool QQuickVector2DValueType::isEqual(const QVariant &other) const
123 if (other.userType() != QMetaType::QVector2D)
126 QVector2D otherVector = other.value<QVector2D>();
127 return (v == otherVector);
130 qreal QQuickVector2DValueType::x() const
135 qreal QQuickVector2DValueType::y() const
140 void QQuickVector2DValueType::setX(qreal x)
145 void QQuickVector2DValueType::setY(qreal y)
150 qreal QQuickVector2DValueType::dotProduct(const QVector2D &vec) const
152 return QVector2D::dotProduct(v, vec);
155 QVector2D QQuickVector2DValueType::times(const QVector2D &vec) const
160 QVector2D QQuickVector2DValueType::times(qreal scalar) const
165 QVector2D QQuickVector2DValueType::plus(const QVector2D &vec) const
170 QVector2D QQuickVector2DValueType::minus(const QVector2D &vec) const
175 QVector2D QQuickVector2DValueType::normalized() const
177 return v.normalized();
180 qreal QQuickVector2DValueType::length() const
185 QVector3D QQuickVector2DValueType::toVector3d() const
187 return v.toVector3D();
190 QVector4D QQuickVector2DValueType::toVector4d() const
192 return v.toVector4D();
195 bool QQuickVector2DValueType::fuzzyEquals(const QVector2D &vec, qreal epsilon) const
197 qreal absEps = qAbs(epsilon);
198 if (qAbs(v.x() - vec.x()) > absEps)
200 if (qAbs(v.y() - vec.y()) > absEps)
205 bool QQuickVector2DValueType::fuzzyEquals(const QVector2D &vec) const
207 return qFuzzyCompare(v, vec);
211 QQuickVector3DValueType::QQuickVector3DValueType(QObject *parent)
212 : QQmlValueTypeBase<QVector3D>(QMetaType::QVector3D, parent)
216 QString QQuickVector3DValueType::toString() const
218 return QString(QLatin1String("QVector3D(%1, %2, %3)")).arg(v.x()).arg(v.y()).arg(v.z());
221 bool QQuickVector3DValueType::isEqual(const QVariant &other) const
223 if (other.userType() != QMetaType::QVector3D)
226 QVector3D otherVector = other.value<QVector3D>();
227 return (v == otherVector);
230 qreal QQuickVector3DValueType::x() const
235 qreal QQuickVector3DValueType::y() const
240 qreal QQuickVector3DValueType::z() const
245 void QQuickVector3DValueType::setX(qreal x)
250 void QQuickVector3DValueType::setY(qreal y)
255 void QQuickVector3DValueType::setZ(qreal z)
260 QVector3D QQuickVector3DValueType::crossProduct(const QVector3D &vec) const
262 return QVector3D::crossProduct(v, vec);
265 qreal QQuickVector3DValueType::dotProduct(const QVector3D &vec) const
267 return QVector3D::dotProduct(v, vec);
270 QVector3D QQuickVector3DValueType::times(const QMatrix4x4 &m) const
275 QVector3D QQuickVector3DValueType::times(const QVector3D &vec) const
280 QVector3D QQuickVector3DValueType::times(qreal scalar) const
285 QVector3D QQuickVector3DValueType::plus(const QVector3D &vec) const
290 QVector3D QQuickVector3DValueType::minus(const QVector3D &vec) const
295 QVector3D QQuickVector3DValueType::normalized() const
297 return v.normalized();
300 qreal QQuickVector3DValueType::length() const
305 QVector2D QQuickVector3DValueType::toVector2d() const
307 return v.toVector2D();
310 QVector4D QQuickVector3DValueType::toVector4d() const
312 return v.toVector4D();
315 bool QQuickVector3DValueType::fuzzyEquals(const QVector3D &vec, qreal epsilon) const
317 qreal absEps = qAbs(epsilon);
318 if (qAbs(v.x() - vec.x()) > absEps)
320 if (qAbs(v.y() - vec.y()) > absEps)
322 if (qAbs(v.z() - vec.z()) > absEps)
327 bool QQuickVector3DValueType::fuzzyEquals(const QVector3D &vec) const
329 return qFuzzyCompare(v, vec);
333 QQuickVector4DValueType::QQuickVector4DValueType(QObject *parent)
334 : QQmlValueTypeBase<QVector4D>(QMetaType::QVector4D, parent)
338 QString QQuickVector4DValueType::toString() const
340 return QString(QLatin1String("QVector4D(%1, %2, %3, %4)")).arg(v.x()).arg(v.y()).arg(v.z()).arg(v.w());
343 bool QQuickVector4DValueType::isEqual(const QVariant &other) const
345 if (other.userType() != QMetaType::QVector4D)
348 QVector4D otherVector = other.value<QVector4D>();
349 return (v == otherVector);
352 qreal QQuickVector4DValueType::x() const
357 qreal QQuickVector4DValueType::y() const
362 qreal QQuickVector4DValueType::z() const
367 qreal QQuickVector4DValueType::w() const
372 void QQuickVector4DValueType::setX(qreal x)
377 void QQuickVector4DValueType::setY(qreal y)
382 void QQuickVector4DValueType::setZ(qreal z)
387 void QQuickVector4DValueType::setW(qreal w)
392 qreal QQuickVector4DValueType::dotProduct(const QVector4D &vec) const
394 return QVector4D::dotProduct(v, vec);
397 QVector4D QQuickVector4DValueType::times(const QVector4D &vec) const
402 QVector4D QQuickVector4DValueType::times(const QMatrix4x4 &m) const
407 QVector4D QQuickVector4DValueType::times(qreal scalar) const
412 QVector4D QQuickVector4DValueType::plus(const QVector4D &vec) const
417 QVector4D QQuickVector4DValueType::minus(const QVector4D &vec) const
422 QVector4D QQuickVector4DValueType::normalized() const
424 return v.normalized();
427 qreal QQuickVector4DValueType::length() const
432 QVector2D QQuickVector4DValueType::toVector2d() const
434 return v.toVector2D();
437 QVector3D QQuickVector4DValueType::toVector3d() const
439 return v.toVector3D();
442 bool QQuickVector4DValueType::fuzzyEquals(const QVector4D &vec, qreal epsilon) const
444 qreal absEps = qAbs(epsilon);
445 if (qAbs(v.x() - vec.x()) > absEps)
447 if (qAbs(v.y() - vec.y()) > absEps)
449 if (qAbs(v.z() - vec.z()) > absEps)
451 if (qAbs(v.w() - vec.w()) > absEps)
456 bool QQuickVector4DValueType::fuzzyEquals(const QVector4D &vec) const
458 return qFuzzyCompare(v, vec);
461 QQuickQuaternionValueType::QQuickQuaternionValueType(QObject *parent)
462 : QQmlValueTypeBase<QQuaternion>(QMetaType::QQuaternion, parent)
466 QString QQuickQuaternionValueType::toString() const
468 return QString(QLatin1String("QQuaternion(%1, %2, %3, %4)")).arg(v.scalar()).arg(v.x()).arg(v.y()).arg(v.z());
471 qreal QQuickQuaternionValueType::scalar() const
476 qreal QQuickQuaternionValueType::x() const
481 qreal QQuickQuaternionValueType::y() const
486 qreal QQuickQuaternionValueType::z() const
491 void QQuickQuaternionValueType::setScalar(qreal scalar)
496 void QQuickQuaternionValueType::setX(qreal x)
501 void QQuickQuaternionValueType::setY(qreal y)
506 void QQuickQuaternionValueType::setZ(qreal z)
512 QQuickMatrix4x4ValueType::QQuickMatrix4x4ValueType(QObject *parent)
513 : QQmlValueTypeBase<QMatrix4x4>(QMetaType::QMatrix4x4, parent)
517 QMatrix4x4 QQuickMatrix4x4ValueType::times(const QMatrix4x4 &m) const
522 QVector4D QQuickMatrix4x4ValueType::times(const QVector4D &vec) const
527 QVector3D QQuickMatrix4x4ValueType::times(const QVector3D &vec) const
532 QMatrix4x4 QQuickMatrix4x4ValueType::times(qreal factor) const
537 QMatrix4x4 QQuickMatrix4x4ValueType::plus(const QMatrix4x4 &m) const
542 QMatrix4x4 QQuickMatrix4x4ValueType::minus(const QMatrix4x4 &m) const
547 QVector4D QQuickMatrix4x4ValueType::row(int n) const
552 QVector4D QQuickMatrix4x4ValueType::column(int m) const
557 qreal QQuickMatrix4x4ValueType::determinant() const
559 return v.determinant();
562 QMatrix4x4 QQuickMatrix4x4ValueType::inverted() const
567 QMatrix4x4 QQuickMatrix4x4ValueType::transposed() const
569 return v.transposed();
572 bool QQuickMatrix4x4ValueType::fuzzyEquals(const QMatrix4x4 &m, qreal epsilon) const
574 qreal absEps = qAbs(epsilon);
575 for (int i = 0; i < 4; ++i) {
576 for (int j = 0; j < 4; ++j) {
577 if (qAbs(v(i,j) - m(i,j)) > absEps) {
585 bool QQuickMatrix4x4ValueType::fuzzyEquals(const QMatrix4x4 &m) const
587 return qFuzzyCompare(v, m);
590 QString QQuickMatrix4x4ValueType::toString() const
592 return QString(QLatin1String("QMatrix4x4(%1, %2, %3, %4, %5, %6, %7, %8, %9, %10, %11, %12, %13, %14, %15, %16)"))
593 .arg(v(0, 0)).arg(v(0, 1)).arg(v(0, 2)).arg(v(0, 3))
594 .arg(v(1, 0)).arg(v(1, 1)).arg(v(1, 2)).arg(v(1, 3))
595 .arg(v(2, 0)).arg(v(2, 1)).arg(v(2, 2)).arg(v(2, 3))
596 .arg(v(3, 0)).arg(v(3, 1)).arg(v(3, 2)).arg(v(3, 3));
599 bool QQuickMatrix4x4ValueType::isEqual(const QVariant &other) const
601 if (other.userType() != qMetaTypeId<QMatrix4x4>())
604 QMatrix4x4 otherMatrix = other.value<QMatrix4x4>();
605 return (v == otherMatrix);
609 QQuickFontValueType::QQuickFontValueType(QObject *parent)
610 : QQmlValueTypeBase<QFont>(QMetaType::QFont, parent),
616 void QQuickFontValueType::onLoad()
618 pixelSizeSet = false;
619 pointSizeSet = false;
622 QString QQuickFontValueType::toString() const
624 return QString(QLatin1String("QFont(%1)")).arg(v.toString());
627 QString QQuickFontValueType::family() const
632 void QQuickFontValueType::setFamily(const QString &family)
637 bool QQuickFontValueType::bold() const
642 void QQuickFontValueType::setBold(bool b)
647 QQuickFontValueType::FontWeight QQuickFontValueType::weight() const
649 return (QQuickFontValueType::FontWeight)v.weight();
652 void QQuickFontValueType::setWeight(QQuickFontValueType::FontWeight w)
654 v.setWeight((QFont::Weight)w);
657 bool QQuickFontValueType::italic() const
662 void QQuickFontValueType::setItalic(bool b)
667 bool QQuickFontValueType::underline() const
669 return v.underline();
672 void QQuickFontValueType::setUnderline(bool b)
677 bool QQuickFontValueType::overline() const
682 void QQuickFontValueType::setOverline(bool b)
687 bool QQuickFontValueType::strikeout() const
689 return v.strikeOut();
692 void QQuickFontValueType::setStrikeout(bool b)
697 qreal QQuickFontValueType::pointSize() const
699 if (v.pointSizeF() == -1) {
701 dpi = qt_defaultDpi();
702 return v.pixelSize() * qreal(72.) / qreal(dpi);
704 return v.pointSizeF();
707 void QQuickFontValueType::setPointSize(qreal size)
710 qWarning() << "Both point size and pixel size set. Using pixel size.";
716 v.setPointSizeF(size);
718 pointSizeSet = false;
722 int QQuickFontValueType::pixelSize() const
724 if (v.pixelSize() == -1) {
726 dpi = qt_defaultDpi();
727 return (v.pointSizeF() * dpi) / qreal(72.);
729 return v.pixelSize();
732 void QQuickFontValueType::setPixelSize(int size)
736 qWarning() << "Both point size and pixel size set. Using pixel size.";
737 v.setPixelSize(size);
740 pixelSizeSet = false;
744 QQuickFontValueType::Capitalization QQuickFontValueType::capitalization() const
746 return (QQuickFontValueType::Capitalization)v.capitalization();
749 void QQuickFontValueType::setCapitalization(QQuickFontValueType::Capitalization c)
751 v.setCapitalization((QFont::Capitalization)c);
754 qreal QQuickFontValueType::letterSpacing() const
756 return v.letterSpacing();
759 void QQuickFontValueType::setLetterSpacing(qreal size)
761 v.setLetterSpacing(QFont::AbsoluteSpacing, size);
764 qreal QQuickFontValueType::wordSpacing() const
766 return v.wordSpacing();
769 void QQuickFontValueType::setWordSpacing(qreal size)
771 v.setWordSpacing(size);