Salome HOME
4f7f9485fa2ddd9dd98459625fb5831ad7323224
[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 <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       getGeometryGUI()->action( 211 )->setMenuText( newMode == 1 ? tr( "GEOM_MEN_WIREFRAME" ) : tr("GEOM_MEN_SHADING") );
107       getGeometryGUI()->menuMgr()->update();
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() == SVTK_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() == SVTK_Viewer::Type() ) {
342     SVTK_ViewWindow* wnd = dynamic_cast<SVTK_ViewWindow*>( viewWindow );
343     SVTK_RenderWindowInteractor* myRenderInter = wnd ? wnd->getRWInteractor() : 0;
344     if( myRenderInter )
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() == SVTK_Viewer::Type() ) {
380     SVTK_ViewWindow* wnd = dynamic_cast<SVTK_ViewWindow*>( viewWindow );
381     SVTK_RenderWindowInteractor* myRenderInter = wnd ? wnd->getRWInteractor() : 0;
382     if( myRenderInter )
383       dispMode = myRenderInter->GetDisplayMode();
384   } 
385   else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
386     OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
387     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
388     AIS_DisplayMode mode = (AIS_DisplayMode)ic->DisplayMode();
389     dispMode = (mode == AIS_WireFrame ? 0 : 1 );
390   }
391   return dispMode;
392 }
393
394 //=====================================================================================
395 // function : DisplayGUI::InvertDisplayMode()
396 // purpose  : Invert display mode ( shadin <-> wireframe ) for the viewer 
397 //            (current viewer if <viewWindow> = 0 )
398 //=====================================================================================
399 void DisplayGUI::InvertDisplayMode( SUIT_ViewWindow* viewWindow )
400 {
401   SetDisplayMode( 1 - GetDisplayMode( viewWindow ) );
402 }
403
404 //=====================================================================================
405 // function : DisplayGUI::ChangeDisplayMode()
406 // purpose  : Set display mode for selected objects in the viewer given
407 //            (current viewer if <viewWindow> = 0 )
408 //=====================================================================================
409 void DisplayGUI::ChangeDisplayMode( const int mode, SUIT_ViewWindow* viewWindow )
410 {
411   if ( !viewWindow ) 
412     viewWindow = SUIT_Session::session()->activeApplication()->desktop()->activeWindow();
413
414   SalomeApp_Application* app = dynamic_cast< SalomeApp_Application* >( SUIT_Session::session()->activeApplication() );
415   if ( !app ) return;
416
417   SalomeApp_SelectionMgr* aSelMgr = app->selectionMgr();
418   if ( !aSelMgr ) return;
419   
420   SUIT_OverrideCursor();
421
422   SALOME_ListIO aList;
423   
424   if ( viewWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
425     SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>( viewWindow );
426     SVTK_RenderWindowInteractor* rwi = vw ? vw->getRWInteractor() : 0;
427
428     if( !rwi )
429       return;
430
431     aSelMgr->selectedObjects( aList );
432     SALOME_ListIteratorOfListIO It( aList );
433
434     for( ;It.More(); It.Next() ) {
435       SVTK_Viewer* stvkViewer = dynamic_cast<SVTK_Viewer*>(vw->getViewManager()->getViewModel());
436       SVTK_Prs* vtkPrs = stvkViewer ? dynamic_cast<SVTK_Prs*>( stvkViewer->CreatePrs( It.Value()->getEntry() ) ) : 0;
437       if ( vtkPrs && !vtkPrs->IsNull() ) {
438         if ( mode == 0 )
439           rwi->ChangeRepresentationToWireframe( vtkPrs->GetObjects() );
440         else if ( mode == 1 )
441           rwi->ChangeRepresentationToSurface( vtkPrs->GetObjects() );
442       }
443     }
444     rwi->Render();
445   }
446   else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
447     OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
448     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
449
450     aSelMgr->selectedObjects( aList );
451     SALOME_ListIteratorOfListIO It( aList );
452
453     for( ;It.More(); It.Next() ) {
454       SOCC_Viewer* soccViewer = (SOCC_Viewer*)(viewWindow->getViewManager()->getViewModel());
455       SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( soccViewer->CreatePrs( It.Value()->getEntry() ) );
456       if ( occPrs && !occPrs->IsNull() ) {
457         AIS_ListOfInteractive shapes; occPrs->GetObjects( shapes );
458         AIS_ListIteratorOfListOfInteractive interIter( shapes );
459         for ( ; interIter.More(); interIter.Next() ) {
460           if ( mode == 0 )
461             ic->SetDisplayMode( interIter.Value(), AIS_WireFrame, false );
462           else if ( mode == 1 )
463             ic->SetDisplayMode( interIter.Value(), AIS_Shaded, false );
464         }
465       }
466     }
467     ic->UpdateCurrentViewer();
468   }
469 }
470
471 //=====================================================================================
472 // EXPORTED METHODS
473 //=====================================================================================
474 extern "C"
475 {
476 #ifdef WNT
477         __declspec( dllexport )
478 #endif
479   GEOMGUI* GetLibGUI( GeometryGUI* parent )
480   {
481     return DisplayGUI::GetDisplayGUI( parent );
482   }
483 }