ctkSimplePythonManager.cpp.in 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // QT includes
  2. #include <QFile>
  3. #include <QFileInfo>
  4. #include <QApplication>
  5. // PythonQT includes
  6. #include <PythonQt.h>
  7. // CTK includes
  8. #include "ctkSimplePythonManager.h"
  9. #include "ctkSimplePythonQtDecorators.h"
  10. #include "ctkSimplePythonShellConfigure.h" // For CTK_WRAP_PYTHONQT_{LIGHT, FULL} and CTK_BUILD_SHARED_LIBS
  11. #if defined(CTK_WRAP_PYTHONQT_LIGHT) || defined(CTK_WRAP_PYTHONQT_FULL)
  12. # ifndef CTK_BUILD_SHARED_LIBS
  13. // PythonQt wrapper initialization methods
  14. @CTK_PYTHONQT_INITIALIZATION_METHOD_DEFINITION@
  15. # endif
  16. #endif
  17. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  18. # if defined(CMAKE_INTDIR)
  19. # define VTK_PYTHON_LIBRARY_DIR VTK_PYTHON_LIBRARY_DIR_BUILD "/" CMAKE_INTDIR
  20. # else
  21. # define VTK_PYTHON_LIBRARY_DIR VTK_PYTHON_LIBRARY_DIR_BUILD
  22. # endif
  23. #endif
  24. //-----------------------------------------------------------------------------
  25. ctkSimplePythonManager::ctkSimplePythonManager(QObject* _parent) : Superclass(_parent)
  26. {
  27. }
  28. //-----------------------------------------------------------------------------
  29. ctkSimplePythonManager::~ctkSimplePythonManager()
  30. {
  31. }
  32. //-----------------------------------------------------------------------------
  33. QStringList ctkSimplePythonManager::pythonPaths()
  34. {
  35. QStringList paths;
  36. paths << Superclass::pythonPaths();
  37. QString self_dir = QFileInfo(qApp->applicationFilePath()).absolutePath();
  38. QString ctk_python_dir = self_dir;
  39. #if defined(CMAKE_INTDIR)
  40. ctk_python_dir.append("/..");
  41. #endif
  42. ctk_python_dir.append("/Python");
  43. paths << QFileInfo(ctk_python_dir).absoluteFilePath();
  44. #ifdef CTK_BUILD_SHARED_LIBS
  45. // Path containing python module
  46. paths << self_dir;
  47. #endif
  48. #ifdef CTK_WRAP_PYTHONQT_USE_VTK
  49. // Try to put the VTK python module location in sys.path.
  50. QString vtk_package_dir = "@VTK_DIR@/Wrapping/Python";
  51. bool found_vtk = false;
  52. QFileInfo fi(vtk_package_dir);
  53. vtk_package_dir = fi.absoluteFilePath();
  54. if (fi.isDir())
  55. {
  56. // This executable is running from the build tree. Prepend the
  57. // library directory and package directory to the search path.
  58. paths << vtk_package_dir;
  59. paths << VTK_PYTHON_LIBRARY_DIR;
  60. found_vtk = true;
  61. }
  62. Q_ASSERT(found_vtk);
  63. if (!found_vtk)
  64. {
  65. // TODO Handle case when the application is started from an installed tree
  66. }
  67. #endif
  68. return paths;
  69. }
  70. //-----------------------------------------------------------------------------
  71. void ctkSimplePythonManager::preInitialization()
  72. {
  73. Superclass::preInitialization();
  74. #if defined(CTK_WRAP_PYTHONQT_LIGHT) || defined(CTK_WRAP_PYTHONQT_FULL)
  75. # ifndef CTK_BUILD_SHARED_LIBS
  76. // Initialize wrappers
  77. @CTK_PYTHONQT_INITIALIZATION_METHOD_CALL@
  78. # endif
  79. #endif
  80. // Register decorators
  81. this->registerPythonQtDecorator(new ctkSimplePythonQtDecorators(this));
  82. // Add object to python interpreter context
  83. this->addObjectToPythonMain("_ctkSimplePythonShellInstance", qApp);
  84. }
  85. //-----------------------------------------------------------------------------
  86. void ctkSimplePythonManager::executeInitializationScripts()
  87. {
  88. QString self_dir = QFileInfo(qApp->applicationFilePath()).absolutePath();
  89. QString initFile = self_dir;
  90. #if defined(CMAKE_INTDIR)
  91. initFile.append("/..");
  92. #endif
  93. initFile.append("/Python/ctkSimplePythonShell.py");
  94. Q_ASSERT(QFile::exists(initFile));
  95. // Evaluate application script
  96. this->executeFile(initFile);
  97. }