Merge branch 'buildsystem'
[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     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';
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, int metaObjectRevision>
171 int qmlRegisterUncreatableType(const char *uri, int versionMajor, int versionMinor, const char *qmlName, const QString& reason)
172 {
173     QML_GETTYPENAMES
174
175     QQmlPrivate::RegisterType type = {
176         1,
177
178         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
179         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
180         0, 0,
181         reason,
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         metaObjectRevision
196     };
197
198     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
199 }
200
201 template<typename T>
202 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
203 {
204     QML_GETTYPENAMES
205
206     QQmlPrivate::RegisterType type = {
207         0, 
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         0
227     };
228
229     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
230 }
231
232 template<typename T, int metaObjectRevision>
233 int qmlRegisterType(const char *uri, int versionMajor, int versionMinor, const char *qmlName)
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, qmlName, &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 template<typename T, int metaObjectRevision>
264 int qmlRegisterRevision(const char *uri, int versionMajor, int versionMinor)
265 {
266     QML_GETTYPENAMES
267
268     QQmlPrivate::RegisterType type = {
269         1,
270
271         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
272         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
273         sizeof(T), QQmlPrivate::createInto<T>,
274         QString(),
275
276         uri, versionMajor, versionMinor, 0, &T::staticMetaObject,
277
278         QQmlPrivate::attachedPropertiesFunc<T>(),
279         QQmlPrivate::attachedPropertiesMetaObject<T>(),
280
281         QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(),
282         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
283         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
284
285         0, 0,
286
287         0,
288         metaObjectRevision
289     };
290
291     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
292 }
293
294
295 template<typename T, typename E>
296 int qmlRegisterExtendedType()
297 {
298     QML_GETTYPENAMES
299
300     QQmlPrivate::RegisterType type = {
301         0, 
302
303         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
304         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
305         0, 0,
306         QString(),
307
308         0, 0, 0, 0, &T::staticMetaObject,
309
310         QQmlPrivate::attachedPropertiesFunc<T>(),
311         QQmlPrivate::attachedPropertiesMetaObject<T>(),
312
313         QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(), 
314         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
315         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
316
317         QQmlPrivate::createParent<E>, &E::staticMetaObject,
318
319         0,
320         0
321     };
322
323     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
324 }
325
326 template<typename T, typename E>
327 int qmlRegisterExtendedType(const char *uri, int versionMajor, int versionMinor, 
328                             const char *qmlName)
329 {
330     QML_GETTYPENAMES
331
332     QQmlAttachedPropertiesFunc attached = QQmlPrivate::attachedPropertiesFunc<E>();
333     const QMetaObject * attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<E>(); 
334     if (!attached) {
335         attached = QQmlPrivate::attachedPropertiesFunc<T>();
336         attachedMetaObject = QQmlPrivate::attachedPropertiesMetaObject<T>();
337     }
338
339     QQmlPrivate::RegisterType type = {
340         0, 
341
342         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
343         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
344         sizeof(T), QQmlPrivate::createInto<T>,
345         QString(),
346
347         uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
348
349         attached,
350         attachedMetaObject,
351
352         QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(), 
353         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
354         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
355
356         QQmlPrivate::createParent<E>, &E::staticMetaObject,
357
358         0,
359         0
360     };
361
362     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
363 }
364
365 template<typename T>
366 int qmlRegisterInterface(const char *typeName)
367 {
368     QByteArray name(typeName);
369
370     QByteArray pointerName(name + '*');
371     QByteArray listName("QQmlListProperty<" + name + '>');
372
373     QQmlPrivate::RegisterInterface qmlInterface = {
374         0,
375
376         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
377         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
378
379         qobject_interface_iid<T *>()
380     };
381
382     return QQmlPrivate::qmlregister(QQmlPrivate::InterfaceRegistration, &qmlInterface);
383 }
384
385 template<typename T>
386 int qmlRegisterCustomType(const char *uri, int versionMajor, int versionMinor, 
387                           const char *qmlName, QQmlCustomParser *parser)
388 {
389     QML_GETTYPENAMES
390
391     QQmlPrivate::RegisterType type = {
392         0, 
393
394         qRegisterNormalizedMetaType<T *>(pointerName.constData()),
395         qRegisterNormalizedMetaType<QQmlListProperty<T> >(listName.constData()),
396         sizeof(T), QQmlPrivate::createInto<T>,
397         QString(),
398
399         uri, versionMajor, versionMinor, qmlName, &T::staticMetaObject,
400
401         QQmlPrivate::attachedPropertiesFunc<T>(),
402         QQmlPrivate::attachedPropertiesMetaObject<T>(),
403
404         QQmlPrivate::StaticCastSelector<T,QQmlParserStatus>::cast(), 
405         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueSource>::cast(),
406         QQmlPrivate::StaticCastSelector<T,QQmlPropertyValueInterceptor>::cast(),
407
408         0, 0,
409
410         parser,
411         0
412     };
413
414     return QQmlPrivate::qmlregister(QQmlPrivate::TypeRegistration, &type);
415 }
416
417 class QQmlContext;
418 class QQmlEngine;
419 class QJSValue;
420 class QJSEngine;
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);
426
427 template<typename T>
428 QObject *qmlAttachedPropertiesObject(const QObject *obj, bool create = true)
429 {
430     static int idx = -1;
431     return qmlAttachedPropertiesObject(&idx, obj, &T::staticMetaObject, create);
432 }
433
434 Q_QML_EXPORT void qmlRegisterBaseTypes(const char *uri, int versionMajor, int versionMinor);
435
436 inline int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor,
437                                 QJSValue (*callback)(QQmlEngine *, QJSEngine *))
438 {
439     QQmlPrivate::RegisterModuleApi api = {
440         0,
441
442         uri, versionMajor, versionMinor,
443
444         callback, 0, 0
445     };
446
447     return QQmlPrivate::qmlregister(QQmlPrivate::ModuleApiRegistration, &api);
448 }
449
450 template <typename T>
451 inline int qmlRegisterModuleApi(const char *uri, int versionMajor, int versionMinor,
452                                 QObject *(*callback)(QQmlEngine *, QJSEngine *))
453 {
454     QQmlPrivate::RegisterModuleApi api = {
455         1,
456
457         uri, versionMajor, versionMinor,
458
459         0, callback, &T::staticMetaObject
460     };
461
462     return QQmlPrivate::qmlregister(QQmlPrivate::ModuleApiRegistration, &api);
463 }
464
465 QT_END_NAMESPACE
466
467 QML_DECLARE_TYPE(QObject)
468 Q_DECLARE_METATYPE(QVariant)
469
470 QT_END_HEADER
471
472 #endif // QQML_H