diff options
Diffstat (limited to 'src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp')
-rw-r--r-- | src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp | 88 |
1 files changed, 58 insertions, 30 deletions
diff --git a/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp b/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp index 43cafe3..0e4217e 100644 --- a/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp +++ b/src/declarative/graphicsitems/qdeclarativevisualitemmodel.cpp @@ -44,6 +44,7 @@ #include "qdeclarativeitem.h" #include <qdeclarativecontext.h> +#include <qdeclarativecontext_p.h> #include <qdeclarativeengine.h> #include <qdeclarativeexpression.h> #include <qdeclarativepackage_p.h> @@ -55,6 +56,7 @@ #include <qdeclarativeguard_p.h> #include <qdeclarativeglobal_p.h> +#include <qgraphicsscene.h> #include <qlistmodelinterface_p.h> #include <qhash.h> #include <qlist.h> @@ -136,8 +138,8 @@ public: } \endcode */ -QDeclarativeVisualItemModel::QDeclarativeVisualItemModel() - : QDeclarativeVisualModel(*(new QDeclarativeVisualItemModelPrivate)) +QDeclarativeVisualItemModel::QDeclarativeVisualItemModel(QObject *parent) + : QDeclarativeVisualModel(*(new QDeclarativeVisualItemModelPrivate), parent) { } @@ -267,7 +269,8 @@ public: } if (m_roles.count() == 1) m_roleNames.insert("modelData", m_roles.at(0)); - m_roleNames.insert("hasModelChildren", 0); + if (m_roles.count()) + m_roleNames.insert("hasModelChildren", 0); } else if (m_listAccessor) { m_roleNames.insert("modelData", 0); if (m_listAccessor->type() == QDeclarativeListAccessor::Instance) { @@ -283,15 +286,19 @@ public: } } + QHash<int,int> roleToPropId; void createMetaData() { if (!m_metaDataCreated) { ensureRoles(); - QHash<QByteArray, int>::const_iterator it = m_roleNames.begin(); - while (it != m_roleNames.end()) { - m_delegateDataType->createProperty(it.key()); - ++it; + if (m_roleNames.count()) { + QHash<QByteArray, int>::const_iterator it = m_roleNames.begin(); + while (it != m_roleNames.end()) { + int propId = m_delegateDataType->createProperty(it.key()) - m_delegateDataType->propertyOffset(); + roleToPropId.insert(*it, propId); + ++it; + } + m_metaDataCreated = true; } - m_metaDataCreated = true; } } @@ -381,7 +388,6 @@ public: private: friend class QDeclarativeVisualDataModelData; - QHash<int,int> roleToProp; }; class QDeclarativeVisualDataModelData : public QObject @@ -398,6 +404,8 @@ public: int propForRole(int) const; void setValue(int, const QVariant &); + void ensureProperties(); + Q_SIGNALS: void indexChanged(); @@ -410,9 +418,11 @@ private: int QDeclarativeVisualDataModelData::propForRole(int id) const { - QHash<int,int>::const_iterator it = m_meta->roleToProp.find(id); - if (it != m_meta->roleToProp.end()) - return m_meta->roleToProp[id]; + QDeclarativeVisualDataModelPrivate *model = QDeclarativeVisualDataModelPrivate::get(m_model); + QHash<int,int>::const_iterator it = model->roleToPropId.find(id); + if (it != model->roleToPropId.end()) + return *it; + return -1; } @@ -436,8 +446,7 @@ int QDeclarativeVisualDataModelDataMetaObject::createProperty(const char *name, if ((!model->m_listModelInterface || !model->m_abstractItemModel) && model->m_listAccessor) { if (model->m_listAccessor->type() == QDeclarativeListAccessor::ListProperty) { model->ensureRoles(); - QObject *object = model->m_listAccessor->at(data->m_index).value<QObject*>(); - if (object && (object->property(name).isValid() || qstrcmp(name,"modelData")==0)) + if (qstrcmp(name,"modelData") == 0) return QDeclarativeOpenMetaObject::createProperty(name, type); } } @@ -469,12 +478,10 @@ QVariant QDeclarativeVisualDataModelDataMetaObject::initialValue(int propId) model->ensureRoles(); QHash<QByteArray,int>::const_iterator it = model->m_roleNames.find(propName); if (it != model->m_roleNames.end()) { - roleToProp.insert(*it, propId); QVariant value = model->m_listModelInterface->data(data->m_index, *it); return value; } else if (model->m_roles.count() == 1 && propName == "modelData") { //for compatability with other lists, assign modelData if there is only a single role - roleToProp.insert(model->m_roles.first(), propId); QVariant value = model->m_listModelInterface->data(data->m_index, model->m_roles.first()); return value; } @@ -486,7 +493,6 @@ QVariant QDeclarativeVisualDataModelDataMetaObject::initialValue(int propId) } 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); } @@ -501,18 +507,23 @@ QDeclarativeVisualDataModelData::QDeclarativeVisualDataModelData(int index, : m_index(index), m_model(model), m_meta(new QDeclarativeVisualDataModelDataMetaObject(this, QDeclarativeVisualDataModelPrivate::get(model)->m_delegateDataType)) { - QDeclarativeVisualDataModelPrivate *modelPriv = QDeclarativeVisualDataModelPrivate::get(model); - if (modelPriv->m_metaDataCacheable) { - if (!modelPriv->m_metaDataCreated) - modelPriv->createMetaData(); - m_meta->setCached(true); - } + ensureProperties(); } QDeclarativeVisualDataModelData::~QDeclarativeVisualDataModelData() { } +void QDeclarativeVisualDataModelData::ensureProperties() +{ + QDeclarativeVisualDataModelPrivate *modelPriv = QDeclarativeVisualDataModelPrivate::get(m_model); + if (modelPriv->m_metaDataCacheable && !modelPriv->m_metaDataCreated) { + modelPriv->createMetaData(); + if (modelPriv->m_metaDataCreated) + m_meta->setCached(true); + } +} + int QDeclarativeVisualDataModelData::index() const { return m_index; @@ -625,8 +636,8 @@ QDeclarativeVisualDataModel::QDeclarativeVisualDataModel() { } -QDeclarativeVisualDataModel::QDeclarativeVisualDataModel(QDeclarativeContext *ctxt) -: QDeclarativeVisualModel(*(new QDeclarativeVisualDataModelPrivate(ctxt))) +QDeclarativeVisualDataModel::QDeclarativeVisualDataModel(QDeclarativeContext *ctxt, QObject *parent) +: QDeclarativeVisualModel(*(new QDeclarativeVisualDataModelPrivate(ctxt)), parent) { } @@ -759,7 +770,7 @@ void QDeclarativeVisualDataModel::setModel(const QVariant &model) } /*! - \qmlproperty component VisualDataModel::delegate + \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 @@ -963,13 +974,18 @@ QDeclarativeVisualDataModel::ReleaseFlags QDeclarativeVisualDataModel::release(Q } if (d->m_cache.releaseItem(obj)) { + // Remove any bindings to avoid warnings due to parent change. + QObjectPrivate *p = QObjectPrivate::get(obj); + Q_ASSERT(p->declarativeData); + QDeclarativeData *d = static_cast<QDeclarativeData*>(p->declarativeData); + if (d->ownContext && d->context) + d->context->clearExpressions(); + if (inPackage) { emit destroyingPackage(qobject_cast<QDeclarativePackage*>(obj)); } else { - if (item->hasFocus()) - item->clearFocus(); - item->setOpacity(0.0); - static_cast<QGraphicsItem*>(item)->setParentItem(0); + if (item->scene()) + item->scene()->removeItem(item); } stat |= Destroyed; obj->deleteLater(); @@ -1030,6 +1046,11 @@ QDeclarativeItem *QDeclarativeVisualDataModel::item(int index, const QByteArray if (!ccontext) ccontext = qmlContext(this); QDeclarativeContext *ctxt = new QDeclarativeContext(ccontext); QDeclarativeVisualDataModelData *data = new QDeclarativeVisualDataModelData(index, this); + if ((!d->m_listModelInterface || !d->m_abstractItemModel) && d->m_listAccessor + && d->m_listAccessor->type() == QDeclarativeListAccessor::ListProperty) { + ctxt->setContextObject(d->m_listAccessor->at(index).value<QObject*>()); + ctxt = new QDeclarativeContext(ctxt, ctxt); + } ctxt->setContextProperty(QLatin1String("model"), data); ctxt->setContextObject(data); d->m_completePending = false; @@ -1201,6 +1222,13 @@ void QDeclarativeVisualDataModel::_q_itemsChanged(int index, int count, QModelIndex index = d->m_abstractItemModel->index(idx, 0, d->m_root); data->setValue(propId, d->m_abstractItemModel->data(index, role)); } + } else { + QString roleName; + if (d->m_listModelInterface) + roleName = d->m_listModelInterface->toString(role); + else if (d->m_abstractItemModel) + roleName = d->m_abstractItemModel->roleNames().value(role); + qmlInfo(this) << "Changing role not present in item: " << roleName; } } } |