diff options
author | Paul Lemire <paul.lemire@kdab.com> | 2014-07-15 10:35:24 +0200 |
---|---|---|
committer | Sean Harmer <sean.harmer@kdab.com> | 2014-07-25 16:29:36 +0200 |
commit | fcb40ff6d71f4561401e6b2bd4d7fc706fff8eee (patch) | |
tree | 942096e3c60e7d08349fa5c1491620e6101146f3 /src | |
parent | 38149bff7c4b89b59f73f4f4d6d5bd1ce9dafbc1 (diff) |
qmlRegisterCustomExtendedType, qmlRegisterExtendedUncreatableType
There are cases in Qt3D where we'd like to be able to register uncreatable
extended types. The main use case is having an abstract class that has an
extension class so as to have a clean separation between the C++
and QML API. Implementations of the abstract can then be easily registered
to QML and rely on the extension class for QML specific properties.
The other feature we'll need in the near future is the ability to create
extended QML types that use a custom parser.
Two new type registration method were added to qqml.h to fulfill those
needs. Unit tests for those are present in qqmlecmascript and qqmllanguage.
Change-Id: I15b2cd791ffd36b537305af1873491c079d4094e
Reviewed-by: Sean Harmer <sean.harmer@kdab.com>
Reviewed-by: Simon Hausmann <simon.hausmann@digia.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/qml/doc/src/qmlfunctions.qdoc | 38 | ||||
-rw-r--r-- | src/qml/qml/qqml.h | 77 |
2 files changed, 115 insertions, 0 deletions
diff --git a/src/qml/doc/src/qmlfunctions.qdoc b/src/qml/doc/src/qmlfunctions.qdoc index d3d3174193..e95784dc5c 100644 --- a/src/qml/doc/src/qmlfunctions.qdoc +++ b/src/qml/doc/src/qmlfunctions.qdoc @@ -157,6 +157,44 @@ */ /*! + \fn int qmlRegisterExtendedUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message) + \relates QQmlEgine + + This template function registers the C++ type and its extension + in the QML system with the name \a qmlName in the library imported + from \a uri having version number composed from \a versionMajor and + \a versionMinor. + + While the type has a name and a type, it cannot be created, and the + given error \a message will result if creation is attempted. + + This is useful where the type is only intended for providing attached + properties, enum values or an abstract base class with its extension. + + Returns the QML type id. + + #include <QtQml> to use this function. + + \sa qmlRegisterUncreatableType() +*/ + +/*! + \fn int qmlRegisterCustomExtendedType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, QQmlCustomParser *parser) + \relates QQmlEgine + + This template function registers the C++ type and its extension + in the QML system with the name \a qmlName in the library imported + from \a uri having version number composed from \a versionMajor and + \a versionMinor. Properties from the C++ type or its extension that + cannot be resolved directly by the QML system will be resolved using + the \a parser provided. + + Returns the QML type id. + + #include <QtQml> to use this function. +*/ + +/*! \fn int qmlRegisterTypeNotAvailable(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message) \relates QQmlEngine diff --git a/src/qml/qml/qqml.h b/src/qml/qml/qqml.h index 721f2cc5a8..86cb395c3d 100644 --- a/src/qml/qml/qqml.h +++ b/src/qml/qml/qqml.h @@ -198,6 +198,44 @@ int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMin return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type); } +template<typename T, typename E> +int qmlRegisterExtendedUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason) +{ + QML_GETTYPENAMES + + QQmlAttachedPropertiesFunc attached = QQmlPrivate::attachedPropertiesFunc<E>(); + const QMetaObject * attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<E>(); + if (!attached) { + attached = QQmlPrivate::attachedPropertiesFunc<T>(); + attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<T>(); + } + + QQmlPrivate::RegisterType type = { + 0, + + qRegisterNormalizedMetaType<T *>(pointerName.constData()), + qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()), + 0, 0, + reason, + + uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject, + + attached, + attachedMetaObject, + + QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(), + QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(), + QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(), + + QQmlPrivate::createParent<E>, &E::staticMetaObject, + + 0, + 0 + }; + + return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type); +} + template<typename T> int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName) { @@ -414,6 +452,45 @@ int qmlRegisterCustomType(const char *uri, int versionMajor, int versionMinor, return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type); } +template<typename T, typename E> +int qmlRegisterCustomExtendedType(const char *uri, int versionMajor, int versionMinor, + const char *qmlName, QQmlCustomParser *parser) +{ + QML_GETTYPENAMES + + QQmlAttachedPropertiesFunc attached = QQmlPrivate::attachedPropertiesFunc<E>(); + const QMetaObject * attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<E>(); + if (!attached) { + attached = QQmlPrivate::attachedPropertiesFunc<T>(); + attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<T>(); + } + + QQmlPrivate::RegisterType type = { + 0, + + qRegisterNormalizedMetaType<T *>(pointerName.constData()), + qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()), + sizeof(T), QQmlPrivate::createInto<T>, + QString(), + + uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject, + + attached, + attachedMetaObject, + + QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(), + QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(), + QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(), + + QQmlPrivate::createParent<E>, &E::staticMetaObject, + + parser, + 0 + }; + + return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type); +} + class QQmlContext; class QQmlEngine; class QJSValue; |