1 /***************************************************************************
2     File                 : ScriptingEnv.h
3     Project              : QtiPlot
4     --------------------------------------------------------------------
5     Copyright            : (C) 2006 by Ion Vasilief, Knut Franke
6     Email (use @ for *)  : ion_vasilief*yahoo.fr
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 SCRIPTINGENV_H
30 #define SCRIPTINGENV_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 
41 class ApplicationWindow;
42 class Script;
43 
44 //! An interpreter for evaluating scripting code. Abstract.
45   /**
46    * ScriptingEnv objects represent a running interpreter, possibly with global
47    * variables, and are responsible for generating Script objects (which do
48    * the actual evaluation of code).
49    */
50 class ScriptingEnv : public QObject
51 {
52   Q_OBJECT
53 
54   public:
55     ScriptingEnv(ApplicationWindow *parent, const char *langName);
56     //! Part of the initialization is deferred from the constructor until after the signals have been connected.
initialize()57     virtual bool initialize() { return true; };
58     //! initialization of the interpreter may fail; or there could be other errors setting up the environment
initialized()59     bool initialized() const { return d_initialized; }
60     //! whether asynchronuous execution is enabled (if supported by the implementation)
isRunning()61     virtual bool isRunning() const { return false; }
62 
63     //! Instantiate the Script subclass matching the ScriptEnv subclass.
newScript(const QString &,QObject *,const QString &)64     virtual Script *newScript(const QString&, QObject*, const QString&) { return 0; }
65 
66     //! If an exception / error occured, return a nicely formated stack backtrace.
stackTraceString()67     virtual QString stackTraceString() { return QString::null; }
68 
69     //! Return a list of supported mathematical functions. These should be imported into the global namespace.
mathFunctions()70     virtual const QStringList mathFunctions() const { return QStringList(); }
71     //! Return a documentation string for the given mathematical function.
mathFunctionDoc(const QString &)72     virtual const QString mathFunctionDoc(const QString&) const { return QString::null; }
73     //! Return a list of file extensions commonly used for this language.
fileExtensions()74     virtual const QStringList fileExtensions() const { return QStringList(); };
75     //! Construct a filter expression from fileExtension(), suitable for QFileDialog.
76     const QString fileFilter() const;
77 
application()78 	ApplicationWindow *application(){return d_parent;};
79 
80   public slots:
81     // global variables
setQObject(QObject *,const char *)82     virtual bool setQObject(QObject*, const char*) { return false; }
setInt(int,const char *)83     virtual bool setInt(int, const char*) { return false; }
setDouble(double,const char *)84     virtual bool setDouble(double, const char*) { return false; }
85 
86     //! Clear the global environment. What exactly happens depends on the implementation.
clear()87     virtual void clear() {}
88     //! If the implementation supports asynchronuos execution, deactivate it.
stopExecution()89     virtual void stopExecution() {}
90     //! If the implementation supports asynchronuos execution, activate it.
startExecution()91     virtual void startExecution() {}
92 
93     //! Increase the reference count. This should only be called by scripted and Script to avoid memory leaks.
94     void incref();
95     //! Decrease the reference count. This should only be called by scripted and Script to avoid segfaults.
96     void decref();
97 
98   signals:
99     //! signal an error condition / exception
100     void error(const QString & message, const QString & scriptName, int lineNumber);
101     //! output that is not handled by a Script
102     void print(const QString & output);
103 
104   protected:
105     //! whether the interpreter has been successfully initialized
106     bool d_initialized;
107     //! the context in which we are running
108     ApplicationWindow *d_parent;
109 
110   private:
111     //! the reference counter
112     int d_refcount;
113 };
114 
115 #endif
116