summaryrefslogtreecommitdiffstats
path: root/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp')
-rw-r--r--src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp101
1 files changed, 65 insertions, 36 deletions
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<QByteArray,int>::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<QByteArray,int>::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<int,QByteArray> 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<QModelIndex>(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);