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 "qqmlscriptstring.h" |
5 | #include "qqmlscriptstring_p.h" |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | /*! |
10 | \class QQmlScriptString |
11 | \brief The QQmlScriptString class encapsulates a script and its context. |
12 | \inmodule QtQml |
13 | |
14 | QQmlScriptString is used to create QObject properties that accept a script "assignment" from QML. |
15 | |
16 | Normally, the following QML would result in a binding being established for the \c script |
17 | property; i.e. \c script would be assigned the value obtained from running \c {myObj.value = Math.max(myValue, 100)} |
18 | |
19 | \qml |
20 | MyType { |
21 | script: myObj.value = Math.max(myValue, 100) |
22 | } |
23 | \endqml |
24 | |
25 | If instead the property had a type of QQmlScriptString, |
26 | the script itself -- \e {myObj.value = Math.max(myValue, 100)} -- would be passed to the \c script property |
27 | and the class could choose how to handle it. Typically, the class will evaluate |
28 | the script at some later time using a QQmlExpression. |
29 | |
30 | \code |
31 | QQmlExpression expr(scriptString); |
32 | expr.evaluate(); |
33 | \endcode |
34 | |
35 | \sa QQmlExpression |
36 | */ |
37 | |
38 | const QQmlScriptStringPrivate* QQmlScriptStringPrivate::get(const QQmlScriptString &script) |
39 | { |
40 | return script.d.constData(); |
41 | } |
42 | |
43 | /*! |
44 | Constructs an empty instance. |
45 | */ |
46 | QQmlScriptString::QQmlScriptString() |
47 | : d() |
48 | { |
49 | } |
50 | |
51 | /*! |
52 | \internal |
53 | */ |
54 | QQmlScriptString::QQmlScriptString(const QString &script, QQmlContext *context, QObject *scope) |
55 | : d(new QQmlScriptStringPrivate) |
56 | { |
57 | d->script = script; |
58 | d->context = context; |
59 | d->scope = scope; |
60 | } |
61 | |
62 | /*! |
63 | Copies \a other. |
64 | */ |
65 | QQmlScriptString::QQmlScriptString(const QQmlScriptString &other) |
66 | : d(other.d) |
67 | { |
68 | } |
69 | |
70 | /*! |
71 | \internal |
72 | */ |
73 | QQmlScriptString::~QQmlScriptString() |
74 | { |
75 | } |
76 | |
77 | /*! |
78 | Assigns \a other to this. |
79 | */ |
80 | QQmlScriptString &QQmlScriptString::operator=(const QQmlScriptString &other) |
81 | { |
82 | d = other.d; |
83 | return *this; |
84 | } |
85 | |
86 | /*! |
87 | Returns \c true if this and the \a other QQmlScriptString objects are equal. |
88 | |
89 | \sa operator!=() |
90 | */ |
91 | bool QQmlScriptString::operator==(const QQmlScriptString &other) const |
92 | { |
93 | if (d == other.d) |
94 | return true; |
95 | if (!d) |
96 | return false; |
97 | |
98 | if (d->isNumberLiteral || other.d->isNumberLiteral) |
99 | return d->isNumberLiteral && other.d->isNumberLiteral && d->numberValue == other.d->numberValue; |
100 | |
101 | if (d->isStringLiteral || other.d->isStringLiteral) |
102 | return d->isStringLiteral && other.d->isStringLiteral && d->script == other.d->script; |
103 | |
104 | if (d->script == QLatin1String("true" ) || |
105 | d->script == QLatin1String("false" ) || |
106 | d->script == QLatin1String("undefined" ) || |
107 | d->script == QLatin1String("null" )) |
108 | return d->script == other.d->script; |
109 | |
110 | return d->context == other.d->context && |
111 | d->scope == other.d->scope && |
112 | d->script == other.d->script && |
113 | d->bindingId == other.d->bindingId; |
114 | } |
115 | |
116 | /*! |
117 | Returns \c true if this and the \a other QQmlScriptString objects are different. |
118 | |
119 | \sa operator==() |
120 | */ |
121 | bool QQmlScriptString::operator!=(const QQmlScriptString &other) const |
122 | { |
123 | return !operator==(other); |
124 | } |
125 | |
126 | /*! |
127 | Returns whether the QQmlScriptString is empty. |
128 | */ |
129 | bool QQmlScriptString::isEmpty() const |
130 | { |
131 | if (!d) |
132 | return true; |
133 | if (!d->script.isEmpty()) |
134 | return false; |
135 | return d->bindingId == -1; |
136 | } |
137 | |
138 | /*! |
139 | Returns whether the content of the QQmlScriptString is the \c undefined literal. |
140 | */ |
141 | bool QQmlScriptString::isUndefinedLiteral() const |
142 | { |
143 | return d && d->script == QLatin1String("undefined" ); |
144 | } |
145 | |
146 | /*! |
147 | Returns whether the content of the QQmlScriptString is the \c null literal. |
148 | */ |
149 | bool QQmlScriptString::isNullLiteral() const |
150 | { |
151 | return d && d->script == QLatin1String("null" ); |
152 | } |
153 | |
154 | /*! |
155 | If the content of the QQmlScriptString is a string literal, returns that string. |
156 | Otherwise returns a null QString. |
157 | */ |
158 | QString QQmlScriptString::stringLiteral() const |
159 | { |
160 | if (d && d->isStringLiteral) |
161 | return d->script.mid(position: 1, n: d->script.size()-2); |
162 | return QString(); |
163 | } |
164 | |
165 | /*! |
166 | If the content of the QQmlScriptString is a number literal, returns that number and |
167 | sets \a ok to true. Otherwise returns 0.0 and sets \a ok to false. |
168 | */ |
169 | qreal QQmlScriptString::numberLiteral(bool *ok) const |
170 | { |
171 | if (ok) |
172 | *ok = d && d->isNumberLiteral; |
173 | return (d && d->isNumberLiteral) ? d->numberValue : 0.; |
174 | } |
175 | |
176 | /*! |
177 | If the content of the QQmlScriptString is a boolean literal, returns the boolean value and |
178 | sets \a ok to true. Otherwise returns false and sets \a ok to false. |
179 | */ |
180 | bool QQmlScriptString::booleanLiteral(bool *ok) const |
181 | { |
182 | bool isTrue = d && d->script == QLatin1String("true" ); |
183 | bool isFalse = !isTrue && d && d->script == QLatin1String("false" ); |
184 | if (ok) |
185 | *ok = isTrue || isFalse; |
186 | return isTrue ? true : false; |
187 | } |
188 | |
189 | QT_END_NAMESPACE |
190 | |
191 | #include "moc_qqmlscriptstring.cpp" |
192 | |
193 | |