1 | // Copyright (C) 2023 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
3 | |
4 | #include "qqmljsvaluetypefromstringcheck_p.h" |
5 | #include <private/qqmlstringconverters_p.h> |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | using namespace Qt::StringLiterals; |
10 | |
11 | /*! |
12 | \internal |
13 | \class ValueTypeFromStringError |
14 | |
15 | Helps to differentiate between three states: |
16 | \list |
17 | \li a value type was constructed by a string in a deprecated way, e.g. "50x70" for a QSizeF |
18 | \li a value type was constructed by an invalid string in a deprecated way, e.g. "50,70" for a QSizeF |
19 | \li a value type was constructed by a string in a non-deprecated way. |
20 | \endlist |
21 | */ |
22 | |
23 | /*! |
24 | \internal |
25 | Checks if known value types are being constructed in a deprecated way, and constructs the code for |
26 | the fix-it. |
27 | */ |
28 | QQmlJSStructuredTypeError QQmlJSValueTypeFromStringCheck::hasError(const QString &typeName, |
29 | const QString &value) |
30 | { |
31 | if (typeName == u"QPointF" || typeName == u"QPoint" ) { |
32 | std::array<double, 2> numbers; |
33 | if (!QQmlStringConverters::isValidNumberString<2, u','>(s: value, numbers: &numbers)) |
34 | return QQmlJSStructuredTypeError::withInvalidString(); |
35 | |
36 | const auto result = QQmlJSStructuredTypeError::fromSuggestedString( |
37 | enhancedString: u"({ x: %1, y: %2 })"_s .arg(a: numbers[0]).arg(a: numbers[1])); |
38 | return result; |
39 | } else if (typeName == u"QSizeF" || typeName == u"QSize" ) { |
40 | std::array<double, 2> numbers; |
41 | if (!QQmlStringConverters::isValidNumberString<2, u'x'>(s: value, numbers: &numbers)) |
42 | return QQmlJSStructuredTypeError::withInvalidString(); |
43 | |
44 | const auto result = QQmlJSStructuredTypeError::fromSuggestedString( |
45 | enhancedString: u"({ width: %1, height: %2 })"_s .arg(a: numbers[0]).arg(a: numbers[1])); |
46 | return result; |
47 | } else if (typeName == u"QRectF" || typeName == u"QRect" ) { |
48 | std::array<double, 4> numbers; |
49 | if (!QQmlStringConverters::isValidNumberString<4, u',', u',', u'x'>(s: value, numbers: &numbers)) |
50 | return QQmlJSStructuredTypeError::withInvalidString(); |
51 | |
52 | const auto result = QQmlJSStructuredTypeError::fromSuggestedString( |
53 | enhancedString: u"({ x: %1, y: %2, width: %3, height: %4 })"_s .arg(a: numbers[0]) |
54 | .arg(a: numbers[1]) |
55 | .arg(a: numbers[2]) |
56 | .arg(a: numbers[3])); |
57 | return result; |
58 | } |
59 | |
60 | return QQmlJSStructuredTypeError::withValidString(); |
61 | } |
62 | |
63 | QT_END_NAMESPACE |
64 | |