Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Toggle main menu visibility
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
5
#
include
"qqmlscriptstring.h"
6
#
include
"qqmlscriptstring_p.h"
7
8
QT_BEGIN_NAMESPACE
9
10
/*!
11
\class QQmlScriptString
12
\brief The QQmlScriptString class encapsulates a script and its context.
13
\inmodule QtQml
14
15
QQmlScriptString is used to create QObject properties that accept a script "assignment" from QML.
16
17
Normally, the following QML would result in a binding being established for the \c script
18
property; i.e. \c script would be assigned the value obtained from running \c {myObj.value = Math.max(myValue, 100)}
19
20
\qml
21
MyType {
22
script: myObj.value = Math.max(myValue, 100)
23
}
24
\endqml
25
26
If instead the property had a type of QQmlScriptString,
27
the script itself -- \e {myObj.value = Math.max(myValue, 100)} -- would be passed to the \c script property
28
and the class could choose how to handle it. Typically, the class will evaluate
29
the script at some later time using a QQmlExpression.
30
31
\code
32
QQmlExpression expr(scriptString);
33
expr.evaluate();
34
\endcode
35
36
\note A QQmlScriptString can always be evaluated through a QQmlExpression, but
37
its source code is not guaranteed to be retrievable. If the QML module is
38
compiled ahead of time, the source code of non-literal script strings may be
39
discarded and cannot be recovered via \l QQmlExpression::expression(). Avoid
40
relying on the textual form of a script string; evaluate it instead.
41
42
\sa QQmlExpression
43
*/
44
45
const
QQmlScriptStringPrivate* QQmlScriptStringPrivate::get(
const
QQmlScriptString &script)
46
{
47
return
script.d.constData();
48
}
49
50
QQmlScriptString QQmlScriptStringPrivate::create(
const
QString &script, QQmlContext *context,
51
QObject *scope)
52
{
53
return
QQmlScriptString(script, context, scope);
54
}
55
56
/*!
57
Constructs an empty instance.
58
*/
59
QQmlScriptString::QQmlScriptString()
60
: d()
61
{
62
}
63
64
/*!
65
\internal
66
*/
67
QQmlScriptString::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
/*!
76
Copies \a other.
77
*/
78
QQmlScriptString::QQmlScriptString(
const
QQmlScriptString &other)
79
: d(other.d)
80
{
81
}
82
83
/*!
84
\internal
85
*/
86
QQmlScriptString::~QQmlScriptString()
87
{
88
}
89
90
/*!
91
Assigns \a other to this.
92
*/
93
QQmlScriptString &QQmlScriptString::operator=(
const
QQmlScriptString &other)
94
{
95
d = other.d;
96
return
*
this
;
97
}
98
99
/*!
100
Returns \c true if this and the \a other QQmlScriptString objects are equal.
101
102
\sa operator!=()
103
*/
104
bool
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
/*!
130
Returns \c true if this and the \a other QQmlScriptString objects are different.
131
132
\sa operator==()
133
*/
134
bool
QQmlScriptString::operator!=(
const
QQmlScriptString &other)
const
135
{
136
return
!operator==(other);
137
}
138
139
/*!
140
Returns whether the QQmlScriptString is empty.
141
*/
142
bool
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
/*!
152
Returns whether the content of the QQmlScriptString is the \c undefined literal.
153
*/
154
bool
QQmlScriptString::isUndefinedLiteral()
const
155
{
156
return
d && d->script == QLatin1String(
"undefined"
);
157
}
158
159
/*!
160
Returns whether the content of the QQmlScriptString is the \c null literal.
161
*/
162
bool
QQmlScriptString::isNullLiteral()
const
163
{
164
return
d && d->script == QLatin1String(
"null"
);
165
}
166
167
/*!
168
If the content of the QQmlScriptString is a string literal, returns that string.
169
Otherwise returns a null QString.
170
*/
171
QString 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
/*!
179
If the content of the QQmlScriptString is a number literal, returns that number and
180
sets \a ok to true. Otherwise returns 0.0 and sets \a ok to false.
181
*/
182
qreal 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
/*!
190
If the content of the QQmlScriptString is a boolean literal, returns the boolean value and
191
sets \a ok to true. Otherwise returns false and sets \a ok to false.
192
*/
193
bool
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
202
QT_END_NAMESPACE
203
204
#
include
"moc_qqmlscriptstring.cpp"
QT_BEGIN_NAMESPACE
Combined button and popup list for selecting options.
Definition
qsequentialanimationgroup.cpp:47
qtdeclarative
src
qml
qml
qqmlscriptstring.cpp
Generated on
for Qt by
1.18.0