]> SALOME platform Git repositories - modules/geom.git/blob - src/DisplayGUI/DisplayGUI.cxx
Salome HOME
0020907: Werror in GEOM: integrate patch from Erwan ADAM
[modules/geom.git] / src / DisplayGUI / DisplayGUI.cxx
1 //  Copyright (C) 2007-2010  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
23 // GEOM GEOMGUI : GUI for Geometry component
24 // File   : DisplayGUI.cxx
25 // Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
26 //
27 #include "DisplayGUI.h"
28 #include <GeometryGUI.h>
29 #include "GeometryGUI_Operations.h"
30 #include <GEOM_Displayer.h>
31 #include <GEOM_AISShape.hxx>
32 #include <GEOM_Actor.h>
33
34 #include <SUIT_Desktop.h>
35 #include <SUIT_ViewWindow.h>
36 #include <SUIT_OverrideCursor.h>
37
38 #include <OCCViewer_ViewManager.h>
39 #include <OCCViewer_ViewModel.h>
40
41 #include <SALOME_ListIO.hxx>
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 <vtkActorCollection.h>
60 #include <vtkRenderer.h>
61
62 #include <QAction>
63
64 //=======================================================================
65 // function : DisplayGUI::DisplayGUI()
66 // purpose  : Constructor
67 //=======================================================================
68 DisplayGUI::DisplayGUI( GeometryGUI* parent ) : GEOMGUI( parent )
69 {
70 }
71
72 //=======================================================================
73 // function : DisplayGUI::~DisplayGUI()
74 // purpose  : Destructor
75 //=======================================================================
76 DisplayGUI::~DisplayGUI()
77 {
78 }
79
80
81 //=======================================================================
82 // function : DisplayGUI::OnGUIEvent()
83 // purpose  : Dispatch menu command
84 //=======================================================================
85 bool DisplayGUI::OnGUIEvent(int theCommandID, SUIT_Desktop* parent)
86 {
87   SalomeApp_Application* app = getGeometryGUI()->getApp();
88   if (!app) return false;
89
90   LightApp_SelectionMgr *Sel = app->selectionMgr();
91   SALOME_ListIO selected;
92   Sel->selectedObjects( selected );
93
94   switch ( theCommandID ) {
95   case GEOMOp::OpDisplayMode:    // MENU VIEW - DISPLAY MODE - WIREFRAME/SHADING
96     InvertDisplayMode();
97     getGeometryGUI()->action( GEOMOp::OpDisplayMode )->setText
98       ( GetDisplayMode() == 1 ? tr( "GEOM_MEN_WIREFRAME" ) : tr("GEOM_MEN_SHADING") );
99     getGeometryGUI()->menuMgr()->update();
100     break;
101   case GEOMOp::OpShowAll:        // MENU VIEW - SHOW ALL
102     getGeometryGUI()->EmitSignalDeactivateDialog();
103     DisplayAll();
104     break;
105   case GEOMOp::OpShowOnly:       // POPUP MENU - SHOW ONLY
106     getGeometryGUI()->EmitSignalDeactivateDialog();
107     DisplayOnly();
108     break;
109   case GEOMOp::OpHideAll:        // MENU VIEW - HIDE ALL
110     EraseAll();
111     break;
112   case GEOMOp::OpHide:           // POPUP MENU - HIDE
113     Erase();
114     break;
115   case GEOMOp::OpShow:           // POPUP MENU - SHOW
116     getGeometryGUI()->EmitSignalDeactivateDialog();
117     Display();
118     break;
119   case GEOMOp::OpSwitchVectors:  // MENU VIEW - DISPLAY MODE - SHOW EDGE DIRECTION
120     SetVectorMode(!GetVectorMode());
121     getGeometryGUI()->action( GEOMOp::OpSwitchVectors )->setText
122       ( GetVectorMode() ? tr("MEN_VECTOR_MODE_ON") : tr( "MEN_VECTOR_MODE_OFF" ) );
123     getGeometryGUI()->menuMgr()->update();
124     break;
125   case GEOMOp::OpWireframe:      // POPUP MENU - DISPLAY MODE - WIREFRAME
126     ChangeDisplayMode( 0 );
127     break;
128   case GEOMOp::OpShading:        // POPUP MENU - DISPLAY MODE - SHADING
129     ChangeDisplayMode( 1 );
130     break;
131   case GEOMOp::OpVectors:        // POPUP MENU - DISPLAY MODE - SHOW EDGE DIRECTION
132     ChangeDisplayMode( 2 );
133     break;
134   default:
135     app->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
136     break;
137   }
138   Sel->setSelectedObjects( selected );
139   return true;
140 }
141
142 //=====================================================================================
143 // function : DisplayGUI::DisplayAll()
144 // purpose  : Display all GEOM objects
145 //=====================================================================================
146 void DisplayGUI::DisplayAll()
147 {
148   SalomeApp_Application* app = getGeometryGUI()->getApp();
149   if ( !app ) return;
150
151   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
152   if ( !appStudy ) return;
153   _PTR(Study) aStudy = appStudy->studyDS();
154   if ( !aStudy ) return;
155   _PTR(SComponent) SC ( aStudy->FindComponent( "GEOM" ) );
156   if ( !SC )
157     return;
158
159   SALOME_ListIO listIO;
160   _PTR(ChildIterator) anIter ( aStudy->NewChildIterator( SC ) );
161   anIter->InitEx( true );
162
163   SUIT_OverrideCursor();
164
165   while( anIter->More() ) {
166     _PTR(SObject) valSO ( anIter->Value() );
167     _PTR(SObject) refSO;
168     if ( !valSO->ReferencedObject( refSO ) ) {
169       listIO.Append( new SALOME_InteractiveObject(valSO->GetID().c_str(),
170                                                   SC->ComponentDataType().c_str(),
171                                                   valSO->GetName().c_str()) );
172     } 
173     anIter->Next();
174   }
175   GEOM_Displayer( appStudy ).Display( listIO, true );
176 }
177
178 //=====================================================================================
179 // function : DisplayGUI::EraseAll()
180 // purpose  : Erase all GEOM objects
181 //=====================================================================================
182 void DisplayGUI::EraseAll()
183 {
184   SUIT_OverrideCursor();
185
186   SUIT_Application* app = getGeometryGUI()->getApp();
187   if ( app ) {
188     SUIT_ViewWindow* vw = app->desktop()->activeWindow();
189     if ( vw ) {
190       SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
191       SUIT_ViewManager* vman = vw->getViewManager();
192       if ( vman->getType() == OCCViewer_Viewer::Type() || 
193            vman->getType() == SVTK_Viewer::Type() ) {
194         GEOM_Displayer( appStudy ).EraseAll();
195       }
196     }
197   }
198 }
199
200 //=====================================================================================
201 // function : DisplayGUI::DisplayOnly()
202 // purpose  : Display selected GEOM objects and erase other
203 //=====================================================================================
204 void DisplayGUI::DisplayOnly()
205 {
206   EraseAll();
207   Display();
208 }
209
210 //=====================================================================================
211 // function : DisplayGUI::Display()
212 // purpose  : Display selected GEOM objects
213 //=====================================================================================
214 void DisplayGUI::Display()
215 {
216   SALOME_ListIO listIO;
217
218   SalomeApp_Application* app = getGeometryGUI()->getApp();
219   if ( !app ) return;
220
221   SalomeApp_Study* anActiveStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
222   if ( !anActiveStudy ) return;
223
224   //get SalomeApp selection manager
225   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
226   if ( !aSelMgr ) return;
227
228   SALOME_ListIO aList;
229   aSelMgr->selectedObjects( aList );
230   SALOME_ListIteratorOfListIO It( aList );
231
232   SUIT_OverrideCursor();
233
234   for( ;It.More();It.Next() ) {
235     Handle(SALOME_InteractiveObject) anIObject = It.Value();
236     if ( anIObject->hasEntry() ) {
237       _PTR(SObject) SO ( anActiveStudy->studyDS()->FindObjectID( anIObject->getEntry() ) );
238       if ( SO && QString(SO->GetID().c_str()) == QString(SO->GetFatherComponent()->GetID().c_str()) ) {
239         _PTR(SComponent) SC ( SO->GetFatherComponent() );
240         // if component is selected
241         listIO.Clear();
242         _PTR(ChildIterator) anIter ( anActiveStudy->studyDS()->NewChildIterator( SO ) );
243         anIter->InitEx( true );
244         while( anIter->More() ) {
245           _PTR(SObject) valSO ( anIter->Value() );
246           _PTR(SObject) refSO;
247           if ( !valSO->ReferencedObject( refSO ) ) {
248             listIO.Append( new SALOME_InteractiveObject(valSO->GetID().c_str(),
249                                                         SC->ComponentDataType().c_str(),
250                                                         valSO->GetName().c_str()) );
251           }
252           anIter->Next();
253         }
254         break;
255       }
256       else {
257         listIO.Append( anIObject );
258       }
259     }
260     else {
261       listIO.Append( anIObject );
262     }
263   }
264   GEOM_Displayer( anActiveStudy ).Display( listIO, true );
265 }
266
267
268 //=====================================================================================
269 // function : DisplayGUI::Erase()
270 // purpose  : Erase selected GEOM objects
271 //=====================================================================================
272 void DisplayGUI::Erase()
273 {
274   SALOME_ListIO listIO;
275
276   SalomeApp_Application* app = getGeometryGUI()->getApp();
277   if ( !app ) return;
278
279   SalomeApp_Study* anActiveStudy = dynamic_cast<SalomeApp_Study*>( app->activeStudy() );
280   if ( !anActiveStudy ) return;
281
282   //get SalomeApp selection manager
283   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
284   if ( !aSelMgr ) return;
285
286   SALOME_ListIO aList;
287   aSelMgr->selectedObjects( aList );
288   SALOME_ListIteratorOfListIO It( aList );
289
290   SUIT_OverrideCursor();
291
292   for( ; It.More(); It.Next() ) {
293     Handle(SALOME_InteractiveObject) anIObject = It.Value();
294     if ( anIObject->hasEntry() ) {
295       _PTR(SObject) SO ( anActiveStudy->studyDS()->FindObjectID( anIObject->getEntry() ) );
296       if ( SO && QString(SO->GetID().c_str()) == QString(SO->GetFatherComponent()->GetID().c_str()) ) {
297         _PTR(SComponent) SC ( SO->GetFatherComponent() );
298         // if component is selected
299         listIO.Clear();
300         _PTR(ChildIterator) anIter ( anActiveStudy->studyDS()->NewChildIterator( SO ) );
301         anIter->InitEx( true );
302         while( anIter->More() ) {
303           _PTR(SObject) valSO ( anIter->Value() );
304           _PTR(SObject) refSO;
305           if ( !valSO->ReferencedObject( refSO ) ) {
306             listIO.Append( new SALOME_InteractiveObject(valSO->GetID().c_str(),
307                                                         SC->ComponentDataType().c_str(),
308                                                         valSO->GetName().c_str()) );
309           }
310           anIter->Next();
311         }
312         break;
313       }
314       else {
315         listIO.Append( anIObject );
316       }
317     }
318     else {
319       listIO.Append( anIObject );
320     }
321   }
322
323   SUIT_ViewWindow* viewWindow = app->desktop()->activeWindow();
324   bool aIsForced = true;
325   if(viewWindow->getViewManager()->getType() == SVTK_Viewer::Type())
326     aIsForced = false;
327
328   GEOM_Displayer(anActiveStudy).Erase( listIO, aIsForced);
329   getGeometryGUI()->getApp()->selectionMgr()->clearSelected();
330 }
331
332 //=====================================================================================
333 // function : DisplayGUI::SetDisplayMode()
334 // purpose  : Set display mode for the viewer (current viewer if <viewWindow> - 0 )
335 //=====================================================================================
336 void DisplayGUI::SetDisplayMode( const int mode, SUIT_ViewWindow* viewWindow )
337 {
338   SUIT_OverrideCursor();
339
340   if ( !viewWindow ) 
341     viewWindow = getGeometryGUI()->getApp()->desktop()->activeWindow();
342   if ( viewWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
343     SVTK_View* aView = ((SVTK_ViewWindow*)viewWindow)->getView();
344     aView->SetDisplayMode( mode );
345   } 
346   else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
347     OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
348     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
349     AIS_DisplayMode newmode = (mode == 1 ? AIS_Shaded : AIS_WireFrame);
350     AIS_ListOfInteractive List;
351     ic->DisplayedObjects( List );
352     AIS_ListOfInteractive List1;
353     ic->ObjectsInCollector( List1 );
354     List.Append( List1 );
355
356     AIS_ListIteratorOfListOfInteractive ite( List );
357     while( ite.More() ) {
358       if( ite.Value()->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
359         Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast( ite.Value() );
360         ic->SetDisplayMode( aSh, Standard_Integer( newmode ),true );
361       }
362       ite.Next();
363     }
364
365     ic->SetDisplayMode( newmode, Standard_False );
366   }
367 }
368
369 //=====================================================================================
370 // function : DisplayGUI::GetDisplayMode()
371 // purpose  : Get display mode of the viewer (current viewer if <viewWindow> - 0 )
372 //=====================================================================================
373 int DisplayGUI::GetDisplayMode( SUIT_ViewWindow* viewWindow )
374 {
375   int dispMode = 0;
376   if ( !viewWindow ) 
377     viewWindow = getGeometryGUI()->getApp()->desktop()->activeWindow();
378   if ( viewWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
379     SVTK_View* aView = ((SVTK_ViewWindow*)viewWindow)->getView();
380     dispMode = aView->GetDisplayMode();
381   } 
382   else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
383     OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
384     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
385     AIS_DisplayMode mode = (AIS_DisplayMode)ic->DisplayMode();
386     dispMode = (mode == AIS_WireFrame ? 0 : 1 );
387   }
388   return dispMode;
389 }
390
391 //=====================================================================================
392 // function : DisplayGUI::SetVectorsMode()
393 // purpose  : Set vector mode for the viewer
394 //=====================================================================================
395 void DisplayGUI::SetVectorMode( const bool mode, SUIT_ViewWindow* viewWindow )
396 {
397   SUIT_OverrideCursor();
398
399   if ( !viewWindow ) 
400     viewWindow = getGeometryGUI()->getApp()->desktop()->activeWindow();
401   if ( viewWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
402     viewWindow->setCustomData( "VectorsMode", QVariant( mode ) );
403     SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>( viewWindow );
404     vtkActorCollection* allActors = vw->getRenderer()->GetActors();
405     allActors->InitTraversal();
406     while (vtkActor* actor = allActors->GetNextActor()) {
407       if (actor->GetVisibility()) { // only for visible actors
408         GEOM_Actor* aGeomActor = 0;
409         if ( actor->IsA( "GEOM_Actor" ) ) {
410           aGeomActor = GEOM_Actor::SafeDownCast( actor );
411           if ( aGeomActor )
412             aGeomActor->SetVectorMode( mode );
413         }
414       }
415     }
416   }
417   else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
418     viewWindow->setCustomData( "VectorsMode", QVariant( mode ) );
419     OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
420     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
421     AIS_ListOfInteractive List;
422     ic->DisplayedObjects( List );
423     AIS_ListOfInteractive List1;
424     ic->ObjectsInCollector( List1 );
425     List.Append( List1 );
426
427     AIS_ListIteratorOfListOfInteractive ite( List );
428     while( ite.More() ) {
429       if( ite.Value()->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
430         Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast( ite.Value() );
431         aSh->SetDisplayVectors(mode);
432         ic->RecomputePrsOnly(ite.Value());
433       }
434       ite.Next();
435     }
436   }
437 }
438
439 //=====================================================================================
440 // function : DisplayGUI::GetVectorMode()
441 // purpose  : Get the "show edge direction" mode of the viewer
442 //=====================================================================================
443 int DisplayGUI::GetVectorMode( SUIT_ViewWindow* viewWindow )
444 {
445   if ( !viewWindow ) 
446     viewWindow = getGeometryGUI()->getApp()->desktop()->activeWindow();
447   return viewWindow->getCustomData( "VectorsMode" ).toBool();
448 }
449
450 //=====================================================================================
451 // function : DisplayGUI::InvertDisplayMode()
452 // purpose  : Invert display mode ( shadin <-> wireframe ) for the viewer 
453 //            (current viewer if <viewWindow> = 0 )
454 //=====================================================================================
455 void DisplayGUI::InvertDisplayMode( SUIT_ViewWindow* viewWindow )
456 {
457   SetDisplayMode( 1 - GetDisplayMode( viewWindow ) );
458 }
459
460 //=====================================================================================
461 // function : DisplayGUI::ChangeDisplayMode()
462 // purpose  : Set display mode for selected objects in the viewer given
463 //            (current viewer if <viewWindow> = 0 )
464 //=====================================================================================
465 void DisplayGUI::ChangeDisplayMode( const int mode, SUIT_ViewWindow* viewWindow )
466 {
467   SalomeApp_Application* app = getGeometryGUI()->getApp();
468   if ( !app ) return;
469
470   if ( !viewWindow ) 
471     viewWindow = app->desktop()->activeWindow();
472
473   LightApp_SelectionMgr* aSelMgr = app->selectionMgr();
474   if ( !aSelMgr ) return;
475
476   SUIT_OverrideCursor();
477
478   SALOME_ListIO aList;
479
480   if ( viewWindow->getViewManager()->getType() == SVTK_Viewer::Type() ) {
481     SVTK_ViewWindow* vw = dynamic_cast<SVTK_ViewWindow*>( viewWindow );
482     SVTK_View* aView = vw->getView();
483
484     aSelMgr->selectedObjects( aList );
485     SALOME_ListIteratorOfListIO It( aList );
486
487     for( ;It.More(); It.Next() ) {
488       SVTK_Viewer* stvkViewer = dynamic_cast<SVTK_Viewer*>(vw->getViewManager()->getViewModel());
489       SVTK_Prs* vtkPrs =
490         stvkViewer ? dynamic_cast<SVTK_Prs*>( stvkViewer->CreatePrs( It.Value()->getEntry() ) ) : 0;
491       if ( vtkPrs && !vtkPrs->IsNull() ) {
492         if ( mode == 0 )
493           aView->ChangeRepresentationToWireframe( vtkPrs->GetObjects() );
494         else if ( mode == 1 )
495           aView->ChangeRepresentationToSurface( vtkPrs->GetObjects() );
496         else if ( mode == 2 ) {
497           vtkActorCollection* anActors = vtkPrs->GetObjects();
498           anActors->InitTraversal();
499           while (vtkActor* anAct = anActors->GetNextActor()) {
500             GEOM_Actor* aGeomActor = GEOM_Actor::SafeDownCast(anAct);
501             aGeomActor->SetVectorMode(!aGeomActor->GetVectorMode());
502           }
503         }
504       }
505     }
506     aView->Repaint();
507   }
508   else if ( viewWindow->getViewManager()->getType() == OCCViewer_Viewer::Type() ) {
509     OCCViewer_Viewer* v3d = ((OCCViewer_ViewManager*)(viewWindow->getViewManager()))->getOCCViewer();
510     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
511
512     aSelMgr->selectedObjects( aList );
513     SALOME_ListIteratorOfListIO It( aList );
514
515     for( ;It.More(); It.Next() ) {
516       SOCC_Viewer* soccViewer = (SOCC_Viewer*)(viewWindow->getViewManager()->getViewModel());
517       SOCC_Prs* occPrs = dynamic_cast<SOCC_Prs*>( soccViewer->CreatePrs( It.Value()->getEntry() ) );
518       if ( occPrs && !occPrs->IsNull() ) {
519         AIS_ListOfInteractive shapes; occPrs->GetObjects( shapes );
520         AIS_ListIteratorOfListOfInteractive interIter( shapes );
521         for ( ; interIter.More(); interIter.Next() ) {
522           if ( mode == 0 )
523             ic->SetDisplayMode( interIter.Value(), AIS_WireFrame, false );
524           else if ( mode == 1 )
525             ic->SetDisplayMode( interIter.Value(), AIS_Shaded, false );
526           if (mode == 2 ) {
527             Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast( interIter.Value() );
528             if ( !aSh.IsNull() ) {
529               aSh->SetDisplayVectors(!aSh->isShowVectors());
530               ic->RecomputePrsOnly(interIter.Value());
531             }
532           }
533         }
534       }
535     }
536     ic->UpdateCurrentViewer();
537   }
538 }
539
540 //=====================================================================================
541 // EXPORTED METHODS
542 //=====================================================================================
543 extern "C"
544 {
545 #ifdef WIN32
546   __declspec( dllexport )
547 #endif
548   GEOMGUI* GetLibGUI( GeometryGUI* parent )
549   {
550     return new DisplayGUI( parent );
551   }
552 }