1 /****************************************************************************
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
40 ****************************************************************************/
42 #include "private/qdeclarativestringconverters_p.h"
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>
54 static uchar fromHex(const uchar c, const uchar c2)
57 if (c >= '0' && c <= '9')
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;
64 if (c2 >= '0' && c2 <= '9')
66 else if (c2 >= 'A' && c2 <= 'F')
67 rv += (c2 - 'A' + 10);
68 else if (c2 >= 'a' && c2 <= 'f')
69 rv += (c2 - 'a' + 10);
74 static uchar fromHex(const QString &s, int idx)
76 uchar c = s.at(idx).toAscii();
77 uchar c2 = s.at(idx + 1).toAscii();
78 return fromHex(c, c2);
81 QVariant QDeclarativeStringConverters::variantFromString(const QString &s)
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);
100 QVariant QDeclarativeStringConverters::variantFromString(const QString &s, int preferredType, bool *ok)
102 switch (preferredType) {
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));
137 QColor QDeclarativeStringConverters::colorFromString(const QString &s, bool *ok)
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);
145 return QColor(r, g, b, a);
148 if (ok) *ok = rv.isValid();
153 #ifndef QT_NO_DATESTRING
154 QDate QDeclarativeStringConverters::dateFromString(const QString &s, bool *ok)
156 QDate d = QDate::fromString(s, Qt::ISODate);
157 if (ok) *ok = d.isValid();
161 QTime QDeclarativeStringConverters::timeFromString(const QString &s, bool *ok)
163 QTime t = QTime::fromString(s, Qt::ISODate);
164 if (ok) *ok = t.isValid();
168 QDateTime QDeclarativeStringConverters::dateTimeFromString(const QString &s, bool *ok)
170 QDateTime d = QDateTime::fromString(s, Qt::ISODate);
171 if (ok) *ok = d.isValid();
174 #endif // QT_NO_DATESTRING
176 //expects input of "x,y"
177 QPointF QDeclarativeStringConverters::pointFFromString(const QString &s, bool *ok)
179 if (s.count(QLatin1Char(',')) != 1) {
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) {
197 return QPointF(xCoord, yCoord);
200 //expects input of "widthxheight"
201 QSizeF QDeclarativeStringConverters::sizeFFromString(const QString &s, bool *ok)
203 if (s.count(QLatin1Char('x')) != 1) {
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) {
221 return QSizeF(width, height);
224 //expects input of "x,y,widthxheight" //### use space instead of second comma?
225 QRectF QDeclarativeStringConverters::rectFFromString(const QString &s, bool *ok)
227 if (s.count(QLatin1Char(',')) != 2 || s.count(QLatin1Char('x')) != 1) {
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) {
249 return QRectF(x, y, width, height);
252 //expects input of "x,y,z"
253 QVector3D QDeclarativeStringConverters::vector3DFromString(const QString &s, bool *ok)
255 if (s.count(QLatin1Char(',')) != 2) {
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) {
275 return QVector3D(xCoord, yCoord, zCoord);