]> SALOME platform Git repositories - modules/gui.git/blob - src/SVTK/SVTK_ViewModel.cxx
Salome HOME
Initial version
[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
93 SVTK_Viewer
94 ::contextMenuPopup(QPopupMenu* thePopup)
95 {
96   if (thePopup->count() > 0) thePopup->insertSeparator();
97   thePopup->insertItem("Change background...", this, SLOT(onChangeBgColor()));
98   if(SUIT_ViewWindow* aViewWindow = myViewManager->getActiveView()){
99     if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)){
100       if(!aView->getToolBar()->isVisible()){
101         thePopup->insertSeparator();
102         thePopup->insertItem("Show toolbar", this, SLOT(onShowToolbar()));
103       }
104     }
105   }
106 }
107
108 //==========================================================
109 void
110 SVTK_Viewer
111 ::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
112 {
113   if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
114     if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
115       switch(event->button()) {
116       case LeftButton:
117         aRWI->LeftButtonPressed(event) ;
118         break ;
119       case MidButton:
120         aRWI->MiddleButtonPressed(event) ;
121         break ;
122       case RightButton:
123         aRWI->RightButtonPressed(event) ;
124         break;
125       default:
126         break ;
127       }
128     }
129   }
130 }
131
132 //==========================================================
133 void 
134 SVTK_Viewer
135 ::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
136 {
137   if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
138     if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
139       aRWI->MouseMove( event );
140     }
141   }
142 }
143
144 //==========================================================
145 void 
146 SVTK_Viewer
147 ::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
148 {
149   if(SVTK_ViewWindow* aVW = dynamic_cast<SVTK_ViewWindow*>(vw)){
150     if(SVTK_RenderWindowInteractor* aRWI = aVW->getRWInteractor()){
151       switch(event->button()) {
152       case LeftButton:
153         aRWI->LeftButtonReleased(event) ;
154         break ;
155       case MidButton:
156         aRWI->MiddleButtonReleased(event) ;
157         break ;
158       case RightButton:
159         aRWI->RightButtonReleased(event) ;
160         break;
161       default:
162         break ;
163       }
164     }
165   }
166 }
167
168 //==========================================================
169 void 
170 SVTK_Viewer
171 ::enableSelection(bool isEnabled)
172 {
173   mySelectionEnabled = isEnabled;
174   //!! To be done for view windows
175 }
176
177 //==========================================================
178 void
179 SVTK_Viewer
180 ::enableMultiselection(bool isEnable)
181 {
182   myMultiSelectionEnabled = isEnable;
183   //!! To be done for view windows
184 }
185
186 //==========================================================
187 void
188 SVTK_Viewer
189 ::onChangeBgColor()
190 {
191   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
192   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
193     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
194       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)){
195         QColor aColor = QColorDialog::getColor( aView->backgroundColor(), aView);
196         if ( aColor.isValid() )
197           aView->setBackgroundColor(aColor);
198       }
199 }
200
201 //==========================================================
202 void
203 SVTK_Viewer
204 ::onShowToolbar() 
205 {
206   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
207   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
208     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
209       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
210         aView->getToolBar()->show();    
211 }
212
213 //==========================================================
214 void
215 SVTK_Viewer
216 ::Display( const SALOME_VTKPrs* prs )
217 {
218   // try do downcast object
219   if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
220     if(aPrs->IsNull())
221       return;
222     if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
223       // get SALOMEDS Study
224       _PTR(Study) aStudy(getStudyDS());
225       anActorCollection->InitTraversal();
226       while(vtkActor* anActor = anActorCollection->GetNextActor()){
227         if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
228           // Set visibility flag
229           Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
230           if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
231             ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),true,this);
232           }
233           // just display the object
234           QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
235           for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
236             if(SUIT_ViewWindow* aViewWindow = aViews.at(i)){
237               if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)){
238                 if(SVTK_RenderWindowInteractor* aRWI = aView->getRWInteractor()){
239                   aRWI->Display(anAct,false);
240                   if(anAct->IsSetCamera()){
241                     vtkRenderer* aRenderer =  aView->getRenderer();
242                     anAct->SetCamera( aRenderer->GetActiveCamera() );
243                   }
244                 }
245               }
246             }
247           }
248         }
249       }
250     }
251   }
252 }
253
254 //==========================================================
255 void
256 SVTK_Viewer
257 ::Erase( const SALOME_VTKPrs* prs, const bool forced )
258 {
259   // try do downcast object
260   if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
261     if(aPrs->IsNull())
262       return;
263     if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
264       // get SALOMEDS Study
265       _PTR(Study) aStudy(getStudyDS());
266       anActorCollection->InitTraversal();
267       while(vtkActor* anActor = anActorCollection->GetNextActor())
268         if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
269           // Set visibility flag
270           Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
271           if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
272             ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
273           }
274           // just display the object
275           QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
276           for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
277             if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
278               if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
279                 if(SVTK_RenderWindowInteractor* aRWI = aView->getRWInteractor())
280                   if ( forced )
281                     aRWI->Remove(anAct,false);
282                   else
283                     aRWI->Erase(anAct,forced);
284           }
285         }
286     }
287   }
288 }
289   
290 //==========================================================
291 void
292 SVTK_Viewer
293 ::EraseAll( const bool forced )
294 {
295   _PTR(Study) aStudy(getStudyDS());
296   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
297   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
298     if(SUIT_ViewWindow* aViewWindow = aViews.at(i)){
299       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow)){
300         vtkRenderer* aRenderer =  aView->getRenderer();
301         vtkActorCollection* anActorCollection = aRenderer->GetActors();
302         anActorCollection->InitTraversal();
303         while(vtkActor* anActor = anActorCollection->GetNextActor()){
304           if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
305             // Set visibility flag
306             Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
307             if(!anObj.IsNull() && anObj->hasEntry() && aStudy)
308               ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
309             if(forced)
310               aRenderer->RemoveActor(anAct);
311             else{
312               // just erase actor
313               anAct->SetVisibility( false );
314               // erase dependent actors
315               vtkActorCollection* aCollection = vtkActorCollection::New();
316               anAct->GetChildActors( aCollection );
317               aCollection->InitTraversal();
318               while(vtkActor* aSubAct = aCollection->GetNextActor())
319                 aSubAct->SetVisibility( false );
320               aCollection->Delete();
321             }
322           }
323         }
324       }
325     }
326   }
327   Repaint();
328 }
329
330 //==========================================================
331 SALOME_Prs* 
332 SVTK_Viewer
333 ::CreatePrs( const char* entry )
334 {
335   SVTK_Prs* prs = new SVTK_Prs();
336   if ( entry ) {
337     vtkRenderer* rnr =  ( (SVTK_ViewWindow*) getViewManager()->getActiveView() )->getRenderer();
338     vtkActorCollection* theActors = rnr->GetActors();
339     theActors->InitTraversal();
340     vtkActor* ac;
341     while( ( ac = theActors->GetNextActor() ) ) {
342       SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
343       if ( anActor && anActor->hasIO() && !strcmp( anActor->getIO()->getEntry(), entry ) ) {
344         prs->AddObject( ac );
345       }
346     }
347   }
348   return prs;
349 }
350
351 //==========================================================
352 void
353 SVTK_Viewer
354 ::BeforeDisplay( SALOME_Displayer* d )
355 {
356   d->BeforeDisplay( this, SALOME_VTKViewType() );
357 }
358
359 //==========================================================
360 void
361 SVTK_Viewer::AfterDisplay( SALOME_Displayer* d )
362 {
363   d->AfterDisplay( this, SALOME_VTKViewType() );
364 }
365
366 //==========================================================
367 bool
368 SVTK_Viewer
369 ::isVisible( const Handle(SALOME_InteractiveObject)& io )
370 {
371   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
372   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
373     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
374       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
375         if(SVTK_RenderWindowInteractor* aRWI = aView->getRWInteractor())
376           if(!aRWI->isVisible( io ))
377             return false;
378   return true;
379 }
380
381 //==========================================================
382 void 
383 SVTK_Viewer
384 ::Repaint()
385 {
386 //  if (theUpdateTrihedron) onAdjustTrihedron();
387   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
388   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
389     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
390       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewWindow))
391         if(SVTK_RenderWindow* aRW = aView->getRenderWindow())
392           aRW->update();
393 }
394
395 void 
396 SVTK_Viewer
397 ::onSelectionChanged()
398 {
399   emit selectionChanged();
400 }
401