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