Script.h
Go to the documentation of this file.
/***************************************************************************
File : Script.h
Project : QtiPlot
--------------------------------------------------------------------
Copyright : (C) 2006 by Ion Vasilief, Knut Franke
Email (use @ for *) : ion_vasilief*yahoo.fr, knut.franke*gmx.de
Description : Scripting abstraction layer
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the Free Software *
* Foundation, Inc., 51 Franklin Street, Fifth Floor, *
* Boston, MA 02110-1301 USA *
* *
***************************************************************************/
#ifndef SCRIPT_H
#define SCRIPT_H
#include <QVariant>
#include <QString>
#include <QStringList>
#include <QObject>
#include <QStringList>
#include <QEvent>
#include "customevents.h"
#include "ScriptingEnv.h"
class ApplicationWindow;
class Script : public QObject
{
Q_OBJECT
public:
Script(ScriptingEnv *env, const QString &code, QObject *context=0, const QString &name="<input>")
: Env(env), Code(code), Name(name), compiled(notCompiled)
{ Env->incref(); Context = context; EmitErrors=true; }
~Script() { Env->decref(); }
const QString code() const { return Code; }
QObject* context() const { return Context; }
const QString name() const { return Name; }
bool emitErrors() const { return EmitErrors; }
virtual void addCode(const QString &code) { Code.append(code); compiled = notCompiled; emit codeChanged(); }
virtual void setCode(const QString &code) { Code=code; compiled = notCompiled; emit codeChanged(); }
virtual void setContext(QObject *context) { Context = context; compiled = notCompiled; }
void setName(const QString &name) { Name = name; compiled = notCompiled; }
void setEmitErrors(bool yes) { EmitErrors = yes; }
ScriptingEnv *scriptingEnv(){return Env;};
public slots:
virtual bool compile(bool for_eval=true);
virtual QVariant eval();
virtual bool exec();
// local variables
virtual bool setQObject(const QObject*, const char*) { return false; }
virtual bool setInt(int, const char*) { return false; }
virtual bool setDouble(double, const char*) { return false; }
signals:
void codeChanged();
void error(const QString & message, const QString & scriptName, int lineNumber);
void print(const QString & output);
protected:
ScriptingEnv *Env;
QString Code, Name;
QObject *Context;
enum compileStatus { notCompiled, isCompiled, compileErr } compiled;
bool EmitErrors;
void emit_error(const QString & message, int lineNumber)
{ if(EmitErrors) emit error(message, Name, lineNumber); }
};
class ScriptingLangManager
{
public:
static ScriptingEnv *newEnv(ApplicationWindow *parent);
static ScriptingEnv *newEnv(const char *name, ApplicationWindow *parent);
static QStringList languages();
static int numLanguages();
private:
typedef ScriptingEnv*(*ScriptingEnvConstructor)(ApplicationWindow*);
typedef struct {
const char *name;
ScriptingEnvConstructor constructor;
} ScriptingLang;
static ScriptingLang langs[];
};
class ScriptingChangeEvent : public QEvent
{
public:
ScriptingChangeEvent(ScriptingEnv *e) : QEvent(SCRIPTING_CHANGE_EVENT), env(e) {}
ScriptingEnv *scriptingEnv() const { return env; }
Type type() const { return SCRIPTING_CHANGE_EVENT; }
private:
ScriptingEnv *env;
};
class scripted
{
public:
scripted(ScriptingEnv* env);
~scripted();
void scriptingChangeEvent(ScriptingChangeEvent*);
ScriptingEnv *scriptingEnv(){return scriptEnv;};
protected:
ScriptingEnv *scriptEnv;
};
#endif
ScriptingEnv::incref
void incref()
Increase the reference count. This should only be called by scripted and Script to avoid memory leaks...
Definition: ScriptingEnv.cpp:59
Script::Code
QString Code
Definition: Script.h:103
Script::exec
virtual bool exec()
Execute the Code, returning false on an error / exception.
Definition: Script.cpp:91
Script::Env
ScriptingEnv * Env
Definition: Script.h:102
Script::setContext
virtual void setContext(QObject *context)
Set the context in which the code is to be executed.
Definition: Script.h:73
scripted::scriptingEnv
ScriptingEnv * scriptingEnv()
Definition: Script.h:158
ScriptingChangeEvent::type
Type type() const
Definition: Script.h:141
ScriptingLangManager::ScriptingLang
Definition: Script.h:127
ScriptingLangManager::langs
static ScriptingLang langs[]
global registry of available languages
Definition: Script.h:132
customevents.h
ScriptingEnv
An interpreter for evaluating scripting code. Abstract.
Definition: ScriptingEnv.h:50
scripted::scriptEnv
ScriptingEnv * scriptEnv
Definition: Script.h:158
Script::Context
QObject * Context
Definition: Script.h:104
scripted::scriptingChangeEvent
void scriptingChangeEvent(ScriptingChangeEvent *)
Definition: Script.cpp:108
ScriptingEnv.h
Script::eval
virtual QVariant eval()
Evaluate the Code, returning QVariant() on an error / exception.
Definition: Script.cpp:85
ScriptingLangManager::newEnv
static ScriptingEnv * newEnv(ApplicationWindow *parent)
Return an instance of the first implementation we can find.
Definition: Script.cpp:53
ScriptingLangManager::numLanguages
static int numLanguages()
Return the number of available implementations.
Script::emit_error
void emit_error(const QString &message, int lineNumber)
Definition: Script.h:108
ScriptingLangManager
keeps a static list of available interpreters and instantiates them on demand
Definition: Script.h:113
ScriptingLangManager::ScriptingLang::constructor
ScriptingEnvConstructor constructor
Definition: Script.h:129
Script::setName
void setName(const QString &name)
Like QObject::setName, but with unicode support.
Definition: Script.h:75
Script::setCode
virtual void setCode(const QString &code)
Set the code that will be executed when calling exec() or eval()
Definition: Script.h:71
Script::EmitErrors
bool EmitErrors
Definition: Script.h:106
ScriptingChangeEvent
notify an object that it should update its scripting environment (see class scripted) ...
Definition: Script.h:136
Script::setInt
virtual bool setInt(int, const char *)
Definition: Script.h:90
ScriptingLangManager::ScriptingLang::name
const char * name
Definition: Script.h:128
ScriptingChangeEvent::scriptingEnv
ScriptingEnv * scriptingEnv() const
Definition: Script.h:140
Script::emitErrors
bool emitErrors() const
Return whether errors / exceptions are to be emitted or silently ignored.
Definition: Script.h:67
Script
A chunk of scripting code. Abstract.
Definition: Script.h:50
ScriptingChangeEvent::ScriptingChangeEvent
ScriptingChangeEvent(ScriptingEnv *e)
Definition: Script.h:139
scripted::scripted
scripted(ScriptingEnv *env)
Definition: Script.cpp:97
ApplicationWindow
QtiPlot's main window.
Definition: ApplicationWindow.h:133
Script::Script
Script(ScriptingEnv *env, const QString &code, QObject *context=0, const QString &name="<input>")
Definition: Script.h:55
ScriptingEnv::decref
void decref()
Decrease the reference count. This should only be called by scripted and Script to avoid segfaults...
Definition: ScriptingEnv.cpp:64
SCRIPTING_CHANGE_EVENT
#define SCRIPTING_CHANGE_EVENT
Definition: customevents.h:37
Script::compiled
enum Script::compileStatus compiled
Script::notCompiled
Definition: Script.h:105
Script::compileStatus
compileStatus
Definition: Script.h:105
ScriptingChangeEvent::env
ScriptingEnv * env
Definition: Script.h:143
Script::setDouble
virtual bool setDouble(double, const char *)
Definition: Script.h:91
Script::context
QObject * context() const
Return the context in which the code is to be executed.
Definition: Script.h:63
Script::print
void print(const QString &output)
output generated by the code
scripted
Interface for maintaining a reference to the current ScriptingEnv.
Definition: Script.h:152
Script::~Script
~Script()
Definition: Script.h:58
Script::addCode
virtual void addCode(const QString &code)
Append to the code that will be executed when calling exec() or eval()
Definition: Script.h:69
Script::isCompiled
Definition: Script.h:105
Script::name
const QString name() const
Like QObject::name, but with unicode support.
Definition: Script.h:65
scripted::~scripted
~scripted()
Definition: Script.cpp:103
Script::scriptingEnv
ScriptingEnv * scriptingEnv()
Definition: Script.h:78
Script::codeChanged
void codeChanged()
This is emitted whenever the code to be executed by exec() and eval() is changed. ...
Script::setEmitErrors
void setEmitErrors(bool yes)
Set whether errors / exceptions are to be emitted or silently ignored.
Definition: Script.h:77
Script::error
void error(const QString &message, const QString &scriptName, int lineNumber)
signal an error condition / exception
Script::compileErr
Definition: Script.h:105
Script::setQObject
virtual bool setQObject(const QObject *, const char *)
Definition: Script.h:89
Script::compile
virtual bool compile(bool for_eval=true)
Compile the Code. Return true if the implementation doesn't support compilation.
Definition: Script.cpp:77
ScriptingLangManager::ScriptingEnvConstructor
ScriptingEnv *(* ScriptingEnvConstructor)(ApplicationWindow *)
Definition: Script.h:126
Script::code
const QString code() const
Return the code that will be executed when calling exec() or eval()
Definition: Script.h:61
Script::Name
QString Name
Definition: Script.h:103
ScriptingLangManager::languages
static QStringList languages()
Return the names of available implementations.
Definition: Script.cpp:69