ctkAbstractPythonManager.cpp 6.4 KB

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