From 66b8d89feb2850dbc06503fa66f5963d48616c1d Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Tue, 23 Feb 2010 15:40:26 +1000 Subject: Add support for setting the root index when using a QAbstractItemModel. Task-number: QT-2777 --- doc/src/declarative/elements.qdoc | 1 + .../graphicsitems/qmlgraphicsvisualitemmodel.cpp | 165 ++++++++++++++++++++- .../graphicsitems/qmlgraphicsvisualitemmodel_p.h | 7 + 3 files changed, 169 insertions(+), 4 deletions(-) diff --git a/doc/src/declarative/elements.qdoc b/doc/src/declarative/elements.qdoc index 682a2ac..b218b64 100644 --- a/doc/src/declarative/elements.qdoc +++ b/doc/src/declarative/elements.qdoc @@ -88,6 +88,7 @@ The following table lists the QML elements provided by the Qt Declarative module \o \l Binding \o \l ListModel, \l ListElement \o \l VisualItemModel +\o \l VisualDataModel \o \l XmlListModel and XmlRole \o \l DateTimeFormatter \o \l NumberFormatter diff --git a/src/declarative/graphicsitems/qmlgraphicsvisualitemmodel.cpp b/src/declarative/graphicsitems/qmlgraphicsvisualitemmodel.cpp index b96f399..4f28da7 100644 --- a/src/declarative/graphicsitems/qmlgraphicsvisualitemmodel.cpp +++ b/src/declarative/graphicsitems/qmlgraphicsvisualitemmodel.cpp @@ -327,7 +327,7 @@ public: if (m_listModelInterface) return m_listModelInterface->count(); if (m_abstractItemModel) - return m_abstractItemModel->rowCount(); + return m_abstractItemModel->rowCount(m_root); if (m_listAccessor) return m_listAccessor->count(); return 0; @@ -348,6 +348,8 @@ public: QVariant m_modelVariant; QmlListAccessor *m_listAccessor; + + QModelIndex m_root; }; class QmlGraphicsVisualDataModelDataMetaObject : public QmlOpenMetaObject @@ -461,7 +463,7 @@ QVariant QmlGraphicsVisualDataModelDataMetaObject::initialValue(int propId) QHash::const_iterator it = model->m_roleNames.find(propName); if (it != model->m_roleNames.end()) { roleToProp.insert(*it, propId); - QModelIndex index = model->m_abstractItemModel->index(data->m_index, 0); + QModelIndex index = model->m_abstractItemModel->index(data->m_index, 0, model->m_root); return model->m_abstractItemModel->data(index, *it); } } @@ -560,6 +562,39 @@ QmlGraphicsVisualDataModelData *QmlGraphicsVisualDataModelPrivate::data(QObject //--------------------------------------------------------------------------- +/*! + \qmlclass VisualDataModel QmlGraphicsVisualDataModel + \brief The VisualDataModel encapsulates a model and delegate + + A VisualDataModel encapsulates a model and the delegate that will + be instantiated for items in the model. + + It is usually not necessary to create a VisualDataModel directly, + since the QML views will create one internally. + + The example below illustrates using a VisualDataModel with a ListView. + + \code + VisualDataModel { + id: visualModel + model: myModel + delegate: Component { + Rectangle { + height: 25 + width: 100 + Text { text: "Name:" + name} + } + } + } + ListView { + width: 100 + height: 100 + anchors.fill: parent + model: visualModel + } + \endcode +*/ + QmlGraphicsVisualDataModel::QmlGraphicsVisualDataModel() : QmlGraphicsVisualModel(*(new QmlGraphicsVisualDataModelPrivate(0))) { @@ -579,6 +614,20 @@ QmlGraphicsVisualDataModel::~QmlGraphicsVisualDataModel() d->m_delegateDataType->release(); } +/*! + \qmlproperty model VisualDataModel::model + This property holds the model providing data for the VisualDataModel. + + The model provides a set of data that is used to create the items + for a view. For large or dynamic datasets the model is usually + provided by a C++ model object. The C++ model object must be a \l + {QAbstractItemModel} subclass or a simple list. + + Models can also be created directly in QML, using a \l{ListModel} or + \l{XmlListModel}. + + \sa {qmlmodels}{Data Models} +*/ QVariant QmlGraphicsVisualDataModel::model() const { Q_D(const QmlGraphicsVisualDataModel); @@ -682,6 +731,16 @@ void QmlGraphicsVisualDataModel::setModel(const QVariant &model) } } +/*! + \qmlproperty component VisualDataModel::delegate + + The delegate provides a template defining each item instantiated by a view. + The index is exposed as an accessible \c index property. Properties of the + model are also available depending upon the type of \l {qmlmodels}{Data Model}. + + Here is an example delegate: + \snippet doc/src/snippets/declarative/listview/listview.qml 0 +*/ QmlComponent *QmlGraphicsVisualDataModel::delegate() const { Q_D(const QmlGraphicsVisualDataModel); @@ -705,6 +764,105 @@ void QmlGraphicsVisualDataModel::setDelegate(QmlComponent *delegate) } } +/*! + \qmlproperty QModelIndex VisualDataModel::rootIndex + + QAbstractItemModel provides a heirachical tree of data, whereas + QML only operates on list data. rootIndex allows the children of + any node in a QAbstractItemModel to be provided by this model. + + This property only affects models of type QAbstractItemModel. + + \code + // main.cpp + Q_DECLARE_METATYPE(QModelIndex) + + class MyModel : public QDirModel + { + Q_OBJECT + public: + MyModel(QmlContext *ctxt) : QDirModel(), context(ctxt) { + QHash roles = roleNames(); + roles.insert(FilePathRole, "path"); + setRoleNames(roles); + context->setContextProperty("myModel", this); + context->setContextProperty("myRoot", QVariant::fromValue(index(0,0,QModelIndex()))); + } + + Q_INVOKABLE void setRoot(const QString &path) { + QModelIndex root = index(path); + context->setContextProperty("myRoot", QVariant::fromValue(root)); + } + + QmlContext *context; + }; + + int main(int argc, char ** argv) + { + QApplication app(argc, argv); + + QmlView view; + view.setSource(QUrl("qrc:view.qml")); + + MyModel model(view.rootContext()); + + view.execute(); + view.show(); + + return app.exec(); + } + + #include "main.moc" + \endcode + + \code + // view.qml + import Qt 4.6 + + ListView { + width: 200 + height: 200 + model: VisualDataModel { + model: myModel + rootIndex: myRoot + delegate: Component { + Rectangle { + height: 25; width: 100 + Text { text: path } + MouseRegion { + anchors.fill: parent; + onClicked: myModel.setRoot(path) + } + } + } + } + } + \endcode + +*/ +QModelIndex QmlGraphicsVisualDataModel::rootIndex() const +{ + Q_D(const QmlGraphicsVisualDataModel); + return d->m_root; +} + +void QmlGraphicsVisualDataModel::setRootIndex(const QModelIndex &root) +{ + Q_D(QmlGraphicsVisualDataModel); + if (d->m_root != root) { + int oldCount = d->modelCount(); + d->m_root = root; + int newCount = d->modelCount(); + if (d->m_delegate && oldCount) + emit itemsRemoved(0, oldCount); + if (d->m_delegate && newCount) + emit itemsInserted(0, newCount); + if (newCount != oldCount) + emit countChanged(); + emit rootIndexChanged(); + } +} + QString QmlGraphicsVisualDataModel::part() const { Q_D(const QmlGraphicsVisualDataModel); @@ -786,7 +944,6 @@ QmlGraphicsItem *QmlGraphicsVisualDataModel::item(int index, const QByteArray &v if (d->modelCount() <= 0 || !d->m_delegate) return 0; - QObject *nobj = d->m_cache.getItem(index); if (!nobj) { QmlContext *ccontext = d->m_context; @@ -943,7 +1100,7 @@ void QmlGraphicsVisualDataModel::_q_itemsChanged(int index, int count, if (d->m_listModelInterface) { data->setValue(propId, d->m_listModelInterface->data(ii, QList() << role).value(role)); } else if (d->m_abstractItemModel) { - QModelIndex index = d->m_abstractItemModel->index(ii, 0); + QModelIndex index = d->m_abstractItemModel->index(ii, 0, d->m_root); data->setValue(propId, d->m_abstractItemModel->data(index, role)); } } diff --git a/src/declarative/graphicsitems/qmlgraphicsvisualitemmodel_p.h b/src/declarative/graphicsitems/qmlgraphicsvisualitemmodel_p.h index 49f9b27..7dc41a8 100644 --- a/src/declarative/graphicsitems/qmlgraphicsvisualitemmodel_p.h +++ b/src/declarative/graphicsitems/qmlgraphicsvisualitemmodel_p.h @@ -49,6 +49,8 @@ QT_BEGIN_HEADER +Q_DECLARE_METATYPE(QModelIndex) + QT_BEGIN_NAMESPACE QT_MODULE(Declarative) @@ -147,6 +149,7 @@ class Q_DECLARATIVE_EXPORT QmlGraphicsVisualDataModel : public QmlGraphicsVisual Q_PROPERTY(QmlComponent *delegate READ delegate WRITE setDelegate) Q_PROPERTY(QString part READ part WRITE setPart) Q_PROPERTY(QObject *parts READ parts CONSTANT) + Q_PROPERTY(QModelIndex rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged) Q_CLASSINFO("DefaultProperty", "delegate") public: QmlGraphicsVisualDataModel(); @@ -159,6 +162,9 @@ public: QmlComponent *delegate() const; void setDelegate(QmlComponent *); + QModelIndex rootIndex() const; + void setRootIndex(const QModelIndex &root); + QString part() const; void setPart(const QString &); @@ -178,6 +184,7 @@ public: Q_SIGNALS: void createdPackage(int index, QmlPackage *package); void destroyingPackage(QmlPackage *package); + void rootIndexChanged(); private Q_SLOTS: void _q_itemsChanged(int, int, const QList &); -- cgit v0.12