Initial import from the monolithic Qt.
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativestringconverters.cpp
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 #include "private/qdeclarativestringconverters_p.h"
43
44 #include <QtGui/qcolor.h>
45 #include <QtGui/qvector3d.h>
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 static uchar fromHex(const uchar c, const uchar c2)
55 {
56     uchar rv = 0;
57     if (c >= '0' && c <= '9')
58         rv += (c - '0') * 16;
59     else if (c >= 'A' && c <= 'F')
60         rv += (c - 'A' + 10) * 16;
61     else if (c >= 'a' && c <= 'f')
62         rv += (c - 'a' + 10) * 16;
63
64     if (c2 >= '0' && c2 <= '9')
65         rv += (c2 - '0');
66     else if (c2 >= 'A' && c2 <= 'F')
67         rv += (c2 - 'A' + 10);
68     else if (c2 >= 'a' && c2 <= 'f')
69         rv += (c2 - 'a' + 10);
70
71     return rv;
72 }
73
74 static uchar fromHex(const QString &s, int idx)
75 {
76     uchar c = s.at(idx).toAscii();
77     uchar c2 = s.at(idx + 1).toAscii();
78     return fromHex(c, c2);
79 }
80
81 QVariant QDeclarativeStringConverters::variantFromString(const QString &s)
82 {
83     if (s.isEmpty())
84         return QVariant(s);
85     bool ok = false;
86     QRectF r = rectFFromString(s, &ok);
87     if (ok) return QVariant(r);
88     QColor c = colorFromString(s, &ok);
89     if (ok) return QVariant(c);
90     QPointF p = pointFFromString(s, &ok);
91     if (ok) return QVariant(p);
92     QSizeF sz = sizeFFromString(s, &ok);
93     if (ok) return QVariant(sz);
94     QVector3D v = vector3DFromString(s, &ok);
95     if (ok) return QVariant::fromValue(v);
96
97     return QVariant(s);
98 }
99
100 QVariant QDeclarativeStringConverters::variantFromString(const QString &s, int preferredType, bool *ok)
101 {
102     switch (preferredType) {
103     case QMetaType::Int:
104         return QVariant(int(qRound(s.toDouble(ok))));
105     case QMetaType::UInt:
106         return QVariant(uint(qRound(s.toDouble(ok))));
107     case QMetaType::QColor:
108         return QVariant::fromValue(colorFromString(s, ok));
109 #ifndef QT_NO_DATESTRING
110     case QMetaType::QDate:
111         return QVariant::fromValue(dateFromString(s, ok));
112     case QMetaType::QTime:
113         return QVariant::fromValue(timeFromString(s, ok));
114     case QMetaType::QDateTime:
115         return QVariant::fromValue(dateTimeFromString(s, ok));
116 #endif // QT_NO_DATESTRING
117     case QMetaType::QPointF:
118         return QVariant::fromValue(pointFFromString(s, ok));
119     case QMetaType::QPoint:
120         return QVariant::fromValue(pointFFromString(s, ok).toPoint());
121     case QMetaType::QSizeF:
122         return QVariant::fromValue(sizeFFromString(s, ok));
123     case QMetaType::QSize:
124         return QVariant::fromValue(sizeFFromString(s, ok).toSize());
125     case QMetaType::QRectF:
126         return QVariant::fromValue(rectFFromString(s, ok));
127     case QMetaType::QRect:
128         return QVariant::fromValue(rectFFromString(s, ok).toRect());
129     case QMetaType::QVector3D:
130         return QVariant::fromValue(vector3DFromString(s, ok));
131     default:
132         if (ok) *ok = false;
133         return QVariant();
134     }
135 }
136
137 QColor QDeclarativeStringConverters::colorFromString(const QString &s, bool *ok)
138 {
139     if (s.length() == 9 && s.startsWith(QLatin1Char('#'))) {
140         uchar a = fromHex(s, 1);
141         uchar r = fromHex(s, 3);
142         uchar g = fromHex(s, 5);
143         uchar b = fromHex(s, 7);
144         if (ok) *ok = true;
145         return QColor(r, g, b, a);
146     } else {
147         QColor rv(s);
148         if (ok) *ok = rv.isValid();
149         return rv;
150     }
151 }
152
153 #ifndef QT_NO_DATESTRING
154 QDate QDeclarativeStringConverters::dateFromString(const QString &s, bool *ok)
155 {
156     QDate d = QDate::fromString(s, Qt::ISODate);
157     if (ok) *ok =  d.isValid();
158     return d;
159 }
160
161 QTime QDeclarativeStringConverters::timeFromString(const QString &s, bool *ok)
162 {
163     QTime t = QTime::fromString(s, Qt::ISODate);
164     if (ok) *ok = t.isValid();
165     return t;
166 }
167
168 QDateTime QDeclarativeStringConverters::dateTimeFromString(const QString &s, bool *ok)
169 {
170     QDateTime d = QDateTime::fromString(s, Qt::ISODate);
171     if (ok) *ok =  d.isValid();
172     return d;
173 }
174 #endif // QT_NO_DATESTRING
175
176 //expects input of "x,y"
177 QPointF QDeclarativeStringConverters::pointFFromString(const QString &s, bool *ok)
178 {
179     if (s.count(QLatin1Char(',')) != 1) {
180         if (ok)
181             *ok = false;
182         return QPointF();
183     }
184
185     bool xGood, yGood;
186     int index = s.indexOf(QLatin1Char(','));
187     qreal xCoord = s.left(index).toDouble(&xGood);
188     qreal yCoord = s.mid(index+1).toDouble(&yGood);
189     if (!xGood || !yGood) {
190         if (ok)
191             *ok = false;
192         return QPointF();
193     }
194
195     if (ok)
196         *ok = true;
197     return QPointF(xCoord, yCoord);
198 }
199
200 //expects input of "widthxheight"
201 QSizeF QDeclarativeStringConverters::sizeFFromString(const QString &s, bool *ok)
202 {
203     if (s.count(QLatin1Char('x')) != 1) {
204         if (ok)
205             *ok = false;
206         return QSizeF();
207     }
208
209     bool wGood, hGood;
210     int index = s.indexOf(QLatin1Char('x'));
211     qreal width = s.left(index).toDouble(&wGood);
212     qreal height = s.mid(index+1).toDouble(&hGood);
213     if (!wGood || !hGood) {
214         if (ok)
215             *ok = false;
216         return QSizeF();
217     }
218
219     if (ok)
220         *ok = true;
221     return QSizeF(width, height);
222 }
223
224 //expects input of "x,y,widthxheight" //### use space instead of second comma?
225 QRectF QDeclarativeStringConverters::rectFFromString(const QString &s, bool *ok)
226 {
227     if (s.count(QLatin1Char(',')) != 2 || s.count(QLatin1Char('x')) != 1) {
228         if (ok)
229             *ok = false;
230         return QRectF();
231     }
232
233     bool xGood, yGood, wGood, hGood;
234     int index = s.indexOf(QLatin1Char(','));
235     qreal x = s.left(index).toDouble(&xGood);
236     int index2 = s.indexOf(QLatin1Char(','), index+1);
237     qreal y = s.mid(index+1, index2-index-1).toDouble(&yGood);
238     index = s.indexOf(QLatin1Char('x'), index2+1);
239     qreal width = s.mid(index2+1, index-index2-1).toDouble(&wGood);
240     qreal height = s.mid(index+1).toDouble(&hGood);
241     if (!xGood || !yGood || !wGood || !hGood) {
242         if (ok)
243             *ok = false;
244         return QRectF();
245     }
246
247     if (ok)
248         *ok = true;
249     return QRectF(x, y, width, height);
250 }
251
252 //expects input of "x,y,z"
253 QVector3D QDeclarativeStringConverters::vector3DFromString(const QString &s, bool *ok)
254 {
255     if (s.count(QLatin1Char(',')) != 2) {
256         if (ok)
257             *ok = false;
258         return QVector3D();
259     }
260
261     bool xGood, yGood, zGood;
262     int index = s.indexOf(QLatin1Char(','));
263     int index2 = s.indexOf(QLatin1Char(','), index+1);
264     qreal xCoord = s.left(index).toDouble(&xGood);
265     qreal yCoord = s.mid(index+1, index2-index-1).toDouble(&yGood);
266     qreal zCoord = s.mid(index2+1).toDouble(&zGood);
267     if (!xGood || !yGood || !zGood) {
268         if (ok)
269             *ok = false;
270         return QVector3D();
271     }
272
273     if (ok)
274         *ok = true;
275     return QVector3D(xCoord, yCoord, zCoord);
276 }
277
278 QT_END_NAMESPACE