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