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"
43 #include <private/qqmlglobal_p.h>
44 #include <private/qqmlinstruction_p.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 QVariant QQmlStringConverters::variantFromString(const QString &s)
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);
67 return QQml_valueTypeProvider()->createVariantFromString(s);
70 QVariant QQmlStringConverters::variantFromString(const QString &s, int preferredType, bool *ok)
72 switch (preferredType) {
74 return QVariant(int(qRound(s.toDouble(ok))));
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());
98 return QQml_valueTypeProvider()->createVariantFromString(preferredType, s, ok);
102 QVariant QQmlStringConverters::colorFromString(const QString &s, bool *ok)
104 return QQml_colorProvider()->colorFromString(s, ok);
107 unsigned QQmlStringConverters::rgbaFromString(const QString &s, bool *ok)
109 return QQml_colorProvider()->rgbaFromString(s, ok);
112 #ifndef QT_NO_DATESTRING
113 QDate QQmlStringConverters::dateFromString(const QString &s, bool *ok)
115 QDate d = QDate::fromString(s, Qt::ISODate);
116 if (ok) *ok = d.isValid();
120 QTime QQmlStringConverters::timeFromString(const QString &s, bool *ok)
122 QTime t = QTime::fromString(s, Qt::ISODate);
123 if (ok) *ok = t.isValid();
127 QDateTime QQmlStringConverters::dateTimeFromString(const QString &s, bool *ok)
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);
136 #endif // QT_NO_DATESTRING
138 //expects input of "x,y"
139 QPointF QQmlStringConverters::pointFFromString(const QString &s, bool *ok)
141 if (s.count(QLatin1Char(',')) != 1) {
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) {
159 return QPointF(xCoord, yCoord);
162 //expects input of "widthxheight"
163 QSizeF QQmlStringConverters::sizeFFromString(const QString &s, bool *ok)
165 if (s.count(QLatin1Char('x')) != 1) {
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) {
183 return QSizeF(width, height);
186 //expects input of "x,y,widthxheight" //### use space instead of second comma?
187 QRectF QQmlStringConverters::rectFFromString(const QString &s, bool *ok)
189 if (s.count(QLatin1Char(',')) != 2 || s.count(QLatin1Char('x')) != 1) {
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) {
211 return QRectF(x, y, width, height);
214 bool QQmlStringConverters::createFromString(int type, const QString &s, void *data, size_t n)
223 Q_ASSERT(n >= sizeof(int));
224 int *p = reinterpret_cast<int *>(data);
225 *p = int(qRound(s.toDouble(&ok)));
228 case QMetaType::UInt:
230 Q_ASSERT(n >= sizeof(uint));
231 uint *p = reinterpret_cast<uint *>(data);
232 *p = uint(qRound(s.toDouble(&ok)));
235 #ifndef QT_NO_DATESTRING
236 case QMetaType::QDate:
238 Q_ASSERT(n >= sizeof(QDate));
239 QDate *p = reinterpret_cast<QDate *>(data);
240 *p = dateFromString(s, &ok);
243 case QMetaType::QTime:
245 Q_ASSERT(n >= sizeof(QTime));
246 QTime *p = reinterpret_cast<QTime *>(data);
247 *p = timeFromString(s, &ok);
250 case QMetaType::QDateTime:
252 Q_ASSERT(n >= sizeof(QDateTime));
253 QDateTime *p = reinterpret_cast<QDateTime *>(data);
254 *p = dateTimeFromString(s, &ok);
257 #endif // QT_NO_DATESTRING
258 case QMetaType::QPointF:
260 Q_ASSERT(n >= sizeof(QPointF));
261 QPointF *p = reinterpret_cast<QPointF *>(data);
262 *p = pointFFromString(s, &ok);
265 case QMetaType::QPoint:
267 Q_ASSERT(n >= sizeof(QPoint));
268 QPoint *p = reinterpret_cast<QPoint *>(data);
269 *p = pointFFromString(s, &ok).toPoint();
272 case QMetaType::QSizeF:
274 Q_ASSERT(n >= sizeof(QSizeF));
275 QSizeF *p = reinterpret_cast<QSizeF *>(data);
276 *p = sizeFFromString(s, &ok);
279 case QMetaType::QSize:
281 Q_ASSERT(n >= sizeof(QSize));
282 QSize *p = reinterpret_cast<QSize *>(data);
283 *p = sizeFFromString(s, &ok).toSize();
286 case QMetaType::QRectF:
288 Q_ASSERT(n >= sizeof(QRectF));
289 QRectF *p = reinterpret_cast<QRectF *>(data);
290 *p = rectFFromString(s, &ok);
293 case QMetaType::QRect:
295 Q_ASSERT(n >= sizeof(QRect));
296 QRect *p = reinterpret_cast<QRect *>(data);
297 *p = rectFFromString(s, &ok).toRect();
301 return QQml_valueTypeProvider()->createValueFromString(type, s, data, n);