ctkAbstractPythonManager.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. //-----------------------------------------------------------------------------
  24. ctkAbstractPythonManager::ctkAbstractPythonManager(QObject* _parent) : Superclass(_parent)
  25. {
  26. }
  27. //-----------------------------------------------------------------------------
  28. ctkAbstractPythonManager::~ctkAbstractPythonManager()
  29. {
  30. PythonQt::cleanup();
  31. }
  32. //-----------------------------------------------------------------------------
  33. PythonQtObjectPtr ctkAbstractPythonManager::mainContext()
  34. {
  35. if (!PythonQt::self())
  36. {
  37. this->initPythonQt();
  38. }
  39. if (PythonQt::self())
  40. {
  41. return PythonQt::self()->getMainModule();
  42. }
  43. return PythonQtObjectPtr();
  44. }
  45. //-----------------------------------------------------------------------------
  46. void ctkAbstractPythonManager::initPythonQt()
  47. {
  48. PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
  49. // Python maps SIGINT (control-c) to its own handler. We will remap it
  50. // to the default so that control-c works.
  51. #ifdef SIGINT
  52. signal(SIGINT, SIG_DFL);
  53. #endif
  54. PythonQtObjectPtr _mainContext = PythonQt::self()->getMainModule();
  55. this->connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)),
  56. SLOT(printStdout(const QString&)));
  57. this->connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)),
  58. SLOT(printStderr(const QString&)));
  59. QStringList initCode;
  60. initCode << "import sys";
  61. foreach (QString path, this->pythonPaths())
  62. {
  63. initCode << QString("sys.path.append('%1')").arg(QDir::fromNativeSeparators(path));
  64. }
  65. _mainContext.evalScript(initCode.join("\n"));
  66. this->preInitialization();
  67. emit this->pythonInitialized();
  68. }
  69. //-----------------------------------------------------------------------------
  70. QStringList ctkAbstractPythonManager::pythonPaths()
  71. {
  72. return QStringList();
  73. }
  74. //-----------------------------------------------------------------------------
  75. void ctkAbstractPythonManager::preInitialization()
  76. {
  77. }
  78. //-----------------------------------------------------------------------------
  79. void ctkAbstractPythonManager::registerPythonQtDecorator(QObject* decorator)
  80. {
  81. PythonQt::self()->addDecorators(decorator);
  82. }
  83. //-----------------------------------------------------------------------------
  84. void ctkAbstractPythonManager::registerClassForPythonQt(const QMetaObject* metaobject)
  85. {
  86. PythonQt::self()->registerClass(metaobject);
  87. }
  88. //-----------------------------------------------------------------------------
  89. void ctkAbstractPythonManager::registerCPPClassForPythonQt(const char* name)
  90. {
  91. PythonQt::self()->registerCPPClass(name);
  92. }
  93. //-----------------------------------------------------------------------------
  94. QVariant ctkAbstractPythonManager::executeString(const QString& code)
  95. {
  96. QVariant ret;
  97. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  98. if (main)
  99. {
  100. ret = main.evalScript(code, Py_single_input);
  101. }
  102. return ret;
  103. }
  104. //-----------------------------------------------------------------------------
  105. void ctkAbstractPythonManager::executeFile(const QString& filename)
  106. {
  107. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  108. if (main)
  109. {
  110. main.evalFile(filename);
  111. }
  112. }
  113. //-----------------------------------------------------------------------------
  114. void ctkAbstractPythonManager::addObjectToPythonMain(const QString& name, QObject* obj)
  115. {
  116. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  117. if (main && obj)
  118. {
  119. main.addObject(name, obj);
  120. }
  121. }
  122. //-----------------------------------------------------------------------------
  123. QVariant ctkAbstractPythonManager::getVariable(const QString& name)
  124. {
  125. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  126. if (main)
  127. {
  128. return PythonQt::self()->getVariable(main, name);
  129. }
  130. return QVariant();
  131. }
  132. //-----------------------------------------------------------------------------
  133. void ctkAbstractPythonManager::printStdout(const QString& text)
  134. {
  135. std::cout << qPrintable(text);
  136. }
  137. //-----------------------------------------------------------------------------
  138. void ctkAbstractPythonManager::printStderr(const QString& text)
  139. {
  140. std::cout << qPrintable(text);
  141. }