1 | /****************************************************************************
|
---|
2 | **
|
---|
3 | ** Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies).
|
---|
4 | ** All rights reserved.
|
---|
5 | ** Contact: Nokia Corporation ([email protected])
|
---|
6 | **
|
---|
7 | ** This file is part of the documentation of the Qt Toolkit.
|
---|
8 | **
|
---|
9 | ** $QT_BEGIN_LICENSE:FDL$
|
---|
10 | ** Commercial Usage
|
---|
11 | ** Licensees holding valid Qt Commercial licenses may use this file in
|
---|
12 | ** accordance with the Qt Commercial License Agreement provided with the
|
---|
13 | ** Software or, alternatively, in accordance with the terms contained in a
|
---|
14 | ** written agreement between you and Nokia.
|
---|
15 | **
|
---|
16 | ** GNU Free Documentation License
|
---|
17 | ** Alternatively, this file may be used under the terms of the GNU Free
|
---|
18 | ** Documentation License version 1.3 as published by the Free Software
|
---|
19 | ** Foundation and appearing in the file included in the packaging of this
|
---|
20 | ** file.
|
---|
21 | **
|
---|
22 | ** If you have questions regarding the use of this file, please contact
|
---|
23 | ** Nokia at [email protected].
|
---|
24 | ** $QT_END_LICENSE$
|
---|
25 | **
|
---|
26 | ****************************************************************************/
|
---|
27 |
|
---|
28 | /*!
|
---|
29 | \page qtscriptextensions.html
|
---|
30 | \title Creating QtScript Extensions
|
---|
31 | \brief A guide to creating and using QtScript extensions.
|
---|
32 |
|
---|
33 | QtScript extensions can make additional functionality available to scripts
|
---|
34 | evaluated by a QScriptEngine. Extensions are imported by calling
|
---|
35 | the QScriptEngine::importExtension() function.
|
---|
36 |
|
---|
37 | There are three ways to create an extension:
|
---|
38 |
|
---|
39 | \list
|
---|
40 | \o Subclass QScriptExtensionPlugin and implement the desired functionality.
|
---|
41 | \o Implement the functionality in a script file.
|
---|
42 | \o Use a hybrid approach, where part of the functionality is implemented in a
|
---|
43 | QScriptExtensionPlugin, and part is implemented in a script file.
|
---|
44 | \endlist
|
---|
45 |
|
---|
46 | The (dot-qualified) extension name is used to determine the path (relative to
|
---|
47 | the application's plugin path) where QScriptEngine will look for the script
|
---|
48 | file that will initialize the extension; if a file called \c{__init__.js}
|
---|
49 | (usually located in \c{[application plugin path]/script/foo/}) is
|
---|
50 | found in the corresponding folder, its contents will be evaluated by the engine
|
---|
51 | when the extension is imported.
|
---|
52 | As an example, if the extension is called \c{"foo.bar.baz"}, the engine will look
|
---|
53 | for \c{__init__.js} in \c{foo/bar/baz}. Additionally, before importing
|
---|
54 | \c{"foo.bar.baz"}, the engine will ensure that the extensions \c{"foo"} and \c{"foo.bar"}
|
---|
55 | are imported, locating and evaluating the corresponding \c{__init__.js}
|
---|
56 | in the same manner (in folders \c{foo} and \c{foo/bar}, respectively).
|
---|
57 |
|
---|
58 | The contents of \c{__init__.js} are evaluated in a new QScriptContext,
|
---|
59 | as if it were the body of a function. The engine's Global Object acts as
|
---|
60 | the \c{this} object. The following local variables are initially available
|
---|
61 | to the script:
|
---|
62 |
|
---|
63 | \list
|
---|
64 | \o \bold{__extension__}: The name of the extension (e.g. \c{"foo.bar.baz"}).
|
---|
65 | \o \bold{__setupPackage__}: A convenience function for setting up a "namespace" in the script environment. A typical application is to call \c{__setupPackage__()} with \c{__extension__} as argument; e.g. \c{__setupPackage__("foo.bar.baz")} would ensure that the object chain represented by the expression \c{foo.bar.baz} exists in the script environment. (This function is semantically equivalent to QScriptExtensionPlugin::setupPackage().)
|
---|
66 | \o \bold{__postInit__}: By default, this variable is undefined. If you assign a function to it, that function will be called \bold{after} the C++ plugin's initialize() function has been called. You can use this to perform further initialization that depends on e.g. native functions that the C++ plugin registers.
|
---|
67 | \endlist
|
---|
68 |
|
---|
69 | An example of a simple \c{__init__.js}:
|
---|
70 |
|
---|
71 | \snippet doc/src/snippets/code/doc_src_qtscriptextensions.qdoc 0
|
---|
72 |
|
---|
73 | QScriptEngine will look for a QScriptExtensionPlugin that provides
|
---|
74 | the relevant extension by querying each plugin for its keys()
|
---|
75 | until a match is found. The plugin's initialize() function will be
|
---|
76 | called \bold{after} the relevant \c{__init__.js} (if any) has been
|
---|
77 | evaluated.
|
---|
78 |
|
---|
79 | Continuining with the example of our imaginary extension \c{"foo.bar.baz"},
|
---|
80 | the following steps will be performed by QScriptEngine::importExtension():
|
---|
81 |
|
---|
82 | \list
|
---|
83 | \o If it exists, \c{foo/__init__.js} is evaluated.
|
---|
84 | \o If a plugin with \c{"foo"} in its list of keys is found, its initialize() function is called with \c{"foo"} as key.
|
---|
85 | \o If it exists, \c{foo/bar/__init__.js} is evaluated.
|
---|
86 | \o If a plugin with \c{"foo.bar"} in its list of keys is found, its initialize() function is called with \c{"foo.bar"} as key.
|
---|
87 | \o If it exists, \c{foo/bar/baz/__init__.js} is evaluated.
|
---|
88 | \o If a plugin with "foo.bar.baz" in its list of keys is found, its initialize() function is called with \c{"foo.bar.baz"} as key.
|
---|
89 | \endlist
|
---|
90 |
|
---|
91 | \section1 Static Extensions
|
---|
92 |
|
---|
93 | When an extension is compiled and linked into your application as a
|
---|
94 | static plugin, Qt Script will look for the optional \c{__init__.js}
|
---|
95 | script in a resource, prefixed by \c{:/qtscriptextension}. For example,
|
---|
96 | if the extension key is "foo.bar", Qt Script will evaluate the contents
|
---|
97 | of the file \c{:/qtscriptextension/foo/bar/__init__.js}, if it
|
---|
98 | exists. Note that if the resource is built into the plugin, you may
|
---|
99 | need to use the Q_INIT_RESOURCE() macro to initialize the resource
|
---|
100 | before importing the extension.
|
---|
101 | */
|
---|