Salome HOME
From Community Forum: Save/Load incorrectly works if there are several Python modules...
[modules/gui.git] / src / SALOME_PYQT / SALOME_PYQT_GUI / SALOME_PYQT_Module.cxx
index 482060181055b430c0f389653b573489b66b77fa..4b44353888ab88c3b17cdc9f75d47a7099c192e3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2014  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -29,6 +29,8 @@
 #include <SALOME_LifeCycleCORBA.hxx>
 #include <Container_init_python.hxx>
 
+#include <QCoreApplication>
+
 //
 // NB: Python requests.
 // General rule for Python requests created by Python-based GUI modules
@@ -43,7 +45,7 @@
 // and to get C API from sip : sipBuildResult for example
 //
 
-#define INIT_FUNCTION initSalomePyQtGUILight
+#define INIT_FUNCTION PyInit_SalomePyQtGUILight
 #if defined(SIP_STATIC_MODULE)
 extern "C" void INIT_FUNCTION();
 #else
@@ -64,14 +66,13 @@ PyMODINIT_FUNC INIT_FUNCTION();
 extern "C" {
   SALOME_PYQT_EXPORT CAM_Module* createModule()
   {
-    static bool alreadyInitialized = false;
+    QCoreApplication* app = QCoreApplication::instance();
+    bool alreadyInitialized = app && app->property( "salome_pyqt_gui_light_initialized" ).toBool();
 
     if ( !alreadyInitialized ) {
-      // call only once (see comment above) !
-      PyEval_RestoreThread( KERNEL_PYTHON::_gtstate);
+      PyLockWrapper lck; // GIL acquisition
       INIT_FUNCTION();
-      PyEval_ReleaseThread( KERNEL_PYTHON::_gtstate);
-      alreadyInitialized = !alreadyInitialized;
+      if ( app ) app->setProperty( "salome_pyqt_gui_light_initialized", true );
     }
 
     return new SALOME_PYQT_Module();
@@ -111,16 +112,12 @@ SALOME_PYQT_Module::~SALOME_PYQT_Module()
 */
 QString SALOME_PYQT_Module::engineIOR() const
 {
-  // call helper to get IOR from Python module
-  static QString ior;
+  // first call helper to get IOR from Python module
+  QString ior = myHelper->engineIOR();
 
+  // if IOR is still not specified, try default implementation
+  // which loads engine to the default FactoryServer container.
   if ( ior.isEmpty() ) {
-    // first call helper to get IOR from Python module
-    ior = myHelper->engineIOR();
-  }
-  if ( ior.isEmpty() ) {
-    // if IOR is still not specified, try default implementation
-    // which loads engine to the default FactoryServer container.
     Engines::EngineComponent_var comp;
     // temporary solution
     try {
@@ -245,6 +242,18 @@ void SALOME_PYQT_Module::preferencesChanged( const QString& section, const QStri
   myHelper->preferencesChanged( section, parameter );
 }
 
+/*!
+  \brief Called when study is closed
+  \param study study being closed
+  \sa PyModuleHelper::studyClosed()
+*/
+void SALOME_PYQT_Module::studyClosed( SUIT_Study* study )
+{
+  // call helper
+  myHelper->modelClosed( study );
+  SalomeApp_Module::studyClosed( study );
+}
+
 /*!
   \brief Test if object \a what can be dragged by the user.
   \param what data object being tested