9c01ee4bd6e1577167165033e3bac57d9da548ca
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqml.h
1 /****************************************************************************
2 **
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
5 **
6 ** This file is part of the QtQml module of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** GNU Lesser General Public License Usage
10 ** This file may be used under the terms of the GNU Lesser General Public
11 ** License version 2.1 as published by the Free Software Foundation and
12 ** appearing in the file LICENSE.LGPL included in the packaging of this
13 ** file. Please review the following information to ensure the GNU Lesser
14 ** General Public License version 2.1 requirements will be met:
15 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
16 **
17 ** In addition, as a special exception, Nokia gives you certain additional
18 ** rights. These rights are described in the Nokia Qt LGPL Exception
19 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
20 **
21 ** GNU General Public License Usage
22 ** Alternatively, this file may be used under the terms of the GNU General
23 ** Public License version 3.0 as published by the Free Software Foundation
24 ** and appearing in the file LICENSE.GPL included in the packaging of this
25 ** file. Please review the following information to ensure the GNU General
26 ** Public License version 3.0 requirements will be met:
27 ** http://www.gnu.org/copyleft/gpl.html.
28 **
29 ** Other Usage
30 ** Alternatively, this file may be used in accordance with the terms and
31 ** conditions contained in a signed written agreement between you and Nokia.
32 **
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #ifndef QQML_H
43 #define QQML_H
44
45 #include <QtQml/qqmlprivate.h>
46 #include <QtQml/qqmlparserstatus.h>
47 #include <QtQml/qqmlpropertyvaluesource.h>
48 #include <QtQml/qqmllist.h>
49
50 #include <QtCore/qbytearray.h>
51 #include <QtCore/qmetaobject.h>
52
53 QT_BEGIN_HEADER
54
55 #define QML_VERSION     0x020000
56 #define QML_VERSION_STR "2.0"
57
58 #define QML_DECLARE_TYPE(TYPE) \
59     Q_DECLARE_METATYPE(TYPE *) \
60     Q_DECLARE_METATYPE(QQmlListProperty<TYPE>) 
61
62 #define QML_DECLARE_TYPE_HASMETATYPE(TYPE) \
63     Q_DECLARE_METATYPE(QQmlListProperty<TYPE>) 
64
65 #define QML_DECLARE_INTERFACE(INTERFACE) \
66     QML_DECLARE_TYPE(INTERFACE)
67
68 #define QML_DECLARE_INTERFACE_HASMETATYPE(INTERFACE) \
69     QML_DECLARE_TYPE_HASMETATYPE(INTERFACE)
70
71 enum { /* TYPEINFO flags */
72     QML_HAS_ATTACHED_PROPERTIES = 0x01
73 };
74
75 #define QML_DECLARE_TYPEINFO(TYPE, FLAGS) \
76 QT_BEGIN_NAMESPACE \
77 template <> \
78 class QQmlTypeInfo<TYPE > \
79 { \
80 public: \
81     enum { \
82         hasAttachedProperties = (((FLAGS) & QML_HAS_ATTACHED_PROPERTIES) == QML_HAS_ATTACHED_PROPERTIES) \
83     }; \
84 }; \
85 QT_END_NAMESPACE
86
87 QT_BEGIN_NAMESPACE
88
89
90 class QQmlPropertyValueInterceptor;
91
92 #define QML_GETTYPENAMES \
93     const char *className = T::staticMetaObject.className(); \
94     int nameLen = strlen(className); \
95     QVarLengthArray<char,48> pointerName(nameLen+2); \
96     memcpy(pointerName.data(), className, nameLen); \
97     pointerName[nameLen] = '*'; \
98     pointerName[nameLen+1] = '\0'; \
99     int listLen = 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';
105
106 template<typename T>
107 int qmlRegisterType()
108 {
109     QML_GETTYPENAMES
110
111     QQmlPrivate::RegisterType type = {
112         0, 
113
114         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
115         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
116         0, 0,
117         QString(),
118
119         0, 0, 0, 0, &T::staticMetaObject,
120
121         QQmlPrivate::attachedPropertiesFunc<T>(),
122         QQmlPrivate::attachedPropertiesMetaObject<T>(),
123
124         QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(), 
125         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
126         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
127
128         0, 0,
129
130         0,
131         0
132     };
133
134     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
135 }
136
137 int Q_QML_EXPORT qmlRegisterTypeNotAvailable(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& message);
138
139 template<typename T>
140 int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason)
141 {
142     QML_GETTYPENAMES
143
144     QQmlPrivate::RegisterType type = {
145         0,
146
147         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
148         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
149         0, 0,
150         reason,
151
152         uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
153
154         QQmlPrivate::attachedPropertiesFunc<T>(),
155         QQmlPrivate::attachedPropertiesMetaObject<T>(),
156
157         QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
158         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
159         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
160
161         0, 0,
162
163         0,
164         0
165     };
166
167     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
168 }
169
170 template<typename T>
171 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
172 {
173     QML_GETTYPENAMES
174
175     QQmlPrivate::RegisterType type = {
176         0, 
177
178         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
179         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
180         sizeof(T), QQmlPrivate::createInto<T>,
181         QString(),
182
183         uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
184
185         QQmlPrivate::attachedPropertiesFunc<T>(),
186         QQmlPrivate::attachedPropertiesMetaObject<T>(),
187
188         QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(), 
189         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
190         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
191
192         0, 0,
193
194         0,
195         0
196     };
197
198     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
199 }
200
201 template<typename T, int metaObjectRevision>
202 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
203 {
204     QML_GETTYPENAMES
205
206     QQmlPrivate::RegisterType type = {
207         1,
208
209         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
210         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
211         sizeof(T), QQmlPrivate::createInto<T>,
212         QString(),
213
214         uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
215
216         QQmlPrivate::attachedPropertiesFunc<T>(),
217         QQmlPrivate::attachedPropertiesMetaObject<T>(),
218
219         QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
220         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
221         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
222
223         0, 0,
224
225         0,
226         metaObjectRevision
227     };
228
229     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
230 }
231
232 template<typename T, int metaObjectRevision>
233 int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor)
234 {
235     QML_GETTYPENAMES
236
237     QQmlPrivate::RegisterType type = {
238         1,
239
240         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
241         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
242         sizeof(T), QQmlPrivate::createInto<T>,
243         QString(),
244
245         uri, versionMajor, versionMinor, 0, &T::staticMetaObject,
246
247         QQmlPrivate::attachedPropertiesFunc<T>(),
248         QQmlPrivate::attachedPropertiesMetaObject<T>(),
249
250         QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
251         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
252         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
253
254         0, 0,
255
256         0,
257         metaObjectRevision
258     };
259
260     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
261 }
262
263
264 template<typename T, typename E>
265 int qmlRegisterExtendedType()
266 {
267     QML_GETTYPENAMES
268
269     QQmlPrivate::RegisterType type = {
270         0, 
271
272         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
273         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
274         0, 0,
275         QString(),
276
277         0, 0, 0, 0, &T::staticMetaObject,
278
279         QQmlPrivate::attachedPropertiesFunc<T>(),
280         QQmlPrivate::attachedPropertiesMetaObject<T>(),
281
282         QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(), 
283         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
284         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
285
286         QQmlPrivate::createParent<E>, &E::staticMetaObject,
287
288         0,
289         0
290     };
291
292     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
293 }
294
295 template<typename T, typename E>
296 int qmlRegisterExtendedType(const char *uri, int versionMajor, int versionMinor, 
297                             const char *qmlName)
298 {
299     QML_GETTYPENAMES
300
301     QQmlAttachedPropertiesFunc attached = QQmlPrivate::attachedPropertiesFunc<E>();
302     const QMetaObject * attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<E>(); 
303     if (!attached) {
304         attached = QQmlPrivate::attachedPropertiesFunc<T>();
305         attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<T>();
306     }
307
308     QQmlPrivate::RegisterType type = {
309         0, 
310
311         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
312         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
313         sizeof(T), QQmlPrivate::createInto<T>,
314         QString(),
315
316         uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
317
318         attached,
319         attachedMetaObject,
320
321         QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(), 
322         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
323         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
324
325         QQmlPrivate::createParent<E>, &E::staticMetaObject,
326
327         0,
328         0
329     };
330
331     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
332 }
333
334 template<typename T>
335 int qmlRegisterInterface(const char *typeName)
336 {
337     QByteArray name(typeName);
338
339     QByteArray pointerName(name + '*');
340     QByteArray listName("QQmlListProperty<" + name + '>');
341
342     QQmlPrivate::RegisterInterface qmlInterface = {
343         0,
344
345         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
346         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
347
348         qobject_interface_iid<T *>()
349     };
350
351     return QQmlPrivate::qmlregister(QQmlPrivate::InterfaceRegistration, &qmlInterface);
352 }
353
354 template<typename T>
355 int qmlRegisterCustomType(const char *uri, int versionMajor, int versionMinor, 
356                           const char *qmlName, QQmlCustomParser *parser)
357 {
358     QML_GETTYPENAMES
359
360     QQmlPrivate::RegisterType type = {
361         0, 
362
363         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
364         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
365         sizeof(T), QQmlPrivate::createInto<T>,
366         QString(),
367
368         uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
369
370         QQmlPrivate::attachedPropertiesFunc<T>(),
371         QQmlPrivate::attachedPropertiesMetaObject<T>(),
372
373         QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(), 
374         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
375         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
376
377         0, 0,
378
379         parser,
380         0
381     };
382
383     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
384 }
385
386 class QQmlContext;
387 class QQmlEngine;
388 class QJSValue;
389 class QJSEngine;
390 Q_QML_EXPORT void qmlExecuteDeferred(QObject *);
391 Q_QML_EXPORT QQmlContext *qmlContext(const QObject *);
392 Q_QML_EXPORT QQmlEngine *qmlEngine(const QObject *);
393 Q_QML_EXPORT QObject *qmlAttachedPropertiesObjectById(int, const QObject *, bool create = true);
394 Q_QML_EXPORT QObject *qmlAttachedPropertiesObject(int *, const QObject *, const QMetaObject *, bool create);
395
396 template<typename T>
397 QObject *qmlAttachedPropertiesObject(const QObject *obj, bool create = true)
398 {
399     static int idx = -1;
400     return qmlAttachedPropertiesObject(&idx, obj, &T::staticMetaObject, create);
401 }
402
403 Q_QML_EXPORT void qmlRegisterBaseTypes(const char *uri, int versionMajor, int versionMinor);
404
405 inline int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor,
406                                 QJSValue (*callback)(QQmlEngine *, QJSEngine *))
407 {
408     QQmlPrivate::RegisterModuleApi api = {
409         0,
410
411         uri, versionMajor, versionMinor,
412
413         callback, 0, 0
414     };
415
416     return QQmlPrivate::qmlregister(QQmlPrivate::ModuleApiRegistration, &api);
417 }
418
419 inline int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor,
420                                 QObject *(*callback)(QQmlEngine *, QJSEngine *))
421 {
422     QQmlPrivate::RegisterModuleApi api = {
423         0,
424
425         uri, versionMajor, versionMinor,
426
427         0, callback, 0 // unknown QObject instance type
428     };
429
430     return QQmlPrivate::qmlregister(QQmlPrivate::ModuleApiRegistration, &api);
431 }
432
433 template <typename T>
434 inline int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor,
435                                 QObject *(*callback)(QQmlEngine *, QJSEngine *))
436 {
437     QQmlPrivate::RegisterModuleApi api = {
438         1,
439
440         uri, versionMajor, versionMinor,
441
442         0, callback, &T::staticMetaObject
443     };
444
445     return QQmlPrivate::qmlregister(QQmlPrivate::ModuleApiRegistration, &api);
446 }
447
448 QT_END_NAMESPACE
449
450 QML_DECLARE_TYPE(QObject)
451 Q_DECLARE_METATYPE(QVariant)
452
453 QT_END_HEADER
454
455 #endif // QQML_H