]> SALOME platform Git repositories - modules/geom.git/blob - src/DisplayGUI/DisplayGUI.cxx
Salome HOME
Merge with version on tag OCC-V2_1_0d
[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 "QAD_RightFrame.h"
34 #include "QAD_WaitCursor.h"
35 #include "QAD_Desktop.h"
36 #include "VTKViewer_ViewFrame.h"
37 #include "VTKViewer_RenderWindowInteractor.h"
38 #include "OCCViewer_ViewFrame.h"
39 #include "OCCViewer_Viewer3d.h"
40 #include "SALOME_ListIteratorOfListIO.hxx"
41 #include "VTKViewer_Prs.h"
42 #include "OCCViewer_Prs.h"
43
44 #include <AIS_ListIteratorOfListOfInteractive.hxx>
45
46 using namespace std;
47
48 DisplayGUI* DisplayGUI::myGUIObject = 0;
49
50 //=======================================================================
51 // function : DisplayGUI::GetDisplayGUI()
52 // purpose  : Get the only DisplayGUI object [ static ]
53 //=======================================================================
54 DisplayGUI* DisplayGUI::GetDisplayGUI()
55 {
56   if ( myGUIObject == 0 ) {
57     // init DisplayGUI only once
58     myGUIObject = new DisplayGUI();
59   }
60   return myGUIObject;
61 }
62
63 //=======================================================================
64 // function : DisplayGUI::DisplayGUI()
65 // purpose  : Constructor
66 //=======================================================================
67 DisplayGUI::DisplayGUI() : GEOMGUI()
68 {
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, QAD_Desktop* parent)
86 {
87   DisplayGUI* myDisplayGUI = GetDisplayGUI();
88
89   switch (theCommandID) {
90   case 211: // MENU VIEW - WIREFRAME/SHADING
91     {
92       myDisplayGUI->InvertDisplayMode();
93       int newMode = myDisplayGUI->GetDisplayMode();
94       QAD_Application::getDesktop()->menuBar()->changeItem( 211, newMode == 1 ? tr( "GEOM_MEN_WIREFRAME" ) : tr("GEOM_MEN_SHADING") );
95       break;
96     }
97   case 212: // MENU VIEW - DISPLAY ALL
98     {
99       myDisplayGUI->DisplayAll();
100       break;
101     }
102   case 213: // MENU VIEW - DISPLAY ONLY
103     {
104       myDisplayGUI->DisplayOnly();
105       break;
106     }
107   case 214: // MENU VIEW - ERASE ALL
108     {
109       myDisplayGUI->EraseAll();
110       break;
111     }
112   case 215: // MENU VIEW - ERASE
113     {
114       myDisplayGUI->Erase();
115       break;
116     }
117   case 216: // MENU VIEW - DISPLAY
118     {
119       myDisplayGUI->Display();
120       break;
121     }
122   case 80311: // POPUP VIEWER - WIREFRAME
123     {
124       myDisplayGUI->ChangeDisplayMode( 0 );
125       break;
126     }
127   case 80312: // POPUP VIEWER - SHADING
128     {
129       myDisplayGUI->ChangeDisplayMode( 1 );
130       break;
131     }
132   default:
133     {
134       parent->putInfo(tr("GEOM_PRP_COMMAND").arg(theCommandID));
135       break;
136     }
137   }
138   return true;
139 }
140
141 //=====================================================================================
142 // function : DisplayGUI::DisplayAll()
143 // purpose  : Display all GEOM objects
144 //=====================================================================================
145 void DisplayGUI::DisplayAll()
146 {
147   QAD_WaitCursor wc;
148   SALOMEDS::Study_var aStudy = QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
149   SALOMEDS::SComponent_var SC = aStudy->FindComponent( "GEOM" );
150   if ( SC->_is_nil() )
151     return;
152
153   SALOME_ListIO listIO;
154   SALOMEDS::ChildIterator_var anIter = aStudy->NewChildIterator( SC );
155   anIter->InitEx( true );
156   while( anIter->More() ) {
157     SALOMEDS::SObject_var valSO = anIter->Value();
158     SALOMEDS::SObject_var refSO;
159     if ( !valSO->ReferencedObject( refSO ) ) {
160       listIO.Append( new SALOME_InteractiveObject( valSO->GetID(), SC->ComponentDataType() ,valSO->GetName()) );
161     } 
162     anIter->Next();
163   }
164   GEOM_Displayer().Display( listIO, true );
165 }
166
167 //=====================================================================================
168 // function : DisplayGUI::EraseAll()
169 // purpose  : Erase all GEOM objects
170 //=====================================================================================
171 void DisplayGUI::EraseAll()
172 {
173   QAD_WaitCursor wc;
174   QAD_ViewFrame* vf = QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame();
175   if ( vf->getTypeView() == VIEW_OCC || vf->getTypeView() == VIEW_VTK )
176     vf->EraseAll();
177 }
178
179 //=====================================================================================
180 // function : DisplayGUI::DisplayOnly()
181 // purpose  : Display selected GEOM objects and erase other
182 //=====================================================================================
183 void DisplayGUI::DisplayOnly()
184 {
185   EraseAll();
186   Display();
187 }
188
189 //=====================================================================================
190 // function : DisplayGUI::Display()
191 // purpose  : Display selected GEOM objects
192 //=====================================================================================
193 void DisplayGUI::Display()
194 {
195   QAD_WaitCursor wc;
196   SALOME_ListIO listIO;
197
198   QAD_Study* anActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
199   SALOME_Selection* Sel = SALOME_Selection::Selection( anActiveStudy->getSelection() );
200   SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
201   for( ;It.More();It.Next() ) {
202     Handle(SALOME_InteractiveObject) anIObject = It.Value();
203     if ( anIObject->hasEntry() ) {
204       SALOMEDS::SObject_var SO = anActiveStudy->getStudyDocument()->FindObjectID( anIObject->getEntry() );
205       if ( !SO->_is_nil() && QString( SO->GetID() ) == QString( SO->GetFatherComponent()->GetID() ) ) {
206         SALOMEDS::SComponent_var SC = SO->GetFatherComponent();
207         // if component is selected
208         listIO.Clear();
209         SALOMEDS::ChildIterator_var anIter = anActiveStudy->getStudyDocument()->NewChildIterator( SO );
210         anIter->InitEx( true );
211         while( anIter->More() ) {
212           SALOMEDS::SObject_var valSO = anIter->Value();
213           SALOMEDS::SObject_var refSO;
214           if ( !valSO->ReferencedObject( refSO ) ) {
215             listIO.Append( new SALOME_InteractiveObject( valSO->GetID(), SC->ComponentDataType() ,valSO->GetName()) );
216           } 
217           anIter->Next();
218         }
219         break;
220       }
221       else {
222         listIO.Append( anIObject );
223       }
224     }
225     else {
226       listIO.Append( anIObject );
227     }
228   }
229   GEOM_Displayer().Display( listIO, true );
230 }
231
232
233 //=====================================================================================
234 // function : DisplayGUI::Erase()
235 // purpose  : Erase selected GEOM objects
236 //=====================================================================================
237 void DisplayGUI::Erase()
238 {
239   QAD_WaitCursor wc;
240   SALOME_ListIO listIO;
241
242   QAD_Study* anActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
243   SALOME_Selection* Sel = SALOME_Selection::Selection( anActiveStudy->getSelection() );
244   SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
245   for( ;It.More();It.Next() ) {
246     Handle(SALOME_InteractiveObject) anIObject = It.Value();
247     if ( anIObject->hasEntry() ) {
248       SALOMEDS::SObject_var SO = anActiveStudy->getStudyDocument()->FindObjectID( anIObject->getEntry() );
249       if ( !SO->_is_nil() && QString( SO->GetID() ) == QString( SO->GetFatherComponent()->GetID() ) ) {
250         SALOMEDS::SComponent_var SC = SO->GetFatherComponent();
251         // if component is selected
252         listIO.Clear();
253         SALOMEDS::ChildIterator_var anIter = anActiveStudy->getStudyDocument()->NewChildIterator( SO );
254         anIter->InitEx( true );
255         while( anIter->More() ) {
256           SALOMEDS::SObject_var valSO = anIter->Value();
257           SALOMEDS::SObject_var refSO;
258           if ( !valSO->ReferencedObject( refSO ) ) {
259             listIO.Append( new SALOME_InteractiveObject( valSO->GetID(), SC->ComponentDataType() ,valSO->GetName()) );
260           } 
261           anIter->Next();
262         }
263         break;
264       }
265       else {
266         listIO.Append( anIObject );
267       }
268     }
269     else {
270       listIO.Append( anIObject );
271     }
272   }
273   GEOM_Displayer().Erase( listIO, true );
274   Sel->ClearIObjects();
275 }
276
277 //=====================================================================================
278 // function : DisplayGUI::SetDisplayMode()
279 // purpose  : Set display mode for the viewer (current viewer if <viewFrame> - 0 )
280 //=====================================================================================
281 void DisplayGUI::SetDisplayMode( const int mode, QAD_ViewFrame* viewFrame )
282 {
283   QAD_WaitCursor wc;
284   if ( !viewFrame ) 
285     viewFrame = QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame();
286   if( viewFrame->getTypeView() == VIEW_VTK ) {
287     VTKViewer_RenderWindowInteractor* myRenderInter= ((VTKViewer_ViewFrame*)viewFrame)->getRWInteractor();
288     myRenderInter->SetDisplayMode( mode );
289   } 
290   else if( viewFrame->getTypeView() == VIEW_OCC ) {
291     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)viewFrame)->getViewer();
292     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
293     AIS_DisplayMode newmode = (mode == 1 ? AIS_Shaded : AIS_WireFrame);
294     AIS_ListOfInteractive List;
295     ic->DisplayedObjects( List );
296     AIS_ListOfInteractive List1;
297     ic->ObjectsInCollector( List1 );
298     List.Append( List1 );
299     
300     AIS_ListIteratorOfListOfInteractive ite( List );
301     while( ite.More() ) {
302       if( ite.Value()->IsInstance( STANDARD_TYPE(GEOM_AISShape) ) ) {
303         Handle(GEOM_AISShape) aSh = Handle(GEOM_AISShape)::DownCast( ite.Value() );
304         ic->SetDisplayMode( aSh, Standard_Integer( newmode ),true );
305       }
306       ite.Next();
307     }
308     
309     ic->SetDisplayMode( newmode, Standard_False );
310   }
311 }
312
313 //=====================================================================================
314 // function : DisplayGUI::GetDisplayMode()
315 // purpose  : Get display mode of the viewer (current viewer if <viewFrame> - 0 )
316 //=====================================================================================
317 int DisplayGUI::GetDisplayMode( QAD_ViewFrame* viewFrame )
318 {
319   int dispMode = 0;
320   if ( !viewFrame ) 
321     viewFrame = QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame();
322   if( viewFrame->getTypeView() == VIEW_VTK) {
323     VTKViewer_RenderWindowInteractor* myRenderInter= ((VTKViewer_ViewFrame*)viewFrame)->getRWInteractor();
324     dispMode = myRenderInter->GetDisplayMode();
325   } 
326   else if( viewFrame->getTypeView() == VIEW_OCC) {
327     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)viewFrame)->getViewer();
328     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
329     AIS_DisplayMode mode = (AIS_DisplayMode)ic->DisplayMode();
330     dispMode = (mode == AIS_WireFrame ? 0 : 1 );
331   }
332   return dispMode;
333 }
334
335 //=====================================================================================
336 // function : DisplayGUI::InvertDisplayMode()
337 // purpose  : Invert display mode ( shadin <-> wireframe ) for the viewer 
338 //            (current viewer if <viewFrame> = 0 )
339 //=====================================================================================
340 void DisplayGUI::InvertDisplayMode( QAD_ViewFrame* viewFrame )
341 {
342   SetDisplayMode( 1 - GetDisplayMode( viewFrame ) );
343 }
344
345 //=====================================================================================
346 // function : DisplayGUI::ChangeDisplayMode()
347 // purpose  : Set display mode for selected objects in the viewer given
348 //            (current viewer if <viewFrame> = 0 )
349 //=====================================================================================
350 void DisplayGUI::ChangeDisplayMode( const int mode, QAD_ViewFrame* viewFrame )
351 {
352   QAD_WaitCursor wc;
353   if ( !viewFrame ) 
354     viewFrame = QAD_Application::getDesktop()->getActiveStudy()->getActiveStudyFrame()->getRightFrame()->getViewFrame();
355   SALOME_Selection* Sel = SALOME_Selection::Selection(QAD_Application::getDesktop()->getActiveStudy()->getSelection());
356   if ( viewFrame->getTypeView() == VIEW_VTK ) {
357     VTKViewer_RenderWindowInteractor* myRenderInter = ((VTKViewer_ViewFrame*)viewFrame)->getRWInteractor();
358     SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
359     for( ;It.More(); It.Next() ) {
360       VTKViewer_Prs* vtkPrs = dynamic_cast<VTKViewer_Prs*>( viewFrame->CreatePrs( It.Value()->getEntry() ) );
361       if ( vtkPrs && !vtkPrs->IsNull() ) {
362         if ( mode == 0 )
363           myRenderInter->ChangeRepresentationToWireframe( vtkPrs->GetObjects() );
364         else if ( mode == 1 )
365           myRenderInter->ChangeRepresentationToSurface( vtkPrs->GetObjects() );
366       }
367     }
368     myRenderInter->Render();
369   }
370   else if ( viewFrame->getTypeView() == VIEW_OCC ) {
371     OCCViewer_Viewer3d* v3d = ((OCCViewer_ViewFrame*)viewFrame)->getViewer();
372     Handle(AIS_InteractiveContext) ic = v3d->getAISContext();
373     SALOME_ListIteratorOfListIO It( Sel->StoredIObjects() );
374     for( ;It.More(); It.Next() ) {
375       OCCViewer_Prs* occPrs = dynamic_cast<OCCViewer_Prs*>( viewFrame->CreatePrs( It.Value()->getEntry() ) );
376       if ( occPrs && !occPrs->IsNull() ) {
377         AIS_ListOfInteractive shapes; occPrs->GetObjects( shapes );
378         AIS_ListIteratorOfListOfInteractive interIter( shapes );
379         for ( ; interIter.More(); interIter.Next() ) {
380           if ( mode == 0 )
381             ic->SetDisplayMode( interIter.Value(), AIS_WireFrame, false );
382           else if ( mode == 1 )
383             ic->SetDisplayMode( interIter.Value(), AIS_Shaded, false );
384         }
385       }
386     }
387     ic->UpdateCurrentViewer();
388   }
389 }
390
391 //=====================================================================================
392 // EXPORTED METHODS
393 //=====================================================================================
394 extern "C"
395 {
396   GEOMGUI* GetLibGUI()
397   {
398     return DisplayGUI::GetDisplayGUI();
399   }
400 }