Qt 4.8
Functions
QDeclarativeStringConverters Namespace Reference

Functions

QColor Q_DECLARATIVE_PRIVATE_EXPORT colorFromString (const QString &, bool *ok=0)
 
QDate Q_DECLARATIVE_PRIVATE_EXPORT dateFromString (const QString &, bool *ok=0)
 
QDateTime Q_DECLARATIVE_PRIVATE_EXPORT dateTimeFromString (const QString &, bool *ok=0)
 
QPointF Q_DECLARATIVE_PRIVATE_EXPORT pointFFromString (const QString &, bool *ok=0)
 
QRectF Q_DECLARATIVE_PRIVATE_EXPORT rectFFromString (const QString &, bool *ok=0)
 
QSizeF Q_DECLARATIVE_PRIVATE_EXPORT sizeFFromString (const QString &, bool *ok=0)
 
QTime Q_DECLARATIVE_PRIVATE_EXPORT timeFromString (const QString &, bool *ok=0)
 
QVariant Q_DECLARATIVE_PRIVATE_EXPORT variantFromString (const QString &)
 
QVariant Q_DECLARATIVE_PRIVATE_EXPORT variantFromString (const QString &, int preferredType, bool *ok=0)
 
QVector3D Q_DECLARATIVE_PRIVATE_EXPORT vector3DFromString (const QString &, bool *ok=0)
 

Function Documentation

◆ colorFromString()

QColor QDeclarativeStringConverters::colorFromString ( const QString s,
bool *  ok = 0 
)

Definition at line 143 of file qdeclarativestringconverters.cpp.

Referenced by QDeclarativePropertyAnimationPrivate::convertVariant(), QDeclarativeEnginePrivate::darker(), QDeclarativeCompiler::genLiteralAssignment(), QDeclarativeEnginePrivate::lighter(), QDeclarativeCompiler::testLiteralAssignment(), QDeclarativeEnginePrivate::tint(), and variantFromString().

144 {
145  if (s.length() == 9 && s.startsWith(QLatin1Char('#'))) {
146  uchar a = fromHex(s, 1);
147  uchar r = fromHex(s, 3);
148  uchar g = fromHex(s, 5);
149  uchar b = fromHex(s, 7);
150  if (ok) *ok = true;
151  return QColor(r, g, b, a);
152  } else {
153  QColor rv(s);
154  if (ok) *ok = rv.isValid();
155  return rv;
156  }
157 }
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
long ASN1_INTEGER_get ASN1_INTEGER * a
unsigned char uchar
Definition: qglobal.h:994
static uchar fromHex(const uchar c, const uchar c2)
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ dateFromString()

QDate QDeclarativeStringConverters::dateFromString ( const QString s,
bool *  ok = 0 
)

Definition at line 160 of file qdeclarativestringconverters.cpp.

Referenced by QDeclarativeCompiler::genLiteralAssignment(), QDeclarativeCompiler::testLiteralAssignment(), and variantFromString().

161 {
163  if (ok) *ok = d.isValid();
164  return d;
165 }
double d
Definition: qnumeric_p.h:62
bool isValid() const
Returns true if this date is valid; otherwise returns false.
Definition: qdatetime.cpp:340
The QDate class provides date functions.
Definition: qdatetime.h:55
static QDate fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the QDate represented by the string, using the format given, or an invalid date if the string...
Definition: qdatetime.cpp:1203

◆ dateTimeFromString()

QDateTime QDeclarativeStringConverters::dateTimeFromString ( const QString s,
bool *  ok = 0 
)

Definition at line 174 of file qdeclarativestringconverters.cpp.

Referenced by QDeclarativeCompiler::genLiteralAssignment(), QDeclarativeCompiler::testLiteralAssignment(), and variantFromString().

175 {
177  if (ok) *ok = d.isValid();
178  return d;
179 }
double d
Definition: qnumeric_p.h:62
bool isValid() const
Returns true if both the date and the time are valid; otherwise returns false.
Definition: qdatetime.cpp:2346
static QDateTime fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the QDateTime represented by the string, using the format given, or an invalid datetime if th...
Definition: qdatetime.cpp:3487
The QDateTime class provides date and time functions.
Definition: qdatetime.h:216

◆ pointFFromString()

QPointF QDeclarativeStringConverters::pointFFromString ( const QString s,
bool *  ok = 0 
)

Definition at line 183 of file qdeclarativestringconverters.cpp.

Referenced by QDeclarativePropertyAnimationPrivate::convertVariant(), QDeclarativeCompiler::genLiteralAssignment(), QDeclarativeCompiler::testLiteralAssignment(), and variantFromString().

184 {
185  if (s.count(QLatin1Char(',')) != 1) {
186  if (ok)
187  *ok = false;
188  return QPointF();
189  }
190 
191  bool xGood, yGood;
192  int index = s.indexOf(QLatin1Char(','));
193  qreal xCoord = s.left(index).toDouble(&xGood);
194  qreal yCoord = s.mid(index+1).toDouble(&yGood);
195  if (!xGood || !yGood) {
196  if (ok)
197  *ok = false;
198  return QPointF();
199  }
200 
201  if (ok)
202  *ok = true;
203  return QPointF(xCoord, yCoord);
204 }
double qreal
Definition: qglobal.h:1193
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
int count() const
Definition: qstring.h:103
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
double toDouble(bool *ok=0) const
Returns the string converted to a double value.
Definition: qstring.cpp:6227
quint16 index
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ rectFFromString()

QRectF QDeclarativeStringConverters::rectFFromString ( const QString s,
bool *  ok = 0 
)

Definition at line 231 of file qdeclarativestringconverters.cpp.

Referenced by QDeclarativePropertyAnimationPrivate::convertVariant(), QDeclarativeCompiler::genLiteralAssignment(), QDeclarativeCompiler::testLiteralAssignment(), and variantFromString().

232 {
233  if (s.count(QLatin1Char(',')) != 2 || s.count(QLatin1Char('x')) != 1) {
234  if (ok)
235  *ok = false;
236  return QRectF();
237  }
238 
239  bool xGood, yGood, wGood, hGood;
240  int index = s.indexOf(QLatin1Char(','));
241  qreal x = s.left(index).toDouble(&xGood);
242  int index2 = s.indexOf(QLatin1Char(','), index+1);
243  qreal y = s.mid(index+1, index2-index-1).toDouble(&yGood);
244  index = s.indexOf(QLatin1Char('x'), index2+1);
245  qreal width = s.mid(index2+1, index-index2-1).toDouble(&wGood);
246  qreal height = s.mid(index+1).toDouble(&hGood);
247  if (!xGood || !yGood || !wGood || !hGood) {
248  if (ok)
249  *ok = false;
250  return QRectF();
251  }
252 
253  if (ok)
254  *ok = true;
255  return QRectF(x, y, width, height);
256 }
double qreal
Definition: qglobal.h:1193
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
int count() const
Definition: qstring.h:103
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
double toDouble(bool *ok=0) const
Returns the string converted to a double value.
Definition: qstring.cpp:6227
quint16 index
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ sizeFFromString()

QSizeF QDeclarativeStringConverters::sizeFFromString ( const QString s,
bool *  ok = 0 
)

Definition at line 207 of file qdeclarativestringconverters.cpp.

Referenced by QDeclarativePropertyAnimationPrivate::convertVariant(), QDeclarativeCompiler::genLiteralAssignment(), QDeclarativeCompiler::testLiteralAssignment(), and variantFromString().

208 {
209  if (s.count(QLatin1Char('x')) != 1) {
210  if (ok)
211  *ok = false;
212  return QSizeF();
213  }
214 
215  bool wGood, hGood;
216  int index = s.indexOf(QLatin1Char('x'));
217  qreal width = s.left(index).toDouble(&wGood);
218  qreal height = s.mid(index+1).toDouble(&hGood);
219  if (!wGood || !hGood) {
220  if (ok)
221  *ok = false;
222  return QSizeF();
223  }
224 
225  if (ok)
226  *ok = true;
227  return QSizeF(width, height);
228 }
double qreal
Definition: qglobal.h:1193
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
int count() const
Definition: qstring.h:103
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
double toDouble(bool *ok=0) const
Returns the string converted to a double value.
Definition: qstring.cpp:6227
quint16 index
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55

◆ timeFromString()

QTime QDeclarativeStringConverters::timeFromString ( const QString s,
bool *  ok = 0 
)

Definition at line 167 of file qdeclarativestringconverters.cpp.

Referenced by QDeclarativeCompiler::genLiteralAssignment(), QDeclarativeCompiler::testLiteralAssignment(), and variantFromString().

168 {
170  if (ok) *ok = t.isValid();
171  return t;
172 }
The QTime class provides clock time functions.
Definition: qdatetime.h:148
static QTime fromString(const QString &s, Qt::DateFormat f=Qt::TextDate)
Returns the time represented in the string as a QTime using the format given, or an invalid time if t...
Definition: qdatetime.cpp:1928
bool isValid() const
Returns true if the time is valid; otherwise returns false.
Definition: qdatetime.cpp:1566

◆ variantFromString() [1/2]

QVariant QDeclarativeStringConverters::variantFromString ( const QString s)

Definition at line 81 of file qdeclarativestringconverters.cpp.

Referenced by QDeclarativePropertyPrivate::write().

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 QVariant::fromValue(v);
96 
97  return QVariant(s);
98 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
The QColor class provides colors based on RGB, HSV or CMYK values.
Definition: qcolor.h:67
The QVector3D class represents a vector or vertex in 3D space.
Definition: qvector3d.h:60
QColor Q_DECLARATIVE_PRIVATE_EXPORT colorFromString(const QString &, bool *ok=0)
unsigned char c[8]
Definition: qnumeric_p.h:62
The QPointF class defines a point in the plane using floating point precision.
Definition: qpoint.h:214
QVector3D Q_DECLARATIVE_PRIVATE_EXPORT vector3DFromString(const QString &, bool *ok=0)
The QSizeF class defines the size of a two-dimensional object using floating point precision...
Definition: qsize.h:202
QRectF Q_DECLARATIVE_PRIVATE_EXPORT rectFFromString(const QString &, bool *ok=0)
The QRectF class defines a rectangle in the plane using floating point precision. ...
Definition: qrect.h:511
QPointF Q_DECLARATIVE_PRIVATE_EXPORT pointFFromString(const QString &, bool *ok=0)
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
QSizeF Q_DECLARATIVE_PRIVATE_EXPORT sizeFFromString(const QString &, bool *ok=0)

◆ variantFromString() [2/2]

QVariant QDeclarativeStringConverters::variantFromString ( const QString s,
int  preferredType,
bool *  ok = 0 
)

Definition at line 106 of file qdeclarativestringconverters.cpp.

107 {
108  switch (preferredType) {
109  case QMetaType::Int:
110  return QVariant(int(qRoundDouble(s.toDouble(ok))));
111  case QMetaType::UInt:
112  return QVariant(uint(qRoundDouble(s.toDouble(ok))));
113  case QMetaType::QColor:
114  return QVariant::fromValue(colorFromString(s, ok));
115 #ifndef QT_NO_DATESTRING
116  case QMetaType::QDate:
117  return QVariant::fromValue(dateFromString(s, ok));
118  case QMetaType::QTime:
119  return QVariant::fromValue(timeFromString(s, ok));
122 #endif // QT_NO_DATESTRING
123  case QMetaType::QPointF:
124  return QVariant::fromValue(pointFFromString(s, ok));
125  case QMetaType::QPoint:
126  return QVariant::fromValue(pointFFromString(s, ok).toPoint());
127  case QMetaType::QSizeF:
128  return QVariant::fromValue(sizeFFromString(s, ok));
129  case QMetaType::QSize:
130  return QVariant::fromValue(sizeFFromString(s, ok).toSize());
131  case QMetaType::QRectF:
132  return QVariant::fromValue(rectFFromString(s, ok));
133  case QMetaType::QRect:
134  return QVariant::fromValue(rectFFromString(s, ok).toRect());
137  default:
138  if (ok) *ok = false;
139  return QVariant();
140  }
141 }
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QColor Q_DECLARATIVE_PRIVATE_EXPORT colorFromString(const QString &, bool *ok=0)
QVector3D Q_DECLARATIVE_PRIVATE_EXPORT vector3DFromString(const QString &, bool *ok=0)
QDateTime Q_DECLARATIVE_PRIVATE_EXPORT dateTimeFromString(const QString &, bool *ok=0)
QRectF Q_DECLARATIVE_PRIVATE_EXPORT rectFFromString(const QString &, bool *ok=0)
QPointF Q_DECLARATIVE_PRIVATE_EXPORT pointFFromString(const QString &, bool *ok=0)
unsigned int uint
Definition: qglobal.h:996
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
QSizeF Q_DECLARATIVE_PRIVATE_EXPORT sizeFFromString(const QString &, bool *ok=0)
double toDouble(bool *ok=0) const
Returns the string converted to a double value.
Definition: qstring.cpp:6227
QTime Q_DECLARATIVE_PRIVATE_EXPORT timeFromString(const QString &, bool *ok=0)
QDate Q_DECLARATIVE_PRIVATE_EXPORT dateFromString(const QString &, bool *ok=0)

◆ vector3DFromString()

QVector3D QDeclarativeStringConverters::vector3DFromString ( const QString s,
bool *  ok = 0 
)

Definition at line 259 of file qdeclarativestringconverters.cpp.

Referenced by QDeclarativePropertyAnimationPrivate::convertVariant(), QDeclarativeCompiler::genLiteralAssignment(), QDeclarativeCompiler::testLiteralAssignment(), and variantFromString().

260 {
261  if (s.count(QLatin1Char(',')) != 2) {
262  if (ok)
263  *ok = false;
264  return QVector3D();
265  }
266 
267  bool xGood, yGood, zGood;
268  int index = s.indexOf(QLatin1Char(','));
269  int index2 = s.indexOf(QLatin1Char(','), index+1);
270  qreal xCoord = s.left(index).toDouble(&xGood);
271  qreal yCoord = s.mid(index+1, index2-index-1).toDouble(&yGood);
272  qreal zCoord = s.mid(index2+1).toDouble(&zGood);
273  if (!xGood || !yGood || !zGood) {
274  if (ok)
275  *ok = false;
276  return QVector3D();
277  }
278 
279  if (ok)
280  *ok = true;
281  return QVector3D(xCoord, yCoord, zCoord);
282 }
The QVector3D class represents a vector or vertex in 3D space.
Definition: qvector3d.h:60
double qreal
Definition: qglobal.h:1193
QString left(int n) const Q_REQUIRED_RESULT
Returns a substring that contains the n leftmost characters of the string.
Definition: qstring.cpp:3664
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
int count() const
Definition: qstring.h:103
QString mid(int position, int n=-1) const Q_REQUIRED_RESULT
Returns a string that contains n characters of this string, starting at the specified position index...
Definition: qstring.cpp:3706
double toDouble(bool *ok=0) const
Returns the string converted to a double value.
Definition: qstring.cpp:6227
quint16 index
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55