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