Salome HOME
Popup item "Dump view"
[modules/gui.git] / src / SVTK / SVTK_ViewModel.cxx
1 #include <qpopupmenu.h>
2 #include <qcolordialog.h>
3
4 #include <vtkCamera.h>
5 #include <vtkRenderer.h>
6 #include <vtkActorCollection.h>
7
8 #include "SUIT_Session.h"
9
10 #include "SVTK_Selection.h"
11 #include "SVTK_ViewModel.h"
12 #include "SVTK_ViewWindow.h"
13 #include "SVTK_Prs.h"
14
15 #include "SVTK_RenderWindowInteractor.h"
16 #include "SVTK_RenderWindow.h"
17
18 #include "ToolsGUI.h"
19
20 #include "SALOME_Actor.h"
21 #include "SALOME_InteractiveObject.hxx"
22
23 #include "Utils_ORB_INIT.hxx"
24 #include "Utils_SINGLETON.hxx"
25 #include "SALOME_ModuleCatalog_impl.hxx"
26 #include "SALOME_NamingService.hxx"
27
28 #include "SALOMEDSClient.hxx"
29 #include "SALOMEDS_StudyManager.hxx"
30
31 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
32 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from 
33 // SALOMEDS::StudyManager - no linkage with SalomeApp. 
34
35 static _PTR(Study) getStudyDS() 
36 {
37   SALOMEDSClient_Study* aStudy = NULL;
38   _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
39
40   // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
41   //    id of its underlying SALOMEDS::Study
42   SUIT_Application* app = SUIT_Session::session()->activeApplication();
43   if ( !app )  return _PTR(Study)(aStudy); 
44   SUIT_Study* stud = app->activeStudy();
45   if ( !stud ) return _PTR(Study)(aStudy);  
46   const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
47   // get SALOMEDS_Study with this id from StudyMgr
48   return aMgr->GetStudyByID( id );
49 }
50
51 //==========================================================
52 SVTK_Viewer
53 ::SVTK_Viewer()
54 {
55 }
56
57 //==========================================================
58 SVTK_Viewer
59 ::~SVTK_Viewer() 
60 {
61 }
62
63 //==========================================================
64 SUIT_ViewWindow* 
65 SVTK_Viewer
66 ::createView( SUIT_Desktop* theDesktop )
67 {
68   return new SVTK_ViewWindow( theDesktop, this );
69 }
70
71 //==========================================================
72 void 
73 SVTK_Viewer
74 ::setViewManager(SUIT_ViewManager* theViewManager)
75 {
76   SUIT_ViewModel::setViewManager(theViewManager);
77
78   if(!theViewManager)
79     return;
80
81   connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
82           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
83   
84   connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
85           this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
86   
87   connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
88           this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
89 }
90
91 //==========================================================
92 void SVTK_Viewer::contextMenuPopup( QPopupMenu* thePopup )
93 {
94   thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
95   thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
96
97   thePopup->insertSeparator();
98
99   SVTK_ViewWindow* aView = (SVTK_ViewWindow*)(myViewManager->getActiveView());
100   if ( aView && !aView->getToolBar()->isVisible() )
101     thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
102 }
103
104 //==========================================================
105 void SVTK_Viewer::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
106 {
107   if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
108     if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
109       switch(event->button()) {
110       case LeftButton:
111         aRWI->LeftButtonPressed(event) ;
112         break ;
113       case MidButton:
114         aRWI->MiddleButtonPressed(event) ;
115         break ;
116       case RightButton:
117         aRWI->RightButtonPressed(event) ;
118         break;
119       default:
120         break ;
121       }
122     }
123   }
124 }
125
126 //==========================================================
127 void 
128 SVTK_Viewer
129 ::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
130 {
131   if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
132     if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
133       aRWI->MouseMove( event );
134     }
135   }
136 }
137
138 //==========================================================
139 void 
140 SVTK_Viewer
141 ::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
142 {
143   if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
144     if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
145       switch(event->button()) {
146       case LeftButton:
147         aRWI->LeftButtonReleased(event) ;
148         break ;
149       case MidButton:
150         aRWI->MiddleButtonReleased(event) ;
151         break ;
152       case RightButton:
153         aRWI->RightButtonReleased(event) ;
154         break;
155       default:
156         break ;
157       }
158     }
159   }
160 }
161
162 //==========================================================
163 void 
164 SVTK_Viewer
165 ::enableSelection(bool isEnabled)
166 {
167   mySelectionEnabled = isEnabled;
168   //!! To be done for view windows
169 }
170
171 //==========================================================
172 void
173 SVTK_Viewer
174 ::enableMultiselection(bool isEnable)
175 {
176   myMultiSelectionEnabled = isEnable;
177   //!! To be done for view windows
178 }
179
180 void SVTK_Viewer::onDumpView()
181 {
182   SVTK_ViewWindow* aView = (SVTK_ViewWindow*)(myViewManager->getActiveView());
183   if ( aView )
184     aView->onDumpView();
185 }
186
187 //==========================================================
188 void SVTK_Viewer::onChangeBgColor()
189 {
190   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
191   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
192     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
193       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)){
194         QColor aColor = QColorDialog::getColor( aView->backgroundColor(), aView);
195         if ( aColor.isValid() )
196           aView->setBackgroundColor(aColor);
197       }
198 }
199
200 //==========================================================
201 void
202 SVTK_Viewer
203 ::onShowToolbar() 
204 {
205   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
206   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
207     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
208       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
209         aView->getToolBar()->show();    
210 }
211
212 //==========================================================
213 void
214 SVTK_Viewer
215 ::Display( const SALOME_VTKPrs* prs )
216 {
217   // try do downcast object
218   if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
219     if(aPrs->IsNull())
220       return;
221     if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
222       // get SALOMEDS Study
223       _PTR(Study) aStudy(getStudyDS());
224       anActorCollection->InitTraversal();
225       while(vtkActor* anActor = anActorCollection->GetNextActor()){
226         if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
227           // Set visibility flag
228           Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
229           if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
230             ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),true,this);
231           }
232           // just display the object
233           QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
234           for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
235             if(SUIT_ViewWindow* aViewWindow = aViews.at(i)){
236               if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)){
237                 if(SVTK_RenderWindowInteractor* aRWI = aView->getRWInteractor()){
238                   aRWI->Display(anAct,false);
239                   if(anAct->IsSetCamera()){
240                     vtkRenderer* aRenderer =  aView->getRenderer();
241                     anAct->SetCamera( aRenderer->GetActiveCamera() );
242                   }
243                 }
244               }
245             }
246           }
247         }
248       }
249     }
250   }
251 }
252
253 //==========================================================
254 void
255 SVTK_Viewer
256 ::Erase( const SALOME_VTKPrs* prs, const bool forced )
257 {
258   // try do downcast object
259   if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
260     if(aPrs->IsNull())
261       return;
262     if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
263       // get SALOMEDS Study
264       _PTR(Study) aStudy(getStudyDS());
265       anActorCollection->InitTraversal();
266       while(vtkActor* anActor = anActorCollection->GetNextActor())
267         if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
268           // Set visibility flag
269           Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
270           if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
271             ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
272           }
273           // just display the object
274           QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
275           for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
276             if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
277               if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
278                 if(SVTK_RenderWindowInteractor* aRWI = aView->getRWInteractor())
279                   if ( forced )
280                     aRWI->Remove(anAct,false);
281                   else
282                     aRWI->Erase(anAct,forced);
283           }
284         }
285     }
286   }
287 }
288   
289 //==========================================================
290 void
291 SVTK_Viewer
292 ::EraseAll( const bool forced )
293 {
294   _PTR(Study) aStudy(getStudyDS());
295   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
296   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
297     if(SUIT_ViewWindow* aViewWindow = aViews.at(i)){
298       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)){
299         vtkRenderer* aRenderer =  aView->getRenderer();
300         vtkActorCollection* anActorCollection = aRenderer->GetActors();
301         anActorCollection->InitTraversal();
302         while(vtkActor* anActor = anActorCollection->GetNextActor()){
303           if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
304             // Set visibility flag
305             Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
306             if(!anObj.IsNull() && anObj->hasEntry() && aStudy)
307               ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
308             if(forced)
309               aRenderer->RemoveActor(anAct);
310             else{
311               // just erase actor
312               anAct->SetVisibility( false );
313               // erase dependent actors
314               vtkActorCollection* aCollection = vtkActorCollection::New();
315               anAct->GetChildActors( aCollection );
316               aCollection->InitTraversal();
317               while(vtkActor* aSubAct = aCollection->GetNextActor())
318                 aSubAct->SetVisibility( false );
319               aCollection->Delete();
320             }
321           }
322         }
323       }
324     }
325   }
326   Repaint();
327 }
328
329 //==========================================================
330 SALOME_Prs* 
331 SVTK_Viewer
332 ::CreatePrs( const char* entry )
333 {
334   SVTK_Prs* prs = new SVTK_Prs();
335   if ( entry ) {
336     vtkRenderer* rnr =  ( (SVTK_ViewWindow*) getViewManager()->getActiveView() )->getRenderer();
337     vtkActorCollection* theActors = rnr->GetActors();
338     theActors->InitTraversal();
339     vtkActor* ac;
340     while( ( ac = theActors->GetNextActor() ) ) {
341       SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
342       if ( anActor && anActor->hasIO() && !strcmp( anActor->getIO()->getEntry(), entry ) ) {
343         prs->AddObject( ac );
344       }
345     }
346   }
347   return prs;
348 }
349
350 //==========================================================
351 void
352 SVTK_Viewer
353 ::BeforeDisplay( SALOME_Displayer* d )
354 {
355   d->BeforeDisplay( this, SALOME_VTKViewType() );
356 }
357
358 //==========================================================
359 void
360 SVTK_Viewer::AfterDisplay( SALOME_Displayer* d )
361 {
362   d->AfterDisplay( this, SALOME_VTKViewType() );
363 }
364
365 //==========================================================
366 bool
367 SVTK_Viewer
368 ::isVisible( const Handle(SALOME_InteractiveObject)& io )
369 {
370   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
371   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
372     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
373       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
374         if(SVTK_RenderWindowInteractor* aRWI = aView->getRWInteractor())
375           if(!aRWI->isVisible( io ))
376             return false;
377   return true;
378 }
379
380 //==========================================================
381 void 
382 SVTK_Viewer
383 ::Repaint()
384 {
385 //  if (theUpdateTrihedron) onAdjustTrihedron();
386   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
387   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
388     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
389       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
390         if(SVTK_RenderWindow* aRW = aView->getRenderWindow())
391           aRW->update();
392 }
393
394 void 
395 SVTK_Viewer
396 ::onSelectionChanged()
397 {
398   emit selectionChanged();
399 }
400