QtiPlot  0.9.8.2
Script.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : Script.h
3  Project : QtiPlot
4  --------------------------------------------------------------------
5  Copyright : (C) 2006 by Ion Vasilief, Knut Franke
6  Email (use @ for *) : ion_vasilief*yahoo.fr, knut.franke*gmx.de
7  Description : Scripting abstraction layer
8 
9  ***************************************************************************/
10 
11 /***************************************************************************
12  * *
13  * This program is free software; you can redistribute it and/or modify *
14  * it under the terms of the GNU General Public License as published by *
15  * the Free Software Foundation; either version 2 of the License, or *
16  * (at your option) any later version. *
17  * *
18  * This program is distributed in the hope that it will be useful, *
19  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
20  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
21  * GNU General Public License for more details. *
22  * *
23  * You should have received a copy of the GNU General Public License *
24  * along with this program; if not, write to the Free Software *
25  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
26  * Boston, MA 02110-1301 USA *
27  * *
28  ***************************************************************************/
29 #ifndef SCRIPT_H
30 #define SCRIPT_H
31 
32 #include <QVariant>
33 #include <QString>
34 #include <QStringList>
35 #include <QObject>
36 #include <QStringList>
37 #include <QEvent>
38 
39 #include "customevents.h"
40 #include "ScriptingEnv.h"
41 
42 class ApplicationWindow;
43 
45 
50 class Script : public QObject
51 {
52  Q_OBJECT
53 
54  public:
55  Script(ScriptingEnv *env, const QString &code, QObject *context=0, const QString &name="<input>")
56  : Env(env), Code(code), Name(name), compiled(notCompiled)
57  { Env->incref(); Context = context; EmitErrors=true; }
58  ~Script() { Env->decref(); }
59 
61  const QString code() const { return Code; }
63  QObject* context() const { return Context; }
65  const QString name() const { return Name; }
67  bool emitErrors() const { return EmitErrors; }
69  virtual void addCode(const QString &code) { Code.append(code); compiled = notCompiled; emit codeChanged(); }
71  virtual void setCode(const QString &code) { Code=code; compiled = notCompiled; emit codeChanged(); }
73  virtual void setContext(QObject *context) { Context = context; compiled = notCompiled; }
75  void setName(const QString &name) { Name = name; compiled = notCompiled; }
77  void setEmitErrors(bool yes) { EmitErrors = yes; }
79 
80  public slots:
82  virtual bool compile(bool for_eval=true);
84  virtual QVariant eval();
86  virtual bool exec();
87 
88  // local variables
89  virtual bool setQObject(const QObject*, const char*) { return false; }
90  virtual bool setInt(int, const char*) { return false; }
91  virtual bool setDouble(double, const char*) { return false; }
92 
93  signals:
95  void codeChanged();
97  void error(const QString & message, const QString & scriptName, int lineNumber);
99  void print(const QString & output);
100 
101  protected:
103  QString Code, Name;
104  QObject *Context;
107 
108  void emit_error(const QString & message, int lineNumber)
109  { if(EmitErrors) emit error(message, Name, lineNumber); }
110 };
111 
114 {
115  public:
117  static ScriptingEnv *newEnv(ApplicationWindow *parent);
119  static ScriptingEnv *newEnv(const char *name, ApplicationWindow *parent);
121  static QStringList languages();
123  static int numLanguages();
124 
125  private:
126  typedef ScriptingEnv*(*ScriptingEnvConstructor)(ApplicationWindow*);
127  typedef struct {
128  const char *name;
130  } ScriptingLang;
133 };
134 
136 class ScriptingChangeEvent : public QEvent
137 {
138  public:
140  ScriptingEnv *scriptingEnv() const { return env; }
141  Type type() const { return SCRIPTING_CHANGE_EVENT; }
142  private:
144 };
145 
147 
152 class scripted
153 {
154  public:
155  scripted(ScriptingEnv* env);
156  ~scripted();
159 
160  protected:
162 };
163 
164 #endif
void incref()
Increase the reference count. This should only be called by scripted and Script to avoid memory leaks...
Definition: ScriptingEnv.cpp:59
QString Code
Definition: Script.h:103
virtual bool exec()
Execute the Code, returning false on an error / exception.
Definition: Script.cpp:91
ScriptingEnv * Env
Definition: Script.h:102
virtual void setContext(QObject *context)
Set the context in which the code is to be executed.
Definition: Script.h:73
ScriptingEnv * scriptingEnv()
Definition: Script.h:158
Type type() const
Definition: Script.h:141
Definition: Script.h:127
static ScriptingLang langs[]
global registry of available languages
Definition: Script.h:132
An interpreter for evaluating scripting code. Abstract.
Definition: ScriptingEnv.h:50
ScriptingEnv * scriptEnv
Definition: Script.h:158
QObject * Context
Definition: Script.h:104
void scriptingChangeEvent(ScriptingChangeEvent *)
Definition: Script.cpp:108
virtual QVariant eval()
Evaluate the Code, returning QVariant() on an error / exception.
Definition: Script.cpp:85
static ScriptingEnv * newEnv(ApplicationWindow *parent)
Return an instance of the first implementation we can find.
Definition: Script.cpp:53
static int numLanguages()
Return the number of available implementations.
void emit_error(const QString &message, int lineNumber)
Definition: Script.h:108
keeps a static list of available interpreters and instantiates them on demand
Definition: Script.h:113
ScriptingEnvConstructor constructor
Definition: Script.h:129
void setName(const QString &name)
Like QObject::setName, but with unicode support.
Definition: Script.h:75
virtual void setCode(const QString &code)
Set the code that will be executed when calling exec() or eval()
Definition: Script.h:71
bool EmitErrors
Definition: Script.h:106
notify an object that it should update its scripting environment (see class scripted) ...
Definition: Script.h:136
virtual bool setInt(int, const char *)
Definition: Script.h:90
const char * name
Definition: Script.h:128
ScriptingEnv * scriptingEnv() const
Definition: Script.h:140
bool emitErrors() const
Return whether errors / exceptions are to be emitted or silently ignored.
Definition: Script.h:67
A chunk of scripting code. Abstract.
Definition: Script.h:50
ScriptingChangeEvent(ScriptingEnv *e)
Definition: Script.h:139
scripted(ScriptingEnv *env)
Definition: Script.cpp:97
QtiPlot's main window.
Definition: ApplicationWindow.h:133
Script(ScriptingEnv *env, const QString &code, QObject *context=0, const QString &name="<input>")
Definition: Script.h:55
void decref()
Decrease the reference count. This should only be called by scripted and Script to avoid segfaults...
Definition: ScriptingEnv.cpp:64
#define SCRIPTING_CHANGE_EVENT
Definition: customevents.h:37
enum Script::compileStatus compiled
Definition: Script.h:105
compileStatus
Definition: Script.h:105
ScriptingEnv * env
Definition: Script.h:143
virtual bool setDouble(double, const char *)
Definition: Script.h:91
QObject * context() const
Return the context in which the code is to be executed.
Definition: Script.h:63
void print(const QString &output)
output generated by the code
Interface for maintaining a reference to the current ScriptingEnv.
Definition: Script.h:152
~Script()
Definition: Script.h:58
virtual void addCode(const QString &code)
Append to the code that will be executed when calling exec() or eval()
Definition: Script.h:69
Definition: Script.h:105
const QString name() const
Like QObject::name, but with unicode support.
Definition: Script.h:65
~scripted()
Definition: Script.cpp:103
ScriptingEnv * scriptingEnv()
Definition: Script.h:78
void codeChanged()
This is emitted whenever the code to be executed by exec() and eval() is changed. ...
void setEmitErrors(bool yes)
Set whether errors / exceptions are to be emitted or silently ignored.
Definition: Script.h:77
void error(const QString &message, const QString &scriptName, int lineNumber)
signal an error condition / exception
Definition: Script.h:105
virtual bool setQObject(const QObject *, const char *)
Definition: Script.h:89
virtual bool compile(bool for_eval=true)
Compile the Code. Return true if the implementation doesn't support compilation.
Definition: Script.cpp:77
ScriptingEnv *(* ScriptingEnvConstructor)(ApplicationWindow *)
Definition: Script.h:126
const QString code() const
Return the code that will be executed when calling exec() or eval()
Definition: Script.h:61
QString Name
Definition: Script.h:103
static QStringList languages()
Return the names of available implementations.
Definition: Script.cpp:69