QtiPlot  0.9.8.2
PythonScripting.h
Go to the documentation of this file.
1 /***************************************************************************
2  File : PythonScripting.h
3  Project : QtiPlot
4 --------------------------------------------------------------------
5  Copyright : (C) 2006 by Knut Franke
6  Email (use @ for *) : knut.franke*gmx.de
7  Description : Execute Python code from within QtiPlot
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 PYTHON_SCRIPTING_H
30 #define PYTHON_SCRIPTING_H
31 
32 #include "ScriptingEnv.h"
33 #include "PythonScript.h"
34 
35 class QObject;
36 class QString;
37 
38 typedef struct _object PyObject;
39 
41 {
42  Q_OBJECT
43 
44  public:
45  static const char *langName;
48  static ScriptingEnv *constructor(ApplicationWindow *parent) { return new PythonScripting(parent); }
49  bool initialize();
50 
51  void write(const QString &text) { emit print(text); }
52 
54 
58  QString toString(PyObject *object, bool decref=false);
60 
66  PyObject* eval(const QString &code, PyObject *argDict=NULL, const char *name="<qtiplot>");
68 
74  bool exec(const QString &code, PyObject *argDict=NULL, const char *name="<qtiplot>");
75  QString errorMsg();
76 
77  bool isRunning() const;
78  Script *newScript(const QString &code, QObject *context, const QString &name="<input>")
79  {
80  return new PythonScript(this, code, context, name);
81  }
82 
83  bool setQObject(QObject*, const char*, PyObject *dict);
84  bool setQObject(QObject *val, const char *name) { return setQObject(val,name,NULL); }
85  bool setInt(int, const char*, PyObject *dict=NULL);
86  bool setDouble(double, const char*, PyObject *dict=NULL);
87 
88  const QStringList mathFunctions() const;
89  const QString mathFunctionDoc (const QString &name) const;
90  const QStringList fileExtensions() const;
91 
92  PyObject *globalDict() { return globals; }
93  PyObject *sysDict() { return sys; }
94 
95  private:
96  bool loadInitFile(const QString &path);
97 
98  PyObject *globals; // PyDict of global environment
99  PyObject *math; // PyDict of math functions
100  PyObject *sys; // PyDict of sys module
101 };
102 
103 #endif
PyObject * eval(const QString &code, PyObject *argDict=NULL, const char *name="<qtiplot>")
evaluate a Python expression
Definition: PythonScripting.cpp:84
static const char * langName
Definition: PythonScripting.h:45
bool exec(const QString &code, PyObject *argDict=NULL, const char *name="<qtiplot>")
execute a sequence of Python statements
Definition: PythonScripting.cpp:103
~PythonScripting()
Definition: PythonScripting.cpp:287
bool setQObject(QObject *, const char *, PyObject *dict)
Definition: PythonScripting.cpp:357
bool initialize()
Part of the initialization is deferred from the constructor until after the signals have been connect...
Definition: PythonScripting.cpp:265
static ScriptingEnv * constructor(ApplicationWindow *parent)
Definition: PythonScripting.h:48
An interpreter for evaluating scripting code. Abstract.
Definition: ScriptingEnv.h:50
PyObject * math
Definition: PythonScripting.h:99
Definition: PythonScript.h:41
PyObject * sysDict()
Definition: PythonScripting.h:93
bool isRunning() const
whether asynchronuous execution is enabled (if supported by the implementation)
Definition: PythonScripting.cpp:349
A chunk of scripting code. Abstract.
Definition: Script.h:50
PyObject * sys
Definition: PythonScripting.h:100
QString errorMsg()
Definition: PythonScripting.cpp:124
QtiPlot&#39;s main window.
Definition: ApplicationWindow.h:133
const QStringList fileExtensions() const
Return a list of file extensions commonly used for this language.
Definition: PythonScripting.cpp:442
struct _object PyObject
Definition: PythonScript.h:37
bool setInt(int, const char *, PyObject *dict=NULL)
Definition: PythonScripting.cpp:380
bool loadInitFile(const QString &path)
Definition: PythonScripting.cpp:296
void decref()
Decrease the reference count. This should only be called by scripted and Script to avoid segfaults...
Definition: ScriptingEnv.cpp:64
bool setQObject(QObject *val, const char *name)
Definition: PythonScripting.h:84
void print(const QString &output)
output that is not handled by a Script
PythonScripting(ApplicationWindow *parent)
Definition: PythonScripting.cpp:183
const QStringList mathFunctions() const
Return a list of supported mathematical functions. These should be imported into the global namespace...
Definition: PythonScripting.cpp:410
void write(const QString &text)
Definition: PythonScripting.h:51
PyObject * globalDict()
Definition: PythonScripting.h:92
QString toString(PyObject *object, bool decref=false)
like str(object) in Python
Definition: PythonScripting.cpp:66
Definition: PythonScripting.h:40
Script * newScript(const QString &code, QObject *context, const QString &name="<input>")
Instantiate the Script subclass matching the ScriptEnv subclass.
Definition: PythonScripting.h:78
PyObject * globals
Definition: PythonScripting.h:98
bool setDouble(double, const char *, PyObject *dict=NULL)
Definition: PythonScripting.cpp:395
const QString mathFunctionDoc(const QString &name) const
Return a documentation string for the given mathematical function.
Definition: PythonScripting.cpp:428