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 
29 // CTK includes
30 #include "ctkScriptingPythonCoreExport.h"
31 
32 class ctkAbstractPythonManagerPrivate;
33 class PythonQtForeignWrapperFactory;
34 class PythonQtObjectPtr;
35 
37 class CTK_SCRIPTING_PYTHON_CORE_EXPORT ctkAbstractPythonManager : public QObject
38 {
39  Q_OBJECT
40 
41 public:
42  typedef QObject Superclass;
43  ctkAbstractPythonManager(QObject* _parent=NULL);
44  virtual ~ctkAbstractPythonManager();
45 
50  void setInitializationFlags(int flags);
51 
53  int initializationFlags()const;
54 
59  bool initialize();
60 
63  PythonQtObjectPtr mainContext();
64 
65  void addObjectToPythonMain(const QString& name, QObject* obj);
66  void addWrapperFactory(PythonQtForeignWrapperFactory* factory);
67  void registerPythonQtDecorator(QObject* decorator);
68  void registerClassForPythonQt(const QMetaObject* metaobject);
69  void registerCPPClassForPythonQt(const char* name);
70 
72  bool systemExitExceptionHandlerEnabled()const;
73 
75  void setSystemExitExceptionHandlerEnabled(bool value);
76 
82  {
83  EvalInput = 0,
85  SingleInput
86  };
87 
90  QVariant executeString(const QString& code, ExecuteStringMode mode = FileInput);
91 
94  QVariant getVariable(const QString& varName);
95 
97  void executeFile(const QString& filename);
98 
101  void setInitializationFunction(void (*initFunction)());
102 
107  QStringList pythonAttributes(const QString& pythonVariableName,
108  const QString& module = QLatin1String("__main__"),
109  bool appendParenthesis = false) const;
110 
113  bool isPythonInitialized()const;
114 
117  bool pythonErrorOccured()const;
118 
121  void resetErrorFlag();
122 
123 Q_SIGNALS:
124 
128  void pythonPreInitialized();
129 
133  void pythonInitialized();
134 
138  void systemExitExceptionRaised(int exitCode);
139 
140 protected Q_SLOTS:
141  void printStderr(const QString&);
142  void printStdout(const QString&);
143 
144 protected:
145 
146  void initPythonQt(int flags);
147 
148  virtual QStringList pythonPaths();
149 
151  virtual void preInitialization();
152 
154  virtual void executeInitializationScripts();
155 
156 protected:
157  QScopedPointer<ctkAbstractPythonManagerPrivate> d_ptr;
158 
159 private:
160  Q_DECLARE_PRIVATE(ctkAbstractPythonManager);
161  Q_DISABLE_COPY(ctkAbstractPythonManager);
162 
163 };
164 #endif
QScopedPointer< ctkAbstractPythonManagerPrivate > d_ptr