Docs - add missing images and code, clean up sections
[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 inline quint16 qmlSourceCoordinate(int n)
169 {
170     return (n > 0 && n <= static_cast<int>(USHRT_MAX)) ? static_cast<quint16>(n) : 0;
171 }
172
173 inline int qmlSourceCoordinate(quint16 n)
174 {
175     return (n == 0) ? -1 : static_cast<int>(n);
176 }
177
178 #define IS_SIGNAL_CONNECTED(Sender, SenderType, Name, Arguments) \
179 do { \
180     QObject *sender = (Sender); \
181     void (SenderType::*signal)Arguments = &SenderType::Name; \
182     static QMetaMethod method = QMetaMethod::fromSignal(signal); \
183     static int signalIdx = QMetaObjectPrivate::signalIndex(method); \
184     return QObjectPrivate::get(sender)->isSignalConnected(signalIdx); \
185 } while (0)
186
187 struct QQmlGraphics_DerivedObject : public QObject
188 {
189     void setParent_noEvent(QObject *parent) {
190         bool sce = d_ptr->sendChildEvents;
191         d_ptr->sendChildEvents = false;
192         setParent(parent);
193         d_ptr->sendChildEvents = sce;
194     }
195 };
196
197 /*!
198     Returns true if the case of \a fileName is equivalent to the file case of 
199     \a fileName on disk, and false otherwise.
200
201     This is used to ensure that the behavior of QML on a case-insensitive file 
202     system is the same as on a case-sensitive file system.  This function 
203     performs a "best effort" attempt to determine the real case of the file. 
204     It may have false positives (say the case is correct when it isn't), but it
205     should never have a false negative (say the case is incorrect when it is 
206     correct).
207 */
208 bool QQml_isFileCaseCorrect(const QString &fileName);
209
210 /*!
211     Makes the \a object a child of \a parent.  Note that when using this method,
212     neither \a parent nor the object's previous parent (if it had one) will
213     receive ChildRemoved or ChildAdded events.
214 */
215 inline void QQml_setParent_noEvent(QObject *object, QObject *parent)
216 {
217     static_cast<QQmlGraphics_DerivedObject *>(object)->setParent_noEvent(parent);
218 }
219
220
221 class QQmlValueType;
222 class QV8Engine;
223 class Q_QML_PRIVATE_EXPORT QQmlValueTypeProvider
224 {
225 public:
226     QQmlValueTypeProvider();
227
228     QQmlValueType *createValueType(int);
229
230     bool initValueType(int, void *, size_t);
231     bool destroyValueType(int, void *, size_t);
232     bool copyValueType(int, const void *, void *, size_t);
233
234     QVariant createValueType(int, int, const void *[]);
235     bool createValueFromString(int, const QString &, void *, size_t);
236     bool createStringFromValue(int, const void *, QString *);
237
238     QVariant createVariantFromString(const QString &);
239     QVariant createVariantFromString(int, const QString &, bool *);
240     QVariant createVariantFromJsObject(int, QQmlV8Handle, QV8Engine *, bool*);
241
242     bool equalValueType(int, const void *, const void *, size_t);
243     bool storeValueType(int, const void *, void *, size_t);
244     bool readValueType(int, const void *, size_t, int, void *);
245     bool writeValueType(int, const void *, void *, size_t);
246
247 private:
248     virtual bool create(int, QQmlValueType *&);
249
250     virtual bool init(int, void *, size_t);
251     virtual bool destroy(int, void *, size_t);
252     virtual bool copy(int, const void *, void *, size_t);
253
254     virtual bool create(int, int, const void *[], QVariant *);
255     virtual bool createFromString(int, const QString &, void *, size_t);
256     virtual bool createStringFrom(int, const void *, QString *);
257
258     virtual bool variantFromString(const QString &, QVariant *);
259     virtual bool variantFromString(int, const QString &, QVariant *);
260     virtual bool variantFromJsObject(int, QQmlV8Handle, QV8Engine *, QVariant *);
261
262     virtual bool equal(int, const void *, const void *, size_t);
263     virtual bool store(int, const void *, void *, size_t);
264     virtual bool read(int, const void *, size_t, int, void *);
265     virtual bool write(int, const void *, void *, size_t);
266
267     friend Q_QML_PRIVATE_EXPORT void QQml_addValueTypeProvider(QQmlValueTypeProvider *);
268
269     QQmlValueTypeProvider *next;
270 };
271
272 Q_QML_PRIVATE_EXPORT void QQml_addValueTypeProvider(QQmlValueTypeProvider *);
273 Q_AUTOTEST_EXPORT QQmlValueTypeProvider *QQml_valueTypeProvider();
274
275
276 class Q_QML_PRIVATE_EXPORT QQmlColorProvider
277 {
278 public:
279     virtual QVariant colorFromString(const QString &, bool *);
280     virtual unsigned rgbaFromString(const QString &, bool *);
281
282     virtual QVariant fromRgbF(double, double, double, double);
283     virtual QVariant fromHslF(double, double, double, double);
284     virtual QVariant lighter(const QVariant &, qreal);
285     virtual QVariant darker(const QVariant &, qreal);
286     virtual QVariant tint(const QVariant &, const QVariant &);
287 };
288
289 Q_QML_PRIVATE_EXPORT QQmlColorProvider *QQml_setColorProvider(QQmlColorProvider *);
290 Q_QML_PRIVATE_EXPORT QQmlColorProvider *QQml_colorProvider();
291
292
293 class Q_QML_PRIVATE_EXPORT QQmlGuiProvider
294 {
295 public:
296     virtual QObject *application(QObject *parent);
297     virtual QObject *inputMethod();
298     virtual QStringList fontFamilies();
299     virtual bool openUrlExternally(QUrl &);
300 };
301
302 Q_QML_PRIVATE_EXPORT QQmlGuiProvider *QQml_setGuiProvider(QQmlGuiProvider *);
303 Q_AUTOTEST_EXPORT QQmlGuiProvider *QQml_guiProvider();
304
305 QT_END_NAMESPACE
306
307 QT_END_HEADER
308
309 #endif // QQMLGLOBAL_H