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