Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / SALOME_SWIG / SALOMEGUI_Swig.cxx
index 088c576ebfc6dee4abf061bb271e34fc2a6345ac..d56fafe083388b288bd2ffa72256a0b72d80d327 100644 (file)
@@ -17,7 +17,7 @@
 //  License along with this library; if not, write to the Free Software 
 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
 // 
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 //
 //
 #include "SalomeApp_Study.h"
 #include "SalomeApp_Module.h"
 #include "SalomeApp_DataObject.h"
-#include "SalomeApp_SelectionMgr.h"
+#include "LightApp_SelectionMgr.h"
 #include "SALOME_Prs.h"
 #include "SOCC_ViewModel.h"
 #include "SVTK_ViewModel.h"
+#include "SVTK_ViewWindow.h"
+#include "SOCC_ViewWindow.h"
+#include "SPlot2d_ViewWindow.h"
 
 #include "SALOME_Event.hxx"
 #include "SALOME_ListIO.hxx"
 
 using namespace std;
 
-//////////////////////////////////////////////////////////////////////////////
-// asv : 3.12.04 : added checking for NULL GUI objects in almost all methods.
-// In the scope of fixing bug PAL6869.
-//////////////////////////////////////////////////////////////////////////////
-// (PR : modify comments)
-// Instance of this class is created every time "import salome" line is typed 
-// - in IAPP embedded Python interpretor  (SALOME_Session_Server executable),
-// - in inline Python nodes in Supervisor (in SALOME_Container executable),
-// - in stand-alone Python console outside any executable.
-// SALOME GUI(desktop and other objects) is only available in SALOME_Session_Server
-//////////////////////////////////////////////////////////////////////////////
-// VSR : 19.04.05 : Reimplemented for new SALOME GUI (SUIT-based)
-// All methods are implemeted using Event mechanism.
-// Display/Erase methods use SALOME_Prs/SALOME_View mechanism. It is currently
-// implemented only for OCC and VTK viewers.
-//////////////////////////////////////////////////////////////////////////////
+/*!
+  asv : 3.12.04 : added checking for NULL GUI objects in almost all methods.
+  In the scope of fixing bug PAL6869.
+
+  (PR : modify comments)
+  Instance of this class is created every time "import salome" line is typed 
+  - in IAPP embedded Python interpretor  (SALOME_Session_Server executable),
+  - in inline Python nodes in Supervisor (in SALOME_Container executable),
+  - in stand-alone Python console outside any executable.
+  SALOME GUI(desktop and other objects) is only available in SALOME_Session_Server
+
+  VSR : 19.04.05 : Reimplemented for new SALOME GUI (SUIT-based)
+  All methods are implemeted using Event mechanism.
+  Display/Erase methods use SALOME_Prs/SALOME_View mechanism. It is currently
+  implemented only for OCC and VTK viewers.
+*/
 
 /*!
   getApplication()
@@ -120,6 +123,10 @@ public:
     myResult = (bool)( getApplication() && getApplication()->desktop() );
   }
 };
+
+/*!
+  \return true if GUI is available.
+*/
 bool SALOMEGUI_Swig::hasDesktop()
 {
   return ProcessEvent( new THasDesktopEvent() );
@@ -160,6 +167,10 @@ public:
     }
   }
 };
+
+/*!
+  \return active study's ID or 0 if there is no active study.
+*/
 int SALOMEGUI_Swig::getActiveStudyId()
 {
   return ProcessEvent( new TGetActiveStudyIdEvent() );
@@ -180,6 +191,10 @@ public:
     }
   }
 };
+
+/*!
+  \return active study's name or NULL if there is no active study.
+*/
 const char* SALOMEGUI_Swig::getActiveStudyName()
 {
   string result = ProcessEvent( new TGetActiveStudyNameEvent() );
@@ -190,19 +205,32 @@ const char* SALOMEGUI_Swig::getActiveStudyName()
   SALOMEGUI_Swig::getComponentName
   Returns the name of the component by its user name.
 */
+class TGetModulCatalogEvent: public SALOME_Event {
+public:
+  typedef CORBA::Object_var TResult;
+  TResult myResult;
+  TGetModulCatalogEvent() : myResult(CORBA::Object::_nil()) {}
+  virtual void Execute() {
+    if (SalomeApp_Application* anApp = getApplication())
+      myResult = anApp->namingService()->Resolve("/Kernel/ModulCatalog");
+  }
+};
+
+/*!
+  \return the name of the component by its user name.
+*/
 const char* SALOMEGUI_Swig::getComponentName( const char* componentUserName )
 {
-  if ( SalomeApp_Application* anApp = getApplication() ) { 
-    CORBA::Object_var anObject = anApp->namingService()->Resolve("/Kernel/ModulCatalog");
-    if ( !CORBA::is_nil( anObject ) ) {
-      SALOME_ModuleCatalog::ModuleCatalog_var aCatalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow( anObject );
-      SALOME_ModuleCatalog::ListOfIAPP_Affich_var aModules = aCatalogue->GetComponentIconeList();
-      for ( unsigned int ind = 0; ind < aModules->length(); ind++ ) {
-       string aModuleName     = CORBA::string_dup( aModules[ ind ].modulename ) ;
-       string aModuleUserName = CORBA::string_dup( aModules[ ind ].moduleusername ) ;
-       if ( componentUserName == aModuleUserName )
-         return aModuleName.c_str();
-      }
+  CORBA::Object_var anObject = ProcessEvent(new TGetModulCatalogEvent());
+  if (!CORBA::is_nil(anObject)) {
+    SALOME_ModuleCatalog::ModuleCatalog_var aCatalogue =
+      SALOME_ModuleCatalog::ModuleCatalog::_narrow( anObject );
+    SALOME_ModuleCatalog::ListOfIAPP_Affich_var aModules = aCatalogue->GetComponentIconeList();
+    for ( unsigned int ind = 0; ind < aModules->length(); ind++ ) {
+      CORBA::String_var aModuleName     = aModules[ ind ].modulename;
+      CORBA::String_var aModuleUserName = aModules[ ind ].moduleusername;
+      if ( strcmp(componentUserName, aModuleUserName.in()) == 0 )
+        return aModuleName._retn();
     }
   }
   return 0;
@@ -214,17 +242,16 @@ const char* SALOMEGUI_Swig::getComponentName( const char* componentUserName )
 */
 const char* SALOMEGUI_Swig::getComponentUserName( const char* componentName )
 {
-  if ( SalomeApp_Application* anApp = getApplication() ) { 
-    CORBA::Object_var anObject = anApp->namingService()->Resolve("/Kernel/ModulCatalog");
-    if ( !CORBA::is_nil( anObject ) ) {
-      SALOME_ModuleCatalog::ModuleCatalog_var aCatalogue = SALOME_ModuleCatalog::ModuleCatalog::_narrow( anObject );
-      SALOME_ModuleCatalog::ListOfIAPP_Affich_var aModules = aCatalogue->GetComponentIconeList();
-      for ( unsigned int ind = 0; ind < aModules->length(); ind++ ) {
-       string aModuleName     = CORBA::string_dup( aModules[ ind ].modulename ) ;
-       string aModuleUserName = CORBA::string_dup( aModules[ ind ].moduleusername ) ;
-       if ( componentName == aModuleName )
-         return aModuleUserName.c_str();
-      }
+  CORBA::Object_var anObject = ProcessEvent(new TGetModulCatalogEvent());
+  if (!CORBA::is_nil(anObject)) {
+    SALOME_ModuleCatalog::ModuleCatalog_var aCatalogue =
+      SALOME_ModuleCatalog::ModuleCatalog::_narrow( anObject );
+    SALOME_ModuleCatalog::ListOfIAPP_Affich_var aModules = aCatalogue->GetComponentIconeList();
+    for ( unsigned int ind = 0; ind < aModules->length(); ind++ ) {
+      CORBA::String_var aModuleName     = aModules[ ind ].modulename;
+      CORBA::String_var aModuleUserName = aModules[ ind ].moduleusername;
+      if ( strcmp(componentName, aModuleName.in()) == 0 )
+        return aModuleUserName._retn();
     }
   }
   return 0;
@@ -241,8 +268,8 @@ public:
   TSelectedCountEvent() : myResult( 0 ) {}
   virtual void Execute() {
     if ( SalomeApp_Application* anApp = getApplication() ) {
-      SalomeApp_Study*        aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
-      SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
+      SalomeApp_Study*       aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
+      LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
       if ( aStudy && aSelMgr ) {
        SALOME_ListIO anIOList;
        aSelMgr->selectedObjects( anIOList );
@@ -251,6 +278,10 @@ public:
     }
   }
 };
+
+/*!
+  \return the number of selected objects.
+*/
 int SALOMEGUI_Swig::SelectedCount()
 {
   return ProcessEvent( new TSelectedCountEvent() );
@@ -268,8 +299,8 @@ public:
   TGetSelectedEvent( int theIndex ) : myIndex( theIndex ) {}
   virtual void Execute() {
     if ( SalomeApp_Application* anApp = getApplication() ) {
-      SalomeApp_Study*        aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
-      SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
+      SalomeApp_Study*       aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
+      LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
       if ( aStudy && aSelMgr ) {
        SALOME_ListIO anIOList;
        aSelMgr->selectedObjects( anIOList );
@@ -288,6 +319,10 @@ public:
     }
   }
 };
+
+/*!
+  \return the selected object entry by the given index.
+*/
 const char* SALOMEGUI_Swig::getSelected( int index )
 {
   QString result = ProcessEvent( new TGetSelectedEvent( index ) );
@@ -305,8 +340,8 @@ void SALOMEGUI_Swig::AddIObject( const char* theEntry )
     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
     virtual void Execute() {
       if ( SalomeApp_Application* anApp = getApplication() ) {
-       SalomeApp_Study*        aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
-       SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
+       SalomeApp_Study*       aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
+       LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
        if ( aStudy && aSelMgr ) {
          SALOME_ListIO anIOList;
          anIOList.Append( new SALOME_InteractiveObject( myEntry, "", "" ) );
@@ -329,11 +364,11 @@ void SALOMEGUI_Swig::RemoveIObject( const char* theEntry )
     TEvent( const char* theEntry ) : myEntry( theEntry ) {}
     virtual void Execute() {
       if ( SalomeApp_Application* anApp = getApplication() ) {
-       SalomeApp_Study*        aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
-       SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
+       SalomeApp_Study*       aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
+       LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
        if ( aStudy && aSelMgr ) {
          SALOME_ListIO anIOList;
-         // VSR: temporary solution, until SalomeApp_SelectionMgr::unsetSelectedObjects() method appears
+         // VSR: temporary solution, until LightApp_SelectionMgr::unsetSelectedObjects() method appears
          // Lately this should be replaced by the following:
          // anIOList.Append( new SALOME_InteractiveObject( myEntry, "", "" ) );
          // aSelMgr->unsetSelectedObjects( anIOList );
@@ -364,8 +399,8 @@ void SALOMEGUI_Swig::ClearIObjects()
     TEvent() {}
     virtual void Execute() {
       if ( SalomeApp_Application* anApp = getApplication() ) {
-       SalomeApp_Study*        aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
-       SalomeApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
+       SalomeApp_Study*       aStudy  = dynamic_cast<SalomeApp_Study*>( anApp->activeStudy() ); // for sure!
+       LightApp_SelectionMgr* aSelMgr = anApp->selectionMgr(); 
        if ( aStudy && aSelMgr )
          aSelMgr->clearSelected();
       }
@@ -522,13 +557,203 @@ public:
        SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
        if ( view ) {
          SALOME_Prs* aPrs = view->CreatePrs( myEntry );
-         myResult = aPrs->IsNull();
+         myResult = !aPrs->IsNull();
        }
       }
     }
   }
 };
+
+/*!
+  \return TRUE if the object with given entry is in the current viewer.
+  VSR: For the current moment implemented for OCC and VTK viewers only.
+*/
 bool SALOMEGUI_Swig::IsInCurrentView( const char* theEntry )
 {
   return ProcessEvent( new TIsInViewerEvent( theEntry ) );
 }
+
+/*!
+  Updates (repaint) current view
+*/
+void SALOMEGUI_Swig::UpdateView()
+{
+  class TEvent: public SALOME_Event {
+  public:
+    TEvent() {}
+    virtual void Execute() {
+      if ( SalomeApp_Application* anApp = getApplication() ) {
+       SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
+       if ( window ) {
+         SALOME_View* view = dynamic_cast<SALOME_View*>( window->getViewManager()->getViewModel() );
+         if ( view )
+           view->Repaint();
+       }
+      }
+    }
+  };
+  ProcessVoidEvent( new TEvent() );
+}
+
+/*!
+  Fit all the contents of the current view window
+ */
+void SALOMEGUI_Swig::FitAll()
+{
+  class TEvent: public SALOME_Event {
+  public:
+    TEvent() {}
+    virtual void Execute() {
+      if ( SalomeApp_Application* anApp = getApplication() ) {
+       SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
+       if ( window ) {
+         if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
+           (dynamic_cast<SVTK_ViewWindow*>( window ))->onFitAll();
+         else if ( dynamic_cast<SOCC_ViewWindow*>( window ) )
+           (dynamic_cast<SOCC_ViewWindow*>( window ))->onFitAll();
+         else if ( dynamic_cast<SPlot2d_ViewWindow*>( window ) )
+           (dynamic_cast<SPlot2d_ViewWindow*>( window ))->onFitAll();
+       }
+      }
+    }
+  };
+  ProcessVoidEvent( new TEvent() );
+}
+
+/*!
+  Reset current view window to the default state.
+ */
+void SALOMEGUI_Swig::ResetView()
+{
+  class TEvent: public SALOME_Event {
+  public:
+    TEvent() {}
+    virtual void Execute() {
+      if ( SalomeApp_Application* anApp = getApplication() ) {
+       SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
+       if ( window ) {
+         if ( dynamic_cast<SVTK_ViewWindow*>( window ) )
+           (dynamic_cast<SVTK_ViewWindow*>( window ))->onResetView();
+         else if ( dynamic_cast<SOCC_ViewWindow*>( window ) )
+           (dynamic_cast<SOCC_ViewWindow*>( window ))->onResetView();
+         else if ( dynamic_cast<SPlot2d_ViewWindow*>( window ) )
+           (dynamic_cast<SPlot2d_ViewWindow*>( window ))->onFitAll();
+         // VSR: there is no 'ResetView' functionality for Plot2d viewer,
+         // so we use 'FitAll' instead.
+       }
+      }
+    }
+  };
+  ProcessVoidEvent( new TEvent() );
+}
+
+enum {
+  __ViewTop,
+  __ViewBottom,
+  __ViewLeft,
+  __ViewRight,
+  __ViewFront,
+  __ViewBack
+};
+
+void setView( int view )
+{
+  class TEvent: public SALOME_Event {
+  private:
+    int myView;
+  public:
+    TEvent( int view ) : myView( view ) {}
+    virtual void Execute() {
+      if ( SalomeApp_Application* anApp = getApplication() ) {
+       SUIT_ViewWindow* window = anApp->desktop()->activeWindow();
+       if ( window ) {
+         if ( dynamic_cast<SVTK_ViewWindow*>( window ) ) {
+           switch( myView ) {
+           case __ViewTop:
+             (dynamic_cast<SVTK_ViewWindow*>( window ))->onTopView(); break;
+           case __ViewBottom:
+             (dynamic_cast<SVTK_ViewWindow*>( window ))->onBottomView(); break;
+           case __ViewLeft:
+             (dynamic_cast<SVTK_ViewWindow*>( window ))->onLeftView(); break;
+           case __ViewRight:
+             (dynamic_cast<SVTK_ViewWindow*>( window ))->onRightView(); break;
+           case __ViewFront:
+             (dynamic_cast<SVTK_ViewWindow*>( window ))->onFrontView(); break;
+           case __ViewBack:
+             (dynamic_cast<SVTK_ViewWindow*>( window ))->onBackView(); break;
+           default:
+             break;
+           }
+         }
+         else if ( dynamic_cast<SOCC_ViewWindow*>( window ) ) {
+           switch( myView ) {
+           case __ViewTop:
+             (dynamic_cast<SOCC_ViewWindow*>( window ))->onTopView(); break;
+           case __ViewBottom:
+             (dynamic_cast<SOCC_ViewWindow*>( window ))->onBottomView(); break;
+           case __ViewLeft:
+             (dynamic_cast<SOCC_ViewWindow*>( window ))->onLeftView(); break;
+           case __ViewRight:
+             (dynamic_cast<SOCC_ViewWindow*>( window ))->onRightView(); break;
+           case __ViewFront:
+             (dynamic_cast<SOCC_ViewWindow*>( window ))->onFrontView(); break;
+           case __ViewBack:
+             (dynamic_cast<SOCC_ViewWindow*>( window ))->onBackView(); break;
+           default:
+             break;
+           }
+         }
+       }
+      }
+    }
+  };
+  ProcessVoidEvent( new TEvent( view ) );
+}
+
+/*!
+  Switch current view window to show top view
+ */
+void SALOMEGUI_Swig::ViewTop()
+{
+  setView( __ViewTop );
+}
+
+/*!
+  Switch current view window to show bottom view
+ */
+void SALOMEGUI_Swig::ViewBottom()
+{
+  setView( __ViewBottom );
+}
+
+/*!
+  Switch current view window to show left view
+ */
+void SALOMEGUI_Swig::ViewLeft()
+{
+  setView( __ViewLeft );
+}
+
+/*!
+  Switch current view window to show right view
+ */
+void SALOMEGUI_Swig::ViewRight()
+{
+  setView( __ViewRight );
+}
+
+/*!
+  Switch current view window to show front view
+ */
+void SALOMEGUI_Swig::ViewFront()
+{
+  setView( __ViewFront );
+}
+
+/*!
+  Switch current view window to show back view
+ */
+void SALOMEGUI_Swig::ViewBack()
+{
+  setView( __ViewBack );
+}