|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 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 qVariantFromValue(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 case QMetaType::QDate: |
|
110 return QVariant::fromValue(dateFromString(s, ok)); |
|
111 case QMetaType::QTime: |
|
112 return QVariant::fromValue(timeFromString(s, ok)); |
|
113 case QMetaType::QDateTime: |
|
114 return QVariant::fromValue(dateTimeFromString(s, ok)); |
|
115 case QMetaType::QPointF: |
|
116 return QVariant::fromValue(pointFFromString(s, ok)); |
|
117 case QMetaType::QPoint: |
|
118 return QVariant::fromValue(pointFFromString(s, ok).toPoint()); |
|
119 case QMetaType::QSizeF: |
|
120 return QVariant::fromValue(sizeFFromString(s, ok)); |
|
121 case QMetaType::QSize: |
|
122 return QVariant::fromValue(sizeFFromString(s, ok).toSize()); |
|
123 case QMetaType::QRectF: |
|
124 return QVariant::fromValue(rectFFromString(s, ok)); |
|
125 case QMetaType::QRect: |
|
126 return QVariant::fromValue(rectFFromString(s, ok).toRect()); |
|
127 case QMetaType::QVector3D: |
|
128 return QVariant::fromValue(vector3DFromString(s, ok)); |
|
129 default: |
|
130 if (ok) *ok = false; |
|
131 return QVariant(); |
|
132 } |
|
133 } |
|
134 |
|
135 QColor QDeclarativeStringConverters::colorFromString(const QString &s, bool *ok) |
|
136 { |
|
137 if (s.startsWith(QLatin1Char('#')) && s.length() == 9) { |
|
138 uchar a = fromHex(s, 1); |
|
139 uchar r = fromHex(s, 3); |
|
140 uchar g = fromHex(s, 5); |
|
141 uchar b = fromHex(s, 7); |
|
142 if (ok) *ok = true; |
|
143 return QColor(r, g, b, a); |
|
144 } else { |
|
145 QColor rv; |
|
146 if (s.startsWith(QLatin1Char('#')) || QColor::colorNames().contains(s.toLower())) |
|
147 rv = QColor(s); |
|
148 if (ok) *ok = rv.isValid(); |
|
149 return rv; |
|
150 } |
|
151 } |
|
152 |
|
153 QDate QDeclarativeStringConverters::dateFromString(const QString &s, bool *ok) |
|
154 { |
|
155 QDate d = QDate::fromString(s, Qt::ISODate); |
|
156 if (ok) *ok = d.isValid(); |
|
157 return d; |
|
158 } |
|
159 |
|
160 QTime QDeclarativeStringConverters::timeFromString(const QString &s, bool *ok) |
|
161 { |
|
162 QTime t = QTime::fromString(s, Qt::ISODate); |
|
163 if (ok) *ok = t.isValid(); |
|
164 return t; |
|
165 } |
|
166 |
|
167 QDateTime QDeclarativeStringConverters::dateTimeFromString(const QString &s, bool *ok) |
|
168 { |
|
169 QDateTime d = QDateTime::fromString(s, Qt::ISODate); |
|
170 if (ok) *ok = d.isValid(); |
|
171 return d; |
|
172 } |
|
173 |
|
174 //expects input of "x,y" |
|
175 QPointF QDeclarativeStringConverters::pointFFromString(const QString &s, bool *ok) |
|
176 { |
|
177 if (s.count(QLatin1Char(',')) != 1) { |
|
178 if (ok) |
|
179 *ok = false; |
|
180 return QPointF(); |
|
181 } |
|
182 |
|
183 bool xGood, yGood; |
|
184 int index = s.indexOf(QLatin1Char(',')); |
|
185 qreal xCoord = s.left(index).toDouble(&xGood); |
|
186 qreal yCoord = s.mid(index+1).toDouble(&yGood); |
|
187 if (!xGood || !yGood) { |
|
188 if (ok) |
|
189 *ok = false; |
|
190 return QPointF(); |
|
191 } |
|
192 |
|
193 if (ok) |
|
194 *ok = true; |
|
195 return QPointF(xCoord, yCoord); |
|
196 } |
|
197 |
|
198 //expects input of "widthxheight" |
|
199 QSizeF QDeclarativeStringConverters::sizeFFromString(const QString &s, bool *ok) |
|
200 { |
|
201 if (s.count(QLatin1Char('x')) != 1) { |
|
202 if (ok) |
|
203 *ok = false; |
|
204 return QSizeF(); |
|
205 } |
|
206 |
|
207 bool wGood, hGood; |
|
208 int index = s.indexOf(QLatin1Char('x')); |
|
209 qreal width = s.left(index).toDouble(&wGood); |
|
210 qreal height = s.mid(index+1).toDouble(&hGood); |
|
211 if (!wGood || !hGood) { |
|
212 if (ok) |
|
213 *ok = false; |
|
214 return QSizeF(); |
|
215 } |
|
216 |
|
217 if (ok) |
|
218 *ok = true; |
|
219 return QSizeF(width, height); |
|
220 } |
|
221 |
|
222 //expects input of "x,y,widthxheight" //### use space instead of second comma? |
|
223 QRectF QDeclarativeStringConverters::rectFFromString(const QString &s, bool *ok) |
|
224 { |
|
225 if (s.count(QLatin1Char(',')) != 2 || s.count(QLatin1Char('x')) != 1) { |
|
226 if (ok) |
|
227 *ok = false; |
|
228 return QRectF(); |
|
229 } |
|
230 |
|
231 bool xGood, yGood, wGood, hGood; |
|
232 int index = s.indexOf(QLatin1Char(',')); |
|
233 qreal x = s.left(index).toDouble(&xGood); |
|
234 int index2 = s.indexOf(QLatin1Char(','), index+1); |
|
235 qreal y = s.mid(index+1, index2-index-1).toDouble(&yGood); |
|
236 index = s.indexOf(QLatin1Char('x'), index2+1); |
|
237 qreal width = s.mid(index2+1, index-index2-1).toDouble(&wGood); |
|
238 qreal height = s.mid(index+1).toDouble(&hGood); |
|
239 if (!xGood || !yGood || !wGood || !hGood) { |
|
240 if (ok) |
|
241 *ok = false; |
|
242 return QRectF(); |
|
243 } |
|
244 |
|
245 if (ok) |
|
246 *ok = true; |
|
247 return QRectF(x, y, width, height); |
|
248 } |
|
249 |
|
250 //expects input of "x,y,z" |
|
251 QVector3D QDeclarativeStringConverters::vector3DFromString(const QString &s, bool *ok) |
|
252 { |
|
253 if (s.count(QLatin1Char(',')) != 2) { |
|
254 if (ok) |
|
255 *ok = false; |
|
256 return QVector3D(); |
|
257 } |
|
258 |
|
259 bool xGood, yGood, zGood; |
|
260 int index = s.indexOf(QLatin1Char(',')); |
|
261 int index2 = s.indexOf(QLatin1Char(','), index+1); |
|
262 qreal xCoord = s.left(index).toDouble(&xGood); |
|
263 qreal yCoord = s.mid(index+1, index2-index-1).toDouble(&yGood); |
|
264 qreal zCoord = s.mid(index2+1).toDouble(&zGood); |
|
265 if (!xGood || !yGood || !zGood) { |
|
266 if (ok) |
|
267 *ok = false; |
|
268 return QVector3D(); |
|
269 } |
|
270 |
|
271 if (ok) |
|
272 *ok = true; |
|
273 return QVector3D(xCoord, yCoord, zCoord); |
|
274 } |
|
275 |
|
276 QT_END_NAMESPACE |