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
ctkAbstractPythonManager::executeInitializationScripts
virtual void executeInitializationScripts()
Overload this function to execute script at initialization time.
ctkAbstractPythonManager::executeString
Q_INVOKABLE QVariant executeString(const QString &code, ExecuteStringMode mode=FileInput)
ctkAbstractPythonManager::pythonInitialized
void pythonInitialized()
ctkAbstractPythonManager::setInitializationFlags
void setInitializationFlags(int flags)
ctkAbstractPythonManager::isPythonInitialized
bool isPythonInitialized() const
ctkAbstractPythonManager::preInitialization
virtual void preInitialization()
Overload this function to load Decorator and pythonQt wrapper at initialization time.
ctkAbstractPythonManager::addWrapperFactory
void addWrapperFactory(PythonQtForeignWrapperFactory *factory)
ctkAbstractPythonManager::pythonErrorOccured
bool pythonErrorOccured() const
ctkAbstractPythonManager::printStderr
void printStderr(const QString &)
ctkAbstractPythonManager::initialize
bool initialize()
ctkAbstractPythonManager::systemExitExceptionRaised
void systemExitExceptionRaised(int exitCode)
ctkAbstractPythonManager::FileInput
@ FileInput
Definition: ctkAbstractPythonManager.h:88
ctkAbstractPythonManager::Superclass
QObject Superclass
Definition: ctkAbstractPythonManager.h:46
ctkAbstractPythonManager::initPythonQt
void initPythonQt(int flags)
ctkAbstractPythonManager::d_ptr
QScopedPointer< ctkAbstractPythonManagerPrivate > d_ptr
Definition: ctkAbstractPythonManager.h:180
ctkAbstractPythonManager::setInitializationFunction
void setInitializationFunction(void(*initFunction)())
ctkAbstractPythonManager::addObjectToPythonMain
void addObjectToPythonMain(const QString &name, QObject *obj)
ctkAbstractPythonManager::~ctkAbstractPythonManager
virtual ~ctkAbstractPythonManager()
ctkAbstractPythonManager::registerCPPClassForPythonQt
void registerCPPClassForPythonQt(const char *name)
ctkAbstractPythonManager::getVariable
QVariant getVariable(const QString &varName)
ctkAbstractPythonManager::printStdout
void printStdout(const QString &)
ctkAbstractPythonManager::pythonPaths
virtual QStringList pythonPaths()
ctkAbstractPythonManager::initializationFlags
int initializationFlags() const
ctkAbstractPythonManager::registerPythonQtDecorator
void registerPythonQtDecorator(QObject *decorator)
ctkAbstractPythonManager::setSystemExitExceptionHandlerEnabled
void setSystemExitExceptionHandlerEnabled(bool value)
ctkAbstractPythonManager::registerClassForPythonQt
void registerClassForPythonQt(const QMetaObject *metaobject)
ctkAbstractPythonManager::pythonAttributes
QStringList pythonAttributes(const QString &pythonVariableName, const QString &module=QLatin1String("__main__"), bool appendParenthesis=false) const
ctkAbstractPythonManager::ctkAbstractPythonManager
ctkAbstractPythonManager(QObject *_parent=NULL)
ctkAbstractPythonManager::resetErrorFlag
void resetErrorFlag()
ctkAbstractPythonManager::mainContext
PythonQtObjectPtr mainContext()
ctkAbstractPythonManager
Definition: ctkAbstractPythonManager.h:42
ctkAbstractPythonManager::executeFile
Q_INVOKABLE void executeFile(const QString &filename)
Execute a python script with the given filename.
ctkAbstractPythonManager::dir_object
static QStringList dir_object(PyObject *object, bool appendParenthesis=false)
ctkAbstractPythonManager::pythonModule
static PyObject * pythonModule(const QString &module)
Given a string of the form "<modulename1>[.<modulenameN>...]" containing modules, return the final mo...
ctkAbstractPythonManager::pythonPreInitialized
void pythonPreInitialized()
ctkAbstractPythonManager::ExecuteStringMode
ExecuteStringMode
Definition: ctkAbstractPythonManager.h:86
ctkAbstractPythonManager::splitByDotOutsideParenthesis
static QStringList splitByDotOutsideParenthesis(const QString &pythonVariableName)
ctkAbstractPythonManager::systemExitExceptionHandlerEnabled
bool systemExitExceptionHandlerEnabled() const
ctkAbstractPythonManager::pythonObject
static PyObject * pythonObject(const QString &variableNameAndFunction)