From 3903deaad92f7c4c9e9258a86aeb50c5adca41c8 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Thu, 28 Jan 2010 11:51:53 +1000 Subject: Add reset methods to QmlMetaProperty --- src/declarative/qml/qmlmetaproperty.cpp | 25 ++++++ src/declarative/qml/qmlmetaproperty.h | 2 + .../qmlmetaproperty/tst_qmlmetaproperty.cpp | 93 ++++++++++++++++++++++ 3 files changed, 120 insertions(+) diff --git a/src/declarative/qml/qmlmetaproperty.cpp b/src/declarative/qml/qmlmetaproperty.cpp index 4717782..dbf6548 100644 --- a/src/declarative/qml/qmlmetaproperty.cpp +++ b/src/declarative/qml/qmlmetaproperty.cpp @@ -435,6 +435,17 @@ bool QmlMetaProperty::isDesignable() const } /*! + Returns true if the property is resettable, otherwise false. +*/ +bool QmlMetaProperty::isResettable() const +{ + if (type() & Property && d->core.isValid() && d->object) + return d->core.flags & QmlPropertyCache::Data::IsResettable; + else + return false; +} + +/*! Returns true if the QmlMetaProperty refers to a valid property, otherwise false. */ @@ -967,6 +978,20 @@ bool QmlMetaProperty::write(const QVariant &value) const return write(value, 0); } +/*! + Resets the property value. +*/ +bool QmlMetaProperty::reset() const +{ + if (isResettable()) { + void *args[] = { 0 }; + QMetaObject::metacall(d->object, QMetaObject::ResetProperty, d->core.coreIndex, args); + return true; + } else { + return false; + } +} + bool QmlMetaProperty::write(const QVariant &value, QmlMetaProperty::WriteFlags flags) const { if (d->object && type() & Property && d->core.isValid() && isWritable()) diff --git a/src/declarative/qml/qmlmetaproperty.h b/src/declarative/qml/qmlmetaproperty.h index dcb5905..240f5a2 100644 --- a/src/declarative/qml/qmlmetaproperty.h +++ b/src/declarative/qml/qmlmetaproperty.h @@ -88,6 +88,7 @@ public: enum WriteFlag { BypassInterceptor = 0x01, DontRemoveBinding = 0x02 }; Q_DECLARE_FLAGS(WriteFlags, WriteFlag) bool write(const QVariant &, QmlMetaProperty::WriteFlags) const; + bool reset() const; bool hasChangedNotifier() const; bool needsChangedNotifier() const; @@ -108,6 +109,7 @@ public: bool isDefault() const; bool isWritable() const; bool isDesignable() const; + bool isResettable() const; bool isValid() const; QObject *object() const; diff --git a/tests/auto/declarative/qmlmetaproperty/tst_qmlmetaproperty.cpp b/tests/auto/declarative/qmlmetaproperty/tst_qmlmetaproperty.cpp index a44ea6e..ec6b87f 100644 --- a/tests/auto/declarative/qmlmetaproperty/tst_qmlmetaproperty.cpp +++ b/tests/auto/declarative/qmlmetaproperty/tst_qmlmetaproperty.cpp @@ -113,6 +113,7 @@ private slots: void name(); void read(); void write(); + void reset(); // Functionality void writeObjectToList(); @@ -153,6 +154,7 @@ void tst_qmlmetaproperty::qmlmetaproperty() QCOMPARE(prop.isDefault(), false); QCOMPARE(prop.isWritable(), false); QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); QCOMPARE(prop.isValid(), false); QCOMPARE(prop.object(), (QObject *)0); QCOMPARE(prop.propertyCategory(), QmlMetaProperty::InvalidProperty); @@ -178,9 +180,12 @@ class PropertyObject : public QObject Q_PROPERTY(QRect rectProperty READ rectProperty); Q_PROPERTY(QRect wrectProperty READ wrectProperty WRITE setWRectProperty); Q_PROPERTY(QUrl url READ url WRITE setUrl); + Q_PROPERTY(int resettableProperty READ resettableProperty WRITE setResettableProperty RESET resetProperty); Q_CLASSINFO("DefaultProperty", "defaultProperty"); public: + PropertyObject() : m_resetProperty(9) {} + int defaultProperty() { return 10; } QRect rectProperty() { return QRect(10, 10, 1, 209); } @@ -190,10 +195,15 @@ public: QUrl url() { return m_url; } void setUrl(const QUrl &u) { m_url = u; } + int resettableProperty() const { return m_resetProperty; } + void setResettableProperty(int r) { m_resetProperty = r; } + void resetProperty() { m_resetProperty = 9; } + signals: void clicked(); private: + int m_resetProperty; QRect m_rect; QUrl m_url; }; @@ -230,6 +240,7 @@ void tst_qmlmetaproperty::qmlmetaproperty_object() QCOMPARE(prop.isDefault(), false); QCOMPARE(prop.isWritable(), false); QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); QCOMPARE(prop.isValid(), false); QCOMPARE(prop.object(), (QObject *)0); QCOMPARE(prop.propertyCategory(), QmlMetaProperty::InvalidProperty); @@ -276,6 +287,7 @@ void tst_qmlmetaproperty::qmlmetaproperty_object() QCOMPARE(prop.isDefault(), true); QCOMPARE(prop.isWritable(), false); QCOMPARE(prop.isDesignable(), true); + QCOMPARE(prop.isResettable(), false); QCOMPARE(prop.isValid(), true); QCOMPARE(prop.object(), &dobject); QCOMPARE(prop.propertyCategory(), QmlMetaProperty::Normal); @@ -329,6 +341,7 @@ void tst_qmlmetaproperty::qmlmetaproperty_object_string() QCOMPARE(prop.isDefault(), false); QCOMPARE(prop.isWritable(), false); QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); QCOMPARE(prop.isValid(), false); QCOMPARE(prop.object(), (QObject *)0); QCOMPARE(prop.propertyCategory(), QmlMetaProperty::InvalidProperty); @@ -375,6 +388,7 @@ void tst_qmlmetaproperty::qmlmetaproperty_object_string() QCOMPARE(prop.isDefault(), false); QCOMPARE(prop.isWritable(), false); QCOMPARE(prop.isDesignable(), true); + QCOMPARE(prop.isResettable(), false); QCOMPARE(prop.isValid(), true); QCOMPARE(prop.object(), &dobject); QCOMPARE(prop.propertyCategory(), QmlMetaProperty::Normal); @@ -423,6 +437,7 @@ void tst_qmlmetaproperty::qmlmetaproperty_object_string() QCOMPARE(prop.isDefault(), false); QCOMPARE(prop.isWritable(), false); QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); QCOMPARE(prop.isValid(), true); QCOMPARE(prop.object(), &dobject); QCOMPARE(prop.propertyCategory(), QmlMetaProperty::InvalidProperty); @@ -475,6 +490,7 @@ void tst_qmlmetaproperty::qmlmetaproperty_object_context() QCOMPARE(prop.isDefault(), false); QCOMPARE(prop.isWritable(), false); QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); QCOMPARE(prop.isValid(), false); QCOMPARE(prop.object(), (QObject *)0); QCOMPARE(prop.propertyCategory(), QmlMetaProperty::InvalidProperty); @@ -521,6 +537,7 @@ void tst_qmlmetaproperty::qmlmetaproperty_object_context() QCOMPARE(prop.isDefault(), true); QCOMPARE(prop.isWritable(), false); QCOMPARE(prop.isDesignable(), true); + QCOMPARE(prop.isResettable(), false); QCOMPARE(prop.isValid(), true); QCOMPARE(prop.object(), &dobject); QCOMPARE(prop.propertyCategory(), QmlMetaProperty::Normal); @@ -574,6 +591,7 @@ void tst_qmlmetaproperty::qmlmetaproperty_object_string_context() QCOMPARE(prop.isDefault(), false); QCOMPARE(prop.isWritable(), false); QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); QCOMPARE(prop.isValid(), false); QCOMPARE(prop.object(), (QObject *)0); QCOMPARE(prop.propertyCategory(), QmlMetaProperty::InvalidProperty); @@ -620,6 +638,7 @@ void tst_qmlmetaproperty::qmlmetaproperty_object_string_context() QCOMPARE(prop.isDefault(), false); QCOMPARE(prop.isWritable(), false); QCOMPARE(prop.isDesignable(), true); + QCOMPARE(prop.isResettable(), false); QCOMPARE(prop.isValid(), true); QCOMPARE(prop.object(), &dobject); QCOMPARE(prop.propertyCategory(), QmlMetaProperty::Normal); @@ -668,6 +687,7 @@ void tst_qmlmetaproperty::qmlmetaproperty_object_string_context() QCOMPARE(prop.isDefault(), false); QCOMPARE(prop.isWritable(), false); QCOMPARE(prop.isDesignable(), false); + QCOMPARE(prop.isResettable(), false); QCOMPARE(prop.isValid(), true); QCOMPARE(prop.object(), &dobject); QCOMPARE(prop.propertyCategory(), QmlMetaProperty::InvalidProperty); @@ -973,6 +993,79 @@ void tst_qmlmetaproperty::write() } } +void tst_qmlmetaproperty::reset() +{ + // Invalid + { + QmlMetaProperty p; + QCOMPARE(p.isResettable(), false); + QCOMPARE(p.reset(), false); + } + + // Read-only default prop + { + PropertyObject o; + QmlMetaProperty p(&o); + QCOMPARE(p.isResettable(), false); + QCOMPARE(p.reset(), false); + } + + // Invalid default prop + { + QObject o; + QmlMetaProperty p(&o); + QCOMPARE(p.isResettable(), false); + QCOMPARE(p.reset(), false); + } + + // Non-resettable-only prop by name + { + PropertyObject o; + QmlMetaProperty p(&o, QString("defaultProperty")); + QCOMPARE(p.isResettable(), false); + QCOMPARE(p.reset(), false); + } + + // Resettable prop by name + { + PropertyObject o; + QmlMetaProperty p(&o, QString("resettableProperty")); + + QCOMPARE(p.read(), QVariant(9)); + QCOMPARE(p.write(QVariant(11)), true); + QCOMPARE(p.read(), QVariant(11)); + + QCOMPARE(p.isResettable(), true); + QCOMPARE(p.reset(), true); + + QCOMPARE(p.read(), QVariant(9)); + } + + // Deleted object + { + PropertyObject *o = new PropertyObject; + + QmlMetaProperty p(o, QString("resettableProperty")); + + QCOMPARE(p.isResettable(), true); + QCOMPARE(p.reset(), true); + + delete o; + + QCOMPARE(p.isResettable(), false); + QCOMPARE(p.reset(), false); + } + + // Signal property + { + PropertyObject o; + QmlMetaProperty p(&o, "onClicked"); + + QCOMPARE(p.isResettable(), false); + QCOMPARE(p.reset(), false); + } +} + void tst_qmlmetaproperty::writeObjectToList() { QmlComponent containerComponent(&engine); -- cgit v0.12