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