From 6a61351ae264efb4df17794f97642f63dd0c3690 Mon Sep 17 00:00:00 2001 From: Martin Jones Date: Wed, 21 Apr 2010 16:12:31 +1000 Subject: Add hasModelChildren property to delegates with QAbstractItemModel model type. Also add some helper function to VisualDataModel: - VisualDataModel::modelIndex(int) returns a QModelIndex which can be assigned to VisualDataModel::rootIndex - VisualDataModel::parentModelIndex() returns a QModelIndex which can be assigned to VisualDataModel::rootIndex --- doc/src/declarative/qdeclarativemodels.qdoc | 12 ++- .../graphicsitems/qdeclarativevisualitemmodel.cpp | 101 ++++++++++++------- .../graphicsitems/qdeclarativevisualitemmodel_p.h | 9 +- tests/auto/declarative/declarative.pro | 1 + .../data/visualdatamodel.qml | 11 ++ .../qdeclarativevisualdatamodel.pro | 11 ++ .../tst_qdeclarativevisualdatamodel.cpp | 111 +++++++++++++++++++++ 7 files changed, 215 insertions(+), 41 deletions(-) create mode 100644 tests/auto/declarative/qdeclarativevisualdatamodel/data/visualdatamodel.qml create mode 100644 tests/auto/declarative/qdeclarativevisualdatamodel/qdeclarativevisualdatamodel.pro create mode 100644 tests/auto/declarative/qdeclarativevisualdatamodel/tst_qdeclarativevisualdatamodel.cpp diff --git a/doc/src/declarative/qdeclarativemodels.qdoc b/doc/src/declarative/qdeclarativemodels.qdoc index d8b2a5d..eecc117 100644 --- a/doc/src/declarative/qdeclarativemodels.qdoc +++ b/doc/src/declarative/qdeclarativemodels.qdoc @@ -206,8 +206,16 @@ The default role names set by Qt are: \endtable QAbstractItemModel presents a heirachy of tables. Views currently provided by QML -can only display list data. In order to display child lists of a heirachical model -use the VisualDataModel element with \e rootIndex set to a parent node. +can only display list data. +In order to display child lists of a heirachical model +the VisualDataModel element provides several properties and functions for use +with models of type QAbstractItemModel: +\list +\o \e hasModelChildren role property to determine whether a node has child nodes. +\o \l VisualDataModel::rootIndex allows the root node to be specifed +\o \l VisualDataModel::modelIndex() returns a QModelIndex which can be assigned to VisualDataModel::rootIndex +\o \l VisualDataModel::parentModelIndex() returns a QModelIndex which can be assigned to VisualDataModel::rootIndex +\endlist \section2 QStringList diff --git a/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp b/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp index 207232d..7fa8cc4 100644 --- a/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp +++ b/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp @@ -262,6 +262,7 @@ public: } if (m_roles.count() == 1) m_roleNames.insert("modelData", m_roles.at(0)); + m_roleNames.insert("hasModelChildren", 0); } else if (m_listAccessor) { m_roleNames.insert("modelData", 0); if (m_listAccessor->type() == QDeclarativeListAccessor::Instance) { @@ -473,11 +474,16 @@ QVariant QDeclarativeVisualDataModelDataMetaObject::initialValue(int propId) } } else if (model->m_abstractItemModel) { model->ensureRoles(); - QHash::const_iterator it = model->m_roleNames.find(propName); - if (it != model->m_roleNames.end()) { - roleToProp.insert(*it, propId); + if (propName == "hasModelChildren") { QModelIndex index = model->m_abstractItemModel->index(data->m_index, 0, model->m_root); - return model->m_abstractItemModel->data(index, *it); + return model->m_abstractItemModel->hasChildren(index); + } else { + 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, model->m_root); + return model->m_abstractItemModel->data(index, *it); + } } } Q_ASSERT(!"Can never be reached"); @@ -784,34 +790,13 @@ void QDeclarativeVisualDataModel::setDelegate(QDeclarativeComponent *delegate) \qmlproperty QModelIndex VisualDataModel::rootIndex QAbstractItemModel provides a heirachical tree of data, whereas - QML only operates on list data. rootIndex allows the children of + QML only operates on list data. \c 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(QDeclarativeContext *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)); - } - - QDeclarativeContext *context; - }; int main(int argc, char ** argv) { @@ -819,7 +804,8 @@ void QDeclarativeVisualDataModel::setDelegate(QDeclarativeComponent *delegate) QDeclarativeView view; - MyModel model(view.rootContext()); + QDirModel model; + view.rootContext()->setContextProperty("myModel", &model); view.setSource(QUrl("qrc:view.qml")); view.show(); @@ -835,18 +821,18 @@ void QDeclarativeVisualDataModel::setDelegate(QDeclarativeComponent *delegate) import Qt 4.7 ListView { + id: view width: 200 height: 200 model: VisualDataModel { model: myModel - rootIndex: myRoot delegate: Component { Rectangle { - height: 25; width: 100 - Text { text: path } + height: 25; width: 200 + Text { text: filePath } MouseArea { anchors.fill: parent; - onClicked: myModel.setRoot(path) + onClicked: if (hasModelChildren) view.model.rootIndex = view.model.modelIndex(index) } } } @@ -854,19 +840,21 @@ void QDeclarativeVisualDataModel::setDelegate(QDeclarativeComponent *delegate) } \endcode + \sa modelIndex(), parentModelIndex() */ -QModelIndex QDeclarativeVisualDataModel::rootIndex() const +QVariant QDeclarativeVisualDataModel::rootIndex() const { Q_D(const QDeclarativeVisualDataModel); - return d->m_root; + return QVariant::fromValue(d->m_root); } -void QDeclarativeVisualDataModel::setRootIndex(const QModelIndex &root) +void QDeclarativeVisualDataModel::setRootIndex(const QVariant &root) { Q_D(QDeclarativeVisualDataModel); - if (d->m_root != root) { + QModelIndex modelIndex = qvariant_cast(root); + if (d->m_root != modelIndex) { int oldCount = d->modelCount(); - d->m_root = root; + d->m_root = modelIndex; int newCount = d->modelCount(); if (d->m_delegate && oldCount) emit itemsRemoved(0, oldCount); @@ -878,6 +866,47 @@ void QDeclarativeVisualDataModel::setRootIndex(const QModelIndex &root) } } + +/*! + \qmlmethod QModelIndex VisualDataModel::modelIndex(int index) + + QAbstractItemModel provides a heirachical tree of data, whereas + QML only operates on list data. This function assists in using + tree models in QML. + + Returns a QModelIndex for the specified index. + This value can be assigned to rootIndex. + + \sa rootIndex +*/ +QVariant QDeclarativeVisualDataModel::modelIndex(int idx) const +{ + Q_D(const QDeclarativeVisualDataModel); + if (d->m_abstractItemModel) + return QVariant::fromValue(d->m_abstractItemModel->index(idx, 0, d->m_root)); + return QVariant::fromValue(QModelIndex()); +} + +/*! + \qmlmethod QModelIndex VisualDataModel::parentModelIndex() + + QAbstractItemModel provides a heirachical tree of data, whereas + QML only operates on list data. This function assists in using + tree models in QML. + + Returns a QModelIndex for the parent of the current rootIndex. + This value can be assigned to rootIndex. + + \sa rootIndex +*/ +QVariant QDeclarativeVisualDataModel::parentModelIndex() const +{ + Q_D(const QDeclarativeVisualDataModel); + if (d->m_abstractItemModel) + return QVariant::fromValue(d->m_abstractItemModel->parent(d->m_root)); + return QVariant::fromValue(QModelIndex()); +} + QString QDeclarativeVisualDataModel::part() const { Q_D(const QDeclarativeVisualDataModel); diff --git a/src/declarative/graphicsitems/qdeclarativevisualitemmodel_p.h b/src/declarative/graphicsitems/qdeclarativevisualitemmodel_p.h index d34bcaf..0a9173f 100644 --- a/src/declarative/graphicsitems/qdeclarativevisualitemmodel_p.h +++ b/src/declarative/graphicsitems/qdeclarativevisualitemmodel_p.h @@ -150,7 +150,7 @@ class Q_DECLARATIVE_EXPORT QDeclarativeVisualDataModel : public QDeclarativeVisu Q_PROPERTY(QDeclarativeComponent *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_PROPERTY(QVariant rootIndex READ rootIndex WRITE setRootIndex NOTIFY rootIndexChanged) Q_CLASSINFO("DefaultProperty", "delegate") public: QDeclarativeVisualDataModel(); @@ -163,8 +163,11 @@ public: QDeclarativeComponent *delegate() const; void setDelegate(QDeclarativeComponent *); - QModelIndex rootIndex() const; - void setRootIndex(const QModelIndex &root); + QVariant rootIndex() const; + void setRootIndex(const QVariant &root); + + Q_INVOKABLE QVariant modelIndex(int idx) const; + Q_INVOKABLE QVariant parentModelIndex() const; QString part() const; void setPart(const QString &); diff --git a/tests/auto/declarative/declarative.pro b/tests/auto/declarative/declarative.pro index 58371c1..9b3b3d0 100644 --- a/tests/auto/declarative/declarative.pro +++ b/tests/auto/declarative/declarative.pro @@ -64,6 +64,7 @@ SUBDIRS += \ qdeclarativeimageprovider \ # Cover qdeclarativestyledtext \ # Cover qdeclarativesqldatabase \ # Cover + qdeclarativevisualdatamodel \ # Cover qmlvisual # Cover contains(QT_CONFIG, webkit) { diff --git a/tests/auto/declarative/qdeclarativevisualdatamodel/data/visualdatamodel.qml b/tests/auto/declarative/qdeclarativevisualdatamodel/data/visualdatamodel.qml new file mode 100644 index 0000000..d70f82b --- /dev/null +++ b/tests/auto/declarative/qdeclarativevisualdatamodel/data/visualdatamodel.qml @@ -0,0 +1,11 @@ +import Qt 4.7 + +VisualDataModel { + function setRoot() { + rootIndex = modelIndex(0); + } + function setRootToParent() { + rootIndex = parentModelIndex(); + } + model: myModel +} diff --git a/tests/auto/declarative/qdeclarativevisualdatamodel/qdeclarativevisualdatamodel.pro b/tests/auto/declarative/qdeclarativevisualdatamodel/qdeclarativevisualdatamodel.pro new file mode 100644 index 0000000..d76b582 --- /dev/null +++ b/tests/auto/declarative/qdeclarativevisualdatamodel/qdeclarativevisualdatamodel.pro @@ -0,0 +1,11 @@ +load(qttest_p4) +contains(QT_CONFIG,declarative): QT += declarative gui +macx:CONFIG -= app_bundle + +SOURCES += tst_qdeclarativevisualdatamodel.cpp + +# Define SRCDIR equal to test's source directory +DEFINES += SRCDIR=\\\"$$PWD\\\" + +CONFIG += parallel_test + diff --git a/tests/auto/declarative/qdeclarativevisualdatamodel/tst_qdeclarativevisualdatamodel.cpp b/tests/auto/declarative/qdeclarativevisualdatamodel/tst_qdeclarativevisualdatamodel.cpp new file mode 100644 index 0000000..7de15a3 --- /dev/null +++ b/tests/auto/declarative/qdeclarativevisualdatamodel/tst_qdeclarativevisualdatamodel.cpp @@ -0,0 +1,111 @@ +/**************************************************************************** +** +** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). +** All rights reserved. +** Contact: Nokia Corporation (qt-info@nokia.com) +** +** This file is part of the test suite of the Qt Toolkit. +** +** $QT_BEGIN_LICENSE:LGPL$ +** No Commercial Usage +** This file contains pre-release code and may not be distributed. +** You may use this file in accordance with the terms and conditions +** contained in the Technology Preview License Agreement accompanying +** this package. +** +** GNU Lesser General Public License Usage +** Alternatively, this file may be used under the terms of the GNU Lesser +** General Public License version 2.1 as published by the Free Software +** Foundation and appearing in the file LICENSE.LGPL included in the +** packaging of this file. Please review the following information to +** ensure the GNU Lesser General Public License version 2.1 requirements +** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. +** +** In addition, as a special exception, Nokia gives you certain additional +** rights. These rights are described in the Nokia Qt LGPL Exception +** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. +** +** If you have questions regarding the use of this file, please contact +** Nokia at qt-info@nokia.com. +** +** +** +** +** +** +** +** +** $QT_END_LICENSE$ +** +****************************************************************************/ +#include +#include +#include +#include +#include +#include +#include +#include +#include + +static void initStandardTreeModel(QStandardItemModel *model) +{ + QStandardItem *item; + item = new QStandardItem(QLatin1String("Row 1 Item")); + model->insertRow(0, item); + + item = new QStandardItem(QLatin1String("Row 2 Item")); + item->setCheckable(true); + model->insertRow(1, item); + + QStandardItem *childItem = new QStandardItem(QLatin1String("Row 2 Child Item")); + item->setChild(0, childItem); + + item = new QStandardItem(QLatin1String("Row 3 Item")); + item->setIcon(QIcon()); + model->insertRow(2, item); +} + +class tst_qdeclarativevisualdatamodel : public QObject +{ + Q_OBJECT +public: + tst_qdeclarativevisualdatamodel(); + +private slots: + void rootIndex(); + +private: + QDeclarativeEngine engine; +}; + +tst_qdeclarativevisualdatamodel::tst_qdeclarativevisualdatamodel() +{ +} + +void tst_qdeclarativevisualdatamodel::rootIndex() +{ + QDeclarativeEngine engine; + QDeclarativeComponent c(&engine, QUrl::fromLocalFile(SRCDIR "/data/visualdatamodel.qml")); + + QStandardItemModel model; + initStandardTreeModel(&model); + + engine.rootContext()->setContextProperty("myModel", &model); + + QDeclarativeVisualDataModel *obj = qobject_cast(c.create()); + QVERIFY(obj != 0); + + QMetaObject::invokeMethod(obj, "setRoot"); + QVERIFY(qvariant_cast(obj->rootIndex()) == model.index(0,0)); + + QMetaObject::invokeMethod(obj, "setRootToParent"); + QVERIFY(qvariant_cast(obj->rootIndex()) == QModelIndex()); + + delete obj; +} + + +QTEST_MAIN(tst_qdeclarativevisualdatamodel) + +#include "tst_qdeclarativevisualdatamodel.moc" -- cgit v0.12