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
7QT_BEGIN_NAMESPACE
8
9/*!
10\class QQmlScriptString
11\brief The QQmlScriptString class encapsulates a script and its context.
12\inmodule QtQml
13
14QQmlScriptString is used to create QObject properties that accept a script "assignment" from QML.
15
16Normally, the following QML would result in a binding being established for the \c script
17property; i.e. \c script would be assigned the value obtained from running \c {myObj.value = Math.max(myValue, 100)}
18
19\qml
20MyType {
21 script: myObj.value = Math.max(myValue, 100)
22}
23\endqml
24
25If instead the property had a type of QQmlScriptString,
26the script itself -- \e {myObj.value = Math.max(myValue, 100)} -- would be passed to the \c script property
27and the class could choose how to handle it. Typically, the class will evaluate
28the script at some later time using a QQmlExpression.
29
30\code
31QQmlExpression expr(scriptString);
32expr.evaluate();
33\endcode
34
35\sa QQmlExpression
36*/
37
38const QQmlScriptStringPrivate* QQmlScriptStringPrivate::get(const QQmlScriptString &script)
39{
40 return script.d.constData();
41}
42
43/*!
44Constructs an empty instance.
45*/
46QQmlScriptString::QQmlScriptString()
47: d()
48{
49}
50
51/*!
52 \internal
53*/
54QQmlScriptString::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/*!
63Copies \a other.
64*/
65QQmlScriptString::QQmlScriptString(const QQmlScriptString &other)
66: d(other.d)
67{
68}
69
70/*!
71\internal
72*/
73QQmlScriptString::~QQmlScriptString()
74{
75}
76
77/*!
78Assigns \a other to this.
79*/
80QQmlScriptString &QQmlScriptString::operator=(const QQmlScriptString &other)
81{
82 d = other.d;
83 return *this;
84}
85
86/*!
87Returns \c true if this and the \a other QQmlScriptString objects are equal.
88
89\sa operator!=()
90*/
91bool 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/*!
117Returns \c true if this and the \a other QQmlScriptString objects are different.
118
119\sa operator==()
120*/
121bool QQmlScriptString::operator!=(const QQmlScriptString &other) const
122{
123 return !operator==(other);
124}
125
126/*!
127Returns whether the QQmlScriptString is empty.
128*/
129bool 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/*!
139Returns whether the content of the QQmlScriptString is the \c undefined literal.
140*/
141bool QQmlScriptString::isUndefinedLiteral() const
142{
143 return d && d->script == QLatin1String("undefined");
144}
145
146/*!
147Returns whether the content of the QQmlScriptString is the \c null literal.
148*/
149bool QQmlScriptString::isNullLiteral() const
150{
151 return d && d->script == QLatin1String("null");
152}
153
154/*!
155If the content of the QQmlScriptString is a string literal, returns that string.
156Otherwise returns a null QString.
157*/
158QString 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/*!
166If the content of the QQmlScriptString is a number literal, returns that number and
167sets \a ok to true. Otherwise returns 0.0 and sets \a ok to false.
168*/
169qreal 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/*!
177If the content of the QQmlScriptString is a boolean literal, returns the boolean value and
178sets \a ok to true. Otherwise returns false and sets \a ok to false.
179*/
180bool 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
189QT_END_NAMESPACE
190
191#include "moc_qqmlscriptstring.cpp"
192
193

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