Salome HOME
Porting to Qt-5.9.0, PyQt-5.9.0, sip-4.19.3
[modules/gui.git] / src / SALOME_PYQT / SALOME_PYQT_GUILight / SALOME_PYQT_PyModule.cxx
index 9141c339acc907a4167b992dc0e0be448199d68b..b25bd2c4148c7db3e66b2a8bb6491080976fb767 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2016  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
@@ -65,6 +65,9 @@ const int DEFAULT_GROUP = 40;
 */
 QMutex myInitMutex;
 
+/*! DEBUG mode */
+const bool DEBUG = false;
+
 /*!
   \var IsCallOldMethods
   \brief Allow calling obsolete callback methods.
@@ -165,15 +168,18 @@ public:
   FuncMsg( const QString& funcName )
   {
     myName = funcName;
-    MESSAGE( qPrintable( myName ) << " [ begin ]" );
+    if ( DEBUG )
+      MESSAGE( qPrintable( myName ) << " [ begin ]" );
   }
   ~FuncMsg()
   {
-    MESSAGE( qPrintable( myName ) << " [ end ]" );
+    if ( DEBUG )
+      MESSAGE( qPrintable( myName ) << " [ end ]" );
   }
   void message( const QString& msg )
   {
-    MESSAGE( qPrintable( myName ) << " : " << qPrintable( msg ) );
+    if ( DEBUG )
+      MESSAGE( qPrintable( myName ) << " : " << qPrintable( msg ) );
   }
 private:
   QString myName;
@@ -1156,6 +1162,44 @@ void PyModuleHelper::actionActivated()
   PyInterp_Dispatcher::Get()->Exec( new ActionReq( myInterp, this, myModule->actionId( action ) ) );
 }
 
+/*!
+  \brief update selection from other views or modules.
+
+  Called when selection is modified outside.
+*/
+void PyModuleHelper::selectionUpdated(const QStringList& entries)
+{
+  FuncMsg fmsg( "PyModuleHelper::selectionUpdated()" );
+  MESSAGE("selectionUpdated");
+
+  // perform synchronous request to Python event dispatcher
+  class SelectionReq : public PyInterp_LockRequest
+  {
+  public:
+    SelectionReq( PyInterp_Interp* _py_interp,
+                  PyModuleHelper*  _helper,
+                  const QStringList& _entries )
+      : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
+        myHelper( _helper ),
+        myEntries( _entries  )
+    {
+      MESSAGE("SelectionReq");
+    }
+  protected:
+    virtual void execute()
+    {
+      MESSAGE("execute");
+      myHelper->internalSelectionUpdated( myEntries );
+    }
+  private:
+    PyModuleHelper* myHelper;
+    const QStringList& myEntries;
+  };
+
+  // post request
+  PyInterp_Dispatcher::Get()->Exec( new SelectionReq( myInterp, this, entries ) );
+}
+
 /*!
   \brief Process context popup menu request.
   
@@ -1368,8 +1412,9 @@ void PyModuleHelper::cloneView( SUIT_ViewWindow* view )
 /*!
   \brief Save module data. Called when user saves study.
   \param files output list of files where module stores data
+  \param url study URL
 */
-void PyModuleHelper::save( QStringList& files )
+void PyModuleHelper::save( QStringList& files, const QString& url )
 {
   FuncMsg fmsg( "PyModuleHelper::save()" );
 
@@ -1385,33 +1430,38 @@ void PyModuleHelper::save( QStringList& files )
   public:     
     SaveReq( PyInterp_Interp* _py_interp,
              PyModuleHelper*  _helper,
-             QStringList&     _files )
+             QStringList&     _files,
+             const QString&   _url )
       : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
         myHelper( _helper ) ,
-        myFiles( _files )
+        myFiles( _files ),
+        myUrl( _url )
     {}
   protected:
     virtual void execute()
     {
-      myHelper->internalSave( myFiles );
+      myHelper->internalSave( myFiles, myUrl );
     }
   private:
     PyModuleHelper* myHelper;
     QStringList&    myFiles;
+    QString         myUrl;
   };
   
   // Posting the request only if dispatcher is not busy!
   // Executing the request synchronously
   if ( !PyInterp_Dispatcher::Get()->IsBusy() )
-    PyInterp_Dispatcher::Get()->Exec( new SaveReq( myInterp, this, files ) );
+    PyInterp_Dispatcher::Get()->Exec( new SaveReq( myInterp, this, files, url ) );
 }
 
 /*
- \brief Load module data. Called when user opens study 
- and activates module.
- \param files list of files where module data is stored
+  \brief Load module data. Called when user opens study 
+  and activates module.
+  \param files list of files where module data is stored
+  \param url study URL
+  \return \c true if loading has been finished successfully or \c false otherwise
 */
-bool PyModuleHelper::load( const QStringList& files )
+bool PyModuleHelper::load( const QStringList& files, const QString& url )
 {
   FuncMsg fmsg( "PyModuleHelper::load()" );
 
@@ -1423,27 +1473,30 @@ bool PyModuleHelper::load( const QStringList& files )
     LoadReq( PyInterp_Interp* _py_interp,
              PyModuleHelper*  _helper,
              QStringList      _files,
+             const QString&   _url,
              bool&            _loaded )
       : PyInterp_LockRequest( _py_interp, 0, true ), // this request should be processed synchronously (sync == true)
         myHelper( _helper ) ,
         myFiles( _files ),
+        myUrl( _url ),
         myLoaded( _loaded )
     {}
   protected:
     virtual void execute()
     {
-      myHelper->internalLoad( myFiles, myLoaded );
+      myHelper->internalLoad( myFiles, myUrl, myLoaded );
     }
   private:
     PyModuleHelper* myHelper;
     QStringList     myFiles;
+    QString         myUrl;
     bool&           myLoaded;
   };
   
   // Posting the request only if dispatcher is not busy!
   // Executing the request synchronously
   if ( !PyInterp_Dispatcher::Get()->IsBusy() )
-    PyInterp_Dispatcher::Get()->Exec( new LoadReq( myInterp, this, files, loaded ) );
+    PyInterp_Dispatcher::Get()->Exec( new LoadReq( myInterp, this, files, url, loaded ) );
 
   return loaded;
 }
@@ -1812,7 +1865,12 @@ void PyModuleHelper::setWorkSpace()
   }
 #if SIP_VERSION < 0x040800
   PyObjWrapper pyws( sipBuildResult( 0, "M", aWorkspace, sipClass_QWidget) );
-#else
+#else 
+ #if SIP_VERSION >= 0x041303
+  static const sipTypeDef *sipType_QWidget = 0;
+  if (!sipType_QWidget)
+    sipType_QWidget = sipFindType("QWidget");
+ #endif
   PyObjWrapper pyws( sipBuildResult( 0, "D", aWorkspace, sipType_QWidget , NULL) );
 #endif
   // ... and finally call Python module's setWorkSpace() method (obsolete)
@@ -2205,6 +2263,48 @@ void PyModuleHelper::internalActionActivated( int id )
   }
 }
 
+/*!
+  \brief update selection from other views or modules
+  \internal
+
+  Performs the following actions:
+  - calls Python module's onSelectionpdated(entries) method
+
+  \param list of entries
+*/
+void PyModuleHelper::internalSelectionUpdated(const QStringList& entries)
+{
+  FuncMsg fmsg("--- PyModuleHelper::internalSelectionUpdated()");
+  MESSAGE("internalSelectionUpdated");
+
+  // Python interpreter should be initialized and Python module should be imported first
+  if (!myInterp || !myPyModule)
+    return; // Error
+
+  QStringList* theList = new QStringList(entries);
+
+#if SIP_VERSION < 0x040800
+  PyObjWrapper sipList(sipBuildResult(0, "M", theList, sipClass_QStringList));
+#else
+ #if SIP_VERSION >= 0x041303
+  static const sipTypeDef *sipType_QStringList = 0;
+  if (!sipType_QStringList)
+    sipType_QStringList = sipFindType("QStringList");
+ #endif
+  PyObjWrapper sipList( sipBuildResult( 0, "D", theList, sipType_QStringList, NULL ) );
+#endif
+  if (PyObject_HasAttrString(myPyModule, (char*) "onSelectionUpdated"))
+    {
+      MESSAGE("call onSelectionUpdated");
+      PyObjWrapper res(PyObject_CallMethod(myPyModule, (char*) "onSelectionUpdated", (char*) "O", sipList.get()));
+
+      if (!res)
+        {
+          PyErr_Print();
+        }
+    }
+}
+
 /*!
   \brief Context popup menu handling callback function
   \internal
@@ -2264,6 +2364,11 @@ void PyModuleHelper::internalContextMenu( const QString& context, QMenu* menu )
 #if SIP_VERSION < 0x040800
   PyObjWrapper sipPopup( sipBuildResult( 0, "M", menu, sipClass_QMenu ) );
 #else
+ #if SIP_VERSION >= 0x041303
+  static const sipTypeDef *sipType_QMenu = 0;
+  if (!sipType_QMenu)
+    sipType_QMenu = sipFindType("QMenu");
+ #endif
   PyObjWrapper sipPopup( sipBuildResult( 0, "D", menu, sipType_QMenu, NULL ) );
 #endif
 
@@ -2415,8 +2520,9 @@ void PyModuleHelper::internalCloneView( SUIT_ViewWindow* view )
   \brief Module data saving callback function.
   \internal
   \param files output list of files where module stores data
+  \param url study URL
 */
-void PyModuleHelper::internalSave( QStringList& files )
+void PyModuleHelper::internalSave( QStringList& files, const QString& url )
 {
   FuncMsg fmsg( "--- PyModuleHelper::internalSave()" );
 
@@ -2428,9 +2534,16 @@ void PyModuleHelper::internalSave( QStringList& files )
 
   if ( PyObject_HasAttrString(myPyModule, (char*)"saveFiles") ) {
 
+    // try with two parameters (new syntax)
     PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"saveFiles",
-                                           (char*)"s", files.first().toLatin1().constData() ) );
-
+                                           (char*)"ss",
+                                           files.first().toLatin1().constData(),
+                                           url.toLatin1().constData() ) );
+    if ( !res )
+      // try with single parameter (old syntax)
+      res = PyObject_CallMethod( myPyModule, (char*)"saveFiles",
+                                 (char*)"s", files.first().toLatin1().constData() );
+    
     if ( !res ) {
       PyErr_Print();
     }
@@ -2459,9 +2572,10 @@ void PyModuleHelper::internalSave( QStringList& files )
   \brief Module data loading callback function.
   \internal
   \param files list of files where module data is stored
+  \param url study URL
   \param opened output success flag
 */
-void PyModuleHelper::internalLoad( const QStringList& files, bool& opened )
+void PyModuleHelper::internalLoad( const QStringList& files, const QString& url, bool& opened )
 {
   FuncMsg fmsg( "--- PyModuleHelper::internalLoad()" );
 
@@ -2475,16 +2589,30 @@ void PyModuleHelper::internalLoad( const QStringList& files, bool& opened )
 #if SIP_VERSION < 0x040800
   PyObjWrapper sipList( sipBuildResult( 0, "M", theList, sipClass_QStringList ) );
 #else
+ #if SIP_VERSION >= 0x041303
+  static const sipTypeDef *sipType_QStringList = 0;
+  if (!sipType_QStringList)
+    sipType_QStringList = sipFindType("QStringList");
+ #endif
   PyObjWrapper sipList( sipBuildResult( 0, "D", theList, sipType_QStringList, NULL ) );
 #endif
   if ( PyObject_HasAttrString(myPyModule , (char*)"openFiles") ) {
+
+    // try with two parameters (new syntax)
     PyObjWrapper res( PyObject_CallMethod( myPyModule, (char*)"openFiles",
-                                           (char*)"O", sipList.get()));
-    if( !res || !PyBool_Check( res )) {
+                                           (char*)"Os", sipList.get(),
+                                           url.toLatin1().constData() ) );
+
+    if ( !res )
+      // try with single parameter (old syntax)
+      res = PyObject_CallMethod( myPyModule, (char*)"openFiles",
+                                 (char*)"O", sipList.get() );
+
+    if ( !res || !PyBool_Check( res ) ) {
       PyErr_Print();
       opened = false;
     }
-    else{
+    else {
       opened = PyObject_IsTrue( res );
     }
   }
@@ -2629,6 +2757,11 @@ void PyModuleHelper::internalDropObjects( const DataObjectList& what, SUIT_DataO
 #if SIP_VERSION < 0x040800
   PyObjWrapper sipList( sipBuildResult( 0, "M", theList, sipClass_QStringList) );
 #else
+ #if SIP_VERSION >= 0x041303
+  static const sipTypeDef *sipType_QStringList = 0;
+  if (!sipType_QStringList)
+    sipType_QStringList = sipFindType("QStringList");
+ #endif
   PyObjWrapper sipList( sipBuildResult( 0, "D", theList, sipType_QStringList, NULL) );
 #endif
   if ( PyObject_HasAttrString(myPyModule, (char*)"dropObjects") ) {