ctkSimplePythonManager.cpp.in 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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_build_dir = "/../../CMakeExternals/Build/VTK/Wrapping/Python";
  51. bool found_vtk = false;
  52. QString vtk_package_dir = self_dir;
  53. #if defined(CMAKE_INTDIR)
  54. vtk_package_dir.append("/..");
  55. #endif
  56. vtk_package_dir.append(vtk_build_dir);
  57. QFileInfo fi(vtk_package_dir);
  58. vtk_package_dir = fi.absoluteFilePath();
  59. if (fi.isDir())
  60. {
  61. // This executable is running from the build tree. Prepend the
  62. // library directory and package directory to the search path.
  63. paths << vtk_package_dir;
  64. paths << VTK_PYTHON_LIBRARY_DIR;
  65. found_vtk = true;
  66. }
  67. Q_ASSERT(found_vtk);
  68. if (!found_vtk)
  69. {
  70. // TODO Handle case when the application is started from an installed tree
  71. }
  72. #endif
  73. return paths;
  74. }
  75. //-----------------------------------------------------------------------------
  76. void ctkSimplePythonManager::preInitialization()
  77. {
  78. Superclass::preInitialization();
  79. #if defined(CTK_WRAP_PYTHONQT_LIGHT) || defined(CTK_WRAP_PYTHONQT_FULL)
  80. # ifndef CTK_BUILD_SHARED_LIBS
  81. // Initialize wrappers
  82. @CTK_PYTHONQT_INITIALIZATION_METHOD_CALL@
  83. # endif
  84. #endif
  85. // Register decorators
  86. this->registerPythonQtDecorator(new ctkSimplePythonQtDecorators(this));
  87. // Add object to python interpreter context
  88. this->addObjectToPythonMain("_ctkSimplePythonShellInstance", qApp);
  89. }
  90. //-----------------------------------------------------------------------------
  91. void ctkSimplePythonManager::executeInitializationScripts()
  92. {
  93. QString self_dir = QFileInfo(qApp->applicationFilePath()).absolutePath();
  94. QString initFile = self_dir;
  95. #if defined(CMAKE_INTDIR)
  96. initFile.append("/..");
  97. #endif
  98. initFile.append("/Python/ctkSimplePythonShell.py");
  99. Q_ASSERT(QFile::exists(initFile));
  100. // Evaluate application script
  101. this->executeFile(initFile);
  102. }