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