1 | /**************************************************************************** |
2 | ** |
3 | ** Copyright (C) 2016 The Qt Company Ltd. |
4 | ** Contact: https://www.qt.io/licensing/ |
5 | ** |
6 | ** This file is part of the QtQml module of the Qt Toolkit. |
7 | ** |
8 | ** $QT_BEGIN_LICENSE:LGPL$ |
9 | ** Commercial License Usage |
10 | ** Licensees holding valid commercial Qt licenses may use this file in |
11 | ** accordance with the commercial license agreement provided with the |
12 | ** Software or, alternatively, in accordance with the terms contained in |
13 | ** a written agreement between you and The Qt Company. For licensing terms |
14 | ** and conditions see https://www.qt.io/terms-conditions. For further |
15 | ** information use the contact form at https://www.qt.io/contact-us. |
16 | ** |
17 | ** GNU Lesser General Public License Usage |
18 | ** Alternatively, this file may be used under the terms of the GNU Lesser |
19 | ** General Public License version 3 as published by the Free Software |
20 | ** Foundation and appearing in the file LICENSE.LGPL3 included in the |
21 | ** packaging of this file. Please review the following information to |
22 | ** ensure the GNU Lesser General Public License version 3 requirements |
23 | ** will be met: https://www.gnu.org/licenses/lgpl-3.0.html. |
24 | ** |
25 | ** GNU General Public License Usage |
26 | ** Alternatively, this file may be used under the terms of the GNU |
27 | ** General Public License version 2.0 or (at your option) the GNU General |
28 | ** Public license version 3 or any later version approved by the KDE Free |
29 | ** Qt Foundation. The licenses are as published by the Free Software |
30 | ** Foundation and appearing in the file LICENSE.GPL2 and LICENSE.GPL3 |
31 | ** included in the packaging of this file. Please review the following |
32 | ** information to ensure the GNU General Public License requirements will |
33 | ** be met: https://www.gnu.org/licenses/gpl-2.0.html and |
34 | ** https://www.gnu.org/licenses/gpl-3.0.html. |
35 | ** |
36 | ** $QT_END_LICENSE$ |
37 | ** |
38 | ****************************************************************************/ |
39 | |
40 | #include "qqmlscriptstring.h" |
41 | #include "qqmlscriptstring_p.h" |
42 | |
43 | QT_BEGIN_NAMESPACE |
44 | |
45 | /*! |
46 | \class QQmlScriptString |
47 | \brief The QQmlScriptString class encapsulates a script and its context. |
48 | \inmodule QtQml |
49 | |
50 | QQmlScriptString is used to create QObject properties that accept a script "assignment" from QML. |
51 | |
52 | Normally, the following QML would result in a binding being established for the \c script |
53 | property; i.e. \c script would be assigned the value obtained from running \c {myObj.value = Math.max(myValue, 100)} |
54 | |
55 | \qml |
56 | MyType { |
57 | script: myObj.value = Math.max(myValue, 100) |
58 | } |
59 | \endqml |
60 | |
61 | If instead the property had a type of QQmlScriptString, |
62 | the script itself -- \e {myObj.value = Math.max(myValue, 100)} -- would be passed to the \c script property |
63 | and the class could choose how to handle it. Typically, the class will evaluate |
64 | the script at some later time using a QQmlExpression. |
65 | |
66 | \code |
67 | QQmlExpression expr(scriptString); |
68 | expr.evaluate(); |
69 | \endcode |
70 | |
71 | \sa QQmlExpression |
72 | */ |
73 | |
74 | const QQmlScriptStringPrivate* QQmlScriptStringPrivate::get(const QQmlScriptString &script) |
75 | { |
76 | return script.d.constData(); |
77 | } |
78 | |
79 | /*! |
80 | Constructs an empty instance. |
81 | */ |
82 | QQmlScriptString::QQmlScriptString() |
83 | : d(new QQmlScriptStringPrivate) |
84 | { |
85 | } |
86 | |
87 | /*! |
88 | \internal |
89 | */ |
90 | QQmlScriptString::QQmlScriptString(const QString &script, QQmlContext *context, QObject *scope) |
91 | : d(new QQmlScriptStringPrivate) |
92 | { |
93 | d->script = script; |
94 | d->context = context; |
95 | d->scope = scope; |
96 | } |
97 | |
98 | /*! |
99 | Copies \a other. |
100 | */ |
101 | QQmlScriptString::QQmlScriptString(const QQmlScriptString &other) |
102 | : d(other.d) |
103 | { |
104 | } |
105 | |
106 | /*! |
107 | \internal |
108 | */ |
109 | QQmlScriptString::~QQmlScriptString() |
110 | { |
111 | } |
112 | |
113 | /*! |
114 | Assigns \a other to this. |
115 | */ |
116 | QQmlScriptString &QQmlScriptString::operator=(const QQmlScriptString &other) |
117 | { |
118 | d = other.d; |
119 | return *this; |
120 | } |
121 | |
122 | /*! |
123 | Returns \c true if this and the \a other QQmlScriptString objects are equal. |
124 | |
125 | \sa operator!=() |
126 | */ |
127 | bool QQmlScriptString::operator==(const QQmlScriptString &other) const |
128 | { |
129 | if (d == other.d) |
130 | return true; |
131 | |
132 | if (d->isNumberLiteral || other.d->isNumberLiteral) |
133 | return d->isNumberLiteral && other.d->isNumberLiteral && d->numberValue == other.d->numberValue; |
134 | |
135 | if (d->isStringLiteral || other.d->isStringLiteral) |
136 | return d->isStringLiteral && other.d->isStringLiteral && d->script == other.d->script; |
137 | |
138 | if (d->script == QLatin1String("true" ) || |
139 | d->script == QLatin1String("false" ) || |
140 | d->script == QLatin1String("undefined" ) || |
141 | d->script == QLatin1String("null" )) |
142 | return d->script == other.d->script; |
143 | |
144 | return d->context == other.d->context && |
145 | d->scope == other.d->scope && |
146 | d->script == other.d->script && |
147 | d->bindingId == other.d->bindingId; |
148 | } |
149 | |
150 | /*! |
151 | Returns \c true if this and the \a other QQmlScriptString objects are different. |
152 | |
153 | \sa operator==() |
154 | */ |
155 | bool QQmlScriptString::operator!=(const QQmlScriptString &other) const |
156 | { |
157 | return !operator==(other); |
158 | } |
159 | |
160 | /*! |
161 | Returns whether the QQmlScriptString is empty. |
162 | */ |
163 | bool QQmlScriptString::isEmpty() const |
164 | { |
165 | if (!d->script.isEmpty()) |
166 | return false; |
167 | return d->bindingId == -1; |
168 | } |
169 | |
170 | /*! |
171 | Returns whether the content of the QQmlScriptString is the \c undefined literal. |
172 | */ |
173 | bool QQmlScriptString::isUndefinedLiteral() const |
174 | { |
175 | return d->script == QLatin1String("undefined" ); |
176 | } |
177 | |
178 | /*! |
179 | Returns whether the content of the QQmlScriptString is the \c null literal. |
180 | */ |
181 | bool QQmlScriptString::isNullLiteral() const |
182 | { |
183 | return d->script == QLatin1String("null" ); |
184 | } |
185 | |
186 | /*! |
187 | If the content of the QQmlScriptString is a string literal, returns that string. |
188 | Otherwise returns a null QString. |
189 | */ |
190 | QString QQmlScriptString::stringLiteral() const |
191 | { |
192 | if (d->isStringLiteral) |
193 | return d->script.mid(position: 1, n: d->script.length()-2); |
194 | return QString(); |
195 | } |
196 | |
197 | /*! |
198 | If the content of the QQmlScriptString is a number literal, returns that number and |
199 | sets \a ok to true. Otherwise returns 0.0 and sets \a ok to false. |
200 | */ |
201 | qreal QQmlScriptString::numberLiteral(bool *ok) const |
202 | { |
203 | if (ok) |
204 | *ok = d->isNumberLiteral; |
205 | return d->isNumberLiteral ? d->numberValue : 0.; |
206 | } |
207 | |
208 | /*! |
209 | If the content of the QQmlScriptString is a boolean literal, returns the boolean value and |
210 | sets \a ok to true. Otherwise returns false and sets \a ok to false. |
211 | */ |
212 | bool QQmlScriptString::booleanLiteral(bool *ok) const |
213 | { |
214 | bool isTrue = d->script == QLatin1String("true" ); |
215 | bool isFalse = !isTrue && d->script == QLatin1String("false" ); |
216 | if (ok) |
217 | *ok = isTrue || isFalse; |
218 | return isTrue ? true : false; |
219 | } |
220 | |
221 | QT_END_NAMESPACE |
222 | |
223 | |