SciDAVis  1.D4
Script.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : Script.h
3  Project : SciDAVis
4  --------------------------------------------------------------------
5  Copyright : (C) 2006 by Ion Vasilief,
6  Tilman Benkert,
7  Knut Franke
8  Email (use @ for *) : ion_vasilief*yahoo.fr, thzs*gmx.net
9  Description : Scripting abstraction layer
10 
11  ***************************************************************************/
12 
13 /***************************************************************************
14  * *
15  * This program is free software; you can redistribute it and/or modify *
16  * it under the terms of the GNU General Public License as published by *
17  * the Free Software Foundation; either version 2 of the License, or *
18  * (at your option) any later version. *
19  * *
20  * This program is distributed in the hope that it will be useful, *
21  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
22  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
23  * GNU General Public License for more details. *
24  * *
25  * You should have received a copy of the GNU General Public License *
26  * along with this program; if not, write to the Free Software *
27  * Foundation, Inc., 51 Franklin Street, Fifth Floor, *
28  * Boston, MA 02110-1301 USA *
29  * *
30  ***************************************************************************/
31 #ifndef SCRIPT_H
32 #define SCRIPT_H
33 
34 #include <QVariant>
35 #include <QString>
36 #include <QStringList>
37 #include <QObject>
38 #include <QStringList>
39 #include <QEvent>
40 
41 #include "customevents.h"
42 #include "ScriptingEnv.h"
43 
44 class ApplicationWindow;
45 
47 
52 class Script : public QObject
53 {
54  Q_OBJECT
55 
56  public:
57  Script(ScriptingEnv *env, const QString &code, QObject *context=0, const QString &name="<input>")
58  : Env(env), Code(code), Name(name), compiled(notCompiled)
59  { Env->incref(); Context = context; EmitErrors=true; }
60  ~Script() { Env->decref(); }
61 
63  const QString code() const { return Code; }
65  const QObject* context() const { return Context; }
67  const QString name() const { return Name; }
69  bool emitErrors() const { return EmitErrors; }
71  virtual void addCode(const QString &code) { Code.append(code); compiled = notCompiled; emit codeChanged(); }
73  virtual void setCode(const QString &code) { Code=code; compiled = notCompiled; emit codeChanged(); }
75  virtual void setContext(QObject *context) { Context = context; compiled = notCompiled; }
77  void setName(const QString &name) { Name = name; compiled = notCompiled; }
79  void setEmitErrors(bool yes) { EmitErrors = yes; }
80 
81  public slots:
83  virtual bool compile(bool for_eval=true);
85  virtual QVariant eval();
87  virtual bool exec();
88 
89  // local variables
90  virtual bool setQObject(const QObject*, const char*) { return false; }
91  virtual bool setInt(int, const char*) { return false; }
92  virtual bool setDouble(double, const char*) { return false; }
93 
94  signals:
96  void codeChanged();
98  void error(const QString & message, const QString & scriptName, int lineNumber);
100  void print(const QString & output);
101 
102  protected:
104  QString Code, Name;
105  QObject *Context;
108 
109  void emit_error(const QString & message, int lineNumber)
110  { if(EmitErrors) emit error(message, Name, lineNumber); }
111 };
112 
115 {
116  public:
118  static ScriptingEnv *newEnv(ApplicationWindow *parent);
120  static ScriptingEnv *newEnv(const char *name, ApplicationWindow *parent);
122  static QStringList languages();
124  static int numLanguages();
125 
126  private:
127  typedef ScriptingEnv*(*ScriptingEnvConstructor)(ApplicationWindow*);
128  typedef struct {
129  const char *name;
131  } ScriptingLang;
134 };
135 
137 class ScriptingChangeEvent : public QEvent
138 {
139  public:
141  ScriptingEnv *scriptingEnv() const { return env; }
142  Type type() const { return SCRIPTING_CHANGE_EVENT; }
143  private:
145 };
146 
148 
153 class scripted
154 {
155  public:
156  scripted(ScriptingEnv* env);
157  ~scripted();
159  protected:
161 };
162 
163 #endif
164