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