src/declarative/qml/qdeclarativescriptstring.cpp
changeset 30 5dc02b23752f
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 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 ** No Commercial Usage
       
    11 ** This file contains pre-release code and may not be distributed.
       
    12 ** You may use this file in accordance with the terms and conditions
       
    13 ** contained in the Technology Preview License Agreement accompanying
       
    14 ** this package.
       
    15 **
       
    16 ** GNU Lesser General Public License Usage
       
    17 ** Alternatively, this file may be used under the terms of the GNU Lesser
       
    18 ** General Public License version 2.1 as published by the Free Software
       
    19 ** Foundation and appearing in the file LICENSE.LGPL included in the
       
    20 ** packaging of this file.  Please review the following information to
       
    21 ** ensure the GNU Lesser General Public License version 2.1 requirements
       
    22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
       
    23 **
       
    24 ** In addition, as a special exception, Nokia gives you certain additional
       
    25 ** rights.  These rights are described in the Nokia Qt LGPL Exception
       
    26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
       
    27 **
       
    28 ** If you have questions regarding the use of this file, please contact
       
    29 ** Nokia at qt-info@nokia.com.
       
    30 **
       
    31 **
       
    32 **
       
    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