ctkAbstractPythonManager.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) 2010 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 CTK_PYTHONQT_WRAP_QTGUI
  25. void PythonQt_init_QtGui(PyObject*);
  26. #endif
  27. #ifdef CTK_PYTHONQT_WRAP_QTNETWORK
  28. void PythonQt_init_QtNetwork(PyObject*);
  29. #endif
  30. #ifdef CTK_PYTHONQT_WRAP_QTOPENGL
  31. void PythonQt_init_QtOpenGL(PyObject*);
  32. #endif
  33. #ifdef CTK_PYTHONQT_WRAP_QTSQL
  34. void PythonQt_init_QtSql(PyObject*);
  35. #endif
  36. #ifdef CTK_PYTHONQT_WRAP_QTSVG
  37. void PythonQt_init_QtSvg(PyObject*);
  38. #endif
  39. #ifdef CTK_PYTHONQT_WRAP_QTUITOOLS
  40. void PythonQt_init_QtUITools(PyObject*);
  41. #endif
  42. #ifdef CTK_PYTHONQT_WRAP_QTWEBKIT
  43. void PythonQt_init_QtWebKit(PyObject*);
  44. #endif
  45. #ifdef CTK_PYTHONQT_WRAP_QTXML
  46. void PythonQt_init_QtXml(PyObject*);
  47. #endif
  48. #ifdef CTK_PYTHONQT_WRAP_QTXMLPATTERNS
  49. void PythonQt_init_QtXmlPatterns(PyObject*);
  50. #endif
  51. //-----------------------------------------------------------------------------
  52. ctkAbstractPythonManager::ctkAbstractPythonManager(QObject* _parent) : Superclass(_parent)
  53. {
  54. }
  55. //-----------------------------------------------------------------------------
  56. ctkAbstractPythonManager::~ctkAbstractPythonManager()
  57. {
  58. PyThreadState* state = PyThreadState_Get();
  59. Py_EndInterpreter(state);
  60. PythonQt::cleanup();
  61. }
  62. //-----------------------------------------------------------------------------
  63. PythonQtObjectPtr ctkAbstractPythonManager::mainContext()
  64. {
  65. if (!PythonQt::self())
  66. {
  67. this->initPythonQt();
  68. }
  69. if (PythonQt::self())
  70. {
  71. return PythonQt::self()->getMainModule();
  72. }
  73. return PythonQtObjectPtr();
  74. }
  75. //-----------------------------------------------------------------------------
  76. void ctkAbstractPythonManager::initPythonQt()
  77. {
  78. PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
  79. // Python maps SIGINT (control-c) to its own handler. We will remap it
  80. // to the default so that control-c works.
  81. #ifdef SIGINT
  82. signal(SIGINT, SIG_DFL);
  83. #endif
  84. PythonQtObjectPtr _mainContext = PythonQt::self()->getMainModule();
  85. this->connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)),
  86. SLOT(printStdout(const QString&)));
  87. this->connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)),
  88. SLOT(printStderr(const QString&)));
  89. #ifdef CTK_PYTHONQT_WRAP_QTGUI
  90. PythonQt_init_QtGui(0);
  91. #endif
  92. #ifdef CTK_PYTHONQT_WRAP_QTNETWORK
  93. PythonQt_init_QtNetwork(0);
  94. #endif
  95. #ifdef CTK_PYTHONQT_WRAP_QTOPENGL
  96. PythonQt_init_QtOpenGL(0);
  97. #endif
  98. #ifdef CTK_PYTHONQT_WRAP_QTSQL
  99. PythonQt_init_QtSql(0);
  100. #endif
  101. #ifdef CTK_PYTHONQT_WRAP_QTSVG
  102. PythonQt_init_QtSvg(0);
  103. #endif
  104. #ifdef CTK_PYTHONQT_WRAP_QTUITOOLS
  105. PythonQt_init_QtUITools(0);
  106. #endif
  107. #ifdef CTK_PYTHONQT_WRAP_QTWEBKIT
  108. PythonQt_init_QtWebKit(0);
  109. #endif
  110. #ifdef CTK_PYTHONQT_WRAP_QTXML
  111. PythonQt_init_QtXml(0);
  112. #endif
  113. #ifdef CTK_PYTHONQT_WRAP_QTXMLPATTERNS
  114. PythonQt_init_QtXmlPatterns(0);
  115. #endif
  116. QStringList initCode;
  117. initCode << "import sys";
  118. foreach (QString path, this->pythonPaths())
  119. {
  120. initCode << QString("sys.path.append('%1')").arg(QDir::fromNativeSeparators(path));
  121. }
  122. _mainContext.evalScript(initCode.join("\n"));
  123. this->preInitialization();
  124. emit this->pythonInitialized();
  125. }
  126. //-----------------------------------------------------------------------------
  127. QStringList ctkAbstractPythonManager::pythonPaths()
  128. {
  129. return QStringList();
  130. }
  131. //-----------------------------------------------------------------------------
  132. void ctkAbstractPythonManager::preInitialization()
  133. {
  134. }
  135. //-----------------------------------------------------------------------------
  136. void ctkAbstractPythonManager::registerPythonQtDecorator(QObject* decorator)
  137. {
  138. PythonQt::self()->addDecorators(decorator);
  139. }
  140. //-----------------------------------------------------------------------------
  141. void ctkAbstractPythonManager::registerClassForPythonQt(const QMetaObject* metaobject)
  142. {
  143. PythonQt::self()->registerClass(metaobject);
  144. }
  145. //-----------------------------------------------------------------------------
  146. void ctkAbstractPythonManager::registerCPPClassForPythonQt(const char* name)
  147. {
  148. PythonQt::self()->registerCPPClass(name);
  149. }
  150. //-----------------------------------------------------------------------------
  151. QVariant ctkAbstractPythonManager::executeString(const QString& code)
  152. {
  153. QVariant ret;
  154. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  155. if (main)
  156. {
  157. ret = main.evalScript(code, Py_single_input);
  158. }
  159. return ret;
  160. }
  161. //-----------------------------------------------------------------------------
  162. void ctkAbstractPythonManager::executeFile(const QString& filename)
  163. {
  164. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  165. if (main)
  166. {
  167. main.evalFile(filename);
  168. }
  169. }
  170. //-----------------------------------------------------------------------------
  171. void ctkAbstractPythonManager::addObjectToPythonMain(const QString& name, QObject* obj)
  172. {
  173. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  174. if (main && obj)
  175. {
  176. main.addObject(name, obj);
  177. }
  178. }
  179. //-----------------------------------------------------------------------------
  180. QVariant ctkAbstractPythonManager::getVariable(const QString& name)
  181. {
  182. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  183. if (main)
  184. {
  185. return PythonQt::self()->getVariable(main, name);
  186. }
  187. return QVariant();
  188. }
  189. //-----------------------------------------------------------------------------
  190. void ctkAbstractPythonManager::printStdout(const QString& text)
  191. {
  192. std::cout << qPrintable(text);
  193. }
  194. //-----------------------------------------------------------------------------
  195. void ctkAbstractPythonManager::printStderr(const QString& text)
  196. {
  197. std::cout << qPrintable(text);
  198. }