1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3
4#include "qqmlstringconverters_p.h"
5#include <private/qqmlglobal_p.h>
6
7#include <QtCore/qpoint.h>
8#include <QtCore/qrect.h>
9#include <QtCore/qsize.h>
10#include <QtCore/qvariant.h>
11#include <QtCore/qdatetime.h>
12
13QT_BEGIN_NAMESPACE
14
15QVariant QQmlStringConverters::variantFromString(const QString &s, QMetaType preferredType, bool *ok)
16{
17 switch (preferredType.id()) {
18 case QMetaType::Int:
19 return QVariant(int(qRound(d: s.toDouble(ok))));
20 case QMetaType::UInt:
21 return QVariant(uint(qRound(d: s.toDouble(ok))));
22#if QT_CONFIG(datestring)
23 case QMetaType::QDate:
24 return QVariant::fromValue(value: dateFromString(s, ok));
25 case QMetaType::QTime:
26 return QVariant::fromValue(value: timeFromString(s, ok));
27 case QMetaType::QDateTime:
28 return QVariant::fromValue(value: dateTimeFromString(s, ok));
29#endif // datestring
30 case QMetaType::QPointF:
31 return QVariant::fromValue(value: pointFFromString(s, ok));
32 case QMetaType::QPoint:
33 return QVariant::fromValue(value: pointFFromString(s, ok).toPoint());
34 case QMetaType::QSizeF:
35 return QVariant::fromValue(value: sizeFFromString(s, ok));
36 case QMetaType::QSize:
37 return QVariant::fromValue(value: sizeFFromString(s, ok).toSize());
38 case QMetaType::QRectF:
39 return QVariant::fromValue(value: rectFFromString(s, ok));
40 case QMetaType::QRect:
41 return QVariant::fromValue(value: rectFFromString(s, ok).toRect());
42 default: {
43 const QVariant ret = QQmlValueTypeProvider::createValueType(s, preferredType);
44 if (ret.isValid()) {
45 if (ok)
46 *ok = true;
47 return ret;
48 }
49 if (ok)
50 *ok = false;
51 return QVariant();
52 }
53 }
54}
55
56QVariant QQmlStringConverters::colorFromString(const QString &s, bool *ok)
57{
58 return QQml_colorProvider()->colorFromString(s, ok);
59}
60
61unsigned QQmlStringConverters::rgbaFromString(const QString &s, bool *ok)
62{
63 return QQml_colorProvider()->rgbaFromString(s, ok);
64}
65
66#if QT_CONFIG(datestring)
67QDate QQmlStringConverters::dateFromString(const QString &s, bool *ok)
68{
69 QDate d = QDate::fromString(string: s, format: Qt::ISODate);
70 if (ok) *ok = d.isValid();
71 return d;
72}
73
74QTime QQmlStringConverters::timeFromString(const QString &s, bool *ok)
75{
76 QTime t = QTime::fromString(string: s, format: Qt::ISODate);
77 if (ok) *ok = t.isValid();
78 return t;
79}
80
81QDateTime QQmlStringConverters::dateTimeFromString(const QString &s, bool *ok)
82{
83 QDateTime d = QDateTime::fromString(string: s, format: Qt::ISODate);
84 if (ok) *ok = d.isValid();
85 return d;
86}
87#endif // datestring
88
89//expects input of "x,y"
90QPointF QQmlStringConverters::pointFFromString(const QString &s, bool *ok)
91{
92 if (s.count(c: QLatin1Char(',')) != 1) {
93 if (ok)
94 *ok = false;
95 return QPointF();
96 }
97
98 bool xGood, yGood;
99 int index = s.indexOf(c: QLatin1Char(','));
100 qreal xCoord = QStringView{s}.left(n: index).toDouble(ok: &xGood);
101 qreal yCoord = QStringView{s}.mid(pos: index+1).toDouble(ok: &yGood);
102 if (!xGood || !yGood) {
103 if (ok)
104 *ok = false;
105 return QPointF();
106 }
107
108 if (ok)
109 *ok = true;
110 return QPointF(xCoord, yCoord);
111}
112
113//expects input of "widthxheight"
114QSizeF QQmlStringConverters::sizeFFromString(const QString &s, bool *ok)
115{
116 if (s.count(c: QLatin1Char('x')) != 1) {
117 if (ok)
118 *ok = false;
119 return QSizeF();
120 }
121
122 bool wGood, hGood;
123 int index = s.indexOf(c: QLatin1Char('x'));
124 qreal width = QStringView{s}.left(n: index).toDouble(ok: &wGood);
125 qreal height = QStringView{s}.mid(pos: index+1).toDouble(ok: &hGood);
126 if (!wGood || !hGood) {
127 if (ok)
128 *ok = false;
129 return QSizeF();
130 }
131
132 if (ok)
133 *ok = true;
134 return QSizeF(width, height);
135}
136
137//expects input of "x,y,widthxheight" //### use space instead of second comma?
138QRectF QQmlStringConverters::rectFFromString(const QString &s, bool *ok)
139{
140 if (s.count(c: QLatin1Char(',')) != 2 || s.count(c: QLatin1Char('x')) != 1) {
141 if (ok)
142 *ok = false;
143 return QRectF();
144 }
145
146 bool xGood, yGood, wGood, hGood;
147 int index = s.indexOf(c: QLatin1Char(','));
148 qreal x = QStringView{s}.left(n: index).toDouble(ok: &xGood);
149 int index2 = s.indexOf(c: QLatin1Char(','), from: index+1);
150 qreal y = QStringView{s}.mid(pos: index+1, n: index2-index-1).toDouble(ok: &yGood);
151 index = s.indexOf(c: QLatin1Char('x'), from: index2+1);
152 qreal width = QStringView{s}.mid(pos: index2+1, n: index-index2-1).toDouble(ok: &wGood);
153 qreal height = QStringView{s}.mid(pos: index+1).toDouble(ok: &hGood);
154 if (!xGood || !yGood || !wGood || !hGood) {
155 if (ok)
156 *ok = false;
157 return QRectF();
158 }
159
160 if (ok)
161 *ok = true;
162 return QRectF(x, y, width, height);
163}
164
165QT_END_NAMESPACE
166

source code of qtdeclarative/src/qml/qml/qqmlstringconverters.cpp