Salome HOME
0020327: EDF 1018 GEOM : Missing MakeTangentPlaneOnFace in geompy
[modules/geom.git] / src / DisplayGUI / DisplayGUI.cxx
1 //  Copyright (C) 2007-2008  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  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 //  GEOM GEOMGUI : GUI for Geometry component
23 //  File   : DisplayGUI.cxx
24 //  Author : Vadim SANDLER
25 //  Module : GEOM
26 //  $Header$
27 //
28 #include "DisplayGUI.h"
29 #include "GeometryGUI.h"
30 #include "GEOM_Displayer.h"
31
32 #include <SUIT_Desktop.h>
33 #include <SUIT_Session.h>
34 #include <SUIT_ViewWindow.h>
35 #include <SUIT_OverrideCursor.h>
36
37 #include <OCCViewer_ViewManager.h>
38 #include <OCCViewer_ViewModel.h>
39 #include <OCCViewer_ViewWindow.h>
40
41 #include <SALOME_ListIteratorOfListIO.hxx>
42
43 #include <SVTK_ViewWindow.h>
44 #include <SVTK_View.h>
45 #include <SVTK_ViewModel.h>
46 #include <SOCC_ViewModel.h>
47 #include <SVTK_Prs.h>
48 #include <SOCC_Prs.h>
49
50 #include <QtxActionMenuMgr.h>
51
52 #include <SalomeApp_Application.h>
53 #include <LightApp_SelectionMgr.h>
54 #include <SalomeApp_Study.h>
55
56 #include <AIS_ListIteratorOfListOfInteractive.hxx>
57
58 #include <qmenubar.h>
59
60
61 //=======================================================================
62 // function : DisplayGUI::DisplayGUI()
63 // purpose  : Constructor
64 //=======================================================================
65 DisplayGUI::DisplayGUI( GeometryGUI* parent ) : GEOMGUI( parent )
66 {
67 }
68
69 //=======================================================================
70 // function : DisplayGUI::~DisplayGUI()
71 // purpose  : Destructor
72 //=======================================================================
73 DisplayGUI::~DisplayGUI()
74 {
75 }
76
77
78 //=======================================================================
79 // function : DisplayGUI::OnGUIEvent()
80 // purpose  : Dispatch menu command
81 //=======================================================================
82 bool DisplayGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
83 {
84   SalomeApp_Application* app = getGeometryGUI()->getApp();
85   if (!app) return false;
86
87   LightApp_SelectionMgr *Sel = app->selectionMgr();
88   SALOME_ListIO selected;
89   Sel->selectedObjects( selected );
90
91   switch (theCommandID) {
92   case 211: // MENU VIEW - WIREFRAME/SHADING
93     {
94       InvertDisplayMode();
95       int newMode = GetDisplayMode();
96       getGeometryGUI()->action( 211 )->setMenuText
97         ( newMode == 1 ? tr( "GEOM_MEN_WIREFRAME" ) : tr("GEOM_MEN_SHADING") );
98       getGeometryGUI()->menuMgr()->update();
99       break;
100     }
101   case 212: // MENU VIEW - DISPLAY ALL
102     {
103       getGeometryGUI()->EmitSignalDeactivateDialog();
104       DisplayAll();
105       break;
106     }
107   case 213: // MENU VIEW - DISPLAY ONLY
108     {
109       getGeometryGUI()->EmitSignalDeactivateDialog();
110       DisplayOnly();
111       break;
112     }
113   case 214: // MENU VIEW - ERASE ALL
114     {
115       EraseAll();
116       break;
117     }
118   case 215: // MENU VIEW - ERASE
119     {
120       Erase();
121       break;
122     }
123   case 216: // MENU VIEW - DISPLAY
124     {
125       getGeometryGUI()->EmitSignalDeactivateDialog();
126       Display();
127       break;
128     }
129   case 80311: // POPUP VIEWER - WIREFRAME
130     {
131       ChangeDisplayMode( 0 );
132       break;
133     }
134   case 80312: // POPUP VIEWER - SHADING
135     {
136       ChangeDisplayMode( 1 );
137       break;
138     }
139   default:
140     {
141       app->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
142       break;
143     }
144   }
145   Sel->setSelectedObjects( selected );
146   return true;
147 }
148
149 //=====================================================================================
150 // function : DisplayGUI::DisplayAll()
151 // purpose  : Display all GEOM objects
152 //=====================================================================================
153 void DisplayGUI::DisplayAll()
154 {
155   SalomeApp_Application* app = getGeometryGUI()->getApp();
156   if ( !app ) return;
157
158   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
159   if ( !appStudy ) return;
160   _PTR(Study) aStudy = appStudy->studyDS();
161   if ( !aStudy ) return;
162   _PTR(SComponent) SC ( aStudy->FindComponent( "GEOM" ) );
163   if ( !SC )
164     return;
165
166   SALOME_ListIO listIO;
167   _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( SC ) );
168   anIter->InitEx( true );
169
170   SUIT_OverrideCursor();
171
172   while( anIter->More() ) {
173     _PTR(SObject) valSO ( anIter->Value() );
174     _PTR(SObject) refSO;
175     if ( !valSO->ReferencedObject( refSO ) ) {
176       listIO.Append( new SALOME_InteractiveObject(valSO->GetID().c_str(),
177                                                   SC->ComponentDataType().c_str(),
178                                                   valSO->GetName().c_str()) );
179     } 
180     anIter->Next();
181   }
182   GEOM_Displayer( appStudy ).Display( listIO, true );
183 }
184
185 //=====================================================================================
186 // function : DisplayGUI::EraseAll()
187 // purpose  : Erase all GEOM objects
188 //=====================================================================================
189 void DisplayGUI::EraseAll()
190 {
191   SUIT_OverrideCursor();
192
193   SUIT_Application* app = getGeometryGUI()->getApp();
194   if ( app ) {
195     SUIT_ViewWindow* vw = app->desktop()->activeWindow();
196     if ( vw ) {
197       SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
198       SUIT_ViewManager* vman = vw->getViewManager();
199       if ( vman->getType() == OCCViewer_Viewer::Type() || 
200            vman->getType() == SVTK_Viewer::Type() ) {
201         GEOM_Displayer( appStudy ).EraseAll();
202       }
203     }
204   }
205 }
206
207 //=====================================================================================
208 // function : DisplayGUI::DisplayOnly()
209 // purpose  : Display selected GEOM objects and erase other
210 //=====================================================================================
211 void DisplayGUI::DisplayOnly()
212 {
213   EraseAll();
214   Display();
215 }
216
217 //=====================================================================================
218 // function : DisplayGUI::Display()
219 // purpose  : Display selected GEOM objects
220 //=====================================================================================
221 void DisplayGUI::Display()
222 {
223   SALOME_ListIO listIO;
224
225   SalomeApp_Application* app = getGeometryGUI()->getApp();
226   if ( !app ) return;
227
228   SalomeApp_Study* anActiveStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
229   if ( !anActiveStudy ) return;
230
231   //get SalomeApp selection manager
232   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
233   if ( !aSelMgr ) return;
234
235   SALOME_ListIO aList;
236   aSelMgr->selectedObjects( aList );
237   SALOME_ListIteratorOfListIO It( aList );
238
239   SUIT_OverrideCursor();
240
241   for( ;It.More();It.Next() ) {
242     Handle(SALOME_InteractiveObject) anIObject = It.Value();
243     if ( anIObject->hasEntry() ) {
244       _PTR(SObject) SO ( anActiveStudy->studyDS()->FindObjectID( anIObject->getEntry() ) );
245       if ( SO && QString(SO->GetID().c_str()) == QString(SO->GetFatherComponent()->GetID().c_str()) ) {
246         _PTR(SComponent) SC ( SO->GetFatherComponent() );
247         // if component is selected
248         listIO.Clear();
249         _PTR(ChildIterator) anIter ( anActiveStudy->studyDS()->NewChildIterator( SO ) );
250         anIter->InitEx( true );
251         while( anIter->More() ) {
252           _PTR(SObject) valSO ( anIter->Value() );
253           _PTR(SObject) refSO;
254           if ( !valSO->ReferencedObject( refSO ) ) {
255             listIO.Append( new SALOME_InteractiveObject(valSO->GetID().c_str(),
256                                                         SC->ComponentDataType().c_str(),
257                                                         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 = getGeometryGUI()->getApp();
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   LightApp_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(),
314                                                         SC->ComponentDataType().c_str(),
315                                                         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
330   SUIT_ViewWindow* viewWindow = app->desktop()->activeWindow();
331   bool aIsForced = true;
332   if(viewWindow->getViewManager()->getType() == SVTK_Viewer::Type())
333     aIsForced = false;
334
335   GEOM_Displayer(anActiveStudy).Erase( listIO, aIsForced);
336   getGeometryGUI()->getApp()->selectionMgr()->clearSelected();
337 }
338
339 //=====================================================================================
340 // function : DisplayGUI::SetDisplayMode()
341 // purpose  : Set display mode for the viewer (current viewer if <viewWindow> - 0 )
342 //=====================================================================================
343 void DisplayGUI::SetDisplayMode( const int mode, SUIT_ViewWindow* viewWindow )
344 {
345   SUIT_OverrideCursor();
346
347   if ( !viewWindow ) 
348     viewWindow = getGeometryGUI()->getApp()->desktop()->activeWindow();
349   if ( viewWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
350     SVTK_View* aView = ((SVTK_ViewWindow*)viewWindow)->getView();
351     aView->SetDisplayMode( mode );
352   } 
353   else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
354     OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
355     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
356     AIS_DisplayMode newmode = (mode == 1 ? AIS_Shaded : AIS_WireFrame);
357     AIS_ListOfInteractive List;
358     ic->DisplayedObjects( List );
359     AIS_ListOfInteractive List1;
360     ic->ObjectsInCollector( List1 );
361     List.Append( List1 );
362
363     AIS_ListIteratorOfListOfInteractive ite( List );
364     while( ite.More() ) {
365       if( ite.Value()->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
366         Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast( ite.Value() );
367         ic->SetDisplayMode( aSh, Standard_Integer( newmode ),true );
368       }
369       ite.Next();
370     }
371
372     ic->SetDisplayMode( newmode, Standard_False );
373   }
374 }
375
376 //=====================================================================================
377 // function : DisplayGUI::GetDisplayMode()
378 // purpose  : Get display mode of the viewer (current viewer if <viewWindow> - 0 )
379 //=====================================================================================
380 int DisplayGUI::GetDisplayMode( SUIT_ViewWindow* viewWindow )
381 {
382   int dispMode = 0;
383   if ( !viewWindow ) 
384     viewWindow = getGeometryGUI()->getApp()->desktop()->activeWindow();
385   if ( viewWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
386     SVTK_View* aView = ((SVTK_ViewWindow*)viewWindow)->getView();
387     dispMode = aView->GetDisplayMode();
388   } 
389   else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
390     OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
391     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
392     AIS_DisplayMode mode = (AIS_DisplayMode)ic->DisplayMode();
393     dispMode = (mode == AIS_WireFrame ? 0 : 1 );
394   }
395   return dispMode;
396 }
397
398 //=====================================================================================
399 // function : DisplayGUI::InvertDisplayMode()
400 // purpose  : Invert display mode ( shadin <-> wireframe ) for the viewer 
401 //            (current viewer if <viewWindow> = 0 )
402 //=====================================================================================
403 void DisplayGUI::InvertDisplayMode( SUIT_ViewWindow* viewWindow )
404 {
405   SetDisplayMode( 1 - GetDisplayMode( viewWindow ) );
406 }
407
408 //=====================================================================================
409 // function : DisplayGUI::ChangeDisplayMode()
410 // purpose  : Set display mode for selected objects in the viewer given
411 //            (current viewer if <viewWindow> = 0 )
412 //=====================================================================================
413 void DisplayGUI::ChangeDisplayMode( const int mode, SUIT_ViewWindow* viewWindow )
414 {
415   SalomeApp_Application* app = getGeometryGUI()->getApp();
416   if ( !app ) return;
417
418   if ( !viewWindow ) 
419     viewWindow = app->desktop()->activeWindow();
420
421   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
422   if ( !aSelMgr ) return;
423
424   SUIT_OverrideCursor();
425
426   SALOME_ListIO aList;
427
428   if ( viewWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
429     SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>( viewWindow );
430     SVTK_View* aView = vw->getView();
431
432     aSelMgr->selectedObjects( aList );
433     SALOME_ListIteratorOfListIO It( aList );
434
435     for( ;It.More(); It.Next() ) {
436       SVTK_Viewer* stvkViewer = dynamic_cast<SVTK_Viewer*>(vw->getViewManager()->getViewModel());
437       SVTK_Prs* vtkPrs =
438         stvkViewer ? dynamic_cast<SVTK_Prs*>( stvkViewer->CreatePrs( It.Value()->getEntry() ) ) : 0;
439       if ( vtkPrs && !vtkPrs->IsNull() ) {
440         if ( mode == 0 )
441           aView->ChangeRepresentationToWireframe( vtkPrs->GetObjects() );
442         else if ( mode == 1 )
443           aView->ChangeRepresentationToSurface( vtkPrs->GetObjects() );
444       }
445     }
446     aView->Repaint();
447   }
448   else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
449     OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
450     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
451
452     aSelMgr->selectedObjects( aList );
453     SALOME_ListIteratorOfListIO It( aList );
454
455     for( ;It.More(); It.Next() ) {
456       SOCC_Viewer* soccViewer = (SOCC_Viewer*)(viewWindow->getViewManager()->getViewModel());
457       SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( soccViewer->CreatePrs( It.Value()->getEntry() ) );
458       if ( occPrs && !occPrs->IsNull() ) {
459         AIS_ListOfInteractive shapes; occPrs->GetObjects( shapes );
460         AIS_ListIteratorOfListOfInteractive interIter( shapes );
461         for ( ; interIter.More(); interIter.Next() ) {
462           if ( mode == 0 )
463             ic->SetDisplayMode( interIter.Value(), AIS_WireFrame, false );
464           else if ( mode == 1 )
465             ic->SetDisplayMode( interIter.Value(), AIS_Shaded, false );
466         }
467       }
468     }
469     ic->UpdateCurrentViewer();
470   }
471 }
472
473 //=====================================================================================
474 // EXPORTED METHODS
475 //=====================================================================================
476 extern "C"
477 {
478  GEOM_DISPLAYGUI_EXPORT
479   GEOMGUI* GetLibGUI( GeometryGUI* parent )
480   {
481     return new DisplayGUI( parent );
482   }
483 }