QtiPlot 0.9.8.2
|
00001 /*************************************************************************** 00002 File : Script.h 00003 Project : QtiPlot 00004 -------------------------------------------------------------------- 00005 Copyright : (C) 2006 by Ion Vasilief, Knut Franke 00006 Email (use @ for *) : ion_vasilief*yahoo.fr, knut.franke*gmx.de 00007 Description : Scripting abstraction layer 00008 00009 ***************************************************************************/ 00010 00011 /*************************************************************************** 00012 * * 00013 * This program is free software; you can redistribute it and/or modify * 00014 * it under the terms of the GNU General Public License as published by * 00015 * the Free Software Foundation; either version 2 of the License, or * 00016 * (at your option) any later version. * 00017 * * 00018 * This program is distributed in the hope that it will be useful, * 00019 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00020 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00021 * GNU General Public License for more details. * 00022 * * 00023 * You should have received a copy of the GNU General Public License * 00024 * along with this program; if not, write to the Free Software * 00025 * Foundation, Inc., 51 Franklin Street, Fifth Floor, * 00026 * Boston, MA 02110-1301 USA * 00027 * * 00028 ***************************************************************************/ 00029 #ifndef SCRIPT_H 00030 #define SCRIPT_H 00031 00032 #include <QVariant> 00033 #include <QString> 00034 #include <QStringList> 00035 #include <QObject> 00036 #include <QStringList> 00037 #include <QEvent> 00038 00039 #include "customevents.h" 00040 #include "ScriptingEnv.h" 00041 00042 class ApplicationWindow; 00043 00045 00050 class Script : public QObject 00051 { 00052 Q_OBJECT 00053 00054 public: 00055 Script(ScriptingEnv *env, const QString &code, QObject *context=0, const QString &name="<input>") 00056 : Env(env), Code(code), Name(name), compiled(notCompiled) 00057 { Env->incref(); Context = context; EmitErrors=true; } 00058 ~Script() { Env->decref(); } 00059 00061 const QString code() const { return Code; } 00063 QObject* context() const { return Context; } 00065 const QString name() const { return Name; } 00067 bool emitErrors() const { return EmitErrors; } 00069 virtual void addCode(const QString &code) { Code.append(code); compiled = notCompiled; emit codeChanged(); } 00071 virtual void setCode(const QString &code) { Code=code; compiled = notCompiled; emit codeChanged(); } 00073 virtual void setContext(QObject *context) { Context = context; compiled = notCompiled; } 00075 void setName(const QString &name) { Name = name; compiled = notCompiled; } 00077 void setEmitErrors(bool yes) { EmitErrors = yes; } 00078 ScriptingEnv *scriptingEnv(){return Env;}; 00079 00080 public slots: 00082 virtual bool compile(bool for_eval=true); 00084 virtual QVariant eval(); 00086 virtual bool exec(); 00087 00088 // local variables 00089 virtual bool setQObject(const QObject*, const char*) { return false; } 00090 virtual bool setInt(int, const char*) { return false; } 00091 virtual bool setDouble(double, const char*) { return false; } 00092 00093 signals: 00095 void codeChanged(); 00097 void error(const QString & message, const QString & scriptName, int lineNumber); 00099 void print(const QString & output); 00100 00101 protected: 00102 ScriptingEnv *Env; 00103 QString Code, Name; 00104 QObject *Context; 00105 enum compileStatus { notCompiled, isCompiled, compileErr } compiled; 00106 bool EmitErrors; 00107 00108 void emit_error(const QString & message, int lineNumber) 00109 { if(EmitErrors) emit error(message, Name, lineNumber); } 00110 }; 00111 00113 class ScriptingLangManager 00114 { 00115 public: 00117 static ScriptingEnv *newEnv(ApplicationWindow *parent); 00119 static ScriptingEnv *newEnv(const char *name, ApplicationWindow *parent); 00121 static QStringList languages(); 00123 static int numLanguages(); 00124 00125 private: 00126 typedef ScriptingEnv*(*ScriptingEnvConstructor)(ApplicationWindow*); 00127 typedef struct { 00128 const char *name; 00129 ScriptingEnvConstructor constructor; 00130 } ScriptingLang; 00132 static ScriptingLang langs[]; 00133 }; 00134 00136 class ScriptingChangeEvent : public QEvent 00137 { 00138 public: 00139 ScriptingChangeEvent(ScriptingEnv *e) : QEvent(SCRIPTING_CHANGE_EVENT), env(e) {} 00140 ScriptingEnv *scriptingEnv() const { return env; } 00141 Type type() const { return SCRIPTING_CHANGE_EVENT; } 00142 private: 00143 ScriptingEnv *env; 00144 }; 00145 00147 00152 class scripted 00153 { 00154 public: 00155 scripted(ScriptingEnv* env); 00156 ~scripted(); 00157 void scriptingChangeEvent(ScriptingChangeEvent*); 00158 ScriptingEnv *scriptingEnv(){return scriptEnv;}; 00159 00160 protected: 00161 ScriptingEnv *scriptEnv; 00162 }; 00163 00164 #endif