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

Generated on Thu Feb 7 13:59:27 2008 for QtiPlot by  doxygen 1.5.4