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