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