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