Salome HOME
Update version to 3.2.0a1
[modules/visu.git] / src / VISUGUI / VisuGUI_ViewTools.h
1 //  VISU VISUGUI : GUI of VISU component
2 //
3 //  Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
21 //
22 //
23 //
24 //  File   : VisuGUI_Tools.h
25 //  Author : Sergey Anikin
26 //  Module : VISU
27
28 #ifndef VisuGUI_ViewTools_HeaderFile
29 #define VisuGUI_ViewTools_HeaderFile
30
31 #include "VisuGUI.h"
32
33 #include "SUIT_MessageBox.h"
34 #include "SUIT_ViewManager.h"
35 #include "SUIT_ViewWindow.h"
36
37 #include "SalomeApp_Module.h"
38 #include "SalomeApp_Application.h"
39
40 #include "VISU_Prs3d_i.hh"
41
42 #include "VVTK_ViewModel.h"
43 #include "VVTK_ViewWindow.h"
44
45 #include "SVTK_ViewModel.h"
46 #include "SVTK_ViewWindow.h"
47
48 #include "VisuGUI_Tools.h"
49 #include "VTKViewer_Algorithm.h"
50 #include "SVTK_Functor.h"
51
52 #include "VISU_Table_i.hh"
53 #include "VISU_ViewManager_i.hh"
54 #include "SALOME_ListIO.hxx"
55 #include "SALOME_ListIteratorOfListIO.hxx"
56
57 #include "SPlot2d_ViewModel.h"
58
59 #include "VISU_Actor.h"
60
61 #include <vtkActorCollection.h>
62 #include <vtkRenderer.h>
63
64 class VVTK_Viewer;
65
66 namespace VISU 
67 {
68   //---------------------------------------------------------------
69   template<class TViewer>
70   inline
71   typename TViewer::TViewWindow*
72   GetViewWindow(VisuGUI* theModule, 
73                 const bool theIsViewCreate = false)
74   {
75     typedef typename TViewer::TViewWindow TView;
76     if(SUIT_ViewManager* aViewManager = theModule->getViewManager(TViewer::Type(),theIsViewCreate)){
77       if(SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()){
78         if(TView* aView = dynamic_cast<TView*>(aViewWindow)){
79           aViewWindow->raise();
80           aViewWindow->setFocus();
81           return aView;
82         }
83       }
84     }
85     return NULL;
86   }
87
88
89   //---------------------------------------------------------------
90   template<class TViewer>
91   inline
92   VISU_Actor*
93   PublishInView(VisuGUI* theModule,
94                 Prs3d_i* thePrs,
95                 const bool theIsHighlight = false,
96                 const bool theIsCreateView = false)
97   {
98     typedef typename TViewer::TViewWindow TView;
99     if(TView* aView = GetViewWindow<TViewer>(theModule,theIsCreateView)){
100       QApplication::setOverrideCursor( Qt::waitCursor );
101       try {
102         if(VISU_Actor* anActor = thePrs->CreateActor()){
103           aView->AddActor(anActor);
104           if(theIsHighlight)
105             aView->highlight(anActor->getIO(),true);
106           aView->getRenderer()->ResetCameraClippingRange();
107           aView->Repaint();
108           QApplication::restoreOverrideCursor();
109           return anActor;
110         }
111       } catch(std::exception& exc) {
112         QApplication::restoreOverrideCursor();
113         INFOS(exc.what());
114         SUIT_MessageBox::warn1
115           (GetDesktop(theModule), QObject::tr("WRN_VISU"),
116            QObject::tr("ERR_CANT_CREATE_ACTOR") + ": " + QObject::tr(exc.what()),
117            QObject::tr("BUT_OK"));
118       }
119     }
120     return NULL;
121   }
122
123
124   //---------------------------------------------------------------
125   template<class TViewer>
126   inline
127   VISU_Actor*
128   UpdateViewer(VisuGUI* theModule,
129                VISU::Prs3d_i* thePrs,
130                bool theDispOnly = false,
131                const bool theIsHighlight = false,
132                const bool theIsViewCreate = false)
133   {
134     typedef typename TViewer::TViewWindow TView;
135     if(TView* aView = GetViewWindow<TViewer>(theModule,theIsViewCreate)){
136       vtkRenderer *aRen = aView->getRenderer();
137       vtkActorCollection *anActColl = aRen->GetActors();
138       anActColl->InitTraversal();
139       VISU_Actor* aResActor = NULL;
140       while(vtkActor *anAct = anActColl->GetNextActor()){
141         if(VISU_Actor* anActor = dynamic_cast<VISU_Actor*>(anAct)){
142           if(VISU::Prs3d_i* aPrs3d = anActor->GetPrs3d()){
143             if(thePrs == aPrs3d){
144               try {
145                 thePrs->UpdateActors();
146               } catch (std::runtime_error& exc) {
147                 INFOS(exc.what());
148                 QApplication::restoreOverrideCursor();
149                 SUIT_MessageBox::warn1
150                   (GetDesktop(theModule), QObject::tr("WRN_VISU"),
151                    QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()),
152                    QObject::tr("BUT_OK"));
153
154                 thePrs->RemoveActors();
155                 return NULL;
156               }
157               aResActor = anActor;
158               aResActor->VisibilityOn();
159             }else if(theDispOnly){
160               anActor->VisibilityOff();
161             }
162           }else if(theDispOnly && anActor->GetVisibility()){
163             anActor->VisibilityOff();
164           }
165         }
166       }
167       if(aResActor){
168         if(theIsHighlight)
169           aView->highlight(aResActor->getIO(),true);
170         aView->getRenderer()->ResetCameraClippingRange();
171         aView->Repaint();
172         return aResActor;
173       }
174       return PublishInView<TViewer>(theModule,thePrs,theIsHighlight,theIsViewCreate);
175     }
176     return NULL;
177   }
178
179
180   //---------------------------------------------------------------
181   template<class TViewer>
182   inline
183   void
184   OnEraseAll(VisuGUI* theModule)
185   {
186     typedef typename TViewer::TViewWindow TViewWindow;
187     if(TViewWindow* aViewWindow = GetViewWindow<TViewer>(theModule)){
188       aViewWindow->unHighlightAll();
189       if(vtkRenderer *aRen = aViewWindow->getRenderer()){
190         vtkActorCollection *aCollection = aRen->GetActors();
191         aCollection->InitTraversal();
192         while(vtkActor *anAct = aCollection->GetNextActor()){
193           if(anAct->GetVisibility() > 0)
194             if(SALOME_Actor* anActor = dynamic_cast<SALOME_Actor*>(anAct)){
195               anActor->VisibilityOff();
196             }
197         }
198         aViewWindow->Repaint();
199       }
200     }
201   }
202   
203   template<>
204   inline
205   void
206   OnEraseAll<SPlot2d_Viewer>(VisuGUI* theModule)
207   {
208     if(SPlot2d_Viewer* aPlot2d = GetPlot2dViewer(theModule,false))
209       aPlot2d->EraseAll();
210   }
211
212
213   //---------------------------------------------------------------
214   template<class TVieweWindow>
215   inline
216   VISU_Actor*
217   FindActor(TVieweWindow* theViewWindow,
218             const char* theEntry)
219   {
220     using namespace SVTK;
221     if(vtkRenderer* aRenderer = theViewWindow->getRenderer()){
222       if(vtkActorCollection* aCollection = aRenderer->GetActors()){
223         if(VISU_Actor* anActor = Find<VISU_Actor>(aCollection,TIsSameEntry<VISU_Actor>(theEntry))){
224           return anActor;
225         }
226       }
227     }
228     return NULL;
229   }
230
231   template<class TViewer>
232   inline
233   void
234   ErasePrs3d(VisuGUI* theModule,
235              VISU::Prs3d_i* thePrs,
236              const bool thIsUpdate = true)
237   {
238     typedef typename TViewer::TViewWindow TViewWindow;
239     if(TViewWindow* aViewWindow = GetViewWindow<TViewer>(theModule)){
240       if(VISU_Actor* anActor = FindActor(aViewWindow,thePrs)){
241         anActor->VisibilityOff();
242         if(thIsUpdate)
243           aViewWindow->Repaint();
244       }
245     }
246   }
247
248
249   void
250   ErasePrs(VisuGUI* theModule,
251            VISU::Base_ptr theBase, 
252            bool thIsUpdate);
253 }
254
255 #endif