ctkSimplePythonManager.cpp.in 3.4 KB

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