Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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 "VisuGUI_Tools.h"
34
35 #include "VISU_Prs3d_i.hh"
36 #include "VISU_Table_i.hh"
37 #include "VISU_ViewManager_i.hh"
38
39 #include "VISU_Actor.h"
40
41 #include "SalomeApp_Module.h"
42 #include "SalomeApp_Application.h"
43
44 #include "VVTK_ViewModel.h"
45 #include "VVTK_ViewWindow.h"
46
47 #include "SVTK_Functor.h"
48 #include "SVTK_ViewModel.h"
49 #include "SVTK_ViewWindow.h"
50
51 #include "SPlot2d_ViewModel.h"
52
53 #include "VTKViewer_Algorithm.h"
54
55 #include "SUIT_Session.h"
56 #include "SUIT_MessageBox.h"
57 #include "SUIT_ViewManager.h"
58 #include "SUIT_ViewWindow.h"
59
60 #include "SALOME_ListIO.hxx"
61 #include "SALOME_ListIteratorOfListIO.hxx"
62
63 #include <vtkActorCollection.h>
64 #include <vtkRenderer.h>
65
66 class VVTK_Viewer;
67
68 namespace VISU 
69 {
70   /*! Return active view window, if it instantiates TViewer::TViewWindow class,
71    *  overwise find or create corresponding view window, make it active and return it.
72    *  \note Active VVTK_ViewWindow can be returned by request GetViewWindow<SVTK_Viewer>(),
73    *        because VVTK_ViewWindow inherits SVTK_ViewWindow.
74    */
75   template<class TViewer>
76   inline
77   typename TViewer::TViewWindow*
78   GetViewWindow(VisuGUI* theModule)
79   {
80     typedef typename TViewer::TViewWindow TViewWindow;
81     if (SalomeApp_Application* anApp = theModule->getApp()) {
82       if (TViewWindow* aView = dynamic_cast<TViewWindow*>(anApp->desktop()->activeWindow()))
83         return aView;
84     }
85     SUIT_ViewManager* aViewManager =
86       theModule->getViewManager(TViewer::Type(), /*create = */true);
87     if (aViewManager) {
88       if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView()) {
89         if (TViewWindow* aView = dynamic_cast<TViewWindow*>(aViewWindow)) {
90           aViewWindow->raise();
91           aViewWindow->setFocus();
92           return aView;
93         }
94       }
95     }
96     return NULL;
97   }
98
99   /*! Return active view window, if it instantiates TViewWindow class, overwise return NULL.
100    *  \note Active VVTK_ViewWindow can be returned by request GetActiveViewWindow<SVTK_ViewWindow>(),
101    *        because VVTK_ViewWindow inherits SVTK_ViewWindow.
102    */
103   template<class TViewWindow>
104   inline
105   TViewWindow*
106   GetActiveViewWindow(const SalomeApp_Module* theModule = NULL)
107   {
108     SalomeApp_Application* anApp = NULL;
109     if (theModule)
110       anApp = theModule->getApp();
111     else
112       anApp = dynamic_cast<SalomeApp_Application*>
113         (SUIT_Session::session()->activeApplication());
114
115     if (anApp)
116       if (SUIT_ViewManager* aViewManager = anApp->activeViewManager())
117         if (SUIT_ViewWindow* aViewWindow = aViewManager->getActiveView())
118           return dynamic_cast<TViewWindow*>(aViewWindow);
119
120     return NULL;
121   }
122
123
124   //---------------------------------------------------------------
125   inline
126   VISU_Actor*
127   PublishInView(VisuGUI* theModule,
128                 Prs3d_i* thePrs,
129                 SVTK_ViewWindow* theViewWindow,
130                 const bool theIsHighlight = false)
131   {
132     if (theViewWindow) {
133       QApplication::setOverrideCursor( Qt::waitCursor );
134       try {
135         if (VISU_Actor* anActor = thePrs->CreateActor()) {
136           theViewWindow->AddActor(anActor);
137           if (theIsHighlight)
138             theViewWindow->highlight(anActor->getIO(),true);
139           theViewWindow->getRenderer()->ResetCameraClippingRange();
140           theViewWindow->Repaint();
141           QApplication::restoreOverrideCursor();
142           return anActor;
143         }
144       } catch(std::exception& exc) {
145         thePrs->RemoveActors();
146
147         QApplication::restoreOverrideCursor();
148         INFOS(exc.what());
149         SUIT_MessageBox::warn1
150           (GetDesktop(theModule), QObject::tr("WRN_VISU"),
151            QObject::tr("ERR_CANT_CREATE_ACTOR") + ": " + QObject::tr(exc.what()),
152            QObject::tr("BUT_OK"));
153       }
154     }
155     return NULL;
156   }
157
158
159   //---------------------------------------------------------------
160   inline
161   VISU_Actor*
162   UpdateViewer(VisuGUI* theModule,
163                VISU::Prs3d_i* thePrs,
164                bool theDispOnly = false,
165                const bool theIsHighlight = false)
166   {
167     if (SVTK_ViewWindow* aViewWindow = GetActiveViewWindow<SVTK_ViewWindow>(theModule)) {
168       vtkRenderer *aRen = aViewWindow->getRenderer();
169       vtkActorCollection *anActColl = aRen->GetActors();
170       anActColl->InitTraversal();
171       VISU_Actor* aResActor = NULL;
172       bool isOk = true;
173       while (vtkActor *anAct = anActColl->GetNextActor()) {
174         if (VISU_Actor* anActor = dynamic_cast<VISU_Actor*>(anAct)) {
175           if (VISU::Prs3d_i* aPrs3d = anActor->GetPrs3d()) {
176             if (thePrs == aPrs3d) {
177               try {
178                 aResActor = anActor;
179                 thePrs->UpdateActors();
180                 aResActor->VisibilityOn();
181               } catch (std::runtime_error& exc) {
182                 thePrs->RemoveActors();
183                 isOk = false;
184
185                 INFOS(exc.what());
186                 SUIT_MessageBox::warn1
187                   (GetDesktop(theModule), QObject::tr("WRN_VISU"),
188                    QObject::tr("ERR_CANT_BUILD_PRESENTATION") + ": " + QObject::tr(exc.what()),
189                    QObject::tr("BUT_OK"));
190               }
191             } else if (theDispOnly) {
192               anActor->VisibilityOff();
193             }
194           } else if (theDispOnly && anActor->GetVisibility()) {
195             anActor->VisibilityOff();
196           }
197         }
198       }
199       if (aResActor) {
200         if (theIsHighlight && isOk)
201           aViewWindow->highlight(aResActor->getIO(), true);
202         aViewWindow->getRenderer()->ResetCameraClippingRange();
203         aViewWindow->Repaint();
204         return aResActor;
205       }
206       return PublishInView(theModule, thePrs, aViewWindow, theIsHighlight);
207     }
208     return NULL;
209   }
210
211
212   //---------------------------------------------------------------
213   template<class TViewer>
214   inline
215   void
216   OnEraseAll(VisuGUI* theModule)
217   {
218     typedef typename TViewer::TViewWindow TViewWindow;
219     if (TViewWindow* aViewWindow = GetActiveViewWindow<TViewWindow>(theModule)) {
220       aViewWindow->unHighlightAll();
221       if (vtkRenderer *aRen = aViewWindow->getRenderer()) {
222         vtkActorCollection *aCollection = aRen->GetActors();
223         aCollection->InitTraversal();
224         while (vtkActor *anAct = aCollection->GetNextActor()) {
225           if (anAct->GetVisibility() > 0)
226             if (SALOME_Actor* anActor = dynamic_cast<SALOME_Actor*>(anAct)) {
227               anActor->VisibilityOff();
228             }
229         }
230         aViewWindow->Repaint();
231       }
232     }
233   }
234
235   template<>
236   inline
237   void
238   OnEraseAll<SPlot2d_Viewer>(VisuGUI* theModule)
239   {
240     if (SPlot2d_Viewer* aPlot2d = GetPlot2dViewer(theModule, false))
241       aPlot2d->EraseAll();
242   }
243
244
245   //---------------------------------------------------------------
246   inline
247   void
248   ErasePrs3d(VisuGUI* theModule,
249              VISU::Prs3d_i* thePrs,
250              const bool theDoRepaintVW = true)
251   {
252     if (SVTK_ViewWindow* aViewWindow = GetActiveViewWindow<SVTK_ViewWindow>(theModule)) {
253       if (VISU_Actor* anActor = FindActor(aViewWindow, thePrs)) {
254         anActor->VisibilityOff();
255         if (theDoRepaintVW)
256           aViewWindow->Repaint();
257       }
258     }
259   }
260
261   void
262   ErasePrs(VisuGUI* theModule,
263            CORBA::Object_ptr theObject, 
264            bool theDoRepaintVW);
265 }
266
267 #endif