ctkAbstractPythonManager.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*=========================================================================
  2. Library: CTK
  3. Copyright (c) Kitware Inc.
  4. All rights reserved.
  5. Distributed under a BSD License. See LICENSE.txt file.
  6. This software is distributed "AS IS" WITHOUT ANY WARRANTY; without even
  7. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  8. See the above copyright notice for more information.
  9. =========================================================================*/
  10. // Qt includes
  11. #include <QDir>
  12. #include <QDebug>
  13. // CTK includes
  14. #include "ctkAbstractPythonManager.h"
  15. // PythonQT includes
  16. #include <PythonQt.h>
  17. // STD includes
  18. #include <csignal>
  19. //-----------------------------------------------------------------------------
  20. ctkAbstractPythonManager::ctkAbstractPythonManager(QObject* _parent) : Superclass(_parent)
  21. {
  22. }
  23. //-----------------------------------------------------------------------------
  24. ctkAbstractPythonManager::~ctkAbstractPythonManager()
  25. {
  26. PythonQt::cleanup();
  27. }
  28. //-----------------------------------------------------------------------------
  29. PythonQtObjectPtr ctkAbstractPythonManager::mainContext()
  30. {
  31. if (!PythonQt::self())
  32. {
  33. this->initPythonQt();
  34. }
  35. if (PythonQt::self())
  36. {
  37. return PythonQt::self()->getMainModule();
  38. }
  39. return PythonQtObjectPtr();
  40. }
  41. //-----------------------------------------------------------------------------
  42. void ctkAbstractPythonManager::initPythonQt()
  43. {
  44. PythonQt::init(PythonQt::IgnoreSiteModule | PythonQt::RedirectStdOut);
  45. // Python maps SIGINT (control-c) to its own handler. We will remap it
  46. // to the default so that control-c works.
  47. #ifdef SIGINT
  48. signal(SIGINT, SIG_DFL);
  49. #endif
  50. PythonQtObjectPtr _mainContext = PythonQt::self()->getMainModule();
  51. this->connect(PythonQt::self(), SIGNAL(pythonStdOut(const QString&)),
  52. SLOT(printStdout(const QString&)));
  53. this->connect(PythonQt::self(), SIGNAL(pythonStdErr(const QString&)),
  54. SLOT(printStderr(const QString&)));
  55. QStringList initCode;
  56. initCode << "import sys";
  57. foreach (QString path, this->pythonPaths())
  58. {
  59. initCode << QString("sys.path.append('%1')").arg(QDir::fromNativeSeparators(path));
  60. }
  61. _mainContext.evalScript(initCode.join("\n"));
  62. this->preInitialization();
  63. emit this->pythonInitialized();
  64. }
  65. //-----------------------------------------------------------------------------
  66. QStringList ctkAbstractPythonManager::pythonPaths()
  67. {
  68. return QStringList();
  69. }
  70. //-----------------------------------------------------------------------------
  71. void ctkAbstractPythonManager::preInitialization()
  72. {
  73. }
  74. //-----------------------------------------------------------------------------
  75. void ctkAbstractPythonManager::registerPythonQtDecorator(QObject* decorator)
  76. {
  77. PythonQt::self()->addDecorators(decorator);
  78. }
  79. //-----------------------------------------------------------------------------
  80. void ctkAbstractPythonManager::registerClassForPythonQt(const QMetaObject* metaobject)
  81. {
  82. PythonQt::self()->registerClass(metaobject);
  83. }
  84. //-----------------------------------------------------------------------------
  85. void ctkAbstractPythonManager::registerCPPClassForPythonQt(const char* name)
  86. {
  87. PythonQt::self()->registerCPPClass(name);
  88. }
  89. //-----------------------------------------------------------------------------
  90. QVariant ctkAbstractPythonManager::executeString(const QString& code)
  91. {
  92. QVariant ret;
  93. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  94. if (main)
  95. {
  96. ret = main.evalScript(code, Py_single_input);
  97. }
  98. return ret;
  99. }
  100. //-----------------------------------------------------------------------------
  101. void ctkAbstractPythonManager::executeFile(const QString& filename)
  102. {
  103. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  104. if (main)
  105. {
  106. main.evalFile(filename);
  107. }
  108. }
  109. //-----------------------------------------------------------------------------
  110. void ctkAbstractPythonManager::addObjectToPythonMain(const QString& name, QObject* obj)
  111. {
  112. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  113. if (main && obj)
  114. {
  115. main.addObject(name, obj);
  116. }
  117. }
  118. //-----------------------------------------------------------------------------
  119. QVariant ctkAbstractPythonManager::getVariable(const QString& name)
  120. {
  121. PythonQtObjectPtr main = ctkAbstractPythonManager::mainContext();
  122. if (main)
  123. {
  124. return PythonQt::self()->getVariable(main, name);
  125. }
  126. return QVariant();
  127. }
  128. //-----------------------------------------------------------------------------
  129. void ctkAbstractPythonManager::printStdout(const QString& text)
  130. {
  131. std::cout << qPrintable(text);
  132. }
  133. //-----------------------------------------------------------------------------
  134. void ctkAbstractPythonManager::printStderr(const QString& text)
  135. {
  136. std::cout << qPrintable(text);
  137. }