diff options
author | Michael Brasser <michael.brasser@nokia.com> | 2009-08-27 01:25:21 (GMT) |
---|---|---|
committer | Michael Brasser <michael.brasser@nokia.com> | 2009-08-27 01:25:21 (GMT) |
commit | 0d12a574ca3624ecfe62d4183e02e43f3bfb6509 (patch) | |
tree | f8a7913321059b8f47a90cb5f91e546e061af1a1 /src/declarative | |
parent | baeb25062194c9ddc36c4536662de46b6b09cd68 (diff) | |
download | Qt-0d12a574ca3624ecfe62d4183e02e43f3bfb6509.zip Qt-0d12a574ca3624ecfe62d4183e02e43f3bfb6509.tar.gz Qt-0d12a574ca3624ecfe62d4183e02e43f3bfb6509.tar.bz2 |
Repeater API updates.
Diffstat (limited to 'src/declarative')
-rw-r--r-- | src/declarative/fx/qfxlistview.cpp | 2 | ||||
-rw-r--r-- | src/declarative/fx/qfxrepeater.cpp | 93 | ||||
-rw-r--r-- | src/declarative/fx/qfxrepeater.h | 19 | ||||
-rw-r--r-- | src/declarative/fx/qfxrepeater_p.h | 1 |
4 files changed, 51 insertions, 64 deletions
diff --git a/src/declarative/fx/qfxlistview.cpp b/src/declarative/fx/qfxlistview.cpp index 048cb0f..b2c9a59 100644 --- a/src/declarative/fx/qfxlistview.cpp +++ b/src/declarative/fx/qfxlistview.cpp @@ -894,7 +894,7 @@ QFxListView::~QFxListView() provided by a C++ model object. The C++ model object must be a \l {QAbstractItemModel} subclass, a VisualModel, or a simple list. - Models can also be created directly in QML, using a \l{ListModel}. + Models can also be created directly in QML, using a \l{ListModel} or \l{XmlListModel}. */ QVariant QFxListView::model() const { diff --git a/src/declarative/fx/qfxrepeater.cpp b/src/declarative/fx/qfxrepeater.cpp index a9d9977..fa44f8f 100644 --- a/src/declarative/fx/qfxrepeater.cpp +++ b/src/declarative/fx/qfxrepeater.cpp @@ -47,7 +47,7 @@ QT_BEGIN_NAMESPACE QFxRepeaterPrivate::QFxRepeaterPrivate() -: component(0) +: component(0), count(0) { } @@ -112,42 +112,8 @@ QML_DEFINE_TYPE(Qt,4,6,(QT_VERSION&0x00ff00)>>8,Repeater,QFxRepeater) /*! \internal \class QFxRepeater - \ingroup group_utility \qmlclass Repeater - \brief The QFxRepeater class allows you to repeat a component based on a - data source. - - The QFxRepeater class is used when you want to create a large number of - similar items. For each entry in the data source, an item is instantiated - in a context seeded with data from the data source. - - The data source may be either an object list, a string list or a Qt model. - In each case, the data element and the index is exposed to each instantiated - component. The index is always exposed as an accessible \c index property. - In the case of an object or string list, the data element (of type string - or object) is available as the \c modelData property. In the case of a Qt model, - all roles are available as named properties just like in the view classes. - - As a special case the data source can also be merely a number. In this case it will - create that many instances of the component. They will also be assigned an index - based on the order they are created. - - Items instantiated by the QFxRepeater class are inserted, in order, as - children of the repeater's parent. The insertion starts immediately after - the repeater's position in its parent stacking list. This is to allow - you to use a repeater inside a layout. The following QML example shows how - the instantiated items would visually appear stacked between the red and - blue rectangles. - - \snippet doc/src/snippets/declarative/repeater.qml 0 - - The QFxRepeater instance continues to own all items it instantiates, even - if they are otherwise manipulated. It is illegal to manually delete an item - created by the repeater. On destruction, the repeater will clean up any - items it has instantiated. - - XXX Repeater is very conservative in how it instatiates/deletes items. Also new model entries will not be created and old ones will not be removed. */ @@ -177,27 +143,27 @@ QFxRepeater::~QFxRepeater() } /*! - \qmlproperty any Repeater::dataSource + \qmlproperty any Repeater::model - The Repeater's data source. + The model providing data for the repeater. - The data source may be either an object list, a string list or a Qt model. + The model may be either an object list, a string list or a Qt model. In each case, the data element and the index is exposed to each instantiated component. The index is always exposed as an accessible \c index property. In the case of an object or string list, the data element (of type string or object) is available as the \c modelData property. In the case of a Qt model, all roles are available as named properties just like in the view classes. - As a special case the data source can also be merely a number. In this case it will + As a special case the model can also be merely a number. In this case it will create that many instances of the component. They will also be assigned an index based on the order they are created. */ -QVariant QFxRepeater::dataSource() const +QVariant QFxRepeater::model() const { return QVariant(); } -void QFxRepeater::setDataSource(const QVariant &v) +void QFxRepeater::setModel(const QVariant &v) { Q_D(QFxRepeater); d->dataSource = v; @@ -205,18 +171,18 @@ void QFxRepeater::setDataSource(const QVariant &v) } /*! - \qmlproperty Component Repeater::component + \qmlproperty Component Repeater::delegate \default - The component to repeat. + The delegate provides a template describing what each item instantiated by the repeater should look and act like. */ -QmlComponent *QFxRepeater::component() const +QmlComponent *QFxRepeater::delegate() const { Q_D(const QFxRepeater); return d->component; } -void QFxRepeater::setComponent(QmlComponent *_c) +void QFxRepeater::setDelegate(QmlComponent *_c) { Q_D(QFxRepeater); d->component = _c; @@ -224,6 +190,18 @@ void QFxRepeater::setComponent(QmlComponent *_c) } /*! + \qmlproperty int Repeater::count + + This property holds the number of items in the repeater. +*/ +int QFxRepeater::count() const +{ + Q_D(const QFxRepeater); + return d->count; +} + + +/*! \internal */ void QFxRepeater::componentComplete() @@ -260,10 +238,13 @@ void QFxRepeater::regenerate() QFxItem *lastItem = this; + int count = 0; + if (d->dataSource.type() == QVariant::StringList) { QStringList sl = qvariant_cast<QStringList>(d->dataSource); - for (int ii = 0; ii < sl.size(); ++ii) { + count = sl.size(); + for (int ii = 0; ii < count; ++ii) { QmlContext *ctxt = new QmlContext(qmlContext(this), this); d->deletables << ctxt; @@ -274,11 +255,11 @@ void QFxRepeater::regenerate() lastItem = item; } } else if (QmlMetaType::isList(d->dataSource)) { - int cnt = QmlMetaType::listCount(d->dataSource); - if (cnt <= 0) + count = QmlMetaType::listCount(d->dataSource); + if (count <= 0) return; - for (int ii = 0; ii < cnt; ++ii) { + for (int ii = 0; ii < count; ++ii) { QVariant v = QmlMetaType::listAt(d->dataSource, ii); QObject *o = QmlMetaType::toQObject(v); @@ -292,11 +273,11 @@ void QFxRepeater::regenerate() lastItem = item; } } else if (QListModelInterface *model = qobject_cast<QListModelInterface*>(d->dataSource.value<QObject*>())) { - int cnt = model->count(); - if (cnt <= 0) + count = model->count(); + if (count <= 0) return; - for (int ii = 0; ii < cnt; ++ii) { + for (int ii = 0; ii < count; ++ii) { QmlContext *ctxt = new QmlContext(qmlContext(this), this); d->deletables << ctxt; @@ -322,13 +303,14 @@ void QFxRepeater::regenerate() d->deletables << ctxt; ctxt->setContextProperty(QLatin1String("index"), QVariant(0)); - for (int ii = 1; ii < object->metaObject()->propertyCount(); ++ii) { + count = object->metaObject()->propertyCount(); + for (int ii = 1; ii < count; ++ii) { const QMetaProperty &prop = object->metaObject()->property(ii); ctxt->setContextProperty(QLatin1String(prop.name()), prop.read(object)); } //for compatability with other lists, assign data if there is only a single role (excluding objectName) - if (object->metaObject()->propertyCount() == 2) { + if (count == 2) { const QMetaProperty &prop = object->metaObject()->property(1); ctxt->setContextProperty(QLatin1String("modelData"), prop.read(object)); } @@ -337,7 +319,7 @@ void QFxRepeater::regenerate() } else if (d->dataSource.canConvert(QVariant::Int)){ - int count = qvariant_cast<int>(d->dataSource); + count = qvariant_cast<int>(d->dataSource); for (int ii = 0; ii < count; ++ii) { QmlContext *ctxt = new QmlContext(qmlContext(this), this); @@ -349,5 +331,6 @@ void QFxRepeater::regenerate() lastItem = item; } } + d->count = count; } QT_END_NAMESPACE diff --git a/src/declarative/fx/qfxrepeater.h b/src/declarative/fx/qfxrepeater.h index 3013b61..43afd63 100644 --- a/src/declarative/fx/qfxrepeater.h +++ b/src/declarative/fx/qfxrepeater.h @@ -55,19 +55,22 @@ class Q_DECLARATIVE_EXPORT QFxRepeater : public QFxItem { Q_OBJECT - Q_PROPERTY(QVariant dataSource READ dataSource WRITE setDataSource) //### model - Q_PROPERTY(QmlComponent *component READ component WRITE setComponent) //### delegate - Q_CLASSINFO("DefaultProperty", "component") - //### count + Q_PROPERTY(QVariant model READ model WRITE setModel) + Q_PROPERTY(QmlComponent *delegate READ delegate WRITE setDelegate) + Q_PROPERTY(int count READ count NOTIFY countChanged) + Q_CLASSINFO("DefaultProperty", "delegate") + public: QFxRepeater(QFxItem *parent=0); virtual ~QFxRepeater(); - QVariant dataSource() const; - void setDataSource(const QVariant &); + QVariant model() const; + void setModel(const QVariant &); + + QmlComponent *delegate() const; + void setDelegate(QmlComponent *); - QmlComponent *component() const; - void setComponent(QmlComponent *); + int count() const; private: void regenerate(); diff --git a/src/declarative/fx/qfxrepeater_p.h b/src/declarative/fx/qfxrepeater_p.h index ba69658..65b0973 100644 --- a/src/declarative/fx/qfxrepeater_p.h +++ b/src/declarative/fx/qfxrepeater_p.h @@ -73,6 +73,7 @@ public: QVariant dataSource; QmlComponent *component; + int count; QList<QPointer<QObject> > deletables; }; |