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