Salome HOME
updated copyright message
[modules/gui.git] / src / SALOME_PYQT / SALOME_PYQT_GUILight / SALOME_PYQT_PyModule.cxx
index d7f3cd68e0de7b2569efc829b8a0a0fe5a9a75b9..c630970517c153df9bdc12992a106fe8bca24f2e 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2023  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
@@ -66,7 +66,7 @@ const int DEFAULT_GROUP = 40;
 QMutex myInitMutex;
 
 /*! DEBUG mode */
-const bool DEBUG = false;
+const bool theDEBUG = false;
 
 /*!
   \var IsCallOldMethods
@@ -168,17 +168,18 @@ public:
   FuncMsg( const QString& funcName )
   {
     myName = funcName;
-    if ( DEBUG )
+    if ( theDEBUG )
       MESSAGE( qPrintable( myName ) << " [ begin ]" );
   }
   ~FuncMsg()
   {
-    if ( DEBUG )
+    if ( theDEBUG )
       MESSAGE( qPrintable( myName ) << " [ end ]" );
   }
   void message( const QString& msg )
   {
-    if ( DEBUG )
+    (void)msg; // unused in debug mode
+    if ( theDEBUG )
       MESSAGE( qPrintable( myName ) << " : " << qPrintable( msg ) );
   }
 private:
@@ -371,7 +372,7 @@ QIcon PyModuleHelper::XmlHandler::loadIcon( const QString& fileName )
       SUIT_ResourceMgr* resMgr = module()->getApp()->resourceMgr();
       QPixmap pixmap = resMgr->loadPixmap( module()->name(),
           QApplication::translate( module()->name().toLatin1().data(),
-                                   fileName.toLatin1().data() ) );
+                                   fileName.toUtf8().data() ) );
       if ( !pixmap.isNull() )
         icon = QIcon( pixmap );
   }
@@ -388,7 +389,7 @@ QIcon PyModuleHelper::XmlHandler::loadIcon( const QString& fileName )
 */
 void PyModuleHelper::XmlHandler::createMenu( QDomNode& parentNode, 
                                              const int parentMenuId,
-                                             QMenu*    parentPopup )
+                                             QMenu*    /*parentPopup*/ )
 {
   if ( !module() || parentNode.isNull() )
     return;
@@ -609,7 +610,6 @@ void PyModuleHelper::XmlHandler::insertPopupItems( QDomNode& parentNode, QMenu*
   SALOME GUI modules.
 */
 
-PyModuleHelper::InterpMap PyModuleHelper::myInterpMap;
 LightApp_Module*          PyModuleHelper::myInitModule = 0;
 
 /*!
@@ -1716,46 +1716,26 @@ QString PyModuleHelper::engineIOR() const
     QString&        myIOR;
   };
 
-  static QString anIOR;
-
-  if ( anIOR.isEmpty() ) {
-    // post request
-    PyInterp_Dispatcher::Get()->Exec( new EngineIORReq( myInterp, 
-                                      const_cast<PyModuleHelper*>( this ),
-                                      anIOR ) );
-  }
-
+  QString anIOR;
+  // post request
+  PyInterp_Dispatcher::Get()->Exec( new EngineIORReq( myInterp, 
+                                                     const_cast<PyModuleHelper*>( this ),
+                                                     anIOR ) );
   return anIOR;
 }
 
 /*!
   \brief Initialize python subinterpreter (one per study).
   \internal
-  \param studyId study ID
 */
-void PyModuleHelper::initInterp( int studyId )
+void PyModuleHelper::initInterp()
 {
   FuncMsg fmsg( "--- PyModuleHelper::initInterp()" );
 
-  // check study Id
-  if ( !studyId ) {
-    // Error! Study Id must not be 0!
-    myInterp = 0;
-    return;
-  }
-
   QMutexLocker ml( &myInitMutex );
 
-  // try to find the subinterpreter
-  if ( myInterpMap.contains( studyId ) ) {
-    // found!
-    myInterp = myInterpMap[ studyId ];
-    return;
-  }
-
   myInterp = new SALOME_PYQT_PyInterp();
   myInterp->initialize();
-  myInterpMap[ studyId ] = myInterp;
   
 #ifndef GUI_DISABLE_CORBA
   if ( !SUIT_PYTHON::initialized ) {
@@ -1772,7 +1752,7 @@ void PyModuleHelper::initInterp( int studyId )
     }
     // ... then call a method
     int embedded = 1;
-    PyObjWrapper aRes( PyObject_CallMethod( aMod, (char*)"salome_init", (char*)"ii", studyId, embedded ) );
+    PyObjWrapper aRes( PyObject_CallMethod( aMod, (char*)"salome_init", (char*)"si", NULL, embedded ) );
     if ( !aRes ) {
       // Error!
       PyErr_Print();
@@ -1906,10 +1886,9 @@ void PyModuleHelper::internalInitialize( CAM_Application* app )
   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( app->activeStudy() );
   if ( !aStudy )
     return;
-  int aStudyId = aStudy ? aStudy->id() : 0;
 
   // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
-  initInterp( aStudyId );
+  initInterp();
   if ( !myInterp )
     return; // Error
 
@@ -1953,9 +1932,9 @@ void PyModuleHelper::internalInitialize( CAM_Application* app )
           // parse the return value
           // it should be a map: {integer:integer}
           int aKey, aValue;
-          if( key && PyInt_Check( key ) && value && PyInt_Check( value ) ) {
-            aKey   = PyInt_AsLong( key );
-            aValue = PyInt_AsLong( value );
+          if( key && PyLong_Check( key ) && value && PyLong_Check( value ) ) {
+            aKey   = PyLong_AsLong( key );
+            aValue = PyLong_AsLong( value );
             myWindowsMap[ aKey ] = aValue;
           }
         }
@@ -1973,16 +1952,16 @@ void PyModuleHelper::internalInitialize( CAM_Application* app )
     else {
       // parse the return value
       // result can be one string...
-      if ( PyString_Check( res2 ) ) {
-        myViewMgrList.append( PyString_AsString( res2 ) );
+      if ( PyUnicode_Check( res2 ) ) {
+        myViewMgrList.append( PyUnicode_AsUTF8( res2 ) );
       }
       // ... or list of strings
       else if ( PyList_Check( res2 ) ) {
         int size = PyList_Size( res2 );
         for ( int i = 0; i < size; i++ ) {
           PyObject* value = PyList_GetItem( res2, i );
-          if( value && PyString_Check( value ) ) {
-            myViewMgrList.append( PyString_AsString( value ) );
+          if( value && PyUnicode_Check( value ) ) {
+            myViewMgrList.append( PyUnicode_AsUTF8( value ) );
           }
         }
       }
@@ -2007,10 +1986,11 @@ void PyModuleHelper::internalActivate( SUIT_Study* study )
 
   // get study Id
   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( study );
-  int aStudyId = aStudy ? aStudy->id() : 0;
+  if ( !aStudy )
+    return;
 
   // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
-  initInterp( aStudyId );
+  initInterp();
   if ( !myInterp ) {
     myLastActivateStatus = false;
     return; // Error
@@ -2059,10 +2039,11 @@ void PyModuleHelper::internalCustomize( SUIT_Study* study )
 
   // get study Id
   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( study );
-  int aStudyId = aStudy ? aStudy->id() : 0;
+  if ( !aStudy )
+    return;
 
   // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
-  initInterp( aStudyId );
+  initInterp();
   if ( !myInterp ) {
     myLastActivateStatus = false;
     return; // Error
@@ -2102,7 +2083,7 @@ void PyModuleHelper::internalCustomize( SUIT_Study* study )
 
   \param study parent study
 */
-void PyModuleHelper::internalDeactivate( SUIT_Study* study )
+void PyModuleHelper::internalDeactivate( SUIT_Study* /*study*/ )
 {
   FuncMsg fmsg( "--- PyModuleHelper::internalDeactivate()" );
 
@@ -2135,7 +2116,8 @@ void PyModuleHelper::internalClosedStudy( SUIT_Study* theStudy )
   // Get study Id
   // get study Id
   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( theStudy );
-  int aStudyId = aStudy ? aStudy->id() : 0;
+  if ( !aStudy )
+    return;
 
   // check that Python subinterpreter is initialized and Python module is imported
   if ( !myInterp || !myPyModule ) {
@@ -2144,7 +2126,7 @@ void PyModuleHelper::internalClosedStudy( SUIT_Study* theStudy )
   }
   // then call Python module's deactivate() method
   if ( PyObject_HasAttrString( myPyModule , (char*)"closeStudy" ) ) {
-    PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"closeStudy", (char*)"i", aStudyId ) );
+    PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"closeStudy", (char*)"" ) );
     if( !res ) {
       PyErr_Print();
     }
@@ -2177,8 +2159,8 @@ void PyModuleHelper::internalPreferencesChanged( const QString& section, const Q
     PyObjWrapper res( PyObject_CallMethod( myPyModule,
                                            (char*)"preferenceChanged", 
                                            (char*)"ss", 
-                                           section.toLatin1().constData(), 
-                                           setting.toLatin1().constData() ) );
+                                           section.toUtf8().constData(), 
+                                           setting.toUtf8().constData() ) );
     if( !res ) {
       PyErr_Print();
     }
@@ -2203,12 +2185,11 @@ void PyModuleHelper::internalStudyChanged( SUIT_Study* study )
 
   // get study Id
   LightApp_Study* aStudy = dynamic_cast<LightApp_Study*>( study );
-  int id = aStudy ? aStudy->id() : 0;
-
-  fmsg.message( QString( "study id = %1" ).arg( id ) );
+  if ( !aStudy )
+    return;
 
   // initialize Python subinterpreter (on per study) and put it in <myInterp> variable
-  initInterp( id );
+  initInterp();
   if ( !myInterp )
     return; // Error
 
@@ -2225,7 +2206,7 @@ void PyModuleHelper::internalStudyChanged( SUIT_Study* study )
 
   // call Python module's activeStudyChanged() method
   if ( PyObject_HasAttrString( myPyModule, (char*)"activeStudyChanged" ) ) {
-    PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"activeStudyChanged", (char*)"i", id ) );
+    PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"activeStudyChanged", (char*)"" ) );
     if( !res ) {
       PyErr_Print();
     }
@@ -2331,9 +2312,9 @@ void PyModuleHelper::internalContextMenu( const QString& context, QMenu* menu )
     PyObjWrapper res( PyObject_CallMethod( myPyModule,
                                            (char*)"definePopup",
                                            (char*)"sss",
-                                           context.toLatin1().constData(),
-                                           aObject.toLatin1().constData(),
-                                           aParent.toLatin1().constData() ) );
+                                           context.toUtf8().constData(),
+                                           aObject.toUtf8().constData(),
+                                           aParent.toUtf8().constData() ) );
     if( !res ) {
       PyErr_Print();
     }
@@ -2366,7 +2347,7 @@ void PyModuleHelper::internalContextMenu( const QString& context, QMenu* menu )
                                             (char*)"createPopupMenu",
                                             (char*)"Os",
                                             sipPopup.get(),
-                                            context.toLatin1().constData() ) );
+                                            context.toUtf8().constData() ) );
     if( !res1 ) {
       PyErr_Print();
     }
@@ -2379,9 +2360,9 @@ void PyModuleHelper::internalContextMenu( const QString& context, QMenu* menu )
                                             (char*)"customPopup",
                                             (char*)"Osss",
                                             sipPopup.get(),
-                                            aContext.toLatin1().constData(),
-                                            aObject.toLatin1().constData(),
-                                            aParent.toLatin1().constData() ) );
+                                            aContext.toUtf8().constData(),
+                                            aObject.toUtf8().constData(),
+                                            aParent.toUtf8().constData() ) );
     if( !res2 ) {
       PyErr_Print();
     }
@@ -2525,12 +2506,12 @@ void PyModuleHelper::internalSave( QStringList& files, const QString& url )
     // try with two parameters (new syntax)
     PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"saveFiles",
                                            (char*)"ss",
-                                           files.first().toLatin1().constData(),
-                                           url.toLatin1().constData() ) );
+                                           files.first().toUtf8().constData(),
+                                           url.toUtf8().constData() ) );
     if ( !res )
       // try with single parameter (old syntax)
       res = PyObject_CallMethod( myPyModule, (char*)"saveFiles",
-                                 (char*)"s", files.first().toLatin1().constData() );
+                                 (char*)"s", files.first().toUtf8().constData() );
     
     if ( !res ) {
       PyErr_Print();
@@ -2538,8 +2519,8 @@ void PyModuleHelper::internalSave( QStringList& files, const QString& url )
     else {
       // parse the return value
       // result can be one string...
-      if ( PyString_Check( res ) ) {
-        QString astr = PyString_AsString( res );
+      if ( PyUnicode_Check( res ) ) {
+        QString astr = PyUnicode_AsUTF8( res );
         files.append( astr );
       }
       //also result can be a list...
@@ -2547,8 +2528,8 @@ void PyModuleHelper::internalSave( QStringList& files, const QString& url )
         int size = PyList_Size( res );
         for ( int i = 0; i < size; i++ ) {
           PyObject* value = PyList_GetItem( res, i );
-          if ( value && PyString_Check( value ) ) {
-            files.append( PyString_AsString( value ) );
+          if ( value && PyUnicode_Check( value ) ) {
+            files.append( PyUnicode_AsUTF8( value ) );
           }
         }
       }
@@ -2585,7 +2566,7 @@ void PyModuleHelper::internalLoad( const QStringList& files, const QString& url,
     // try with two parameters (new syntax)
     PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"openFiles",
                                            (char*)"Os", sipList.get(),
-                                           url.toLatin1().constData() ) );
+                                           url.toUtf8().constData() ) );
 
     if ( !res )
       // try with single parameter (old syntax)
@@ -2619,7 +2600,7 @@ void PyModuleHelper::internalDumpPython( QStringList& files )
 
   if ( PyObject_HasAttrString(myPyModule, (char*)"dumpStudy") ) {
     PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"dumpStudy",
-                                           (char*)"s", files.first().toLatin1().constData()));
+                                           (char*)"s", files.first().toUtf8().constData()));
 
     if ( !res ) {
       PyErr_Print();
@@ -2627,8 +2608,8 @@ void PyModuleHelper::internalDumpPython( QStringList& files )
     else {
       // parse the return value
       // result can be one string...
-      if ( PyString_Check( res ) ) {
-        QString astr = PyString_AsString( res );
+      if ( PyUnicode_Check( res ) ) {
+        QString astr = PyUnicode_AsUTF8( res );
         //SCRUTE(astr);
         files.append(astr);
       }
@@ -2637,8 +2618,8 @@ void PyModuleHelper::internalDumpPython( QStringList& files )
         int size = PyList_Size( res );
         for ( int i = 0; i < size; i++ ) {
           PyObject* value = PyList_GetItem( res, i );
-          if( value && PyString_Check( value ) ) {
-            files.append( PyString_AsString( value ) );
+          if( value && PyUnicode_Check( value ) ) {
+            files.append( PyUnicode_AsUTF8( value ) );
           }
         }
       }
@@ -2665,7 +2646,7 @@ bool PyModuleHelper::internalIsDraggable( LightApp_DataObject* what )
 
   if ( PyObject_HasAttrString(myPyModule , (char*)"isDraggable") ) {
     PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"isDraggable",
-                      (char*)"s", what->entry().toLatin1().constData() ) );
+                      (char*)"s", what->entry().toUtf8().constData() ) );
     if( !res || !PyBool_Check( res )) {
       PyErr_Print();
       draggable = false;
@@ -2697,7 +2678,7 @@ bool PyModuleHelper::internalIsDropAccepted( LightApp_DataObject* where )
 
   if ( PyObject_HasAttrString(myPyModule , (char*)"isDropAccepted") ) {
     PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"isDropAccepted",
-                      (char*)"s", where->entry().toLatin1().constData() ) );
+                      (char*)"s", where->entry().toUtf8().constData() ) );
     if( !res || !PyBool_Check( res )) {
       PyErr_Print();
       dropAccepted = false;
@@ -2747,7 +2728,7 @@ void PyModuleHelper::internalDropObjects( const DataObjectList& what, SUIT_DataO
   if ( PyObject_HasAttrString(myPyModule, (char*)"dropObjects") ) {
       PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"dropObjects", (char*)"Osii",
                         sipList.get(),
-                        whereObject->entry().toLatin1().constData(),
+                        whereObject->entry().toUtf8().constData(),
                         row, action ) );
     
     if( !res ) {
@@ -2781,8 +2762,8 @@ QString PyModuleHelper::internalEngineIOR() const
       }
       else {
         // parse the return value, result chould be string
-        if ( PyString_Check( res ) ) {
-          ior = PyString_AsString( res );
+        if ( PyUnicode_Check( res ) ) {
+          ior = PyUnicode_AsUTF8( res );
         }
       }
     }
@@ -2835,7 +2816,7 @@ void PyModuleHelper::internalOBClickedPython( const QString& theObj, int theColu
     return; // Error
 
   if ( PyObject_HasAttrString( myPyModule, (char*)"onObjectBrowserClicked" ) ) {
-    PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"onObjectBrowserClicked", (char*)"si", theObj.toLatin1().constData(), theColumn ) );
+    PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"onObjectBrowserClicked", (char*)"si", theObj.toUtf8().constData(), theColumn ) );
     if( !res ) {
       PyErr_Print();
     }
@@ -2873,8 +2854,8 @@ void PyModuleHelper::onObjectBrowserClicked(SUIT_DataObject* theObj, int theColu
     }
   private:
     PyModuleHelper* myHelper;
-    int    myColumn;
     QString myEntry;
+    int myColumn;
   };
   
   // Posting the request only if dispatcher is not busy!