Docs - add missing images and code, clean up sections
[profile/ivi/qtdeclarative.git] / src / qml / qml / qqmlstringconverters.cpp
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 #include "qqmlstringconverters_p.h"
43 #include <private/qqmlglobal_p.h>
44 #include <private/qqmlinstruction_p.h>
45
46 #include <QtCore/qpoint.h>
47 #include <QtCore/qrect.h>
48 #include <QtCore/qsize.h>
49 #include <QtCore/qvariant.h>
50 #include <QtCore/qdatetime.h>
51
52 QT_BEGIN_NAMESPACE
53
54 QVariant QQmlStringConverters::variantFromString(const QString &s)
55 {
56     if (s.isEmpty())
57         return QVariant(s);
58
59     bool ok = false;
60     QRectF r = rectFFromString(s, &ok);
61     if (ok) return QVariant(r);
62     QPointF p = pointFFromString(s, &ok);
63     if (ok) return QVariant(p);
64     QSizeF sz = sizeFFromString(s, &ok);
65     if (ok) return QVariant(sz);
66
67     return QQml_valueTypeProvider()->createVariantFromString(s);
68 }
69
70 QVariant QQmlStringConverters::variantFromString(const QString &s, int preferredType, bool *ok)
71 {
72     switch (preferredType) {
73     case QMetaType::Int:
74         return QVariant(int(qRound(s.toDouble(ok))));
75     case QMetaType::UInt:
76         return QVariant(uint(qRound(s.toDouble(ok))));
77 #ifndef QT_NO_DATESTRING
78     case QMetaType::QDate:
79         return QVariant::fromValue(dateFromString(s, ok));
80     case QMetaType::QTime:
81         return QVariant::fromValue(timeFromString(s, ok));
82     case QMetaType::QDateTime:
83         return QVariant::fromValue(dateTimeFromString(s, ok));
84 #endif // QT_NO_DATESTRING
85     case QMetaType::QPointF:
86         return QVariant::fromValue(pointFFromString(s, ok));
87     case QMetaType::QPoint:
88         return QVariant::fromValue(pointFFromString(s, ok).toPoint());
89     case QMetaType::QSizeF:
90         return QVariant::fromValue(sizeFFromString(s, ok));
91     case QMetaType::QSize:
92         return QVariant::fromValue(sizeFFromString(s, ok).toSize());
93     case QMetaType::QRectF:
94         return QVariant::fromValue(rectFFromString(s, ok));
95     case QMetaType::QRect:
96         return QVariant::fromValue(rectFFromString(s, ok).toRect());
97     default:
98         return QQml_valueTypeProvider()->createVariantFromString(preferredType, s, ok);
99     }
100 }
101
102 QVariant QQmlStringConverters::colorFromString(const QString &s, bool *ok)
103 {
104     return QQml_colorProvider()->colorFromString(s, ok);
105 }
106
107 unsigned QQmlStringConverters::rgbaFromString(const QString &s, bool *ok)
108 {
109     return QQml_colorProvider()->rgbaFromString(s, ok);
110 }
111
112 #ifndef QT_NO_DATESTRING
113 QDate QQmlStringConverters::dateFromString(const QString &s, bool *ok)
114 {
115     QDate d = QDate::fromString(s, Qt::ISODate);
116     if (ok) *ok =  d.isValid();
117     return d;
118 }
119
120 QTime QQmlStringConverters::timeFromString(const QString &s, bool *ok)
121 {
122     QTime t = QTime::fromString(s, Qt::ISODate);
123     if (ok) *ok = t.isValid();
124     return t;
125 }
126
127 QDateTime QQmlStringConverters::dateTimeFromString(const QString &s, bool *ok)
128 {
129     QDateTime d = QDateTime::fromString(s, Qt::ISODate);
130     if (ok) *ok =  d.isValid();
131     // V8 never parses a date string as local time.  For consistency do the same here.
132     if (d.timeSpec() == Qt::LocalTime)
133         d.setTimeSpec(Qt::UTC);
134     return d;
135 }
136 #endif // QT_NO_DATESTRING
137
138 //expects input of "x,y"
139 QPointF QQmlStringConverters::pointFFromString(const QString &s, bool *ok)
140 {
141     if (s.count(QLatin1Char(',')) != 1) {
142         if (ok)
143             *ok = false;
144         return QPointF();
145     }
146
147     bool xGood, yGood;
148     int index = s.indexOf(QLatin1Char(','));
149     qreal xCoord = s.left(index).toDouble(&xGood);
150     qreal yCoord = s.mid(index+1).toDouble(&yGood);
151     if (!xGood || !yGood) {
152         if (ok)
153             *ok = false;
154         return QPointF();
155     }
156
157     if (ok)
158         *ok = true;
159     return QPointF(xCoord, yCoord);
160 }
161
162 //expects input of "widthxheight"
163 QSizeF QQmlStringConverters::sizeFFromString(const QString &s, bool *ok)
164 {
165     if (s.count(QLatin1Char('x')) != 1) {
166         if (ok)
167             *ok = false;
168         return QSizeF();
169     }
170
171     bool wGood, hGood;
172     int index = s.indexOf(QLatin1Char('x'));
173     qreal width = s.left(index).toDouble(&wGood);
174     qreal height = s.mid(index+1).toDouble(&hGood);
175     if (!wGood || !hGood) {
176         if (ok)
177             *ok = false;
178         return QSizeF();
179     }
180
181     if (ok)
182         *ok = true;
183     return QSizeF(width, height);
184 }
185
186 //expects input of "x,y,widthxheight" //### use space instead of second comma?
187 QRectF QQmlStringConverters::rectFFromString(const QString &s, bool *ok)
188 {
189     if (s.count(QLatin1Char(',')) != 2 || s.count(QLatin1Char('x')) != 1) {
190         if (ok)
191             *ok = false;
192         return QRectF();
193     }
194
195     bool xGood, yGood, wGood, hGood;
196     int index = s.indexOf(QLatin1Char(','));
197     qreal x = s.left(index).toDouble(&xGood);
198     int index2 = s.indexOf(QLatin1Char(','), index+1);
199     qreal y = s.mid(index+1, index2-index-1).toDouble(&yGood);
200     index = s.indexOf(QLatin1Char('x'), index2+1);
201     qreal width = s.mid(index2+1, index-index2-1).toDouble(&wGood);
202     qreal height = s.mid(index+1).toDouble(&hGood);
203     if (!xGood || !yGood || !wGood || !hGood) {
204         if (ok)
205             *ok = false;
206         return QRectF();
207     }
208
209     if (ok)
210         *ok = true;
211     return QRectF(x, y, width, height);
212 }
213
214 bool QQmlStringConverters::createFromString(int type, const QString &s, void *data, size_t n)
215 {
216     Q_ASSERT(data);
217
218     bool ok = false;
219
220     switch (type) {
221     case QMetaType::Int:
222         {
223         Q_ASSERT(n >= sizeof(int));
224         int *p = reinterpret_cast<int *>(data);
225         *p = int(qRound(s.toDouble(&ok)));
226         return ok;
227         }
228     case QMetaType::UInt:
229         {
230         Q_ASSERT(n >= sizeof(uint));
231         uint *p = reinterpret_cast<uint *>(data);
232         *p = uint(qRound(s.toDouble(&ok)));
233         return ok;
234         }
235 #ifndef QT_NO_DATESTRING
236     case QMetaType::QDate:
237         {
238         Q_ASSERT(n >= sizeof(QDate));
239         QDate *p = reinterpret_cast<QDate *>(data);
240         *p = dateFromString(s, &ok);
241         return ok;
242         }
243     case QMetaType::QTime:
244         {
245         Q_ASSERT(n >= sizeof(QTime));
246         QTime *p = reinterpret_cast<QTime *>(data);
247         *p = timeFromString(s, &ok);
248         return ok;
249         }
250     case QMetaType::QDateTime:
251         {
252         Q_ASSERT(n >= sizeof(QDateTime));
253         QDateTime *p = reinterpret_cast<QDateTime *>(data);
254         *p = dateTimeFromString(s, &ok);
255         return ok;
256         }
257 #endif // QT_NO_DATESTRING
258     case QMetaType::QPointF:
259         {
260         Q_ASSERT(n >= sizeof(QPointF));
261         QPointF *p = reinterpret_cast<QPointF *>(data);
262         *p = pointFFromString(s, &ok);
263         return ok;
264         }
265     case QMetaType::QPoint:
266         {
267         Q_ASSERT(n >= sizeof(QPoint));
268         QPoint *p = reinterpret_cast<QPoint *>(data);
269         *p = pointFFromString(s, &ok).toPoint();
270         return ok;
271         }
272     case QMetaType::QSizeF:
273         {
274         Q_ASSERT(n >= sizeof(QSizeF));
275         QSizeF *p = reinterpret_cast<QSizeF *>(data);
276         *p = sizeFFromString(s, &ok);
277         return ok;
278         }
279     case QMetaType::QSize:
280         {
281         Q_ASSERT(n >= sizeof(QSize));
282         QSize *p = reinterpret_cast<QSize *>(data);
283         *p = sizeFFromString(s, &ok).toSize();
284         return ok;
285         }
286     case QMetaType::QRectF:
287         {
288         Q_ASSERT(n >= sizeof(QRectF));
289         QRectF *p = reinterpret_cast<QRectF *>(data);
290         *p = rectFFromString(s, &ok);
291         return ok;
292         }
293     case QMetaType::QRect:
294         {
295         Q_ASSERT(n >= sizeof(QRect));
296         QRect *p = reinterpret_cast<QRect *>(data);
297         *p = rectFFromString(s, &ok).toRect();
298         return ok;
299         }
300     default:
301         return QQml_valueTypeProvider()->createValueFromString(type, s, data, n);
302     }
303 }
304
305 QT_END_NAMESPACE