From 0d12a574ca3624ecfe62d4183e02e43f3bfb6509 Mon Sep 17 00:00:00 2001 From: Michael Brasser Date: Thu, 27 Aug 2009 11:25:21 +1000 Subject: Repeater API updates. --- examples/declarative/easing/easing.qml | 2 +- examples/declarative/follow/pong.qml | 2 +- examples/declarative/listview/itemlist.qml | 2 +- examples/declarative/minehunt/minehunt.qml | 2 +- examples/declarative/velocity/Day.qml | 2 +- examples/declarative/velocity/velocity.qml | 2 +- src/declarative/fx/qfxlistview.cpp | 2 +- src/declarative/fx/qfxrepeater.cpp | 93 ++++++++++++------------------ src/declarative/fx/qfxrepeater.h | 19 +++--- src/declarative/fx/qfxrepeater_p.h | 1 + 10 files changed, 57 insertions(+), 70 deletions(-) diff --git a/examples/declarative/easing/easing.qml b/examples/declarative/easing/easing.qml index 60c2040..df301d8 100644 --- a/examples/declarative/easing/easing.qml +++ b/examples/declarative/easing/easing.qml @@ -56,7 +56,7 @@ Rectangle { anchors.left: Window.left anchors.right: Window.right Repeater { - dataSource: EasingTypes + model: EasingTypes Component { Text { id: Text diff --git a/examples/declarative/follow/pong.qml b/examples/declarative/follow/pong.qml index d2aaba9..0314bb8 100644 --- a/examples/declarative/follow/pong.qml +++ b/examples/declarative/follow/pong.qml @@ -63,7 +63,7 @@ Rectangle { Rectangle { color: "#00ee00"; x: Page.width/2+40; y: 0; width: 40; height: 60 } Rectangle { color: "#000000"; x: Page.width/2+50; y: 10; width: 20; height: 40 } Repeater { - dataSource: Page.height/20 + model: Page.height/20 Rectangle { color: "#00ee00"; x: Page.width/2-5; y: index*20; width: 10; height: 10 } } } diff --git a/examples/declarative/listview/itemlist.qml b/examples/declarative/listview/itemlist.qml index c25ee59..061fab3 100644 --- a/examples/declarative/listview/itemlist.qml +++ b/examples/declarative/listview/itemlist.qml @@ -44,7 +44,7 @@ Rectangle { anchors.centerIn: parent spacing: 20 Repeater { - dataSource: ItemModel.count + model: ItemModel.count Rectangle { width: 5; height: 5 radius: 3 diff --git a/examples/declarative/minehunt/minehunt.qml b/examples/declarative/minehunt/minehunt.qml index 11748a8..4bae64d 100644 --- a/examples/declarative/minehunt/minehunt.qml +++ b/examples/declarative/minehunt/minehunt.qml @@ -130,7 +130,7 @@ Item { anchors.verticalCenter: parent.verticalCenter } Repeater { - dataSource: tiles + model: tiles x: 1 y: 1 Component { diff --git a/examples/declarative/velocity/Day.qml b/examples/declarative/velocity/Day.qml index e488ae1..1a336b7 100644 --- a/examples/declarative/velocity/Day.qml +++ b/examples/declarative/velocity/Day.qml @@ -26,7 +26,7 @@ Rectangle { styleColor: "#dedede" } Repeater { - dataSource: Page.stickies + model: Page.stickies Item { x: Math.random() * 200 + 100 y: Math.random() * 300 + 50 diff --git a/examples/declarative/velocity/velocity.qml b/examples/declarative/velocity/velocity.qml index c2425da..8ac42e1 100644 --- a/examples/declarative/velocity/velocity.qml +++ b/examples/declarative/velocity/velocity.qml @@ -101,7 +101,7 @@ Rectangle { Row { id: Lay Repeater { - dataSource: List + model: List Component { Day { day: name 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(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(d->dataSource.value())) { - 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(d->dataSource); + count = qvariant_cast(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 > deletables; }; -- cgit v0.12