Script.h

Go to the documentation of this file.
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     const QObject* context() const { return Context; }
00065     const QString name() const { return Name; }
00067     const 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 
00079   public slots:
00081     virtual bool compile(bool for_eval=true);
00083     virtual QVariant eval();
00085     virtual bool exec();
00086 
00087     // local variables
00088     virtual bool setQObject(const QObject*, const char*) { return false; }
00089     virtual bool setInt(int, const char*) { return false; }
00090     virtual bool setDouble(double, const char*) { return false; }
00091 
00092   signals:
00094     void codeChanged();
00096     void error(const QString & message, const QString & scriptName, int lineNumber);
00098     void print(const QString & output);
00099 
00100   protected:
00101     ScriptingEnv *Env;
00102     QString Code, Name;
00103     QObject *Context;
00104     enum compileStatus { notCompiled, isCompiled, compileErr } compiled;
00105     bool EmitErrors;
00106 
00107     void emit_error(const QString & message, int lineNumber)
00108       { if(EmitErrors) emit error(message, Name, lineNumber); }
00109 };
00110 
00112 class ScriptingLangManager
00113 {
00114   public:
00116     static ScriptingEnv *newEnv(ApplicationWindow *parent);
00118     static ScriptingEnv *newEnv(const char *name, ApplicationWindow *parent);
00120     static QStringList languages();
00122     static int numLanguages();
00123 
00124   private:
00125     typedef ScriptingEnv*(*ScriptingEnvConstructor)(ApplicationWindow*);
00126     typedef struct {
00127       const char *name;
00128       ScriptingEnvConstructor constructor;
00129     } ScriptingLang;
00131     static ScriptingLang langs[];
00132 };
00133 
00135 class ScriptingChangeEvent : public QEvent
00136 {
00137   public:
00138     ScriptingChangeEvent(ScriptingEnv *e) : QEvent(SCRIPTING_CHANGE_EVENT), env(e) {}
00139     ScriptingEnv *scriptingEnv() const { return env; }
00140     Type type() const { return SCRIPTING_CHANGE_EVENT; }
00141   private:
00142     ScriptingEnv *env;
00143 };
00144 
00146 
00151 class scripted
00152 {
00153   public:
00154    scripted(ScriptingEnv* env);
00155    ~scripted();
00156    void scriptingChangeEvent(ScriptingChangeEvent*);
00157    ScriptingEnv *scriptingEnv(){return scriptEnv;};
00158   
00159   protected:
00160     ScriptingEnv *scriptEnv;
00161 };
00162 
00163 #endif

Generated on Fri Nov 7 03:36:53 2008 for QtiPlot by  doxygen 1.5.6