Qt 4.8
qaxscript.h
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the ActiveQt framework of the Qt Toolkit.
7 **
8 ** $QT_BEGIN_LICENSE:BSD$
9 ** You may use this file under the terms of the BSD license as follows:
10 **
11 ** "Redistribution and use in source and binary forms, with or without
12 ** modification, are permitted provided that the following conditions are
13 ** met:
14 ** * Redistributions of source code must retain the above copyright
15 ** notice, this list of conditions and the following disclaimer.
16 ** * Redistributions in binary form must reproduce the above copyright
17 ** notice, this list of conditions and the following disclaimer in
18 ** the documentation and/or other materials provided with the
19 ** distribution.
20 ** * Neither the name of Digia Plc and its Subsidiary(-ies) nor the names
21 ** of its contributors may be used to endorse or promote products derived
22 ** from this software without specific prior written permission.
23 **
24 **
25 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
36 **
37 ** $QT_END_LICENSE$
38 **
39 ****************************************************************************/
40 
41 #ifndef QAXSCRIPT_H
42 #define QAXSCRIPT_H
43 
44 #include <ActiveQt/qaxobject.h>
45 
46 struct IActiveScript;
47 
49 
51 
52 QT_MODULE(ActiveQt)
53 
54 #ifndef QT_NO_WIN_ACTIVEQT
55 
56 class QAxBase;
57 class QAxScript;
58 class QAxScriptSite;
59 class QAxScriptEngine;
60 class QAxScriptManager;
62 
63 class QAxScriptEngine : public QAxObject
64 {
65 public:
66  enum State {
69  Started = 1,
70  Connected = 2,
72  Closed = 4
73  };
74 
77 
78  bool isValid() const;
79  bool hasIntrospection() const;
80 
81  QString scriptLanguage() const;
82 
83  State state() const;
84  void setState(State st);
85 
86  void addItem(const QString &name);
87 
88  long queryInterface(const QUuid &, void**) const;
89 
90 protected:
91  bool initialize(IUnknown** ptr);
92 
93 private:
95  IActiveScript *engine;
96 
98 };
99 
100 class QAxScript : public QObject
101 {
102  Q_OBJECT
103 
104 public:
106  FunctionNames = 0,
107  FunctionSignatures
108  };
109 
110  QAxScript(const QString &name, QAxScriptManager *manager);
111  ~QAxScript();
112 
113  bool load(const QString &code, const QString &language = QString());
114 
115  QStringList functions(FunctionFlags = FunctionNames) const;
116 
117  QString scriptCode() const;
118  QString scriptName() const;
119  QAxScriptEngine *scriptEngine() const;
120 
121  QVariant call(const QString &function, const QVariant &v1 = QVariant(),
122  const QVariant &v2 = QVariant(),
123  const QVariant &v3 = QVariant(),
124  const QVariant &v4 = QVariant(),
125  const QVariant &v5 = QVariant(),
126  const QVariant &v6 = QVariant(),
127  const QVariant &v7 = QVariant(),
128  const QVariant &v8 = QVariant());
129  QVariant call(const QString &function, QList<QVariant> &arguments);
130 
131 Q_SIGNALS:
132  void entered();
133  void finished();
134  void finished(const QVariant &result);
135  void finished(int code, const QString &source,const QString &description, const QString &help);
136  void stateChanged(int state);
137  void error(int code, const QString &description, int sourcePosition, const QString &sourceText);
138 
139 private:
140  friend class QAxScriptSite;
141  friend class QAxScriptEngine;
142 
143  void updateObjects();
144  QAxBase *findObject(const QString &name);
145 
151 };
152 
153 class QAxScriptManager : public QObject
154 {
155  Q_OBJECT
156 
157 public:
159  ~QAxScriptManager();
160 
161  void addObject(QAxBase *object);
162  void addObject(QObject *object);
163 
165  QStringList scriptNames() const;
166  QAxScript *script(const QString &name) const;
167 
168  QAxScript* load(const QString &code, const QString &name, const QString &language);
169  QAxScript* load(const QString &file, const QString &name);
170 
171  QVariant call(const QString &function, const QVariant &v1 = QVariant(),
172  const QVariant &v2 = QVariant(),
173  const QVariant &v3 = QVariant(),
174  const QVariant &v4 = QVariant(),
175  const QVariant &v5 = QVariant(),
176  const QVariant &v6 = QVariant(),
177  const QVariant &v7 = QVariant(),
178  const QVariant &v8 = QVariant());
179  QVariant call(const QString &function, QList<QVariant> &arguments);
180 
181  static bool registerEngine(const QString &name, const QString &extension, const QString &code = QString());
182  static QString scriptFileFilter();
183 
184 Q_SIGNALS:
185  void error(QAxScript *script, int code, const QString &description, int sourcePosition, const QString &sourceText);
186 
187 private Q_SLOTS:
188  void objectDestroyed(QObject *o);
189  void scriptError(int code, const QString &description, int sourcePosition, const QString &sourceText);
190 
191 private:
192  friend class QAxScript;
194 
195  void updateScript(QAxScript*);
196  QAxScript *scriptForFunction(const QString &function) const;
197 };
198 
199 
200 // QAxScript inlines
201 
203 {
204  return script_code;
205 }
206 
208 {
209  return script_name;
210 }
211 
213 {
214  return script_engine;
215 }
216 
217 // QAxScriptEngine inlines
218 
219 inline bool QAxScriptEngine::isValid() const
220 {
221  return engine != 0;
222 }
223 
225 {
226  return script_language;
227 }
228 
229 // QAxScriptManager inlines
230 
232 
234 {
235  QAxBase *wrapper = qax_create_object_wrapper(object);
236  if (!wrapper) {
237  qWarning("QAxScriptMananger::addObject: Class %s not exposed through the QAxFactory",
238  object->metaObject()->className());
239  Q_ASSERT(wrapper);
240  }
241  addObject(wrapper);
242 }
243 
245 #endif // QT_NO_WIN_ACTIVEQT
246 
248 
249 #endif // QAXSCRIPT_H
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
static bool findObject(const QDBusConnectionPrivate::ObjectTreeNode *root, const QString &fullpath, int &usedLength, QDBusConnectionPrivate::ObjectTreeNode &result)
#define QT_MODULE(x)
Definition: qglobal.h:2783
QString script_code
Definition: qaxscript.h:147
QAxBase * qax_create_object_wrapper(QObject *)
#define QT_BEGIN_HEADER
Definition: qglobal.h:136
QString script_language
Definition: qaxscript.h:97
#define error(msg)
FunctionFlags
This FunctionFlags enum describes formatting for function introspection.
Definition: qaxscript.h:105
void addItem(const QString &name)
Registers an item with the script engine.
Definition: qaxscript.cpp:618
#define Q_SLOTS
Definition: qobjectdefs.h:71
The QString class provides a Unicode character string.
Definition: qstring.h:83
The QAxObject class provides a QObject that wraps a COM object.
Definition: qaxobject.h:54
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
#define Q_SIGNALS
Definition: qobjectdefs.h:72
~QAxScriptEngine()
Destroys the QAxScriptEngine object, releasing all allocated resources.
Definition: qaxscript.cpp:440
QAxScript * script_code
Definition: qaxscript.h:94
QString scriptLanguage() const
Returns the scripting language, for example "VBScript", or "JScript".
Definition: qaxscript.h:224
bool isValid() const
Returns true if the script engine has been initialized correctly; otherwise returns false...
Definition: qaxscript.h:219
QAxScriptManager * script_manager
Definition: qaxscript.h:148
void addObject(QAxBase *object)
Adds object to the manager.
Definition: qaxscript.cpp:1040
QLocale::Language language
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
bool hasIntrospection() const
Returns true if the script engine supports introspection; otherwise returns false.
Definition: qaxscript.cpp:543
QAxScriptManagerPrivate * d
Definition: qaxscript.h:193
QAxScriptEngine * scriptEngine() const
Returns a pointer to the script engine.
Definition: qaxscript.h:212
const char * name
IActiveScript * engine
Definition: qaxscript.h:95
The QStringList class provides a list of strings.
Definition: qstringlist.h:66
Q_CORE_EXPORT void qWarning(const char *,...)
QString script_name
Definition: qaxscript.h:146
const T * ptr(const T &t)
The QAxScriptManager class provides a bridge between application objects and script code...
Definition: qaxscript.h:153
The State element defines configurations of objects and properties.
QAxScriptSite * script_site
Definition: qaxscript.h:150
void setState(State st)
Sets the state of the script engine to st.
Definition: qaxscript.cpp:602
Q_CORE_EXPORT int QT_FASTCALL script(uint ucs4)
#define Q_OBJECT
Definition: qobjectdefs.h:157
QAxScriptEngine(const QString &language, QAxScript *script)
Constructs a QAxScriptEngine object interpreting script code in language provided by the code in scri...
Definition: qaxscript.cpp:421
State state() const
Returns the state of the script engine.
Definition: qaxscript.cpp:584
#define load(x)
QString scriptName() const
Returns the name of the script.
Definition: qaxscript.h:207
QObject * parent() const
Returns a pointer to the parent object.
Definition: qobject.h:273
#define st(var, type, card)
QAxScriptEngine * script_engine
Definition: qaxscript.h:149
const char * className() const
Returns the class name.
Definition: qobjectdefs.h:491
bool initialize(IUnknown **ptr)
Reimplemented Function
Definition: qaxscript.cpp:463
The QAxScript class provides a wrapper around script code.
Definition: qaxscript.h:100
long queryInterface(const QUuid &, void **) const
Requests the interface uuid from the script engine object and sets the value of iface to the provided...
Definition: qaxscript.cpp:567
#define QT_END_HEADER
Definition: qglobal.h:137
The QAxScriptEngine class provides a wrapper around a script engine.
Definition: qaxscript.h:63
The QAxBase class is an abstract class that provides an API to initialize and access a COM object...
Definition: qaxbase.h:66
virtual const QMetaObject * metaObject() const
Returns a pointer to the meta-object of this object.
The QUuid class stores a Universally Unique Identifier (UUID).
Definition: quuid.h:67
QString scriptCode() const
Returns the script&#39;s code, or the null-string if no code has been loaded yet.
Definition: qaxscript.h:202