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