Implement strict mode for qmldir modules
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlglobal_p.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 QQMLGLOBAL_H
43 #define QQMLGLOBAL_H
44
45 #include <private/qtqmlglobal_p.h>
46 #include <QtCore/QObject>
47 #include <private/qqmlpropertycache_p.h>
48 #include <private/qmetaobject_p.h>
49 #include <private/qv8engine_p.h>
50
51 QT_BEGIN_HEADER
52
53 QT_BEGIN_NAMESPACE
54
55
56 #define DEFINE_BOOL_CONFIG_OPTION(name, var) \
57     static bool name() \
58     { \
59         static enum { Yes, No, Unknown } status = Unknown; \
60         if (status == Unknown) { \
61             QByteArray v = qgetenv(#var); \
62             bool value = !v.isEmpty() && v != "0" && v != "false"; \
63             if (value) status = Yes; \
64             else status = No; \
65         } \
66         return status == Yes; \
67     }
68
69 /*!
70     Connect \a Signal of \a Sender to \a Method of \a Receiver.  \a Signal must be
71     of type \a SenderType and \a Receiver of type \a ReceiverType.
72
73     Unlike QObject::connect(), this method caches the lookup of the signal and method
74     indexes.  It also does not require lazy QMetaObjects to be built so should be
75     preferred in all QML code that might interact with QML built objects.
76
77     \code
78         QQuickTextControl *control;
79         QQuickTextEdit *textEdit;
80         qmlobject_connect(control, QQuickTextControl, SIGNAL(updateRequest(QRectF)),
81                           textEdit, QQuickTextEdit, SLOT(updateDocument()));
82     \endcode
83 */
84 #define qmlobject_connect(Sender, SenderType, Signal, Receiver, ReceiverType, Method) \
85 { \
86     SenderType *sender = (Sender); \
87     ReceiverType *receiver = (Receiver); \
88     const char *signal = (Signal); \
89     const char *method = (Method); \
90     static int signalIdx = -1; \
91     static int methodIdx = -1; \
92     if (signalIdx < 0) { \
93         Q_ASSERT(((int)(*signal) - '0') == QSIGNAL_CODE); \
94         signalIdx = SenderType::staticMetaObject.indexOfSignal(signal+1); \
95     } \
96     if (methodIdx < 0) { \
97         int code = ((int)(*method) - '0'); \
98         Q_ASSERT(code == QSLOT_CODE || code == QSIGNAL_CODE); \
99         if (code == QSLOT_CODE) \
100             methodIdx = ReceiverType::staticMetaObject.indexOfSlot(method+1); \
101         else \
102             methodIdx = ReceiverType::staticMetaObject.indexOfSignal(method+1); \
103     } \
104     Q_ASSERT(signalIdx != -1 && methodIdx != -1); \
105     QMetaObject::connect(sender, signalIdx, receiver, methodIdx, Qt::DirectConnection); \
106 }
107
108 /*!
109     Disconnect \a Signal of \a Sender from \a Method of \a Receiver.  \a Signal must be
110     of type \a SenderType and \a Receiver of type \a ReceiverType.
111
112     Unlike QObject::disconnect(), this method caches the lookup of the signal and method
113     indexes.  It also does not require lazy QMetaObjects to be built so should be
114     preferred in all QML code that might interact with QML built objects.
115
116     \code
117         QQuickTextControl *control;
118         QQuickTextEdit *textEdit;
119         qmlobject_disconnect(control, QQuickTextControl, SIGNAL(updateRequest(QRectF)),
120                              textEdit, QQuickTextEdit, SLOT(updateDocument()));
121     \endcode
122 */
123 #define qmlobject_disconnect(Sender, SenderType, Signal, Receiver, ReceiverType, Method) \
124 { \
125     SenderType *sender = (Sender); \
126     ReceiverType *receiver = (Receiver); \
127     const char *signal = (Signal); \
128     const char *method = (Method); \
129     static int signalIdx = -1; \
130     static int methodIdx = -1; \
131     if (signalIdx < 0) { \
132         Q_ASSERT(((int)(*signal) - '0') == QSIGNAL_CODE); \
133         signalIdx = SenderType::staticMetaObject.indexOfSignal(signal+1); \
134     } \
135     if (methodIdx < 0) { \
136         int code = ((int)(*method) - '0'); \
137         Q_ASSERT(code == QSLOT_CODE || code == QSIGNAL_CODE); \
138         if (code == QSLOT_CODE) \
139             methodIdx = ReceiverType::staticMetaObject.indexOfSlot(method+1); \
140         else \
141             methodIdx = ReceiverType::staticMetaObject.indexOfSignal(method+1); \
142     } \
143     Q_ASSERT(signalIdx != -1 && methodIdx != -1); \
144     QMetaObject::disconnect(sender, signalIdx, receiver, methodIdx); \
145 }
146
147 /*!
148     This method is identical to qobject_cast<T>() except that it does not require lazy
149     QMetaObjects to be built, so should be preferred in all QML code that might interact
150     with QML built objects.
151
152     \code
153         QObject *object;
154         if (QQuickTextEdit *textEdit = qmlobject_cast<QQuickTextEdit *>(object)) {
155             // ...Do something...
156         }
157     \endcode
158 */
159 template<class T>
160 T qmlobject_cast(QObject *object)
161 {
162     if (object && QQmlMetaObject::canConvert(object, &reinterpret_cast<T>(object)->staticMetaObject))
163         return static_cast<T>(object);
164     else
165         return 0;
166 }
167
168 #define IS_SIGNAL_CONNECTED(Sender, SenderType, Name, Arguments) \
169 do { \
170     QObject *sender = (Sender); \
171     void (SenderType::*signal)Arguments = &SenderType::Name; \
172     static QMetaMethod method = QMetaMethod::fromSignal(signal); \
173     static int signalIdx = QMetaObjectPrivate::signalIndex(method); \
174     return QObjectPrivate::get(sender)->isSignalConnected(signalIdx); \
175 } while (0)
176
177 struct QQmlGraphics_DerivedObject : public QObject
178 {
179     void setParent_noEvent(QObject *parent) {
180         bool sce = d_ptr->sendChildEvents;
181         d_ptr->sendChildEvents = false;
182         setParent(parent);
183         d_ptr->sendChildEvents = sce;
184     }
185 };
186
187 /*!
188     Returns true if the case of \a fileName is equivalent to the file case of 
189     \a fileName on disk, and false otherwise.
190
191     This is used to ensure that the behavior of QML on a case-insensitive file 
192     system is the same as on a case-sensitive file system.  This function 
193     performs a "best effort" attempt to determine the real case of the file. 
194     It may have false positives (say the case is correct when it isn't), but it
195     should never have a false negative (say the case is incorrect when it is 
196     correct).
197 */
198 bool QQml_isFileCaseCorrect(const QString &fileName);
199
200 /*!
201     Makes the \a object a child of \a parent.  Note that when using this method,
202     neither \a parent nor the object's previous parent (if it had one) will
203     receive ChildRemoved or ChildAdded events.
204 */
205 inline void QQml_setParent_noEvent(QObject *object, QObject *parent)
206 {
207     static_cast<QQmlGraphics_DerivedObject *>(object)->setParent_noEvent(parent);
208 }
209
210
211 class QQmlValueType;
212 class QV8Engine;
213 class Q_QML_PRIVATE_EXPORT QQmlValueTypeProvider
214 {
215 public:
216     QQmlValueTypeProvider();
217
218     QQmlValueType *createValueType(int);
219
220     bool initValueType(int, void *, size_t);
221     bool destroyValueType(int, void *, size_t);
222     bool copyValueType(int, const void *, void *, size_t);
223
224     QVariant createValueType(int, int, const void *[]);
225     bool createValueFromString(int, const QString &, void *, size_t);
226     bool createStringFromValue(int, const void *, QString *);
227
228     QVariant createVariantFromString(const QString &);
229     QVariant createVariantFromString(int, const QString &, bool *);
230     QVariant createVariantFromJsObject(int, QQmlV8Handle, QV8Engine *, bool*);
231
232     bool equalValueType(int, const void *, const void *, size_t);
233     bool storeValueType(int, const void *, void *, size_t);
234     bool readValueType(int, const void *, size_t, int, void *);
235     bool writeValueType(int, const void *, void *, size_t);
236
237 private:
238     virtual bool create(int, QQmlValueType *&);
239
240     virtual bool init(int, void *, size_t);
241     virtual bool destroy(int, void *, size_t);
242     virtual bool copy(int, const void *, void *, size_t);
243
244     virtual bool create(int, int, const void *[], QVariant *);
245     virtual bool createFromString(int, const QString &, void *, size_t);
246     virtual bool createStringFrom(int, const void *, QString *);
247
248     virtual bool variantFromString(const QString &, QVariant *);
249     virtual bool variantFromString(int, const QString &, QVariant *);
250     virtual bool variantFromJsObject(int, QQmlV8Handle, QV8Engine *, QVariant *);
251
252     virtual bool equal(int, const void *, const void *, size_t);
253     virtual bool store(int, const void *, void *, size_t);
254     virtual bool read(int, const void *, size_t, int, void *);
255     virtual bool write(int, const void *, void *, size_t);
256
257     friend Q_QML_PRIVATE_EXPORT void QQml_addValueTypeProvider(QQmlValueTypeProvider *);
258
259     QQmlValueTypeProvider *next;
260 };
261
262 Q_QML_PRIVATE_EXPORT void QQml_addValueTypeProvider(QQmlValueTypeProvider *);
263 Q_AUTOTEST_EXPORT QQmlValueTypeProvider *QQml_valueTypeProvider();
264
265
266 class Q_QML_PRIVATE_EXPORT QQmlColorProvider
267 {
268 public:
269     virtual QVariant colorFromString(const QString &, bool *);
270     virtual unsigned rgbaFromString(const QString &, bool *);
271
272     virtual QVariant fromRgbF(double, double, double, double);
273     virtual QVariant fromHslF(double, double, double, double);
274     virtual QVariant lighter(const QVariant &, qreal);
275     virtual QVariant darker(const QVariant &, qreal);
276     virtual QVariant tint(const QVariant &, const QVariant &);
277 };
278
279 Q_QML_PRIVATE_EXPORT QQmlColorProvider *QQml_setColorProvider(QQmlColorProvider *);
280 Q_QML_PRIVATE_EXPORT QQmlColorProvider *QQml_colorProvider();
281
282
283 class Q_QML_PRIVATE_EXPORT QQmlGuiProvider
284 {
285 public:
286     virtual QObject *application(QObject *parent);
287     virtual QObject *inputMethod();
288     virtual QStringList fontFamilies();
289     virtual bool openUrlExternally(QUrl &);
290 };
291
292 Q_QML_PRIVATE_EXPORT QQmlGuiProvider *QQml_setGuiProvider(QQmlGuiProvider *);
293 Q_AUTOTEST_EXPORT QQmlGuiProvider *QQml_guiProvider();
294
295 QT_END_NAMESPACE
296
297 QT_END_HEADER
298
299 #endif // QQMLGLOBAL_H