ctkAbstractPythonManager.cpp 6.7 KB

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