From f5cb65b35e076facbce45e896902a34da7036135 Mon Sep 17 00:00:00 2001 From: Chris Adams Date: Tue, 26 Jun 2012 18:02:35 +1000 Subject: Fix broken value-type support by allowing property definition In QtQuick 1.x the "variant" property type was supported, which could be used to allow value type properties to be defined in QML. In QtQuick 2.0, we have deprecated the "variant" property, but its replacement ("var") is not suited for defining lightweight C++ type values (such as QColor, QFont, QRectF, QVector3D etc). This commit allows those QML basic types to be used in QML once more, by supporting them in the property definition syntax. Note that since some value types are provided by QtQuick and others are provided by QtQml, if a client imports only QtQml they can define but not use properties of certain types (eg, font). Task-number: QTBUG-21034 Task-number: QTBUG-18217 Change-Id: Ia951a8522f223408d27293bb96c276281a710277 Reviewed-by: Matthew Vogt --- tests/auto/qml/qqmlqt/data/font.qml | 8 +++ tests/auto/qml/qqmlqt/data/matrix4x4.qml | 9 +++ tests/auto/qml/qqmlqt/data/quaternion.qml | 8 +++ tests/auto/qml/qqmlqt/data/vector2.qml | 8 +++ tests/auto/qml/qqmlqt/tst_qqmlqt.cpp | 100 ++++++++++++++++++++++++++++-- 5 files changed, 128 insertions(+), 5 deletions(-) create mode 100644 tests/auto/qml/qqmlqt/data/font.qml create mode 100644 tests/auto/qml/qqmlqt/data/matrix4x4.qml create mode 100644 tests/auto/qml/qqmlqt/data/quaternion.qml create mode 100644 tests/auto/qml/qqmlqt/data/vector2.qml (limited to 'tests/auto/qml/qqmlqt') diff --git a/tests/auto/qml/qqmlqt/data/font.qml b/tests/auto/qml/qqmlqt/data/font.qml new file mode 100644 index 0000000000..9ebf460caf --- /dev/null +++ b/tests/auto/qml/qqmlqt/data/font.qml @@ -0,0 +1,8 @@ +import QtQuick 2.0 + +QtObject { + property variant test1: Qt.font({ family: "Arial", pointSize: 22 }); + property variant test2: Qt.font({ family: "Arial", pointSize: 20, weight: Font.DemiBold, italic: true }); + property variant test3: Qt.font("Arial", 22); + property variant test4: Qt.font({ something: "Arial", other: 22 }); +} diff --git a/tests/auto/qml/qqmlqt/data/matrix4x4.qml b/tests/auto/qml/qqmlqt/data/matrix4x4.qml new file mode 100644 index 0000000000..0185fcb635 --- /dev/null +++ b/tests/auto/qml/qqmlqt/data/matrix4x4.qml @@ -0,0 +1,9 @@ +import QtQuick 2.0 + +QtObject { + property variant test1: Qt.matrix4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16); + property variant test2: Qt.matrix4x4([1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4]); + property variant test3: Qt.matrix4x4(1,2,3,4,5,6); + property variant test4: Qt.matrix4x4([1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5]); + property variant test5: Qt.matrix4x4({ test: 5, subprop: "hello" }); +} diff --git a/tests/auto/qml/qqmlqt/data/quaternion.qml b/tests/auto/qml/qqmlqt/data/quaternion.qml new file mode 100644 index 0000000000..6203bd1e32 --- /dev/null +++ b/tests/auto/qml/qqmlqt/data/quaternion.qml @@ -0,0 +1,8 @@ +import QtQuick 2.0 + +QtObject { + property variant test1: Qt.quaternion(2, 17, 0.9, 0.6); + property variant test2: Qt.quaternion(102, -10, -982.1, 10); + property variant test3: Qt.quaternion(102, -10, -982.1); + property variant test4: Qt.quaternion(102, -10, -982.1, 10, 15); +} diff --git a/tests/auto/qml/qqmlqt/data/vector2.qml b/tests/auto/qml/qqmlqt/data/vector2.qml new file mode 100644 index 0000000000..1ca513eaba --- /dev/null +++ b/tests/auto/qml/qqmlqt/data/vector2.qml @@ -0,0 +1,8 @@ +import QtQuick 2.0 + +QtObject { + property variant test1: Qt.vector2d(1, 0.9); + property variant test2: Qt.vector2d(102, -982.1); + property variant test3: Qt.vector2d(102); + property variant test4: Qt.vector2d(102, -982.1, 10); +} diff --git a/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp b/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp index c81e6771b8..6fd6fc8822 100644 --- a/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp +++ b/tests/auto/qml/qqmlqt/tst_qqmlqt.cpp @@ -48,12 +48,15 @@ #include #include #include -#include #include #include #include +#include #include #include +#include +#include +#include #include "../../shared/util.h" class tst_qqmlqt : public QQmlDataTest @@ -69,8 +72,12 @@ private slots: void rect(); void point(); void size(); - void vector(); + void vector2d(); + void vector3d(); void vector4d(); + void quaternion(); + void matrix4x4(); + void font(); void lighter(); void darker(); void tint(); @@ -217,12 +224,32 @@ void tst_qqmlqt::size() delete object; } -void tst_qqmlqt::vector() +void tst_qqmlqt::vector2d() +{ + QQmlComponent component(&engine, testFileUrl("vector2.qml")); + + QString warning1 = component.url().toString() + ":6: Error: Qt.vector2d(): Invalid arguments"; + QString warning2 = component.url().toString() + ":7: Error: Qt.vector2d(): Invalid arguments"; + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1)); + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2)); + + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(qvariant_cast(object->property("test1")), QVector2D(1, 0.9)); + QCOMPARE(qvariant_cast(object->property("test2")), QVector2D(102, -982.1)); + QCOMPARE(qvariant_cast(object->property("test3")), QVector2D()); + QCOMPARE(qvariant_cast(object->property("test4")), QVector2D()); + + delete object; +} + +void tst_qqmlqt::vector3d() { QQmlComponent component(&engine, testFileUrl("vector.qml")); - QString warning1 = component.url().toString() + ":6: Error: Qt.vector(): Invalid arguments"; - QString warning2 = component.url().toString() + ":7: Error: Qt.vector(): Invalid arguments"; + QString warning1 = component.url().toString() + ":6: Error: Qt.vector3d(): Invalid arguments"; + QString warning2 = component.url().toString() + ":7: Error: Qt.vector3d(): Invalid arguments"; QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1)); QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2)); @@ -257,6 +284,69 @@ void tst_qqmlqt::vector4d() delete object; } +void tst_qqmlqt::quaternion() +{ + QQmlComponent component(&engine, testFileUrl("quaternion.qml")); + + QString warning1 = component.url().toString() + ":6: Error: Qt.quaternion(): Invalid arguments"; + QString warning2 = component.url().toString() + ":7: Error: Qt.quaternion(): Invalid arguments"; + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1)); + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2)); + + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(qvariant_cast(object->property("test1")), QQuaternion(2, 17, 0.9, 0.6)); + QCOMPARE(qvariant_cast(object->property("test2")), QQuaternion(102, -10, -982.1, 10)); + QCOMPARE(qvariant_cast(object->property("test3")), QQuaternion()); + QCOMPARE(qvariant_cast(object->property("test4")), QQuaternion()); + + delete object; +} + +void tst_qqmlqt::matrix4x4() +{ + QQmlComponent component(&engine, testFileUrl("matrix4x4.qml")); + + QString warning1 = component.url().toString() + ":6: Error: Qt.matrix4x4(): Invalid arguments"; + QString warning2 = component.url().toString() + ":7: Error: Qt.matrix4x4(): Invalid argument: not a valid matrix4x4 values array"; + QString warning3 = component.url().toString() + ":8: Error: Qt.matrix4x4(): Invalid argument: not a valid matrix4x4 values array"; + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1)); + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2)); + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning3)); + + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(qvariant_cast(object->property("test1")), QMatrix4x4(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16)); + QCOMPARE(qvariant_cast(object->property("test2")), QMatrix4x4(1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4)); + QCOMPARE(qvariant_cast(object->property("test3")), QMatrix4x4()); + QCOMPARE(qvariant_cast(object->property("test4")), QMatrix4x4()); + QCOMPARE(qvariant_cast(object->property("test5")), QMatrix4x4()); + + delete object; +} + +void tst_qqmlqt::font() +{ + QQmlComponent component(&engine, testFileUrl("font.qml")); + + QString warning1 = component.url().toString() + ":6: Error: Qt.font(): Invalid arguments"; + QString warning2 = component.url().toString() + ":7: Error: Qt.font(): Invalid argument: no valid font subproperties specified"; + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning1)); + QTest::ignoreMessage(QtWarningMsg, qPrintable(warning2)); + + QObject *object = component.create(); + QVERIFY(object != 0); + + QCOMPARE(qvariant_cast(object->property("test1")), QFont("Arial", 22)); + QCOMPARE(qvariant_cast(object->property("test2")), QFont("Arial", 20, QFont::DemiBold, true)); + QCOMPARE(qvariant_cast(object->property("test3")), QFont()); + QCOMPARE(qvariant_cast(object->property("test4")), QFont()); + + delete object; +} + void tst_qqmlqt::lighter() { QQmlComponent component(&engine, testFileUrl("lighter.qml")); -- cgit v1.2.3