From 062e30769c2b04a5d8d0906c39a2bcb35defd3a2 Mon Sep 17 00:00:00 2001 From: Aaron Kennedy Date: Wed, 7 Apr 2010 13:00:08 +1000 Subject: Allow iteration over the Item.children property QTBUG-9645 --- src/gui/graphicsview/qgraphicsitem.cpp | 20 +++++++++++-- src/gui/graphicsview/qgraphicsitem_p.h | 5 +++- .../qdeclarativeitem/data/childrenProperty.qml | 14 +++++++++ .../qdeclarativeitem/data/resourcesProperty.qml | 21 ++++++++++++++ .../qdeclarativeitem/tst_qdeclarativeitem.cpp | 33 ++++++++++++++++++++++ 5 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativeitem/data/childrenProperty.qml create mode 100644 tests/auto/declarative/qdeclarativeitem/data/resourcesProperty.qml diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp index f106b3d..3245aec 100644 --- a/src/gui/graphicsview/qgraphicsitem.cpp +++ b/src/gui/graphicsview/qgraphicsitem.cpp @@ -7614,11 +7614,26 @@ void QGraphicsObject::updateMicroFocus() QGraphicsItem::updateMicroFocus(); } -void QGraphicsItemPrivate::append(QDeclarativeListProperty *list, QGraphicsObject *item) +void QGraphicsItemPrivate::children_append(QDeclarativeListProperty *list, QGraphicsObject *item) { QGraphicsItemPrivate::get(item)->setParentItemHelper(static_cast(list->object), /*newParentVariant=*/0, /*thisPointerVariant=*/0); } +int QGraphicsItemPrivate::children_count(QDeclarativeListProperty *list) +{ + QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast(list->object)); + return d->children.count(); +} + +QGraphicsObject *QGraphicsItemPrivate::children_at(QDeclarativeListProperty *list, int index) +{ + QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast(list->object)); + if (index >= 0 && index < d->children.count()) + return d->children.at(index)->toGraphicsObject(); + else + return 0; +} + /*! Returns a list of this item's children. @@ -7631,7 +7646,8 @@ QDeclarativeListProperty QGraphicsItemPrivate::childrenList() Q_Q(QGraphicsItem); if (isObject) { QGraphicsObject *that = static_cast(q); - return QDeclarativeListProperty(that, &children, QGraphicsItemPrivate::append); + return QDeclarativeListProperty(that, &children, children_append, + children_count, children_at); } else { //QGraphicsItem is not supported for this property return QDeclarativeListProperty(); diff --git a/src/gui/graphicsview/qgraphicsitem_p.h b/src/gui/graphicsview/qgraphicsitem_p.h index 73b8f04..922581d 100644 --- a/src/gui/graphicsview/qgraphicsitem_p.h +++ b/src/gui/graphicsview/qgraphicsitem_p.h @@ -480,9 +480,12 @@ public: void resetFocusProxy(); virtual void subFocusItemChange(); + static void children_append(QDeclarativeListProperty *list, QGraphicsObject *item); + static int children_count(QDeclarativeListProperty *list); + static QGraphicsObject *children_at(QDeclarativeListProperty *list, int); + inline QTransform transformToParent() const; inline void ensureSortedChildren(); - static void append(QDeclarativeListProperty *list, QGraphicsObject *item); static inline bool insertionOrder(QGraphicsItem *a, QGraphicsItem *b); void ensureSequentialSiblingIndex(); inline void sendScenePosChange(); diff --git a/tests/auto/declarative/qdeclarativeitem/data/childrenProperty.qml b/tests/auto/declarative/qdeclarativeitem/data/childrenProperty.qml new file mode 100644 index 0000000..dcd4061 --- /dev/null +++ b/tests/auto/declarative/qdeclarativeitem/data/childrenProperty.qml @@ -0,0 +1,14 @@ +import Qt 4.6 + +Item { + id: root + + property bool test1: root.children.length == 3 + property bool test2: root.children[0] == item1 + property bool test3: root.children[1] == item2 + property bool test4: root.children[2] == item3 + property bool test5: root.children[3] == null + + children: [ Item { id: item1 }, Item { id: item2 }, Item { id: item3 } ] +} + diff --git a/tests/auto/declarative/qdeclarativeitem/data/resourcesProperty.qml b/tests/auto/declarative/qdeclarativeitem/data/resourcesProperty.qml new file mode 100644 index 0000000..fa299be --- /dev/null +++ b/tests/auto/declarative/qdeclarativeitem/data/resourcesProperty.qml @@ -0,0 +1,21 @@ +import Qt 4.6 + +Item { + id: root + + property bool test1 + property bool test2 + property bool test3 + property bool test4 + property bool test5 + + Component.onCompleted: { + test1 = (root.resources.length >= 3) + test2 = root.resources[0] == item1 + test3 = root.resources[1] == item2 + test4 = root.resources[2] == item3 + test5 = root.resources[10] == null + } + + resources: [ Item { id: item1 }, Item { id: item2 }, Item { id: item3 } ] +} diff --git a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp index 46f3517..a6171ae 100644 --- a/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp +++ b/tests/auto/declarative/qdeclarativeitem/tst_qdeclarativeitem.cpp @@ -64,6 +64,9 @@ private slots: void transforms(); void transforms_data(); + void childrenProperty(); + void resourcesProperty(); + private: template T *findItem(QGraphicsObject *parent, const QString &objectName); @@ -429,6 +432,36 @@ void tst_QDeclarativeItem::transforms() QCOMPARE(item->sceneMatrix(), matrix); } +void tst_QDeclarativeItem::childrenProperty() +{ + QDeclarativeComponent component(&engine, SRCDIR "/data/childrenProperty.qml"); + + QObject *o = component.create(); + QVERIFY(o != 0); + + QCOMPARE(o->property("test1").toBool(), true); + QCOMPARE(o->property("test2").toBool(), true); + QCOMPARE(o->property("test3").toBool(), true); + QCOMPARE(o->property("test4").toBool(), true); + QCOMPARE(o->property("test5").toBool(), true); + delete o; +} + +void tst_QDeclarativeItem::resourcesProperty() +{ + QDeclarativeComponent component(&engine, SRCDIR "/data/resourcesProperty.qml"); + + QObject *o = component.create(); + QVERIFY(o != 0); + + QCOMPARE(o->property("test1").toBool(), true); + QCOMPARE(o->property("test2").toBool(), true); + QCOMPARE(o->property("test3").toBool(), true); + QCOMPARE(o->property("test4").toBool(), true); + QCOMPARE(o->property("test5").toBool(), true); + delete o; +} + void tst_QDeclarativeItem::propertyChanges() { QDeclarativeView *canvas = new QDeclarativeView(0); -- cgit v0.12