Merge branch 'master' of git://scm.dev.nokia.troll.no/qt/qtdeclarative
[profile/ivi/qtdeclarative.git] / src / declarative / qml / qdeclarativescriptstring.cpp
1 /****************************************************************************
2 **
3 ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
4 ** All rights reserved.
5 ** Contact: Nokia Corporation (qt-info@nokia.com)
6 **
7 ** This file is part of the QtDeclarative module of the Qt Toolkit.
8 **
9 ** $QT_BEGIN_LICENSE:LGPL$
10 ** GNU Lesser General Public License Usage
11 ** This file may be used under the terms of the GNU Lesser General Public
12 ** License version 2.1 as published by the Free Software Foundation and
13 ** appearing in the file LICENSE.LGPL included in the packaging of this
14 ** file. Please review the following information to ensure the GNU Lesser
15 ** General Public License version 2.1 requirements will be met:
16 ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
17 **
18 ** In addition, as a special exception, Nokia gives you certain additional
19 ** rights. These rights are described in the Nokia Qt LGPL Exception
20 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
21 **
22 ** GNU General Public License Usage
23 ** Alternatively, this file may be used under the terms of the GNU General
24 ** Public License version 3.0 as published by the Free Software Foundation
25 ** and appearing in the file LICENSE.GPL included in the packaging of this
26 ** file. Please review the following information to ensure the GNU General
27 ** Public License version 3.0 requirements will be met:
28 ** http://www.gnu.org/copyleft/gpl.html.
29 **
30 ** Other Usage
31 ** Alternatively, this file may be used in accordance with the terms and
32 ** conditions contained in a signed written agreement between you and Nokia.
33 **
34 **
35 **
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41
42 #include "qdeclarativescriptstring.h"
43
44 QT_BEGIN_NAMESPACE
45
46 class QDeclarativeScriptStringPrivate : public QSharedData
47 {
48 public:
49     QDeclarativeScriptStringPrivate() : context(0), scope(0) {}
50
51     QDeclarativeContext *context;
52     QObject *scope;
53     QString script;
54 };
55
56 /*!
57 \class QDeclarativeScriptString
58 \since 4.7
59 \brief The QDeclarativeScriptString class encapsulates a script and its context.
60
61 QDeclarativeScriptString is used to create QObject properties that accept a script "assignment" from QML.
62
63 Normally, the following QML would result in a binding being established for the \c script
64 property; i.e. \c script would be assigned the value obtained from running \c {myObj.value = Math.max(myValue, 100)}
65
66 \qml
67 MyType {
68     script: myObj.value = Math.max(myValue, 100)
69 }
70 \endqml
71
72 If instead the property had a type of QDeclarativeScriptString,
73 the script itself -- \e {myObj.value = Math.max(myValue, 100)} -- would be passed to the \c script property
74 and the class could choose how to handle it. Typically, the class will evaluate
75 the script at some later time using a QDeclarativeExpression.
76
77 \code
78 QDeclarativeExpression expr(scriptString.context(), scriptString.script(), scriptStr.scopeObject());
79 expr.value();
80 \endcode
81
82 \sa QDeclarativeExpression
83 */
84
85 /*!
86 Constructs an empty instance.
87 */
88 QDeclarativeScriptString::QDeclarativeScriptString()
89 :  d(new QDeclarativeScriptStringPrivate)
90 {
91 }
92
93 /*!
94 Copies \a other.
95 */
96 QDeclarativeScriptString::QDeclarativeScriptString(const QDeclarativeScriptString &other)
97 : d(other.d)
98 {
99 }
100
101 /*!
102 \internal
103 */
104 QDeclarativeScriptString::~QDeclarativeScriptString()
105 {
106 }
107
108 /*!
109 Assigns \a other to this.
110 */
111 QDeclarativeScriptString &QDeclarativeScriptString::operator=(const QDeclarativeScriptString &other)
112 {
113     d = other.d;
114     return *this;
115 }
116
117 /*!
118 Returns the context for the script.
119 */
120 QDeclarativeContext *QDeclarativeScriptString::context() const
121 {
122     return d->context;
123 }
124
125 /*!
126 Sets the \a context for the script.
127 */
128 void QDeclarativeScriptString::setContext(QDeclarativeContext *context)
129 {
130     d->context = context;
131 }
132
133 /*!
134 Returns the scope object for the script.
135 */
136 QObject *QDeclarativeScriptString::scopeObject() const
137 {
138     return d->scope;
139 }
140
141 /*!
142 Sets the scope \a object for the script.
143 */
144 void QDeclarativeScriptString::setScopeObject(QObject *object)
145 {
146     d->scope = object;
147 }
148
149 /*!
150 Returns the script text.
151 */
152 QString QDeclarativeScriptString::script() const
153 {
154     return d->script;
155 }
156
157 /*!
158 Sets the \a script text.
159 */
160 void QDeclarativeScriptString::setScript(const QString &script)
161 {
162     d->script = script;
163 }
164
165 QT_END_NAMESPACE
166