1 /****************************************************************************
3 ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/
6 ** This file is part of the QtQml module of the Qt Toolkit.
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.
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.
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.
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.
40 ****************************************************************************/
42 #include "qqmlstringconverters_p.h"
44 #include <QtGui/qcolor.h>
45 #include <QtGui/qvector3d.h>
46 #include <QtGui/qvector4d.h>
47 #include <QtCore/qpoint.h>
48 #include <QtCore/qrect.h>
49 #include <QtCore/qsize.h>
50 #include <QtCore/qvariant.h>
51 #include <QtCore/qdatetime.h>
55 static uchar fromHex(const uchar c, const uchar c2)
58 if (c >= '0' && c <= '9')
60 else if (c >= 'A' && c <= 'F')
61 rv += (c - 'A' + 10) * 16;
62 else if (c >= 'a' && c <= 'f')
63 rv += (c - 'a' + 10) * 16;
65 if (c2 >= '0' && c2 <= '9')
67 else if (c2 >= 'A' && c2 <= 'F')
68 rv += (c2 - 'A' + 10);
69 else if (c2 >= 'a' && c2 <= 'f')
70 rv += (c2 - 'a' + 10);
75 static uchar fromHex(const QString &s, int idx)
77 uchar c = s.at(idx).toAscii();
78 uchar c2 = s.at(idx + 1).toAscii();
79 return fromHex(c, c2);
82 QVariant QQmlStringConverters::variantFromString(const QString &s)
87 QRectF r = rectFFromString(s, &ok);
88 if (ok) return QVariant(r);
89 QColor c = colorFromString(s, &ok);
90 if (ok) return QVariant(c);
91 QPointF p = pointFFromString(s, &ok);
92 if (ok) return QVariant(p);
93 QSizeF sz = sizeFFromString(s, &ok);
94 if (ok) return QVariant(sz);
95 QVector3D v = vector3DFromString(s, &ok);
96 if (ok) return QVariant::fromValue(v);
97 QVector4D v4 = vector4DFromString(s, &ok);
98 if (ok) return QVariant::fromValue(v4);
103 QVariant QQmlStringConverters::variantFromString(const QString &s, int preferredType, bool *ok)
105 switch (preferredType) {
107 return QVariant(int(qRound(s.toDouble(ok))));
108 case QMetaType::UInt:
109 return QVariant(uint(qRound(s.toDouble(ok))));
110 case QMetaType::QColor:
111 return QVariant::fromValue(colorFromString(s, ok));
112 #ifndef QT_NO_DATESTRING
113 case QMetaType::QDate:
114 return QVariant::fromValue(dateFromString(s, ok));
115 case QMetaType::QTime:
116 return QVariant::fromValue(timeFromString(s, ok));
117 case QMetaType::QDateTime:
118 return QVariant::fromValue(dateTimeFromString(s, ok));
119 #endif // QT_NO_DATESTRING
120 case QMetaType::QPointF:
121 return QVariant::fromValue(pointFFromString(s, ok));
122 case QMetaType::QPoint:
123 return QVariant::fromValue(pointFFromString(s, ok).toPoint());
124 case QMetaType::QSizeF:
125 return QVariant::fromValue(sizeFFromString(s, ok));
126 case QMetaType::QSize:
127 return QVariant::fromValue(sizeFFromString(s, ok).toSize());
128 case QMetaType::QRectF:
129 return QVariant::fromValue(rectFFromString(s, ok));
130 case QMetaType::QRect:
131 return QVariant::fromValue(rectFFromString(s, ok).toRect());
132 case QMetaType::QVector3D:
133 return QVariant::fromValue(vector3DFromString(s, ok));
134 case QMetaType::QVector4D:
135 return QVariant::fromValue(vector4DFromString(s, ok));
142 QColor QQmlStringConverters::colorFromString(const QString &s, bool *ok)
144 if (s.length() == 9 && s.startsWith(QLatin1Char('#'))) {
145 uchar a = fromHex(s, 1);
146 uchar r = fromHex(s, 3);
147 uchar g = fromHex(s, 5);
148 uchar b = fromHex(s, 7);
150 return QColor(r, g, b, a);
153 if (ok) *ok = rv.isValid();
158 #ifndef QT_NO_DATESTRING
159 QDate QQmlStringConverters::dateFromString(const QString &s, bool *ok)
161 QDate d = QDate::fromString(s, Qt::ISODate);
162 if (ok) *ok = d.isValid();
166 QTime QQmlStringConverters::timeFromString(const QString &s, bool *ok)
168 QTime t = QTime::fromString(s, Qt::ISODate);
169 if (ok) *ok = t.isValid();
173 QDateTime QQmlStringConverters::dateTimeFromString(const QString &s, bool *ok)
175 QDateTime d = QDateTime::fromString(s, Qt::ISODate);
176 if (ok) *ok = d.isValid();
179 #endif // QT_NO_DATESTRING
181 //expects input of "x,y"
182 QPointF QQmlStringConverters::pointFFromString(const QString &s, bool *ok)
184 if (s.count(QLatin1Char(',')) != 1) {
191 int index = s.indexOf(QLatin1Char(','));
192 qreal xCoord = s.left(index).toDouble(&xGood);
193 qreal yCoord = s.mid(index+1).toDouble(&yGood);
194 if (!xGood || !yGood) {
202 return QPointF(xCoord, yCoord);
205 //expects input of "widthxheight"
206 QSizeF QQmlStringConverters::sizeFFromString(const QString &s, bool *ok)
208 if (s.count(QLatin1Char('x')) != 1) {
215 int index = s.indexOf(QLatin1Char('x'));
216 qreal width = s.left(index).toDouble(&wGood);
217 qreal height = s.mid(index+1).toDouble(&hGood);
218 if (!wGood || !hGood) {
226 return QSizeF(width, height);
229 //expects input of "x,y,widthxheight" //### use space instead of second comma?
230 QRectF QQmlStringConverters::rectFFromString(const QString &s, bool *ok)
232 if (s.count(QLatin1Char(',')) != 2 || s.count(QLatin1Char('x')) != 1) {
238 bool xGood, yGood, wGood, hGood;
239 int index = s.indexOf(QLatin1Char(','));
240 qreal x = s.left(index).toDouble(&xGood);
241 int index2 = s.indexOf(QLatin1Char(','), index+1);
242 qreal y = s.mid(index+1, index2-index-1).toDouble(&yGood);
243 index = s.indexOf(QLatin1Char('x'), index2+1);
244 qreal width = s.mid(index2+1, index-index2-1).toDouble(&wGood);
245 qreal height = s.mid(index+1).toDouble(&hGood);
246 if (!xGood || !yGood || !wGood || !hGood) {
254 return QRectF(x, y, width, height);
257 //expects input of "x,y,z"
258 QVector3D QQmlStringConverters::vector3DFromString(const QString &s, bool *ok)
260 if (s.count(QLatin1Char(',')) != 2) {
266 bool xGood, yGood, zGood;
267 int index = s.indexOf(QLatin1Char(','));
268 int index2 = s.indexOf(QLatin1Char(','), index+1);
269 qreal xCoord = s.left(index).toDouble(&xGood);
270 qreal yCoord = s.mid(index+1, index2-index-1).toDouble(&yGood);
271 qreal zCoord = s.mid(index2+1).toDouble(&zGood);
272 if (!xGood || !yGood || !zGood) {
280 return QVector3D(xCoord, yCoord, zCoord);
283 //expects input of "x,y,z,w"
284 QVector4D QQmlStringConverters::vector4DFromString(const QString &s, bool *ok)
286 if (s.count(QLatin1Char(',')) != 3) {
292 bool xGood, yGood, zGood, wGood;
293 int index = s.indexOf(QLatin1Char(','));
294 int index2 = s.indexOf(QLatin1Char(','), index+1);
295 int index3 = s.indexOf(QLatin1Char(','), index2+1);
296 qreal xCoord = s.left(index).toDouble(&xGood);
297 qreal yCoord = s.mid(index+1, index2-index-1).toDouble(&yGood);
298 qreal zCoord = s.mid(index2+1, index3-index2-1).toDouble(&zGood);
299 qreal wCoord = s.mid(index3+1).toDouble(&wGood);
300 if (!xGood || !yGood || !zGood || !wGood) {
308 return QVector4D(xCoord, yCoord, zCoord, wCoord);