From 34b805d66c09fb324d1b6a7bcf259e8743ef3894 Mon Sep 17 00:00:00 2001 From: Thomas Hartmann Date: Tue, 14 Sep 2010 16:49:57 +0200 Subject: Completing the interface for children/data/resources This patch makes the handling of properties like children and data easier in Bauhaus. Especially in the case of property alias Reviewed-by: Aaron Kennedy --- .../graphicsitems/qdeclarativeflickable.cpp | 54 ++++++++++++++++++- .../graphicsitems/qdeclarativeflickable_p_p.h | 3 ++ src/declarative/graphicsitems/qdeclarativeitem.cpp | 62 +++++++++++++++++++++- src/declarative/graphicsitems/qdeclarativeitem_p.h | 4 ++ src/gui/graphicsview/qgraphicsitem.cpp | 9 +++- src/gui/graphicsview/qgraphicsitem_p.h | 1 + 6 files changed, 129 insertions(+), 4 deletions(-) diff --git a/src/declarative/graphicsitems/qdeclarativeflickable.cpp b/src/declarative/graphicsitems/qdeclarativeflickable.cpp index c0b664f..bf3d88b 100644 --- a/src/declarative/graphicsitems/qdeclarativeflickable.cpp +++ b/src/declarative/graphicsitems/qdeclarativeflickable.cpp @@ -1051,10 +1051,62 @@ void QDeclarativeFlickablePrivate::data_append(QDeclarativeListProperty o->setParent(prop->object); } +static inline int children_count_helper(QGraphicsObject *object) +{ + QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(object); + return d->children.count(); +} + +static inline QObject *children_at_helper(QGraphicsObject *object, int index) +{ + QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(object); + if (index >= 0 && index < d->children.count()) + return d->children.at(index)->toGraphicsObject(); + else + return 0; +} + +static inline void children_clear_helper(QGraphicsObject *object) +{ + QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(object); + for (int index = 0 ;index < d->children.count();index++) + QGraphicsItemPrivate::get(d->children.at(index))->setParentItemHelper(0, /*newParentVariant=*/0, /*thisPointerVariant=*/0); +} + +int QDeclarativeFlickablePrivate::data_count(QDeclarativeListProperty *prop) +{ + return QDeclarativeItemPrivate::resources_count(prop) + + children_count_helper(static_cast(prop->data)->contentItem); +} + +QObject *QDeclarativeFlickablePrivate::data_at(QDeclarativeListProperty *prop, int i) +{ + int resourcesCount = QDeclarativeItemPrivate::resources_count(prop); + if (i < resourcesCount) + return QDeclarativeItemPrivate::resources_at(prop, i); + const int j = i - resourcesCount; + QGraphicsObject *contentObject = static_cast(prop->data)->contentItem; + if (j < children_count_helper(contentObject)) + children_at_helper(contentObject, j); + return 0; +} + +void QDeclarativeFlickablePrivate::data_clear(QDeclarativeListProperty *prop) +{ + QDeclarativeItemPrivate::resources_clear(prop); + QGraphicsObject *contentObject = + static_cast(prop->data)->contentItem; + children_clear_helper(contentObject); +} + QDeclarativeListProperty QDeclarativeFlickable::flickableData() { Q_D(QDeclarativeFlickable); - return QDeclarativeListProperty(this, (void *)d, QDeclarativeFlickablePrivate::data_append); + return QDeclarativeListProperty(this, (void *)d, QDeclarativeFlickablePrivate::data_append, + QDeclarativeFlickablePrivate::data_count, + QDeclarativeFlickablePrivate::data_at, + QDeclarativeFlickablePrivate::data_clear + ); } QDeclarativeListProperty QDeclarativeFlickable::flickableChildren() diff --git a/src/declarative/graphicsitems/qdeclarativeflickable_p_p.h b/src/declarative/graphicsitems/qdeclarativeflickable_p_p.h index 2da034c..beee741 100644 --- a/src/declarative/graphicsitems/qdeclarativeflickable_p_p.h +++ b/src/declarative/graphicsitems/qdeclarativeflickable_p_p.h @@ -173,6 +173,9 @@ public: // flickableData property static void data_append(QDeclarativeListProperty *, QObject *); + static int data_count(QDeclarativeListProperty *); + static QObject *data_at(QDeclarativeListProperty *, int); + static void data_clear(QDeclarativeListProperty *); }; class QDeclarativeFlickableVisibleArea : public QObject diff --git a/src/declarative/graphicsitems/qdeclarativeitem.cpp b/src/declarative/graphicsitems/qdeclarativeitem.cpp index 11f9179..53a4710 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem.cpp +++ b/src/declarative/graphicsitems/qdeclarativeitem.cpp @@ -44,6 +44,7 @@ #include "private/qdeclarativeevents_p_p.h" #include +#include #include #include @@ -1619,6 +1620,50 @@ void QDeclarativeItemPrivate::data_append(QDeclarativeListProperty *pro } } +static inline int children_count_helper(QDeclarativeListProperty *prop) +{ + QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast(prop->object)); + return d->children.count(); +} + +static inline QObject *children_at_helper(QDeclarativeListProperty *prop, int index) +{ + QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast(prop->object)); + if (index >= 0 && index < d->children.count()) + return d->children.at(index)->toGraphicsObject(); + else + return 0; +} + +static inline void children_clear_helper(QDeclarativeListProperty *prop) +{ + QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast(prop->object)); + for (int index = 0 ;index < d->children.count();index++) + QGraphicsItemPrivate::get(d->children.at(index))->setParentItemHelper(0, /*newParentVariant=*/0, /*thisPointerVariant=*/0); +} + +int QDeclarativeItemPrivate::data_count(QDeclarativeListProperty *prop) +{ + return resources_count(prop) + children_count_helper(prop); +} + +QObject *QDeclarativeItemPrivate::data_at(QDeclarativeListProperty *prop, int i) +{ + int resourcesCount = resources_count(prop); + if (i < resourcesCount) + return resources_at(prop, i); + const int j = i - resourcesCount; + if (j < children_count_helper(prop)) + children_at_helper(prop, j); + return 0; +} + +void QDeclarativeItemPrivate::data_clear(QDeclarativeListProperty *prop) +{ + resources_clear(prop); + children_clear_helper(prop); +} + QObject *QDeclarativeItemPrivate::resources_at(QDeclarativeListProperty *prop, int index) { const QObjectList children = prop->object->children(); @@ -1638,6 +1683,13 @@ int QDeclarativeItemPrivate::resources_count(QDeclarativeListProperty * return prop->object->children().count(); } +void QDeclarativeItemPrivate::resources_clear(QDeclarativeListProperty *prop) +{ + const QObjectList children = prop->object->children(); + for (int index = 0; index < children.count(); index++) + children.at(index)->setParent(0); +} + int QDeclarativeItemPrivate::transform_count(QDeclarativeListProperty *list) { QGraphicsObject *object = qobject_cast(list->object); @@ -1724,7 +1776,11 @@ void QDeclarativeItemPrivate::parentProperty(QObject *o, void *rv, QDeclarativeN QDeclarativeListProperty QDeclarativeItemPrivate::data() { - return QDeclarativeListProperty(q_func(), 0, QDeclarativeItemPrivate::data_append); + return QDeclarativeListProperty(q_func(), 0, QDeclarativeItemPrivate::data_append, + QDeclarativeItemPrivate::data_count, + QDeclarativeItemPrivate::data_at, + QDeclarativeItemPrivate::data_clear + ); } /*! @@ -2413,7 +2469,9 @@ QDeclarativeListProperty QDeclarativeItemPrivate::resources() { return QDeclarativeListProperty(q_func(), 0, QDeclarativeItemPrivate::resources_append, QDeclarativeItemPrivate::resources_count, - QDeclarativeItemPrivate::resources_at); + QDeclarativeItemPrivate::resources_at, + QDeclarativeItemPrivate::resources_clear + ); } /*! diff --git a/src/declarative/graphicsitems/qdeclarativeitem_p.h b/src/declarative/graphicsitems/qdeclarativeitem_p.h index fffb4f7..f85fa27 100644 --- a/src/declarative/graphicsitems/qdeclarativeitem_p.h +++ b/src/declarative/graphicsitems/qdeclarativeitem_p.h @@ -176,11 +176,15 @@ public: // data property static void data_append(QDeclarativeListProperty *, QObject *); + static int data_count(QDeclarativeListProperty *); + static QObject *data_at(QDeclarativeListProperty *, int); + static void data_clear(QDeclarativeListProperty *); // resources property static QObject *resources_at(QDeclarativeListProperty *, int); static void resources_append(QDeclarativeListProperty *, QObject *); static int resources_count(QDeclarativeListProperty *); + static void resources_clear(QDeclarativeListProperty *); // transform property static int transform_count(QDeclarativeListProperty *list); diff --git a/src/gui/graphicsview/qgraphicsitem.cpp b/src/gui/graphicsview/qgraphicsitem.cpp index 7a5b8de..9e43f2f 100644 --- a/src/gui/graphicsview/qgraphicsitem.cpp +++ b/src/gui/graphicsview/qgraphicsitem.cpp @@ -7676,6 +7676,13 @@ QGraphicsObject *QGraphicsItemPrivate::children_at(QDeclarativeListProperty *list) +{ + QGraphicsItemPrivate *d = QGraphicsItemPrivate::get(static_cast(list->object)); + for (int index = 0 ;index < d->children.count();index++) + QGraphicsItemPrivate::get(d->children.at(index))->setParentItemHelper(0, /*newParentVariant=*/0, /*thisPointerVariant=*/0); +} + /*! Returns a list of this item's children. @@ -7689,7 +7696,7 @@ QDeclarativeListProperty QGraphicsItemPrivate::childrenList() if (isObject) { QGraphicsObject *that = static_cast(q); return QDeclarativeListProperty(that, &children, children_append, - children_count, children_at); + children_count, children_at, children_clear); } 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 d60dffb..cce9439 100644 --- a/src/gui/graphicsview/qgraphicsitem_p.h +++ b/src/gui/graphicsview/qgraphicsitem_p.h @@ -487,6 +487,7 @@ public: static void children_append(QDeclarativeListProperty *list, QGraphicsObject *item); static int children_count(QDeclarativeListProperty *list); static QGraphicsObject *children_at(QDeclarativeListProperty *list, int); + static void children_clear(QDeclarativeListProperty *list); inline QTransform transformToParent() const; inline void ensureSortedChildren(); -- cgit v0.12