Salome HOME
94400653a05726b525ad6888cf94db49b65def4e
[modules/geom.git] / src / DisplayGUI / DisplayGUI.cxx
1 //  GEOM GEOMGUI : GUI for Geometry component
2 //
3 //  Copyright (C) 2003  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   : DisplayGUI.cxx
25 //  Author : Vadim SANDLER
26 //  Module : GEOM
27 //  $Header$
28
29 #include "DisplayGUI.h"
30 #include "GeometryGUI.h"
31 #include "GEOM_Displayer.h"
32
33 #include <SUIT_Desktop.h>
34 #include <SUIT_Session.h>
35 #include <SUIT_ViewWindow.h>
36 #include <SUIT_OverrideCursor.h>
37
38 #include <VTKViewer_ViewWindow.h>
39 #include <VTKViewer_RenderWindowInteractor.h>
40 #include <OCCViewer_ViewManager.h>
41 #include <OCCViewer_ViewModel.h>
42 #include <OCCViewer_ViewWindow.h>
43
44 #include <SALOME_ListIteratorOfListIO.hxx>
45
46 #include <SVTK_ViewWindow.h>
47 #include <SVTK_RenderWindowInteractor.h>
48 #include <SVTK_ViewModel.h>
49 #include <SOCC_ViewModel.h>
50 #include <SVTK_Prs.h>
51 #include <SOCC_Prs.h>
52
53 #include <SalomeApp_Application.h>
54 #include <SalomeApp_SelectionMgr.h>
55 #include <SalomeApp_Study.h>
56
57 #include <AIS_ListIteratorOfListOfInteractive.hxx>
58
59 #include <qmenubar.h>
60
61
62 DisplayGUI* DisplayGUI::myGUIObject = 0;
63
64 //=======================================================================
65 // function : DisplayGUI::GetDisplayGUI()
66 // purpose  : Get the only DisplayGUI object [ static ]
67 //=======================================================================
68 DisplayGUI* DisplayGUI::GetDisplayGUI( GeometryGUI* parent )
69 {
70   if ( myGUIObject == 0 ) {
71     // init DisplayGUI only once
72     myGUIObject = new DisplayGUI( parent );
73   }
74   return myGUIObject;
75 }
76
77 //=======================================================================
78 // function : DisplayGUI::DisplayGUI()
79 // purpose  : Constructor
80 //=======================================================================
81 DisplayGUI::DisplayGUI( GeometryGUI* parent ) : GEOMGUI( parent )
82 {
83 }
84
85
86 //=======================================================================
87 // function : DisplayGUI::~DisplayGUI()
88 // purpose  : Destructor
89 //=======================================================================
90 DisplayGUI::~DisplayGUI()
91 {
92 }
93
94
95 //=======================================================================
96 // function : DisplayGUI::OnGUIEvent()
97 // purpose  : Dispatch menu command
98 //=======================================================================
99 bool DisplayGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
100 {
101   DisplayGUI* myDisplayGUI = GetDisplayGUI( getGeometryGUI() );
102
103   switch (theCommandID) {
104   case 211: // MENU VIEW - WIREFRAME/SHADING
105     {
106       myDisplayGUI->InvertDisplayMode();
107       int newMode = myDisplayGUI->GetDisplayMode();
108       SUIT_Session::session()->activeApplication()->desktop()->menuBar()->
109         changeItem( 211, newMode == 1 ? tr( "GEOM_MEN_WIREFRAME" ) : tr("GEOM_MEN_SHADING") );
110       break;
111     }
112   case 212: // MENU VIEW - DISPLAY ALL
113     {
114       myDisplayGUI->DisplayAll();
115       break;
116     }
117   case 213: // MENU VIEW - DISPLAY ONLY
118     {
119       myDisplayGUI->DisplayOnly();
120       break;
121     }
122   case 214: // MENU VIEW - ERASE ALL
123     {
124       myDisplayGUI->EraseAll();
125       break;
126     }
127   case 215: // MENU VIEW - ERASE
128     {
129       myDisplayGUI->Erase();
130       break;
131     }
132   case 216: // MENU VIEW - DISPLAY
133     {
134       myDisplayGUI->Display();
135       break;
136     }
137   case 80311: // POPUP VIEWER - WIREFRAME
138     {
139       myDisplayGUI->ChangeDisplayMode( 0 );
140       break;
141     }
142   case 80312: // POPUP VIEWER - SHADING
143     {
144       myDisplayGUI->ChangeDisplayMode( 1 );
145       break;
146     }
147   default:
148     {
149       SUIT_Session::session()->activeApplication()->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
150       break;
151     }
152   }
153   return true;
154 }
155
156 //=====================================================================================
157 // function : DisplayGUI::DisplayAll()
158 // purpose  : Display all GEOM objects
159 //=====================================================================================
160 void DisplayGUI::DisplayAll()
161 {
162   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
163   if ( !appStudy ) return;
164   _PTR(Study) aStudy = appStudy->studyDS();
165   if ( !aStudy ) return;
166   _PTR(SComponent) SC ( aStudy->FindComponent( "GEOM" ) );
167   if ( !SC )
168     return;
169
170   SALOME_ListIO listIO;
171   _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( SC ) );
172   anIter->InitEx( true );
173
174   SUIT_OverrideCursor();
175
176   while( anIter->More() ) {
177     _PTR(SObject) valSO ( anIter->Value() );
178     _PTR(SObject) refSO;
179     if ( !valSO->ReferencedObject( refSO ) ) {
180       listIO.Append( new SALOME_InteractiveObject( valSO->GetID().c_str(), SC->ComponentDataType().c_str() ,valSO->GetName().c_str() ) );
181     } 
182     anIter->Next();
183   }
184   GEOM_Displayer( appStudy ).Display( listIO, true );
185 }
186
187 //=====================================================================================
188 // function : DisplayGUI::EraseAll()
189 // purpose  : Erase all GEOM objects
190 //=====================================================================================
191 void DisplayGUI::EraseAll()
192 {
193   SUIT_OverrideCursor();
194
195   SUIT_Application* app = SUIT_Session::session()->activeApplication();
196   if ( app ) {
197     SUIT_ViewWindow* vw = app->desktop()->activeWindow();
198     if ( vw ) {
199       SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
200       SUIT_ViewManager* vman = vw->getViewManager();
201       if ( vman->getType() == OCCViewer_Viewer::Type() || 
202            vman->getType() == VTKViewer_Viewer::Type() ) {
203         GEOM_Displayer( appStudy ).EraseAll();
204       }
205     }
206   }
207 }
208
209 //=====================================================================================
210 // function : DisplayGUI::DisplayOnly()
211 // purpose  : Display selected GEOM objects and erase other
212 //=====================================================================================
213 void DisplayGUI::DisplayOnly()
214 {
215   EraseAll();
216   Display();
217 }
218
219 //=====================================================================================
220 // function : DisplayGUI::Display()
221 // purpose  : Display selected GEOM objects
222 //=====================================================================================
223 void DisplayGUI::Display()
224 {
225   SALOME_ListIO listIO;
226   
227   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
228   if ( !app ) return;
229
230   SalomeApp_Study* anActiveStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
231   if ( !anActiveStudy ) return;
232   
233   //get SalomeApp selection manager
234   SalomeApp_SelectionMgr* aSelMgr = app->selectionMgr();
235   if ( !aSelMgr ) return;
236   
237   SALOME_ListIO aList;
238   aSelMgr->selectedObjects( aList );
239   SALOME_ListIteratorOfListIO It( aList );
240   
241   SUIT_OverrideCursor();
242
243   for( ;It.More();It.Next() ) {
244     Handle(SALOME_InteractiveObject) anIObject = It.Value();
245     if ( anIObject->hasEntry() ) {
246       _PTR(SObject) SO ( anActiveStudy->studyDS()->FindObjectID( anIObject->getEntry() ) );
247       if ( SO && QString( SO->GetID().c_str() ) == QString( SO->GetFatherComponent()->GetID().c_str() ) ) {
248         _PTR(SComponent) SC ( SO->GetFatherComponent() );
249         // if component is selected
250         listIO.Clear();
251         _PTR(ChildIterator) anIter ( anActiveStudy->studyDS()->NewChildIterator( SO ) );
252         anIter->InitEx( true );
253         while( anIter->More() ) {
254           _PTR(SObject) valSO ( anIter->Value() );
255           _PTR(SObject) refSO;
256           if ( !valSO->ReferencedObject( refSO ) ) {
257             listIO.Append( new SALOME_InteractiveObject( valSO->GetID().c_str(), SC->ComponentDataType().c_str() ,valSO->GetName().c_str() ) );
258           } 
259           anIter->Next();
260         }
261         break;
262       }
263       else {
264         listIO.Append( anIObject );
265       }
266     }
267     else {
268       listIO.Append( anIObject );
269     }
270   }
271   GEOM_Displayer( anActiveStudy ).Display( listIO, true );
272 }
273
274
275 //=====================================================================================
276 // function : DisplayGUI::Erase()
277 // purpose  : Erase selected GEOM objects
278 //=====================================================================================
279 void DisplayGUI::Erase()
280 {
281   SALOME_ListIO listIO;
282
283   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
284   if ( !app ) return;
285
286   SalomeApp_Study* anActiveStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
287   if ( !anActiveStudy ) return;
288   
289   //get SalomeApp selection manager
290   SalomeApp_SelectionMgr* aSelMgr = app->selectionMgr();
291   if ( !aSelMgr ) return;
292   
293   SALOME_ListIO aList;
294   aSelMgr->selectedObjects( aList );
295   SALOME_ListIteratorOfListIO It( aList );
296
297   SUIT_OverrideCursor();
298
299   for( ;It.More();It.Next() ) {
300     Handle(SALOME_InteractiveObject) anIObject = It.Value();
301     if ( anIObject->hasEntry() ) {
302       _PTR(SObject) SO ( anActiveStudy->studyDS()->FindObjectID( anIObject->getEntry() ) );
303       if ( SO && QString( SO->GetID().c_str() ) == QString( SO->GetFatherComponent()->GetID().c_str() ) ) {
304         _PTR(SComponent) SC ( SO->GetFatherComponent() );
305         // if component is selected
306         listIO.Clear();
307         _PTR(ChildIterator) anIter ( anActiveStudy->studyDS()->NewChildIterator( SO ) );
308         anIter->InitEx( true );
309         while( anIter->More() ) {
310           _PTR(SObject) valSO ( anIter->Value() );
311           _PTR(SObject) refSO;
312           if ( !valSO->ReferencedObject( refSO ) ) {
313             listIO.Append( new SALOME_InteractiveObject( valSO->GetID().c_str(), SC->ComponentDataType().c_str() ,valSO->GetName().c_str() ) );
314           } 
315           anIter->Next();
316         }
317         break;
318       }
319       else {
320         listIO.Append( anIObject );
321       }
322     }
323     else {
324       listIO.Append( anIObject );
325     }
326   }
327   GEOM_Displayer(anActiveStudy).Erase( listIO, true );
328   ((SalomeApp_Application*)(SUIT_Session::session()->activeApplication()))->selectionMgr()->clearSelected();
329 }
330
331 //=====================================================================================
332 // function : DisplayGUI::SetDisplayMode()
333 // purpose  : Set display mode for the viewer (current viewer if <viewWindow> - 0 )
334 //=====================================================================================
335 void DisplayGUI::SetDisplayMode( const int mode, SUIT_ViewWindow* viewWindow )
336 {
337   SUIT_OverrideCursor();
338
339   if ( !viewWindow ) 
340     viewWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
341   if ( viewWindow->getViewManager()->getType() == VTKViewer_Viewer::Type() ) {
342     VTKViewer_RenderWindowInteractor* myRenderInter= ((VTKViewer_ViewWindow*)viewWindow)->getRWInteractor();
343     myRenderInter->SetDisplayMode( mode );
344   } 
345   else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
346     OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
347     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
348     AIS_DisplayMode newmode = (mode == 1 ? AIS_Shaded : AIS_WireFrame);
349     AIS_ListOfInteractive List;
350     ic->DisplayedObjects( List );
351     AIS_ListOfInteractive List1;
352     ic->ObjectsInCollector( List1 );
353     List.Append( List1 );
354     
355     AIS_ListIteratorOfListOfInteractive ite( List );
356     while( ite.More() ) {
357       if( ite.Value()->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
358         Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast( ite.Value() );
359         ic->SetDisplayMode( aSh, Standard_Integer( newmode ),true );
360       }
361       ite.Next();
362     }
363     
364     ic->SetDisplayMode( newmode, Standard_False );
365   }
366 }
367
368 //=====================================================================================
369 // function : DisplayGUI::GetDisplayMode()
370 // purpose  : Get display mode of the viewer (current viewer if <viewWindow> - 0 )
371 //=====================================================================================
372 int DisplayGUI::GetDisplayMode( SUIT_ViewWindow* viewWindow )
373 {
374   int dispMode = 0;
375   if ( !viewWindow ) 
376     viewWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
377   if ( viewWindow->getViewManager()->getType() == VTKViewer_Viewer::Type() ) {
378     VTKViewer_RenderWindowInteractor* myRenderInter= ((VTKViewer_ViewWindow*)viewWindow)->getRWInteractor();
379     dispMode = myRenderInter->GetDisplayMode();
380   } 
381   else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
382     OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
383     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
384     AIS_DisplayMode mode = (AIS_DisplayMode)ic->DisplayMode();
385     dispMode = (mode == AIS_WireFrame ? 0 : 1 );
386   }
387   return dispMode;
388 }
389
390 //=====================================================================================
391 // function : DisplayGUI::InvertDisplayMode()
392 // purpose  : Invert display mode ( shadin <-> wireframe ) for the viewer 
393 //            (current viewer if <viewWindow> = 0 )
394 //=====================================================================================
395 void DisplayGUI::InvertDisplayMode( SUIT_ViewWindow* viewWindow )
396 {
397   SetDisplayMode( 1 - GetDisplayMode( viewWindow ) );
398 }
399
400 //=====================================================================================
401 // function : DisplayGUI::ChangeDisplayMode()
402 // purpose  : Set display mode for selected objects in the viewer given
403 //            (current viewer if <viewWindow> = 0 )
404 //=====================================================================================
405 void DisplayGUI::ChangeDisplayMode( const int mode, SUIT_ViewWindow* viewWindow )
406 {
407   if ( !viewWindow ) 
408     viewWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
409
410   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
411   if ( !app ) return;
412
413   SalomeApp_SelectionMgr* aSelMgr = app->selectionMgr();
414   if ( !aSelMgr ) return;
415   
416   SUIT_OverrideCursor();
417
418   SALOME_ListIO aList;
419   
420   if ( viewWindow->getViewManager()->getType() == VTKViewer_Viewer::Type() ) {
421     SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>( viewWindow );
422     SVTK_RenderWindowInteractor* rwi = vw->getRWInteractor();
423
424     aSelMgr->selectedObjects( aList );
425     SALOME_ListIteratorOfListIO It( aList );
426
427     for( ;It.More(); It.Next() ) {
428       SVTK_Viewer* stvkViewer = (SVTK_Viewer*)(vw->getViewManager()->getViewModel());
429       SVTK_Prs* vtkPrs = dynamic_cast<SVTK_Prs*>( stvkViewer->CreatePrs( It.Value()->getEntry() ) );
430       if ( vtkPrs && !vtkPrs->IsNull() ) {
431         if ( mode == 0 )
432           rwi->ChangeRepresentationToWireframe( vtkPrs->GetObjects() );
433         else if ( mode == 1 )
434           rwi->ChangeRepresentationToSurface( vtkPrs->GetObjects() );
435       }
436     }
437     rwi->Render();
438   }
439   else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
440     OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
441     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
442
443     aSelMgr->selectedObjects( aList );
444     SALOME_ListIteratorOfListIO It( aList );
445
446     for( ;It.More(); It.Next() ) {
447       SOCC_Viewer* soccViewer = (SOCC_Viewer*)(viewWindow->getViewManager()->getViewModel());
448       SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( soccViewer->CreatePrs( It.Value()->getEntry() ) );
449       if ( occPrs && !occPrs->IsNull() ) {
450         AIS_ListOfInteractive shapes; occPrs->GetObjects( shapes );
451         AIS_ListIteratorOfListOfInteractive interIter( shapes );
452         for ( ; interIter.More(); interIter.Next() ) {
453           if ( mode == 0 )
454             ic->SetDisplayMode( interIter.Value(), AIS_WireFrame, false );
455           else if ( mode == 1 )
456             ic->SetDisplayMode( interIter.Value(), AIS_Shaded, false );
457         }
458       }
459     }
460     ic->UpdateCurrentViewer();
461   }
462 }
463
464 //=====================================================================================
465 // EXPORTED METHODS
466 //=====================================================================================
467 extern "C"
468 {
469   GEOMGUI* GetLibGUI( GeometryGUI* parent )
470   {
471     return DisplayGUI::GetDisplayGUI( parent );
472   }
473 }