Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[modules/visu.git] / src / VISUGUI / VisuGUI_ViewTools.h
index f10bf63e9a5ee9e442e82c94967aa191dd359286..bb28861a977638c2bca62288146376f408b06e1d 100644 (file)
 
 #include "VisuGUI.h"
 
-#include "SUIT_MessageBox.h"
-#include "SUIT_ViewManager.h"
-#include "SUIT_ViewWindow.h"
+#include "VisuGUI_Tools.h"
+
+#include "VISU_Prs3d_i.hh"
+#include "VISU_Table_i.hh"
+#include "VISU_ViewManager_i.hh"
+
+#include "VISU_Actor.h"
 
 #include "SalomeApp_Module.h"
 #include "SalomeApp_Application.h"
 
-#include "VISU_Prs3d_i.hh"
-
 #include "VVTK_ViewModel.h"
 #include "VVTK_ViewWindow.h"
 
+#include "SVTK_Functor.h"
 #include "SVTK_ViewModel.h"
 #include "SVTK_ViewWindow.h"
 
-#include "VisuGUI_Tools.h"
+#include "SPlot2d_ViewModel.h"
+
 #include "VTKViewer_Algorithm.h"
-#include "SVTK_Functor.h"
 
-#include "VISU_Table_i.hh"
-#include "VISU_ViewManager_i.hh"
+#include "SUIT_Session.h"
+#include "SUIT_MessageBox.h"
+#include "SUIT_ViewManager.h"
+#include "SUIT_ViewWindow.h"
+
 #include "SALOME_ListIO.hxx"
 #include "SALOME_ListIteratorOfListIO.hxx"
 
-#include "SPlot2d_ViewModel.h"
-
-#include "VISU_Actor.h"
-
 #include <vtkActorCollection.h>
 #include <vtkRenderer.h>
 
@@ -65,17 +67,26 @@ class VVTK_Viewer;
 
 namespace VISU 
 {
-  //---------------------------------------------------------------
+  /*! Return active view window, if it instantiates TViewer::TViewWindow class,
+   *  overwise find or create corresponding view window, make it active and return it.
+   *  \note Active VVTK_ViewWindow can be returned by request GetViewWindow<SVTK_Viewer>(),
+   *        because VVTK_ViewWindow inherits SVTK_ViewWindow.
+   */
   template<class TViewer>
   inline
   typename TViewer::TViewWindow*
-  GetViewWindow(VisuGUI* theModule, 
-               const bool theIsViewCreate = false)
+  GetViewWindow(VisuGUI* theModule)
   {
-    typedef typename TViewer::TViewWindow TView;
-    if(SUIT_ViewManager* aViewManager = theModule->getViewManager(TViewer::Type(),theIsViewCreate)){
-      if(SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()){
-       if(TView* aView = dynamic_cast<TView*>(aViewWindow)){
+    typedef typename TViewer::TViewWindow TViewWindow;
+    if (SalomeApp_Application* anApp = theModule->getApp()) {
+      if (TViewWindow* aView = dynamic_cast<TViewWindow*>(anApp->desktop()->activeWindow()))
+       return aView;
+    }
+    SUIT_ViewManager* aViewManager =
+      theModule->getViewManager(TViewer::Type(), /*create = */true);
+    if (aViewManager) {
+      if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
+       if (TViewWindow* aView = dynamic_cast<TViewWindow*>(aViewWindow)) {
          aViewWindow->raise();
          aViewWindow->setFocus();
          return aView;
@@ -85,30 +96,54 @@ namespace VISU
     return NULL;
   }
 
+  /*! Return active view window, if it instantiates TViewWindow class, overwise return NULL.
+   *  \note Active VVTK_ViewWindow can be returned by request GetActiveViewWindow<SVTK_ViewWindow>(),
+   *        because VVTK_ViewWindow inherits SVTK_ViewWindow.
+   */
+  template<class TViewWindow>
+  inline
+  TViewWindow*
+  GetActiveViewWindow(const SalomeApp_Module* theModule = NULL)
+  {
+    SalomeApp_Application* anApp = NULL;
+    if (theModule)
+      anApp = theModule->getApp();
+    else
+      anApp = dynamic_cast<SalomeApp_Application*>
+       (SUIT_Session::session()->activeApplication());
+
+    if (anApp)
+      if (SUIT_ViewManager* aViewManager = anApp->activeViewManager())
+       if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView())
+         return dynamic_cast<TViewWindow*>(aViewWindow);
+
+    return NULL;
+  }
+
 
   //---------------------------------------------------------------
-  template<class TViewer>
   inline
   VISU_Actor*
   PublishInView(VisuGUI* theModule,
                 Prs3d_i* thePrs,
-               const bool theIsHighlight = false,
-               const bool theIsCreateView = false)
+               SVTK_ViewWindow* theViewWindow,
+               const bool theIsHighlight = false)
   {
-    typedef typename TViewer::TViewWindow TView;
-    if(TView* aView = GetViewWindow<TViewer>(theModule,theIsCreateView)){
+    if (theViewWindow) {
       QApplication::setOverrideCursor( Qt::waitCursor );
       try {
-       if(VISU_Actor* anActor = thePrs->CreateActor()){
-         aView->AddActor(anActor);
-         if(theIsHighlight)
-           aView->highlight(anActor->getIO(),true);
-         aView->getRenderer()->ResetCameraClippingRange();
-         aView->Repaint();
+       if (VISU_Actor* anActor = thePrs->CreateActor()) {
+         theViewWindow->AddActor(anActor);
+         if (theIsHighlight)
+           theViewWindow->highlight(anActor->getIO(),true);
+         theViewWindow->getRenderer()->ResetCameraClippingRange();
+         theViewWindow->Repaint();
          QApplication::restoreOverrideCursor();
          return anActor;
        }
       } catch(std::exception& exc) {
+       thePrs->RemoveActors();
+
        QApplication::restoreOverrideCursor();
        INFOS(exc.what());
        SUIT_MessageBox::warn1
@@ -122,56 +157,53 @@ namespace VISU
 
 
   //---------------------------------------------------------------
-  template<class TViewer>
   inline
   VISU_Actor*
   UpdateViewer(VisuGUI* theModule,
                VISU::Prs3d_i* thePrs,
                bool theDispOnly = false,
-              const bool theIsHighlight = false,
-              const bool theIsViewCreate = false)
+              const bool theIsHighlight = false)
   {
-    typedef typename TViewer::TViewWindow TView;
-    if(TView* aView = GetViewWindow<TViewer>(theModule,theIsViewCreate)){
-      vtkRenderer *aRen = aView->getRenderer();
+    if (SVTK_ViewWindow* aViewWindow = GetActiveViewWindow<SVTK_ViewWindow>(theModule)) {
+      vtkRenderer *aRen = aViewWindow->getRenderer();
       vtkActorCollection *anActColl = aRen->GetActors();
       anActColl->InitTraversal();
       VISU_Actor* aResActor = NULL;
-      while(vtkActor *anAct = anActColl->GetNextActor()){
-       if(VISU_Actor* anActor = dynamic_cast<VISU_Actor*>(anAct)){
-         if(VISU::Prs3d_i* aPrs3d = anActor->GetPrs3d()){
-           if(thePrs == aPrs3d){
+      bool isOk = true;
+      while (vtkActor *anAct = anActColl->GetNextActor()) {
+       if (VISU_Actor* anActor = dynamic_cast<VISU_Actor*>(anAct)) {
+         if (VISU::Prs3d_i* aPrs3d = anActor->GetPrs3d()) {
+           if (thePrs == aPrs3d) {
              try {
+               aResActor = anActor;
                thePrs->UpdateActors();
+               aResActor->VisibilityOn();
              } catch (std::runtime_error& exc) {
+               thePrs->RemoveActors();
+               isOk = false;
+
                INFOS(exc.what());
-               QApplication::restoreOverrideCursor();
                SUIT_MessageBox::warn1
                  (GetDesktop(theModule), QObject::tr("WRN_VISU"),
                   QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()),
                   QObject::tr("BUT_OK"));
-
-               thePrs->RemoveActors();
-               return NULL;
              }
-             aResActor = anActor;
-             aResActor->VisibilityOn();
-           }else if(theDispOnly){
+           } else if (theDispOnly) {
              anActor->VisibilityOff();
            }
-         }else if(theDispOnly && anActor->GetVisibility()){
+         } else if (theDispOnly && anActor->GetVisibility()) {
            anActor->VisibilityOff();
          }
        }
       }
-      if(aResActor){
-       if(theIsHighlight)
-         aView->highlight(aResActor->getIO(),true);
-       aView->getRenderer()->ResetCameraClippingRange();
-       aView->Repaint();
+      if (aResActor) {
+       if (theIsHighlight && isOk)
+         aViewWindow->highlight(aResActor->getIO(), true);
+       aViewWindow->getRenderer()->ResetCameraClippingRange();
+       aViewWindow->Repaint();
        return aResActor;
       }
-      return PublishInView<TViewer>(theModule,thePrs,theIsHighlight,theIsViewCreate);
+      return PublishInView(theModule, thePrs, aViewWindow, theIsHighlight);
     }
     return NULL;
   }
@@ -184,14 +216,14 @@ namespace VISU
   OnEraseAll(VisuGUI* theModule)
   {
     typedef typename TViewer::TViewWindow TViewWindow;
-    if(TViewWindow* aViewWindow = GetViewWindow<TViewer>(theModule)){
+    if (TViewWindow* aViewWindow = GetActiveViewWindow<TViewWindow>(theModule)) {
       aViewWindow->unHighlightAll();
-      if(vtkRenderer *aRen = aViewWindow->getRenderer()){
+      if (vtkRenderer *aRen = aViewWindow->getRenderer()) {
        vtkActorCollection *aCollection = aRen->GetActors();
        aCollection->InitTraversal();
-       while(vtkActor *anAct = aCollection->GetNextActor()){
-         if(anAct->GetVisibility() > 0)
-           if(SALOME_Actor* anActor = dynamic_cast<SALOME_Actor*>(anAct)){
+       while (vtkActor *anAct = aCollection->GetNextActor()) {
+         if (anAct->GetVisibility() > 0)
+           if (SALOME_Actor* anActor = dynamic_cast<SALOME_Actor*>(anAct)) {
              anActor->VisibilityOff();
            }
        }
@@ -199,57 +231,37 @@ namespace VISU
       }
     }
   }
-  
+
   template<>
   inline
   void
   OnEraseAll<SPlot2d_Viewer>(VisuGUI* theModule)
   {
-    if(SPlot2d_Viewer* aPlot2d = GetPlot2dViewer(theModule,false))
+    if (SPlot2d_Viewer* aPlot2d = GetPlot2dViewer(theModule, false))
       aPlot2d->EraseAll();
   }
 
 
   //---------------------------------------------------------------
-  template<class TVieweWindow>
-  inline
-  VISU_Actor*
-  FindActor(TVieweWindow* theViewWindow,
-            const char* theEntry)
-  {
-    using namespace SVTK;
-    if(vtkRenderer* aRenderer = theViewWindow->getRenderer()){
-      if(vtkActorCollection* aCollection = aRenderer->GetActors()){
-        if(VISU_Actor* anActor = Find<VISU_Actor>(aCollection,TIsSameEntry<VISU_Actor>(theEntry))){
-          return anActor;
-        }
-      }
-    }
-    return NULL;
-  }
-
-  template<class TViewer>
   inline
   void
   ErasePrs3d(VisuGUI* theModule,
              VISU::Prs3d_i* thePrs,
-            const bool thIsUpdate = true)
+            const bool theDoRepaintVW = true)
   {
-    typedef typename TViewer::TViewWindow TViewWindow;
-    if(TViewWindow* aViewWindow = GetViewWindow<TViewer>(theModule)){
-      if(VISU_Actor* anActor = FindActor(aViewWindow,thePrs)){
+    if (SVTK_ViewWindow* aViewWindow = GetActiveViewWindow<SVTK_ViewWindow>(theModule)) {
+      if (VISU_Actor* anActor = FindActor(aViewWindow, thePrs)) {
         anActor->VisibilityOff();
-       if(thIsUpdate)
+       if (theDoRepaintVW)
          aViewWindow->Repaint();
       }
     }
   }
 
-
   void
   ErasePrs(VisuGUI* theModule,
-          VISU::Base_ptr theBase
-          bool thIsUpdate);
+          CORBA::Object_ptr theObject
+          bool theDoRepaintVW);
 }
 
 #endif