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