CTK  0.1.0
The Common Toolkit is a community effort to provide support code for medical image analysis, surgical navigation, and related projects.
ctkAbstractPythonManager.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Library: CTK
4 
5  Copyright (c) Kitware Inc.
6 
7  Licensed under the Apache License, Version 2.0 (the "License");
8  you may not use this file except in compliance with the License.
9  You may obtain a copy of the License at
10 
11  http://www.apache.org/licenses/LICENSE-2.0.txt
12 
13  Unless required by applicable law or agreed to in writing, software
14  distributed under the License is distributed on an "AS IS" BASIS,
15  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  See the License for the specific language governing permissions and
17  limitations under the License.
18 
19 =========================================================================*/
20 
21 #ifndef __ctkAbstractPythonManager_h
22 #define __ctkAbstractPythonManager_h
23 
24 // Qt includes
25 #include <QObject>
26 #include <QList>
27 #include <QStringList>
28 #include <QVariant>
29 
30 // PythonQt includes
31 #include <PythonQtPythonInclude.h> // For PyObject
32 
33 // CTK includes
34 #include "ctkScriptingPythonCoreExport.h"
35 
36 class ctkAbstractPythonManagerPrivate;
37 class PythonQtForeignWrapperFactory;
38 class PythonQtObjectPtr;
39 
41 class CTK_SCRIPTING_PYTHON_CORE_EXPORT ctkAbstractPythonManager : public QObject
42 {
43  Q_OBJECT
44 
45 public:
46  typedef QObject Superclass;
47  ctkAbstractPythonManager(QObject* _parent=NULL);
49 
54  void setInitializationFlags(int flags);
55 
57  int initializationFlags()const;
58 
63  bool initialize();
64 
67  PythonQtObjectPtr mainContext();
68 
69  void addObjectToPythonMain(const QString& name, QObject* obj);
70  void addWrapperFactory(PythonQtForeignWrapperFactory* factory);
71  void registerPythonQtDecorator(QObject* decorator);
72  void registerClassForPythonQt(const QMetaObject* metaobject);
73  void registerCPPClassForPythonQt(const char* name);
74 
77 
80 
86  {
87  EvalInput = 0,
89  SingleInput
90  };
91 
94  Q_INVOKABLE QVariant executeString(const QString& code, ExecuteStringMode mode = FileInput);
95 
98  QVariant getVariable(const QString& varName);
99 
101  Q_INVOKABLE void executeFile(const QString& filename);
102 
105  void setInitializationFunction(void (*initFunction)());
106 
110  static QStringList dir_object(PyObject* object,
111  bool appendParenthesis = false);
112 
116  static QStringList splitByDotOutsideParenthesis(const QString& pythonVariableName);
117 
123  QStringList pythonAttributes(const QString& pythonVariableName,
124  const QString& module = QLatin1String("__main__"),
125  bool appendParenthesis = false) const;
126 
128  static PyObject* pythonModule(const QString &module);
129 
132  static PyObject* pythonObject(const QString& variableNameAndFunction);
133 
136  bool isPythonInitialized()const;
137 
140  bool pythonErrorOccured()const;
141 
145 
146 Q_SIGNALS:
147 
152 
157 
161  void systemExitExceptionRaised(int exitCode);
162 
163 protected Q_SLOTS:
164  void printStderr(const QString&);
165  void printStdout(const QString&);
166 
167 protected:
168 
169  void initPythonQt(int flags);
170 
171  virtual QStringList pythonPaths();
172 
174  virtual void preInitialization();
175 
178 
179 protected:
180  QScopedPointer<ctkAbstractPythonManagerPrivate> d_ptr;
181 
182 private:
183  Q_DECLARE_PRIVATE(ctkAbstractPythonManager);
184  Q_DISABLE_COPY(ctkAbstractPythonManager);
185 
186 };
187 #endif
static PyObject * pythonModule(const QString &module)
Given a string of the form "<modulename1>[.<modulenameN>...]" containing modules, return the final mo...
virtual ~ctkAbstractPythonManager()
void printStderr(const QString &)
virtual void preInitialization()
Overload this function to load Decorator and pythonQt wrapper at initialization time.
void setInitializationFlags(int flags)
static PyObject * pythonObject(const QString &variableNameAndFunction)
void registerCPPClassForPythonQt(const char *name)
Q_INVOKABLE void executeFile(const QString &filename)
Execute a python script with the given filename.
int initializationFlags() const
void setInitializationFunction(void(*initFunction)())
QVariant getVariable(const QString &varName)
void setSystemExitExceptionHandlerEnabled(bool value)
Q_INVOKABLE QVariant executeString(const QString &code, ExecuteStringMode mode=FileInput)
bool systemExitExceptionHandlerEnabled() const
static QStringList splitByDotOutsideParenthesis(const QString &pythonVariableName)
virtual QStringList pythonPaths()
void registerPythonQtDecorator(QObject *decorator)
void systemExitExceptionRaised(int exitCode)
virtual void executeInitializationScripts()
Overload this function to execute script at initialization time.
void addObjectToPythonMain(const QString &name, QObject *obj)
void addWrapperFactory(PythonQtForeignWrapperFactory *factory)
QStringList pythonAttributes(const QString &pythonVariableName, const QString &module=QLatin1String("__main__"), bool appendParenthesis=false) const
bool isPythonInitialized() const
void registerClassForPythonQt(const QMetaObject *metaobject)
static QStringList dir_object(PyObject *object, bool appendParenthesis=false)
QScopedPointer< ctkAbstractPythonManagerPrivate > d_ptr
ctkAbstractPythonManager(QObject *_parent=NULL)
bool pythonErrorOccured() const
void printStdout(const QString &)
PythonQtObjectPtr mainContext()
void initPythonQt(int flags)