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