ctkAbstractPythonManager.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7. http://www.commontk.org/LICENSE
  8. Unless required by applicable law or agreed to in writing, software
  9. distributed under the License is distributed on an "AS IS" BASIS,
  10. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. See the License for the specific language governing permissions and
  12. limitations under the License.
  13. =========================================================================*/
  14. // Qt includes
  15. #include <QDir>
  16. #include <QDebug>
  17. // CTK includes
  18. #include "ctkAbstractPythonManager.h"
  19. #include "ctkScriptingPythonCoreConfigure.h"
  20. // PythonQT includes
  21. #include <PythonQt.h>
  22. // STD includes
  23. #include <csignal>
  24. #ifdef __GNUC__
  25. // Disable warnings related to signal() function
  26. // See http://gcc.gnu.org/onlinedocs/gcc/Diagnostic-Pragmas.html
  27. // Note: Ideally the incriminated functions and macros should be fixed upstream ...
  28. #pragma GCC diagnostic ignored "-Wold-style-cast"
  29. #endif
  30. #ifdef CTK_PYTHONQT_WRAP_QTCORE
  31. void PythonQt_init_QtCore(PyObject*);
  32. #endif
  33. #ifdef CTK_PYTHONQT_WRAP_QTGUI
  34. void PythonQt_init_QtGui(PyObject*);
  35. #endif
  36. #ifdef CTK_PYTHONQT_WRAP_QTNETWORK
  37. void PythonQt_init_QtNetwork(PyObject*);
  38. #endif
  39. #ifdef CTK_PYTHONQT_WRAP_QTOPENGL
  40. void PythonQt_init_QtOpenGL(PyObject*);
  41. #endif
  42. #ifdef CTK_PYTHONQT_WRAP_QTSQL
  43. void PythonQt_init_QtSql(PyObject*);
  44. #endif
  45. #ifdef CTK_PYTHONQT_WRAP_QTSVG
  46. void PythonQt_init_QtSvg(PyObject*);
  47. #endif
  48. #ifdef CTK_PYTHONQT_WRAP_QTUITOOLS
  49. void PythonQt_init_QtUiTools(PyObject*);
  50. #endif
  51. #ifdef CTK_PYTHONQT_WRAP_QTWEBKIT
  52. void PythonQt_init_QtWebKit(PyObject*);
  53. #endif
  54. #ifdef CTK_PYTHONQT_WRAP_QTXML
  55. void PythonQt_init_QtXml(PyObject*);
  56. #endif
  57. #ifdef CTK_PYTHONQT_WRAP_QTXMLPATTERNS
  58. void PythonQt_init_QtXmlPatterns(PyObject*);
  59. #endif
  60. //-----------------------------------------------------------------------------
  61. ctkAbstractPythonManager::ctkAbstractPythonManager(QObject* _parent) : Superclass(_parent)
  62. {
  63. }
  64. //-----------------------------------------------------------------------------
  65. ctkAbstractPythonManager::~ctkAbstractPythonManager()
  66. {
  67. PyThreadState* state = PyThreadState_Get();
  68. Py_EndInterpreter(state);
  69. PythonQt::cleanup();
  70. }
  71. //-----------------------------------------------------------------------------
  72. PythonQtObjectPtr ctkAbstractPythonManager::mainContext()
  73. {
  74. if (!PythonQt::self())
  75. {
  76. this->initPythonQt();
  77. }
  78. if (PythonQt::self())
  79. {
  80. return PythonQt::self()->getMainModule();
  81. }
  82. return PythonQtObjectPtr();
  83. }
  84. //-----------------------------------------------------------------------------
  85. void ctkAbstractPythonManager::initPythonQt()
  86. {
  87. PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
  88. // Python maps SIGINT (control-c) to its own handler. We will remap it
  89. // to the default so that control-c works.
  90. #ifdef SIGINT
  91. signal(SIGINT, SIG_DFL);
  92. #endif
  93. PythonQtObjectPtr _mainContext = PythonQt::self()->getMainModule();
  94. this->connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)),
  95. SLOT(printStdout(const QString&)));
  96. this->connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)),
  97. SLOT(printStderr(const QString&)));
  98. #ifdef CTK_PYTHONQT_WRAP_QTCORE
  99. PythonQt_init_QtCore(0);
  100. #endif
  101. #ifdef CTK_PYTHONQT_WRAP_QTGUI
  102. PythonQt_init_QtGui(0);
  103. #endif
  104. #ifdef CTK_PYTHONQT_WRAP_QTNETWORK
  105. PythonQt_init_QtNetwork(0);
  106. #endif
  107. #ifdef CTK_PYTHONQT_WRAP_QTOPENGL
  108. PythonQt_init_QtOpenGL(0);
  109. #endif
  110. #ifdef CTK_PYTHONQT_WRAP_QTSQL
  111. PythonQt_init_QtSql(0);
  112. #endif
  113. #ifdef CTK_PYTHONQT_WRAP_QTSVG
  114. PythonQt_init_QtSvg(0);
  115. #endif
  116. #ifdef CTK_PYTHONQT_WRAP_QTUITOOLS
  117. PythonQt_init_QtUiTools(0);
  118. #endif
  119. #ifdef CTK_PYTHONQT_WRAP_QTWEBKIT
  120. PythonQt_init_QtWebKit(0);
  121. #endif
  122. #ifdef CTK_PYTHONQT_WRAP_QTXML
  123. PythonQt_init_QtXml(0);
  124. #endif
  125. #ifdef CTK_PYTHONQT_WRAP_QTXMLPATTERNS
  126. PythonQt_init_QtXmlPatterns(0);
  127. #endif
  128. QStringList initCode;
  129. initCode << "import sys";
  130. foreach (QString path, this->pythonPaths())
  131. {
  132. initCode << QString("sys.path.append('%1')").arg(QDir::fromNativeSeparators(path));
  133. }
  134. _mainContext.evalScript(initCode.join("\n"));
  135. this->preInitialization();
  136. emit this->pythonInitialized();
  137. }
  138. //-----------------------------------------------------------------------------
  139. QStringList ctkAbstractPythonManager::pythonPaths()
  140. {
  141. return QStringList();
  142. }
  143. //-----------------------------------------------------------------------------
  144. void ctkAbstractPythonManager::preInitialization()
  145. {
  146. }
  147. //-----------------------------------------------------------------------------
  148. void ctkAbstractPythonManager::registerPythonQtDecorator(QObject* decorator)
  149. {
  150. PythonQt::self()->addDecorators(decorator);
  151. }
  152. //-----------------------------------------------------------------------------
  153. void ctkAbstractPythonManager::registerClassForPythonQt(const QMetaObject* metaobject)
  154. {
  155. PythonQt::self()->registerClass(metaobject);
  156. }
  157. //-----------------------------------------------------------------------------
  158. void ctkAbstractPythonManager::registerCPPClassForPythonQt(const char* name)
  159. {
  160. PythonQt::self()->registerCPPClass(name);
  161. }
  162. //-----------------------------------------------------------------------------
  163. QVariant ctkAbstractPythonManager::executeString(const QString& code)
  164. {
  165. QVariant ret;
  166. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  167. if (main)
  168. {
  169. ret = main.evalScript(code, Py_single_input);
  170. }
  171. return ret;
  172. }
  173. //-----------------------------------------------------------------------------
  174. void ctkAbstractPythonManager::executeFile(const QString& filename)
  175. {
  176. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  177. if (main)
  178. {
  179. main.evalFile(filename);
  180. }
  181. }
  182. //-----------------------------------------------------------------------------
  183. void ctkAbstractPythonManager::addObjectToPythonMain(const QString& name, QObject* obj)
  184. {
  185. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  186. if (main && obj)
  187. {
  188. main.addObject(name, obj);
  189. }
  190. }
  191. //-----------------------------------------------------------------------------
  192. QVariant ctkAbstractPythonManager::getVariable(const QString& name)
  193. {
  194. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  195. if (main)
  196. {
  197. return PythonQt::self()->getVariable(main, name);
  198. }
  199. return QVariant();
  200. }
  201. //-----------------------------------------------------------------------------
  202. void ctkAbstractPythonManager::printStdout(const QString& text)
  203. {
  204. std::cout << qPrintable(text);
  205. }
  206. //-----------------------------------------------------------------------------
  207. void ctkAbstractPythonManager::printStderr(const QString& text)
  208. {
  209. std::cout << qPrintable(text);
  210. }