From 8e7d1a91196197eee4e45bbfa9886ab935e2b67c Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Mon, 14 Sep 2015 22:02:55 +0200 Subject: Make QML composite types inherit attached properties Change-Id: Ic06af4805da987dd08e361f2668e7a1788d3eefe Task-number: QTBUG-43581 Reviewed-by: Mitch Curtis Reviewed-by: Liang Qi Reviewed-by: Lars Knoll --- src/qml/qml/qqmlengine.cpp | 9 ++++-- src/qml/qml/qqmlmetatype.cpp | 62 +++++++++++++++++++++++++-------------- src/qml/qml/qqmlmetatype_p.h | 11 +++---- src/qml/qml/qqmlobjectcreator.cpp | 7 ++++- src/qml/qml/qqmlproperty.cpp | 10 ++++--- src/qml/qml/qqmltypewrapper.cpp | 5 ++-- 6 files changed, 67 insertions(+), 37 deletions(-) (limited to 'src/qml/qml') diff --git a/src/qml/qml/qqmlengine.cpp b/src/qml/qml/qqmlengine.cpp index 0c003790dd..be535025e6 100644 --- a/src/qml/qml/qqmlengine.cpp +++ b/src/qml/qml/qqmlengine.cpp @@ -1434,7 +1434,8 @@ QObject *qmlAttachedPropertiesObjectById(int id, const QObject *object, bool cre if (rv || !create) return rv; - QQmlAttachedPropertiesFunc pf = QQmlMetaType::attachedPropertiesFuncById(id); + QQmlEnginePrivate *engine = QQmlEnginePrivate::get(data->context); + QQmlAttachedPropertiesFunc pf = QQmlMetaType::attachedPropertiesFuncById(engine, id); if (!pf) return 0; @@ -1449,8 +1450,10 @@ QObject *qmlAttachedPropertiesObjectById(int id, const QObject *object, bool cre QObject *qmlAttachedPropertiesObject(int *idCache, const QObject *object, const QMetaObject *attachedMetaObject, bool create) { - if (*idCache == -1) - *idCache = QQmlMetaType::attachedPropertiesFuncId(attachedMetaObject); + if (*idCache == -1) { + QQmlEngine *engine = object ? qmlEngine(object) : 0; + *idCache = QQmlMetaType::attachedPropertiesFuncId(engine ? QQmlEnginePrivate::get(engine) : 0, attachedMetaObject); + } if (*idCache == -1 || !object) return 0; diff --git a/src/qml/qml/qqmlmetatype.cpp b/src/qml/qml/qqmlmetatype.cpp index 04c001d305..26271e3f03 100644 --- a/src/qml/qml/qqmlmetatype.cpp +++ b/src/qml/qml/qqmlmetatype.cpp @@ -474,18 +474,24 @@ QQmlType *QQmlType::superType() const return d->superType; } -int QQmlType::resolveCompositeEnumValue(QQmlEnginePrivate *engine, const QString &name, bool *ok) const +QQmlType *QQmlType::resolveCompositeBaseType(QQmlEnginePrivate *engine) const { Q_ASSERT(isComposite()); - *ok = false; if (!engine) - return -1; + return 0; QQmlTypeData *td = engine->typeLoader.getType(sourceUrl()); if (!td || !td->isComplete()) - return -1; + return 0; QQmlCompiledData *cd = td->compiledData(); const QMetaObject *mo = cd->rootPropertyCache->firstCppMetaObject(); - QQmlType *type = QQmlMetaType::qmlType(mo); + return QQmlMetaType::qmlType(mo); +} + +int QQmlType::resolveCompositeEnumValue(QQmlEnginePrivate *engine, const QString &name, bool *ok) const +{ + Q_ASSERT(isComposite()); + *ok = false; + QQmlType *type = resolveCompositeBaseType(engine); if (!type) return -1; return type->enumValue(engine, name, ok); @@ -856,18 +862,26 @@ int QQmlType::metaObjectRevision() const return d->revision; } -QQmlAttachedPropertiesFunc QQmlType::attachedPropertiesFunction() const +QQmlAttachedPropertiesFunc QQmlType::attachedPropertiesFunction(QQmlEnginePrivate *engine) const { - if (d->regType != CppType) - return 0; - return d->extraData.cd->attachedPropertiesFunc; + if (d->regType == CppType) + return d->extraData.cd->attachedPropertiesFunc; + + QQmlType *base = 0; + if (d->regType == CompositeType) + base = resolveCompositeBaseType(engine); + return base ? base->attachedPropertiesFunction(engine) : 0; } -const QMetaObject *QQmlType::attachedPropertiesType() const +const QMetaObject *QQmlType::attachedPropertiesType(QQmlEnginePrivate *engine) const { - if (d->regType != CppType) - return 0; - return d->extraData.cd->attachedPropertiesType; + if (d->regType == CppType) + return d->extraData.cd->attachedPropertiesType; + + QQmlType *base = 0; + if (d->regType == CompositeType) + base = resolveCompositeBaseType(engine); + return base ? base->attachedPropertiesType(engine) : 0; } /* @@ -875,11 +889,15 @@ This is the id passed to qmlAttachedPropertiesById(). This is different from th for the case that a single class is registered under two or more names (eg. Item in Qt 4.7 and QtQuick 1.0). */ -int QQmlType::attachedPropertiesId() const +int QQmlType::attachedPropertiesId(QQmlEnginePrivate *engine) const { - if (d->regType != CppType) - return 0; - return d->extraData.cd->attachedPropertiesId; + if (d->regType == CppType) + return d->extraData.cd->attachedPropertiesId; + + QQmlType *base = 0; + if (d->regType == CompositeType) + base = resolveCompositeBaseType(engine); + return base ? base->attachedPropertiesId(engine) : 0; } int QQmlType::parserStatusCast() const @@ -1560,25 +1578,25 @@ int QQmlMetaType::listType(int id) return 0; } -int QQmlMetaType::attachedPropertiesFuncId(const QMetaObject *mo) +int QQmlMetaType::attachedPropertiesFuncId(QQmlEnginePrivate *engine, const QMetaObject *mo) { QMutexLocker lock(metaTypeDataLock()); QQmlMetaTypeData *data = metaTypeData(); QQmlType *type = data->metaObjectToType.value(mo); - if (type && type->attachedPropertiesFunction()) - return type->attachedPropertiesId(); + if (type && type->attachedPropertiesFunction(engine)) + return type->attachedPropertiesId(engine); else return -1; } -QQmlAttachedPropertiesFunc QQmlMetaType::attachedPropertiesFuncById(int id) +QQmlAttachedPropertiesFunc QQmlMetaType::attachedPropertiesFuncById(QQmlEnginePrivate *engine, int id) { if (id < 0) return 0; QMutexLocker lock(metaTypeDataLock()); QQmlMetaTypeData *data = metaTypeData(); - return data->types.at(id)->attachedPropertiesFunction(); + return data->types.at(id)->attachedPropertiesFunction(engine); } QMetaProperty QQmlMetaType::defaultProperty(const QMetaObject *metaObject) diff --git a/src/qml/qml/qqmlmetatype_p.h b/src/qml/qml/qqmlmetatype_p.h index 40765d461a..c120941a03 100644 --- a/src/qml/qml/qqmlmetatype_p.h +++ b/src/qml/qml/qqmlmetatype_p.h @@ -92,8 +92,8 @@ public: static QObject *toQObject(const QVariant &, bool *ok = 0); static int listType(int); - static int attachedPropertiesFuncId(const QMetaObject *); - static QQmlAttachedPropertiesFunc attachedPropertiesFuncById(int); + static int attachedPropertiesFuncId(QQmlEnginePrivate *engine, const QMetaObject *); + static QQmlAttachedPropertiesFunc attachedPropertiesFuncById(QQmlEnginePrivate *, int); enum TypeCategory { Unknown, Object, List }; static TypeCategory typeCategory(int); @@ -169,9 +169,9 @@ public: int metaObjectRevision() const; bool containsRevisionedAttributes() const; - QQmlAttachedPropertiesFunc attachedPropertiesFunction() const; - const QMetaObject *attachedPropertiesType() const; - int attachedPropertiesId() const; + QQmlAttachedPropertiesFunc attachedPropertiesFunction(QQmlEnginePrivate *engine) const; + const QMetaObject *attachedPropertiesType(QQmlEnginePrivate *engine) const; + int attachedPropertiesId(QQmlEnginePrivate *engine) const; int parserStatusCast() const; const char *interfaceIId() const; @@ -212,6 +212,7 @@ public: int enumValue(QQmlEnginePrivate *engine, const QV4::String *, bool *ok) const; private: QQmlType *superType() const; + QQmlType *resolveCompositeBaseType(QQmlEnginePrivate *engine) const; int resolveCompositeEnumValue(QQmlEnginePrivate *engine, const QString &name, bool *ok) const; friend class QQmlTypePrivate; friend struct QQmlMetaTypeData; diff --git a/src/qml/qml/qqmlobjectcreator.cpp b/src/qml/qml/qqmlobjectcreator.cpp index 58c0b49b3c..c03a463c83 100644 --- a/src/qml/qml/qqmlobjectcreator.cpp +++ b/src/qml/qml/qqmlobjectcreator.cpp @@ -706,7 +706,12 @@ bool QQmlObjectCreator::setPropertyBinding(const QQmlPropertyData *property, con QQmlCompiledData::TypeReference *tr = resolvedTypes.value(binding->propertyNameIndex); Q_ASSERT(tr); QQmlType *attachedType = tr->type; - const int id = attachedType->attachedPropertiesId(); + if (!attachedType) { + QQmlTypeNameCache::Result res = context->imports->query(stringAt(binding->propertyNameIndex)); + if (res.isValid()) + attachedType = res.type; + } + const int id = attachedType->attachedPropertiesId(QQmlEnginePrivate::get(engine)); QObject *qmlObject = qmlAttachedPropertiesObjectById(id, _qobject); if (!populateInstance(binding->value.objectIndex, qmlObject, qmlObject, /*value type property*/0)) return false; diff --git a/src/qml/qml/qqmlproperty.cpp b/src/qml/qml/qqmlproperty.cpp index d3f7070528..1b78ada698 100644 --- a/src/qml/qml/qqmlproperty.cpp +++ b/src/qml/qml/qqmlproperty.cpp @@ -248,10 +248,11 @@ void QQmlPropertyPrivate::initProperty(QObject *obj, const QString &name) QQmlTypeNameCache::Result r = typeNameCache->query(pathName); if (r.isValid()) { if (r.type) { - QQmlAttachedPropertiesFunc func = r.type->attachedPropertiesFunction(); + QQmlEnginePrivate *enginePrivate = QQmlEnginePrivate::get(engine); + QQmlAttachedPropertiesFunc func = r.type->attachedPropertiesFunction(enginePrivate); if (!func) return; // Not an attachable type - currentObject = qmlAttachedPropertiesObjectById(r.type->attachedPropertiesId(), currentObject); + currentObject = qmlAttachedPropertiesObjectById(r.type->attachedPropertiesId(enginePrivate), currentObject); if (!currentObject) return; // Something is broken with the attachable type } else if (r.importNamespace) { if ((ii + 1) == path.count()) return; // No type following the namespace @@ -259,10 +260,11 @@ void QQmlPropertyPrivate::initProperty(QObject *obj, const QString &name) ++ii; r = typeNameCache->query(path.at(ii), r.importNamespace); if (!r.type) return; // Invalid type in namespace - QQmlAttachedPropertiesFunc func = r.type->attachedPropertiesFunction(); + QQmlEnginePrivate *enginePrivate = QQmlEnginePrivate::get(engine); + QQmlAttachedPropertiesFunc func = r.type->attachedPropertiesFunction(enginePrivate); if (!func) return; // Not an attachable type - currentObject = qmlAttachedPropertiesObjectById(r.type->attachedPropertiesId(), currentObject); + currentObject = qmlAttachedPropertiesObjectById(r.type->attachedPropertiesId(enginePrivate), currentObject); if (!currentObject) return; // Something is broken with the attachable type } else if (r.scriptIndex != -1) { diff --git a/src/qml/qml/qqmltypewrapper.cpp b/src/qml/qml/qqmltypewrapper.cpp index d70a4019b2..6c29f2fbb5 100644 --- a/src/qml/qml/qqmltypewrapper.cpp +++ b/src/qml/qml/qqmltypewrapper.cpp @@ -185,7 +185,7 @@ ReturnedValue QmlTypeWrapper::get(const Managed *m, String *name, bool *hasPrope // Fall through to base implementation } else if (w->d()->object) { - QObject *ao = qmlAttachedPropertiesObjectById(type->attachedPropertiesId(), object); + QObject *ao = qmlAttachedPropertiesObjectById(type->attachedPropertiesId(QQmlEnginePrivate::get(v4->qmlEngine())), object); if (ao) return QV4::QObjectWrapper::getQmlProperty(v4, context, ao, name, QV4::QObjectWrapper::IgnoreRevision, hasProperty); @@ -241,7 +241,8 @@ void QmlTypeWrapper::put(Managed *m, String *name, const Value &value) QQmlType *type = w->d()->type; if (type && !type->isSingleton() && w->d()->object) { QObject *object = w->d()->object; - QObject *ao = qmlAttachedPropertiesObjectById(type->attachedPropertiesId(), object); + QQmlEngine *e = scope.engine->qmlEngine(); + QObject *ao = qmlAttachedPropertiesObjectById(type->attachedPropertiesId(QQmlEnginePrivate::get(e)), object); if (ao) QV4::QObjectWrapper::setQmlProperty(v4, context, ao, name, QV4::QObjectWrapper::IgnoreRevision, value); } else if (type && type->isSingleton()) { -- cgit v1.2.3