1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
45 #include <QtQml/qqmlprivate.h>
46 #include <QtQml/qqmlparserstatus.h>
47 #include <QtQml/qqmlpropertyvaluesource.h>
48 #include <QtQml/qqmllist.h>
50 #include <QtCore/qbytearray.h>
51 #include <QtCore/qmetaobject.h>
55 #define QML_VERSION 0x020000
56 #define QML_VERSION_STR "2.0"
58 #define QML_DECLARE_TYPE(TYPE) \
59 Q_DECLARE_METATYPE(TYPE *) \
60 Q_DECLARE_METATYPE(QQmlListProperty<TYPE>)
62 #define QML_DECLARE_TYPE_HASMETATYPE(TYPE) \
63 Q_DECLARE_METATYPE(QQmlListProperty<TYPE>)
65 #define QML_DECLARE_INTERFACE(INTERFACE) \
66 QML_DECLARE_TYPE(INTERFACE)
68 #define QML_DECLARE_INTERFACE_HASMETATYPE(INTERFACE) \
69 QML_DECLARE_TYPE_HASMETATYPE(INTERFACE)
71 enum { /* TYPEINFO flags */
72 QML_HAS_ATTACHED_PROPERTIES = 0x01
75 #define QML_DECLARE_TYPEINFO(TYPE, FLAGS) \
78 class QQmlTypeInfo<TYPE > \
82 hasAttachedProperties = (((FLAGS) & QML_HAS_ATTACHED_PROPERTIES) == QML_HAS_ATTACHED_PROPERTIES) \
90 class QQmlPropertyValueInterceptor;
92 #define QML_GETTYPENAMES \
93 const char *className = T::staticMetaObject.className(); \
94 const int nameLen = int(strlen(className)); \
95 QVarLengthArray<char,48> pointerName(nameLen+2); \
96 memcpy(pointerName.data(), className, nameLen); \
97 pointerName[nameLen] = '*'; \
98 pointerName[nameLen+1] = '\0'; \
99 const int listLen = int(strlen("QQmlListProperty<")); \
100 QVarLengthArray<char,64> listName(listLen + nameLen + 2); \
101 memcpy(listName.data(), "QQmlListProperty<", listLen); \
102 memcpy(listName.data()+listLen, className, nameLen); \
103 listName[listLen+nameLen] = '>'; \
104 listName[listLen+nameLen+1] = '\0';
107 int qmlRegisterType()
111 QQmlPrivate::RegisterType type = {
114 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
115 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
119 0, 0, 0, 0, &T::staticMetaObject,
121 QQmlPrivate::attachedPropertiesFunc<T>(),
122 QQmlPrivate::attachedPropertiesMetaObject<T>(),
124 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
125 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
126 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
134 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
137 int Q_QML_EXPORT qmlRegisterTypeNotAvailable(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message);
140 int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason)
144 QQmlPrivate::RegisterType type = {
147 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
148 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
152 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
154 QQmlPrivate::attachedPropertiesFunc<T>(),
155 QQmlPrivate::attachedPropertiesMetaObject<T>(),
157 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
158 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
159 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
167 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
170 template<typename T, int metaObjectRevision>
171 int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason)
175 QQmlPrivate::RegisterType type = {
178 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
179 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
183 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
185 QQmlPrivate::attachedPropertiesFunc<T>(),
186 QQmlPrivate::attachedPropertiesMetaObject<T>(),
188 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
189 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
190 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
198 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
202 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
206 QQmlPrivate::RegisterType type = {
209 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
210 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
211 sizeof(T), QQmlPrivate::createInto<T>,
214 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
216 QQmlPrivate::attachedPropertiesFunc<T>(),
217 QQmlPrivate::attachedPropertiesMetaObject<T>(),
219 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
220 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
221 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
229 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
232 template<typename T, int metaObjectRevision>
233 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
237 QQmlPrivate::RegisterType type = {
240 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
241 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
242 sizeof(T), QQmlPrivate::createInto<T>,
245 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
247 QQmlPrivate::attachedPropertiesFunc<T>(),
248 QQmlPrivate::attachedPropertiesMetaObject<T>(),
250 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
251 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
252 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
260 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
263 template<typename T, int metaObjectRevision>
264 int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor)
268 QQmlPrivate::RegisterType type = {
271 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
272 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
273 sizeof(T), QQmlPrivate::createInto<T>,
276 uri, versionMajor, versionMinor, 0, &T::staticMetaObject,
278 QQmlPrivate::attachedPropertiesFunc<T>(),
279 QQmlPrivate::attachedPropertiesMetaObject<T>(),
281 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
282 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
283 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
291 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
295 template<typename T, typename E>
296 int qmlRegisterExtendedType()
300 QQmlPrivate::RegisterType type = {
303 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
304 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
308 0, 0, 0, 0, &T::staticMetaObject,
310 QQmlPrivate::attachedPropertiesFunc<T>(),
311 QQmlPrivate::attachedPropertiesMetaObject<T>(),
313 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
314 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
315 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
317 QQmlPrivate::createParent<E>, &E::staticMetaObject,
323 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
326 template<typename T, typename E>
327 int qmlRegisterExtendedType(const char *uri, int versionMajor, int versionMinor,
332 QQmlAttachedPropertiesFunc attached = QQmlPrivate::attachedPropertiesFunc<E>();
333 const QMetaObject * attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<E>();
335 attached = QQmlPrivate::attachedPropertiesFunc<T>();
336 attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<T>();
339 QQmlPrivate::RegisterType type = {
342 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
343 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
344 sizeof(T), QQmlPrivate::createInto<T>,
347 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
352 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
353 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
354 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
356 QQmlPrivate::createParent<E>, &E::staticMetaObject,
362 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
366 int qmlRegisterInterface(const char *typeName)
368 QByteArray name(typeName);
370 QByteArray pointerName(name + '*');
371 QByteArray listName("QQmlListProperty<" + name + '>');
373 QQmlPrivate::RegisterInterface qmlInterface = {
376 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
377 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
379 qobject_interface_iid<T *>()
382 return QQmlPrivate::qmlregister(QQmlPrivate::InterfaceRegistration, &qmlInterface);
386 int qmlRegisterCustomType(const char *uri, int versionMajor, int versionMinor,
387 const char *qmlName, QQmlCustomParser *parser)
391 QQmlPrivate::RegisterType type = {
394 qRegisterNormalizedMetaType<T *>(pointerName.constData()),
395 qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
396 sizeof(T), QQmlPrivate::createInto<T>,
399 uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
401 QQmlPrivate::attachedPropertiesFunc<T>(),
402 QQmlPrivate::attachedPropertiesMetaObject<T>(),
404 QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
405 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
406 QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
414 return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
421 Q_QML_EXPORT void qmlExecuteDeferred(QObject *);
422 Q_QML_EXPORT QQmlContext *qmlContext(const QObject *);
423 Q_QML_EXPORT QQmlEngine *qmlEngine(const QObject *);
424 Q_QML_EXPORT QObject *qmlAttachedPropertiesObjectById(int, const QObject *, bool create = true);
425 Q_QML_EXPORT QObject *qmlAttachedPropertiesObject(int *, const QObject *, const QMetaObject *, bool create);
428 QObject *qmlAttachedPropertiesObject(const QObject *obj, bool create = true)
431 return qmlAttachedPropertiesObject(&idx, obj, &T::staticMetaObject, create);
434 Q_QML_EXPORT void qmlRegisterBaseTypes(const char *uri, int versionMajor, int versionMinor);
436 inline int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName,
437 QJSValue (*callback)(QQmlEngine *, QJSEngine *))
439 QQmlPrivate::RegisterSingletonType api = {
442 uri, versionMajor, versionMinor, typeName,
447 return QQmlPrivate::qmlregister(QQmlPrivate::SingletonRegistration, &api);
450 static const int CurrentSingletonTypeRegistrationVersion = 2;
451 template <typename T>
452 inline int qmlRegisterSingletonType(const char *uri, int versionMajor, int versionMinor, const char *typeName,
453 QObject *(*callback)(QQmlEngine *, QJSEngine *))
457 QQmlPrivate::RegisterSingletonType api = {
458 CurrentSingletonTypeRegistrationVersion,
460 uri, versionMajor, versionMinor, typeName,
462 0, callback, &T::staticMetaObject, qRegisterNormalizedMetaType<T *>(pointerName.constData()), 0
465 return QQmlPrivate::qmlregister(QQmlPrivate::SingletonRegistration, &api);
470 QML_DECLARE_TYPE(QObject)
471 Q_DECLARE_METATYPE(QVariant)