| 1 | /****************************************************************************
|
|---|
| 2 | **
|
|---|
| 3 | ** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
|
|---|
| 4 | ** Contact: Qt Software Information (qt-info@nokia.com)
|
|---|
| 5 | **
|
|---|
| 6 | ** This file is part of the QtScript module of the Qt Toolkit.
|
|---|
| 7 | **
|
|---|
| 8 | ** $QT_BEGIN_LICENSE:LGPL$
|
|---|
| 9 | ** Commercial Usage
|
|---|
| 10 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
|---|
| 11 | ** accordance with the Qt Commercial License Agreement provided with the
|
|---|
| 12 | ** Software or, alternatively, in accordance with the terms contained in
|
|---|
| 13 | ** a written agreement between you and Nokia.
|
|---|
| 14 | **
|
|---|
| 15 | ** GNU Lesser General Public License Usage
|
|---|
| 16 | ** Alternatively, this file may be used under the terms of the GNU Lesser
|
|---|
| 17 | ** General Public License version 2.1 as published by the Free Software
|
|---|
| 18 | ** Foundation and appearing in the file LICENSE.LGPL included in the
|
|---|
| 19 | ** packaging of this file. Please review the following information to
|
|---|
| 20 | ** ensure the GNU Lesser General Public License version 2.1 requirements
|
|---|
| 21 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
|
|---|
| 22 | **
|
|---|
| 23 | ** In addition, as a special exception, Nokia gives you certain
|
|---|
| 24 | ** additional rights. These rights are described in the Nokia Qt LGPL
|
|---|
| 25 | ** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
|
|---|
| 26 | ** package.
|
|---|
| 27 | **
|
|---|
| 28 | ** GNU General Public License Usage
|
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU
|
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software
|
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the
|
|---|
| 32 | ** packaging of this file. Please review the following information to
|
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be
|
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html.
|
|---|
| 35 | **
|
|---|
| 36 | ** If you are unsure which license is appropriate for your use, please
|
|---|
| 37 | ** contact the sales department at qt-sales@nokia.com.
|
|---|
| 38 | ** $QT_END_LICENSE$
|
|---|
| 39 | **
|
|---|
| 40 | ****************************************************************************/
|
|---|
| 41 |
|
|---|
| 42 | #ifndef QT_NO_QOBJECT
|
|---|
| 43 |
|
|---|
| 44 | #include "qscriptable.h"
|
|---|
| 45 |
|
|---|
| 46 | #ifndef QT_NO_SCRIPT
|
|---|
| 47 |
|
|---|
| 48 | #include "qscriptable_p.h"
|
|---|
| 49 |
|
|---|
| 50 | #include "qscriptengine.h"
|
|---|
| 51 | #include "qscriptcontext.h"
|
|---|
| 52 | #include "qscriptvalue.h"
|
|---|
| 53 |
|
|---|
| 54 | QT_BEGIN_NAMESPACE
|
|---|
| 55 |
|
|---|
| 56 | /*!
|
|---|
| 57 | \since 4.3
|
|---|
| 58 | \class QScriptable
|
|---|
| 59 |
|
|---|
| 60 | \brief The QScriptable class provides access to the Qt Script environment from Qt C++ member functions.
|
|---|
| 61 |
|
|---|
| 62 | \ingroup script
|
|---|
| 63 | \mainclass
|
|---|
| 64 |
|
|---|
| 65 | With QScriptEngine::newQObject(), you can expose the signals and
|
|---|
| 66 | slots and properties of any QObject (or subclass) to script
|
|---|
| 67 | code. QScriptable augments this functionality by giving your C++
|
|---|
| 68 | members access to the Qt Script environment they are invoked in;
|
|---|
| 69 | conceptually, it is similar to QObject::sender().
|
|---|
| 70 |
|
|---|
| 71 | By subclassing QScriptable, you get the following functions in your
|
|---|
| 72 | class: thisObject(), argumentCount(), argument(), context() and
|
|---|
| 73 | engine(). With these functions, you have full access to the Qt
|
|---|
| 74 | Script environment from the slots and property access functions of
|
|---|
| 75 | your class, when they are invoked from script code.
|
|---|
| 76 |
|
|---|
| 77 | For example, you can throw a Qt Script exception from a slot;
|
|---|
| 78 | manipulate the `this' object associated with the function call;
|
|---|
| 79 | inspect the arguments stored in the QScriptContext to know the
|
|---|
| 80 | "real" arguments passed to the function from script code; and call
|
|---|
| 81 | script functions from your slot.
|
|---|
| 82 |
|
|---|
| 83 | A typical use case of QScriptable is to implement prototype objects
|
|---|
| 84 | for custom C++ types. You define the scriptable interface of your
|
|---|
| 85 | custom type in a QScriptable subclass using properties and slots;
|
|---|
| 86 | then you wrap an instance of your class using
|
|---|
| 87 | QScriptEngine::newQObject(), and finally pass the result to
|
|---|
| 88 | QScriptEngine::setDefaultPrototype(). See the \l{Default Prototypes Example}
|
|---|
| 89 | to see how this can be done.
|
|---|
| 90 |
|
|---|
| 91 | The following is what subclassing QScriptable typically looks
|
|---|
| 92 | like:
|
|---|
| 93 |
|
|---|
| 94 | \snippet doc/src/snippets/code/src_script_qscriptable.cpp 0
|
|---|
| 95 |
|
|---|
| 96 | The only difference from regular QObject subclassing is that you
|
|---|
| 97 | also inherit from QScriptable.
|
|---|
| 98 |
|
|---|
| 99 | In the implementation of your slots, you can then use the functions
|
|---|
| 100 | inherited from QScriptable:
|
|---|
| 101 |
|
|---|
| 102 | \snippet doc/src/snippets/code/src_script_qscriptable.cpp 1
|
|---|
| 103 |
|
|---|
| 104 | \sa {Default Prototypes Example}, QScriptEngine::newFunction()
|
|---|
| 105 | */
|
|---|
| 106 |
|
|---|
| 107 | /*!
|
|---|
| 108 | \internal
|
|---|
| 109 | */
|
|---|
| 110 | QScriptable::QScriptable()
|
|---|
| 111 | : d_ptr(new QScriptablePrivate())
|
|---|
| 112 | {
|
|---|
| 113 | d_ptr->q_ptr = this;
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /*!
|
|---|
| 117 | \internal
|
|---|
| 118 | */
|
|---|
| 119 | QScriptable::~QScriptable()
|
|---|
| 120 | {
|
|---|
| 121 | delete d_ptr;
|
|---|
| 122 | d_ptr = 0;
|
|---|
| 123 | }
|
|---|
| 124 |
|
|---|
| 125 | /*!
|
|---|
| 126 | Returns a pointer to the QScriptEngine associated with the current
|
|---|
| 127 | Qt function call, or 0 if the Qt function was not invoked from
|
|---|
| 128 | script code.
|
|---|
| 129 | */
|
|---|
| 130 | QScriptEngine *QScriptable::engine() const
|
|---|
| 131 | {
|
|---|
| 132 | Q_D(const QScriptable);
|
|---|
| 133 | return d->engine;
|
|---|
| 134 | }
|
|---|
| 135 |
|
|---|
| 136 | /*!
|
|---|
| 137 | Returns a pointer to the QScriptContext associated with the current
|
|---|
| 138 | Qt function call, or 0 if the Qt function was not invoked from
|
|---|
| 139 | script code.
|
|---|
| 140 | */
|
|---|
| 141 | QScriptContext *QScriptable::context() const
|
|---|
| 142 | {
|
|---|
| 143 | if (QScriptEngine *e = engine())
|
|---|
| 144 | return e->currentContext();
|
|---|
| 145 |
|
|---|
| 146 | return 0;
|
|---|
| 147 | }
|
|---|
| 148 |
|
|---|
| 149 | /*!
|
|---|
| 150 | Returns the `this' object associated with the current Qt function
|
|---|
| 151 | call, or an invalid QScriptValue if the Qt function was not invoked
|
|---|
| 152 | from script code.
|
|---|
| 153 | */
|
|---|
| 154 |
|
|---|
| 155 | QScriptValue QScriptable::thisObject() const
|
|---|
| 156 | {
|
|---|
| 157 | if (QScriptContext *c = context())
|
|---|
| 158 | return c->thisObject();
|
|---|
| 159 |
|
|---|
| 160 | return QScriptValue();
|
|---|
| 161 | }
|
|---|
| 162 |
|
|---|
| 163 | /*!
|
|---|
| 164 | Returns the number of arguments passed to the function in this
|
|---|
| 165 | invocation, or -1 if the Qt function was not invoked from script
|
|---|
| 166 | code.
|
|---|
| 167 |
|
|---|
| 168 | \sa argument()
|
|---|
| 169 | */
|
|---|
| 170 | int QScriptable::argumentCount() const
|
|---|
| 171 | {
|
|---|
| 172 | if (QScriptContext *c = context())
|
|---|
| 173 | return c->argumentCount();
|
|---|
| 174 |
|
|---|
| 175 | return -1;
|
|---|
| 176 | }
|
|---|
| 177 |
|
|---|
| 178 | /*!
|
|---|
| 179 | Returns the function argument at the given \a index, or an invalid
|
|---|
| 180 | QScriptValue if the Qt function was not invoked from script code.
|
|---|
| 181 |
|
|---|
| 182 | \sa argumentCount()
|
|---|
| 183 | */
|
|---|
| 184 | QScriptValue QScriptable::argument(int index) const
|
|---|
| 185 | {
|
|---|
| 186 | if (QScriptContext *c = context())
|
|---|
| 187 | return c->argument(index);
|
|---|
| 188 |
|
|---|
| 189 | return QScriptValue();
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | #endif // QT_NO_SCRIPT
|
|---|
| 193 | #endif // QT_NO_QOBJECT
|
|---|
| 194 |
|
|---|
| 195 | QT_END_NAMESPACE
|
|---|