ctkSimplePythonManager.cpp.in 3.2 KB

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