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