summaryrefslogtreecommitdiffstats
path: root/src/declarative
diff options
context:
space:
mode:
authorMartin Jones <martin.jones@nokia.com>2010-04-21 06:12:31 (GMT)
committerMartin Jones <martin.jones@nokia.com>2010-04-21 06:12:31 (GMT)
commit6a61351ae264efb4df17794f97642f63dd0c3690 (patch)
tree75842665831872f15102eea191b04489b7074729 /src/declarative
parent6abe7f1ef6021a1174d88d1a2811c58241a96707 (diff)
downloadQt-6a61351ae264efb4df17794f97642f63dd0c3690.zip
Qt-6a61351ae264efb4df17794f97642f63dd0c3690.tar.gz
Qt-6a61351ae264efb4df17794f97642f63dd0c3690.tar.bz2
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
Diffstat (limited to 'src/declarative')
-rw-r--r--src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp101
-rw-r--r--src/declarative/graphicsitems/qdeclarativevisualitemmodel_p.h9
2 files changed, 71 insertions, 39 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);
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 &);