PageRenderTime 81ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/doc/src/snippets/code/src_script_qscriptengine.cpp

https://bitbucket.org/kasimling/qt
C++ | 332 lines | 154 code | 73 blank | 105 comment | 9 complexity | 9340caf3ddad97ebf870e7e7b04e3b62 MD5 | raw file
  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 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 documentation of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:BSD$
  10. ** You may use this file under the terms of the BSD license as follows:
  11. **
  12. ** "Redistribution and use in source and binary forms, with or without
  13. ** modification, are permitted provided that the following conditions are
  14. ** met:
  15. ** * Redistributions of source code must retain the above copyright
  16. ** notice, this list of conditions and the following disclaimer.
  17. ** * Redistributions in binary form must reproduce the above copyright
  18. ** notice, this list of conditions and the following disclaimer in
  19. ** the documentation and/or other materials provided with the
  20. ** distribution.
  21. ** * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
  22. ** the names of its contributors may be used to endorse or promote
  23. ** products derived from this software without specific prior written
  24. ** permission.
  25. **
  26. ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  27. ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  28. ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  29. ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  30. ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  31. ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  32. ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  33. ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  34. ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  35. ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
  37. ** $QT_END_LICENSE$
  38. **
  39. ****************************************************************************/
  40. //! [0]
  41. QScriptEngine myEngine;
  42. QScriptValue three = myEngine.evaluate("1 + 2");
  43. //! [0]
  44. //! [1]
  45. QScriptValue fun = myEngine.evaluate("(function(a, b) { return a + b; })");
  46. QScriptValueList args;
  47. args << 1 << 2;
  48. QScriptValue threeAgain = fun.call(QScriptValue(), args);
  49. //! [1]
  50. //! [2]
  51. QString fileName = "helloworld.qs";
  52. QFile scriptFile(fileName);
  53. if (!scriptFile.open(QIODevice::ReadOnly))
  54. // handle error
  55. QTextStream stream(&scriptFile);
  56. QString contents = stream.readAll();
  57. scriptFile.close();
  58. myEngine.evaluate(contents, fileName);
  59. //! [2]
  60. //! [3]
  61. myEngine.globalObject().setProperty("myNumber", 123);
  62. ...
  63. QScriptValue myNumberPlusOne = myEngine.evaluate("myNumber + 1");
  64. //! [3]
  65. //! [4]
  66. QScriptValue result = myEngine.evaluate(...);
  67. if (myEngine.hasUncaughtException()) {
  68. int line = myEngine.uncaughtExceptionLineNumber();
  69. qDebug() << "uncaught exception at line" << line << ":" << result.toString();
  70. }
  71. //! [4]
  72. //! [5]
  73. QPushButton button;
  74. QScriptValue scriptButton = myEngine.newQObject(&button);
  75. myEngine.globalObject().setProperty("button", scriptButton);
  76. myEngine.evaluate("button.checkable = true");
  77. qDebug() << scriptButton.property("checkable").toBoolean();
  78. scriptButton.property("show").call(); // call the show() slot
  79. //! [5]
  80. //! [6]
  81. QScriptValue myAdd(QScriptContext *context, QScriptEngine *engine)
  82. {
  83. QScriptValue a = context->argument(0);
  84. QScriptValue b = context->argument(1);
  85. return a.toNumber() + b.toNumber();
  86. }
  87. //! [6]
  88. //! [7]
  89. QScriptValue fun = myEngine.newFunction(myAdd);
  90. myEngine.globalObject().setProperty("myAdd", fun);
  91. //! [7]
  92. //! [8]
  93. QScriptValue result = myEngine.evaluate("myAdd(myNumber, 1)");
  94. //! [8]
  95. //! [9]
  96. QScriptValue Foo(QScriptContext *context, QScriptEngine *engine)
  97. {
  98. if (context->calledAsConstructor()) {
  99. // initialize the new object
  100. context->thisObject().setProperty("bar", ...);
  101. // ...
  102. // return a non-object value to indicate that the
  103. // thisObject() should be the result of the "new Foo()" expression
  104. return engine->undefinedValue();
  105. } else {
  106. // not called as "new Foo()", just "Foo()"
  107. // create our own object and return that one
  108. QScriptValue object = engine->newObject();
  109. object.setPrototype(context->callee().property("prototype"));
  110. object.setProperty("baz", ...);
  111. return object;
  112. }
  113. }
  114. ...
  115. QScriptValue fooProto = engine->newObject();
  116. fooProto.setProperty("whatever", ...);
  117. engine->globalObject().setProperty("Foo", engine->newFunction(Foo, fooProto));
  118. //! [9]
  119. //! [10]
  120. class Bar { ... };
  121. Q_DECLARE_METATYPE(Bar)
  122. QScriptValue constructBar(QScriptContext *context, QScriptEngine *engine)
  123. {
  124. Bar bar;
  125. // initialize from arguments in context, if desired
  126. ...
  127. return engine->toScriptValue(bar);
  128. }
  129. class BarPrototype : public QObject, public QScriptable
  130. {
  131. // provide the scriptable interface of this type using slots and properties
  132. ...
  133. };
  134. ...
  135. // create and register the Bar prototype and constructor in the engine
  136. BarPrototype *barPrototypeObject = new BarPrototype(...);
  137. QScriptValue barProto = engine->newQObject(barPrototypeObject);
  138. engine->setDefaultPrototype(qMetaTypeId<Bar>, barProto);
  139. QScriptValue barCtor = engine->newFunction(constructBar, barProto);
  140. engine->globalObject().setProperty("Bar", barCtor);
  141. //! [10]
  142. //! [11]
  143. static QScriptValue getSetFoo(QScriptContext *context, QScriptEngine *engine)
  144. {
  145. QScriptValue callee = context->callee();
  146. if (context->argumentCount() == 1) // writing?
  147. callee.setProperty("value", context->argument(0));
  148. return callee.property("value");
  149. }
  150. ....
  151. QScriptValue object = engine.newObject();
  152. object.setProperty("foo", engine.newFunction(getSetFoo),
  153. QScriptValue::PropertyGetter | QScriptValue::PropertySetter);
  154. //! [11]
  155. //! [12]
  156. QScriptValue object = engine.newObject();
  157. object.setProperty("foo", engine.newFunction(getFoo), QScriptValue::PropertyGetter);
  158. object.setProperty("foo", engine.newFunction(setFoo), QScriptValue::PropertySetter);
  159. //! [12]
  160. //! [13]
  161. Q_SCRIPT_DECLARE_QMETAOBJECT(QLineEdit, QWidget*)
  162. ...
  163. QScriptValue lineEditClass = engine.scriptValueFromQMetaObject<QLineEdit>();
  164. engine.globalObject().setProperty("QLineEdit", lineEditClass);
  165. //! [13]
  166. //! [14]
  167. if (hello && world)
  168. print("hello world");
  169. //! [14]
  170. //! [15]
  171. if (hello &&
  172. //! [15]
  173. //! [16]
  174. 0 = 0
  175. //! [16]
  176. //! [17]
  177. ./test.js
  178. //! [17]
  179. //! [18]
  180. foo["bar"]
  181. //! [18]
  182. //! [19]
  183. QScriptEngine engine;
  184. QScriptContext *context = engine.pushContext();
  185. context->activationObject().setProperty("myArg", 123);
  186. engine.evaluate("var tmp = myArg + 42");
  187. ...
  188. engine.popContext();
  189. //! [19]
  190. //! [20]
  191. struct MyStruct {
  192. int x;
  193. int y;
  194. };
  195. //! [20]
  196. //! [21]
  197. Q_DECLARE_METATYPE(MyStruct)
  198. //! [21]
  199. //! [22]
  200. QScriptValue toScriptValue(QScriptEngine *engine, const MyStruct &s)
  201. {
  202. QScriptValue obj = engine->newObject();
  203. obj.setProperty("x", s.x);
  204. obj.setProperty("y", s.y);
  205. return obj;
  206. }
  207. void fromScriptValue(const QScriptValue &obj, MyStruct &s)
  208. {
  209. s.x = obj.property("x").toInt32();
  210. s.y = obj.property("y").toInt32();
  211. }
  212. //! [22]
  213. //! [23]
  214. qScriptRegisterMetaType(engine, toScriptValue, fromScriptValue);
  215. //! [23]
  216. //! [24]
  217. MyStruct s = qscriptvalue_cast<MyStruct>(context->argument(0));
  218. ...
  219. MyStruct s2;
  220. s2.x = s.x + 10;
  221. s2.y = s.y + 20;
  222. QScriptValue v = engine->toScriptValue(s2);
  223. //! [24]
  224. //! [25]
  225. QScriptValue createMyStruct(QScriptContext *, QScriptEngine *engine)
  226. {
  227. MyStruct s;
  228. s.x = 123;
  229. s.y = 456;
  230. return engine->toScriptValue(s);
  231. }
  232. ...
  233. QScriptValue ctor = engine.newFunction(createMyStruct);
  234. engine.globalObject().setProperty("MyStruct", ctor);
  235. //! [25]
  236. //! [26]
  237. Q_DECLARE_METATYPE(QVector<int>)
  238. ...
  239. qScriptRegisterSequenceMetaType<QVector<int> >(engine);
  240. ...
  241. QVector<int> v = qscriptvalue_cast<QVector<int> >(engine->evaluate("[5, 1, 3, 2]"));
  242. qSort(v.begin(), v.end());
  243. QScriptValue a = engine->toScriptValue(v);
  244. qDebug() << a.toString(); // outputs "[1, 2, 3, 5]"
  245. //! [26]
  246. //! [27]
  247. QScriptValue mySpecialQObjectConstructor(QScriptContext *context,
  248. QScriptEngine *engine)
  249. {
  250. QObject *parent = context->argument(0).toQObject();
  251. QObject *object = new QObject(parent);
  252. return engine->newQObject(object, QScriptEngine::ScriptOwnership);
  253. }
  254. ...
  255. QScriptValue ctor = engine.newFunction(mySpecialQObjectConstructor);
  256. QScriptValue metaObject = engine.newQMetaObject(&QObject::staticMetaObject, ctor);
  257. engine.globalObject().setProperty("QObject", metaObject);
  258. QScriptValue result = engine.evaluate("new QObject()");
  259. //! [27]