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\sa QQmlExpression
37*/
38
39const QQmlScriptStringPrivate* QQmlScriptStringPrivate::get(const QQmlScriptString &script)
40{
41 return script.d.constData();
42}
43
44QQmlScriptString QQmlScriptStringPrivate::create(const QString &script, QQmlContext *context,
45 QObject *scope)
46{
47 return QQmlScriptString(script, context, scope);
48}
49
50/*!
51Constructs an empty instance.
52*/
53QQmlScriptString::QQmlScriptString()
54: d()
55{
56}
57
58/*!
59 \internal
60*/
61QQmlScriptString::QQmlScriptString(const QString &script, QQmlContext *context, QObject *scope)
62: d(new QQmlScriptStringPrivate)
63{
64 d->script = script;
65 d->context = context;
66 d->scope = scope;
67}
68
69/*!
70Copies \a other.
71*/
72QQmlScriptString::QQmlScriptString(const QQmlScriptString &other)
73: d(other.d)
74{
75}
76
77/*!
78\internal
79*/
80QQmlScriptString::~QQmlScriptString()
81{
82}
83
84/*!
85Assigns \a other to this.
86*/
87QQmlScriptString &QQmlScriptString::operator=(const QQmlScriptString &other)
88{
89 d = other.d;
90 return *this;
91}
92
93/*!
94Returns \c true if this and the \a other QQmlScriptString objects are equal.
95
96\sa operator!=()
97*/
98bool QQmlScriptString::operator==(const QQmlScriptString &other) const
99{
100 if (d == other.d)
101 return true;
102 if (!d || !other.d)
103 return false;
104
105 if (d->isNumberLiteral || other.d->isNumberLiteral)
106 return d->isNumberLiteral && other.d->isNumberLiteral && d->numberValue == other.d->numberValue;
107
108 if (d->isStringLiteral || other.d->isStringLiteral)
109 return d->isStringLiteral && other.d->isStringLiteral && d->script == other.d->script;
110
111 if (d->script == QLatin1String("true") ||
112 d->script == QLatin1String("false") ||
113 d->script == QLatin1String("undefined") ||
114 d->script == QLatin1String("null"))
115 return d->script == other.d->script;
116
117 return d->context == other.d->context &&
118 d->scope == other.d->scope &&
119 d->script == other.d->script &&
120 d->bindingId == other.d->bindingId;
121}
122
123/*!
124Returns \c true if this and the \a other QQmlScriptString objects are different.
125
126\sa operator==()
127*/
128bool QQmlScriptString::operator!=(const QQmlScriptString &other) const
129{
130 return !operator==(other);
131}
132
133/*!
134Returns whether the QQmlScriptString is empty.
135*/
136bool QQmlScriptString::isEmpty() const
137{
138 if (!d)
139 return true;
140 if (!d->script.isEmpty())
141 return false;
142 return d->bindingId == -1;
143}
144
145/*!
146Returns whether the content of the QQmlScriptString is the \c undefined literal.
147*/
148bool QQmlScriptString::isUndefinedLiteral() const
149{
150 return d && d->script == QLatin1String("undefined");
151}
152
153/*!
154Returns whether the content of the QQmlScriptString is the \c null literal.
155*/
156bool QQmlScriptString::isNullLiteral() const
157{
158 return d && d->script == QLatin1String("null");
159}
160
161/*!
162If the content of the QQmlScriptString is a string literal, returns that string.
163Otherwise returns a null QString.
164*/
165QString QQmlScriptString::stringLiteral() const
166{
167 if (d && d->isStringLiteral)
168 return d->script.mid(1, d->script.size()-2);
169 return QString();
170}
171
172/*!
173If the content of the QQmlScriptString is a number literal, returns that number and
174sets \a ok to true. Otherwise returns 0.0 and sets \a ok to false.
175*/
176qreal QQmlScriptString::numberLiteral(bool *ok) const
177{
178 if (ok)
179 *ok = d && d->isNumberLiteral;
180 return (d && d->isNumberLiteral) ? d->numberValue : 0.;
181}
182
183/*!
184If the content of the QQmlScriptString is a boolean literal, returns the boolean value and
185sets \a ok to true. Otherwise returns false and sets \a ok to false.
186*/
187bool QQmlScriptString::booleanLiteral(bool *ok) const
188{
189 bool isTrue = d && d->script == QLatin1String("true");
190 bool isFalse = !isTrue && d && d->script == QLatin1String("false");
191 if (ok)
192 *ok = isTrue || isFalse;
193 return isTrue ? true : false;
194}
195
196QT_END_NAMESPACE
197
198#include "moc_qqmlscriptstring.cpp"
Combined button and popup list for selecting options.