From 809b5fa1c396688eb943dc9cd3d6a09de9d69468 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 5 Aug 2009 17:08:34 +1000 Subject: Add a VisualItemModel.index attached property to items in the model. --- src/declarative/fx/qfxvisualitemmodel.cpp | 51 ++++++++++++++++++++++++++++++- src/declarative/fx/qfxvisualitemmodel.h | 3 ++ 2 files changed, 53 insertions(+), 1 deletion(-) diff --git a/src/declarative/fx/qfxvisualitemmodel.cpp b/src/declarative/fx/qfxvisualitemmodel.cpp index 6516545..9694b3a 100644 --- a/src/declarative/fx/qfxvisualitemmodel.cpp +++ b/src/declarative/fx/qfxvisualitemmodel.cpp @@ -58,6 +58,47 @@ QML_DECLARE_TYPE(QListModelInterface) QT_BEGIN_NAMESPACE +class QFxVisualItemModelAttached : public QObject +{ + Q_OBJECT + +public: + QFxVisualItemModelAttached(QObject *parent) + : QObject(parent), m_index(0) {} + ~QFxVisualItemModelAttached() { + attachedProperties.remove(parent()); + } + + Q_PROPERTY(int index READ index NOTIFY indexChanged) + int index() const { return m_index; } + void setIndex(int idx) { + if (m_index != idx) { + m_index = idx; + emit indexChanged(); + } + } + + static QFxVisualItemModelAttached *properties(QObject *obj) { + QFxVisualItemModelAttached *rv = attachedProperties.value(obj); + if (!rv) { + rv = new QFxVisualItemModelAttached(obj); + attachedProperties.insert(obj, rv); + } + return rv; + } + +signals: + void indexChanged(); + +public: + int m_index; + + static QHash attachedProperties; +}; + +QHash QFxVisualItemModelAttached::attachedProperties; + + class QFxVisualItemModelPrivate : public QObjectPrivate { Q_DECLARE_PUBLIC(QFxVisualItemModel); @@ -75,6 +116,8 @@ public: void itemAppended() { Q_Q(QFxVisualItemModel); + QFxVisualItemModelAttached *attached = QFxVisualItemModelAttached::properties(children.last()); + attached->setIndex(children.count()-1); emit q->itemsInserted(children.count()-1, 1); emit q->countChanged(); } @@ -88,7 +131,8 @@ public: \brief The VisualItemModel allows items to be provided to a view. The children of the VisualItemModel are provided in a model which - can be used in a view. + can be used in a view. An item can determine its index within the + model via the VisualItemModel.index attached property. The example below places three colored rectangles in a ListView. \code @@ -177,6 +221,11 @@ void QFxVisualItemModelPrivate::ItemList::append(QFxItem *item) model->itemAppended(); } +QFxVisualItemModelAttached *QFxVisualItemModel::qmlAttachedProperties(QObject *obj) +{ + return QFxVisualItemModelAttached::properties(obj); +} + class QFxVisualDataModelParts; class QFxVisualDataModelData; diff --git a/src/declarative/fx/qfxvisualitemmodel.h b/src/declarative/fx/qfxvisualitemmodel.h index 6160068..6165746 100644 --- a/src/declarative/fx/qfxvisualitemmodel.h +++ b/src/declarative/fx/qfxvisualitemmodel.h @@ -101,6 +101,7 @@ private: Q_DISABLE_COPY(QFxVisualModel) }; +class QFxVisualItemModelAttached; class QFxVisualItemModelPrivate; class Q_DECLARATIVE_EXPORT QFxVisualItemModel : public QFxVisualModel { @@ -125,6 +126,8 @@ public: QmlList *children(); + static QFxVisualItemModelAttached *qmlAttachedProperties(QObject *obj); + private: Q_DISABLE_COPY(QFxVisualItemModel) }; -- cgit v0.12