From 09b605ddcc33575316211156ff5979aa92e7e1fd Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Thu, 14 Jun 2012 10:18:45 +1000 Subject: [PATCH] Enable literal Qt enum assignment to enum properties. Assigning Qt global enums to enum properties was previously handled as a binding, rather than a literal assignment. Change-Id: If6bb65f63b34f4e10c0636221ddadb11f7025735 Reviewed-by: Chris Adams --- src/qml/qml/qqmlcompiler.cpp | 18 +++++++-------- .../qml/qqmllanguage/data/assignBasicTypes.qml | 8 +++++++ tests/auto/qml/qqmllanguage/testtypes.h | 27 ++++++++++++++++++++++ tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp | 6 +++++ 4 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/qml/qml/qqmlcompiler.cpp b/src/qml/qml/qqmlcompiler.cpp index 8450221..71c5bdc 100644 --- a/src/qml/qml/qqmlcompiler.cpp +++ b/src/qml/qml/qqmlcompiler.cpp @@ -2480,6 +2480,12 @@ bool QQmlCompiler::buildPropertyLiteralAssignment(QQmlScript::Property *prop, return true; } +struct StaticQtMetaObject : public QObject +{ + static const QMetaObject *get() + { return &static_cast (0)->staticQtMetaObject; } +}; + bool QQmlCompiler::testQualifiedEnumAssignment(QQmlScript::Property *prop, QQmlScript::Object *obj, QQmlScript::Value *v, @@ -2523,13 +2529,13 @@ bool QQmlCompiler::testQualifiedEnumAssignment(QQmlScript::Property *prop, QQmlType *type = 0; unit->imports().resolveType(typeName, &type, 0, 0, 0, 0); - if (!type) + if (!type && typeName != QLatin1String("Qt")) return true; int value = 0; bool ok; - if (toQmlType(obj) == type) { + if (type && toQmlType(obj) == type) { // When these two match, we can short cut the search if (mprop.isFlagType()) { value = mprop.enumerator().keysToValue(enumValue.toUtf8().constData(), &ok); @@ -2540,7 +2546,7 @@ bool QQmlCompiler::testQualifiedEnumAssignment(QQmlScript::Property *prop, // Otherwise we have to search the whole type // This matches the logic in QV8TypeWrapper QByteArray enumName = enumValue.toUtf8(); - const QMetaObject *metaObject = type->baseMetaObject(); + const QMetaObject *metaObject = type ? type->baseMetaObject() : StaticQtMetaObject::get(); ok = false; for (int ii = metaObject->enumeratorCount() - 1; !ok && ii >= 0; --ii) { QMetaEnum e = metaObject->enumerator(ii); @@ -2558,12 +2564,6 @@ bool QQmlCompiler::testQualifiedEnumAssignment(QQmlScript::Property *prop, return true; } -struct StaticQtMetaObject : public QObject -{ - static const QMetaObject *get() - { return &static_cast (0)->staticQtMetaObject; } -}; - // Similar logic to above, but not knowing target property. int QQmlCompiler::evaluateEnum(const QHashedStringRef &scope, const QByteArray& enumValue) const { diff --git a/tests/auto/qml/qqmllanguage/data/assignBasicTypes.qml b/tests/auto/qml/qqmllanguage/data/assignBasicTypes.qml index 28a3401..697278e 100644 --- a/tests/auto/qml/qqmllanguage/data/assignBasicTypes.qml +++ b/tests/auto/qml/qqmllanguage/data/assignBasicTypes.qml @@ -2,6 +2,8 @@ import Test 1.0 MyTypeObject { flagProperty: "FlagVal1 | FlagVal3" enumProperty: "EnumVal2" + qtEnumProperty: Qt.RichText + mirroredEnumProperty: Qt.AlignHCenter stringProperty: "Hello World!" uintProperty: 10 intProperty: -19 @@ -25,4 +27,10 @@ MyTypeObject { urlProperty: "main.qml?with%3cencoded%3edata" objectProperty: MyTypeObject { intProperty: 8 } + + property bool qtEnumTriggeredChange: false + onQtEnumPropertyChanged: qtEnumTriggeredChange = true + + property bool mirroredEnumTriggeredChange: false + onMirroredEnumPropertyChanged: mirroredEnumTriggeredChange = true } diff --git a/tests/auto/qml/qqmllanguage/testtypes.h b/tests/auto/qml/qqmllanguage/testtypes.h index b601a54..f84e42f 100644 --- a/tests/auto/qml/qqmllanguage/testtypes.h +++ b/tests/auto/qml/qqmllanguage/testtypes.h @@ -209,6 +209,7 @@ class MyTypeObject : public QObject { Q_OBJECT Q_ENUMS(MyEnum) + Q_ENUMS(MyMirroredEnum) Q_FLAGS(MyFlags) Q_PROPERTY(QString id READ id WRITE setId) @@ -217,6 +218,8 @@ class MyTypeObject : public QObject Q_PROPERTY(MyFlags flagProperty READ flagProperty WRITE setFlagProperty NOTIFY flagPropertyChanged) Q_PROPERTY(MyEnum enumProperty READ enumProperty WRITE setEnumProperty NOTIFY enumPropertyChanged) Q_PROPERTY(MyEnum readOnlyEnumProperty READ readOnlyEnumProperty) + Q_PROPERTY(Qt::TextFormat qtEnumProperty READ qtEnumProperty WRITE setQtEnumProperty NOTIFY qtEnumPropertyChanged) + Q_PROPERTY(MyMirroredEnum mirroredEnumProperty READ mirroredEnumProperty WRITE setMirroredEnumProperty NOTIFY mirroredEnumPropertyChanged) Q_PROPERTY(QString stringProperty READ stringProperty WRITE setStringProperty NOTIFY stringPropertyChanged) Q_PROPERTY(uint uintProperty READ uintProperty WRITE setUintProperty NOTIFY uintPropertyChanged) Q_PROPERTY(int intProperty READ intProperty WRITE setIntProperty NOTIFY intPropertyChanged) @@ -298,6 +301,28 @@ public: return EnumVal1; } + Qt::TextFormat qtEnumPropertyValue; + Qt::TextFormat qtEnumProperty() const { + return qtEnumPropertyValue; + } + void setQtEnumProperty(Qt::TextFormat v) { + qtEnumPropertyValue = v; + emit qtEnumPropertyChanged(); + } + + enum MyMirroredEnum { + MirroredEnumVal1 = Qt::AlignLeft, + MirroredEnumVal2 = Qt::AlignRight, + MirroredEnumVal3 = Qt::AlignHCenter }; + MyMirroredEnum mirroredEnumPropertyValue; + MyMirroredEnum mirroredEnumProperty() const { + return mirroredEnumPropertyValue; + } + void setMirroredEnumProperty(MyMirroredEnum v) { + mirroredEnumPropertyValue = v; + emit mirroredEnumPropertyChanged(); + } + QString stringPropertyValue; QString stringProperty() const { return stringPropertyValue; @@ -515,6 +540,8 @@ signals: void objectPropertyChanged(); void flagPropertyChanged(); void enumPropertyChanged(); + void qtEnumPropertyChanged(); + void mirroredEnumPropertyChanged(); void stringPropertyChanged(); void uintPropertyChanged(); void intPropertyChanged(); diff --git a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp index c6b0dd6..e324896 100644 --- a/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp +++ b/tests/auto/qml/qqmllanguage/tst_qqmllanguage.cpp @@ -579,6 +579,8 @@ void tst_qqmllanguage::assignBasicTypes() QVERIFY(object != 0); QCOMPARE(object->flagProperty(), MyTypeObject::FlagVal1 | MyTypeObject::FlagVal3); QCOMPARE(object->enumProperty(), MyTypeObject::EnumVal2); + QCOMPARE(object->qtEnumProperty(), Qt::RichText); + QCOMPARE(object->mirroredEnumProperty(), MyTypeObject::MirroredEnumVal3); QCOMPARE(object->stringProperty(), QString("Hello World!")); QCOMPARE(object->uintProperty(), uint(10)); QCOMPARE(object->intProperty(), -19); @@ -606,6 +608,10 @@ void tst_qqmllanguage::assignBasicTypes() MyTypeObject *child = qobject_cast(object->objectProperty()); QVERIFY(child != 0); QCOMPARE(child->intProperty(), 8); + + //these used to go via script. Ensure they no longer do + QCOMPARE(object->property("qtEnumTriggeredChange").toBool(), false); + QCOMPARE(object->property("mirroredEnumTriggeredChange").toBool(), false); } // Test edge case type assignments -- 2.7.4