| 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 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 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 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 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 | #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.startsWith(QLatin1Char('#')) && s.length() == 9) { | 
|---|
| 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; | 
|---|
| 148 | if (s.startsWith(QLatin1Char('#')) || QColor::colorNames().contains(s.toLower())) | 
|---|
| 149 | rv = QColor(s); | 
|---|
| 150 | if (ok) *ok = rv.isValid(); | 
|---|
| 151 | return rv; | 
|---|
| 152 | } | 
|---|
| 153 | } | 
|---|
| 154 |  | 
|---|
| 155 | #ifndef QT_NO_DATESTRING | 
|---|
| 156 | QDate QDeclarativeStringConverters::dateFromString(const QString &s, bool *ok) | 
|---|
| 157 | { | 
|---|
| 158 | QDate d = QDate::fromString(s, Qt::ISODate); | 
|---|
| 159 | if (ok) *ok =  d.isValid(); | 
|---|
| 160 | return d; | 
|---|
| 161 | } | 
|---|
| 162 |  | 
|---|
| 163 | QTime QDeclarativeStringConverters::timeFromString(const QString &s, bool *ok) | 
|---|
| 164 | { | 
|---|
| 165 | QTime t = QTime::fromString(s, Qt::ISODate); | 
|---|
| 166 | if (ok) *ok = t.isValid(); | 
|---|
| 167 | return t; | 
|---|
| 168 | } | 
|---|
| 169 |  | 
|---|
| 170 | QDateTime QDeclarativeStringConverters::dateTimeFromString(const QString &s, bool *ok) | 
|---|
| 171 | { | 
|---|
| 172 | QDateTime d = QDateTime::fromString(s, Qt::ISODate); | 
|---|
| 173 | if (ok) *ok =  d.isValid(); | 
|---|
| 174 | return d; | 
|---|
| 175 | } | 
|---|
| 176 | #endif // QT_NO_DATESTRING | 
|---|
| 177 |  | 
|---|
| 178 | //expects input of "x,y" | 
|---|
| 179 | QPointF QDeclarativeStringConverters::pointFFromString(const QString &s, bool *ok) | 
|---|
| 180 | { | 
|---|
| 181 | if (s.count(QLatin1Char(',')) != 1) { | 
|---|
| 182 | if (ok) | 
|---|
| 183 | *ok = false; | 
|---|
| 184 | return QPointF(); | 
|---|
| 185 | } | 
|---|
| 186 |  | 
|---|
| 187 | bool xGood, yGood; | 
|---|
| 188 | int index = s.indexOf(QLatin1Char(',')); | 
|---|
| 189 | qreal xCoord = s.left(index).toDouble(&xGood); | 
|---|
| 190 | qreal yCoord = s.mid(index+1).toDouble(&yGood); | 
|---|
| 191 | if (!xGood || !yGood) { | 
|---|
| 192 | if (ok) | 
|---|
| 193 | *ok = false; | 
|---|
| 194 | return QPointF(); | 
|---|
| 195 | } | 
|---|
| 196 |  | 
|---|
| 197 | if (ok) | 
|---|
| 198 | *ok = true; | 
|---|
| 199 | return QPointF(xCoord, yCoord); | 
|---|
| 200 | } | 
|---|
| 201 |  | 
|---|
| 202 | //expects input of "widthxheight" | 
|---|
| 203 | QSizeF QDeclarativeStringConverters::sizeFFromString(const QString &s, bool *ok) | 
|---|
| 204 | { | 
|---|
| 205 | if (s.count(QLatin1Char('x')) != 1) { | 
|---|
| 206 | if (ok) | 
|---|
| 207 | *ok = false; | 
|---|
| 208 | return QSizeF(); | 
|---|
| 209 | } | 
|---|
| 210 |  | 
|---|
| 211 | bool wGood, hGood; | 
|---|
| 212 | int index = s.indexOf(QLatin1Char('x')); | 
|---|
| 213 | qreal width = s.left(index).toDouble(&wGood); | 
|---|
| 214 | qreal height = s.mid(index+1).toDouble(&hGood); | 
|---|
| 215 | if (!wGood || !hGood) { | 
|---|
| 216 | if (ok) | 
|---|
| 217 | *ok = false; | 
|---|
| 218 | return QSizeF(); | 
|---|
| 219 | } | 
|---|
| 220 |  | 
|---|
| 221 | if (ok) | 
|---|
| 222 | *ok = true; | 
|---|
| 223 | return QSizeF(width, height); | 
|---|
| 224 | } | 
|---|
| 225 |  | 
|---|
| 226 | //expects input of "x,y,widthxheight" //### use space instead of second comma? | 
|---|
| 227 | QRectF QDeclarativeStringConverters::rectFFromString(const QString &s, bool *ok) | 
|---|
| 228 | { | 
|---|
| 229 | if (s.count(QLatin1Char(',')) != 2 || s.count(QLatin1Char('x')) != 1) { | 
|---|
| 230 | if (ok) | 
|---|
| 231 | *ok = false; | 
|---|
| 232 | return QRectF(); | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | bool xGood, yGood, wGood, hGood; | 
|---|
| 236 | int index = s.indexOf(QLatin1Char(',')); | 
|---|
| 237 | qreal x = s.left(index).toDouble(&xGood); | 
|---|
| 238 | int index2 = s.indexOf(QLatin1Char(','), index+1); | 
|---|
| 239 | qreal y = s.mid(index+1, index2-index-1).toDouble(&yGood); | 
|---|
| 240 | index = s.indexOf(QLatin1Char('x'), index2+1); | 
|---|
| 241 | qreal width = s.mid(index2+1, index-index2-1).toDouble(&wGood); | 
|---|
| 242 | qreal height = s.mid(index+1).toDouble(&hGood); | 
|---|
| 243 | if (!xGood || !yGood || !wGood || !hGood) { | 
|---|
| 244 | if (ok) | 
|---|
| 245 | *ok = false; | 
|---|
| 246 | return QRectF(); | 
|---|
| 247 | } | 
|---|
| 248 |  | 
|---|
| 249 | if (ok) | 
|---|
| 250 | *ok = true; | 
|---|
| 251 | return QRectF(x, y, width, height); | 
|---|
| 252 | } | 
|---|
| 253 |  | 
|---|
| 254 | //expects input of "x,y,z" | 
|---|
| 255 | QVector3D QDeclarativeStringConverters::vector3DFromString(const QString &s, bool *ok) | 
|---|
| 256 | { | 
|---|
| 257 | if (s.count(QLatin1Char(',')) != 2) { | 
|---|
| 258 | if (ok) | 
|---|
| 259 | *ok = false; | 
|---|
| 260 | return QVector3D(); | 
|---|
| 261 | } | 
|---|
| 262 |  | 
|---|
| 263 | bool xGood, yGood, zGood; | 
|---|
| 264 | int index = s.indexOf(QLatin1Char(',')); | 
|---|
| 265 | int index2 = s.indexOf(QLatin1Char(','), index+1); | 
|---|
| 266 | qreal xCoord = s.left(index).toDouble(&xGood); | 
|---|
| 267 | qreal yCoord = s.mid(index+1, index2-index-1).toDouble(&yGood); | 
|---|
| 268 | qreal zCoord = s.mid(index2+1).toDouble(&zGood); | 
|---|
| 269 | if (!xGood || !yGood || !zGood) { | 
|---|
| 270 | if (ok) | 
|---|
| 271 | *ok = false; | 
|---|
| 272 | return QVector3D(); | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | if (ok) | 
|---|
| 276 | *ok = true; | 
|---|
| 277 | return QVector3D(xCoord, yCoord, zCoord); | 
|---|
| 278 | } | 
|---|
| 279 |  | 
|---|
| 280 | QT_END_NAMESPACE | 
|---|