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