Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[modules/gui.git] / src / SVTK / SVTK_ViewModel.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include <qpopupmenu.h>
20 #include <qcolordialog.h>
21
22 #include <vtkCamera.h>
23 #include <vtkRenderer.h>
24 #include <vtkActorCollection.h>
25
26 #include "SUIT_Session.h"
27
28 #include "SVTK_Selection.h"
29 #include "SVTK_ViewModel.h"
30 #include "SVTK_ViewWindow.h"
31 #include "SVTK_View.h"
32 #include "SVTK_MainWindow.h"
33 #include "SVTK_Prs.h"
34
35 #include "VTKViewer_ViewModel.h"
36
37 #include <SALOME_Actor.h>
38 #include <SALOME_InteractiveObject.hxx>
39
40 // in order NOT TO link with SalomeApp, here the code returns SALOMEDS_Study.
41 // SalomeApp_Study::studyDS() does it as well, but -- here it is retrieved from 
42 // SALOMEDS::StudyManager - no linkage with SalomeApp. 
43
44 // Temporarily commented to avoid awful dependecy on SALOMEDS
45 // TODO: better mechanism of storing display/erse status in a study
46 // should be provided...
47 //static _PTR(Study) getStudyDS() 
48 //{
49 //  SALOMEDSClient_Study* aStudy = NULL;
50 //  _PTR(StudyManager) aMgr( new SALOMEDS_StudyManager() );
51   // get id of SUIT_Study, if it's a SalomeApp_Study, it will return
52   //    id of its underlying SALOMEDS::Study
53 //  SUIT_Application* app = SUIT_Session::session()->activeApplication();
54 //  if ( !app )  return _PTR(Study)(aStudy); 
55 //  SUIT_Study* stud = app->activeStudy();
56 //  if ( !stud ) return _PTR(Study)(aStudy);  
57 //  const int id = stud->id(); // virtual method, must return SALOMEDS_Study id
58   // get SALOMEDS_Study with this id from StudyMgr
59 //  return aMgr->GetStudyByID( id );
60 //}
61
62 /*!
63   Constructor
64 */
65 SVTK_Viewer::SVTK_Viewer()
66 {
67   myTrihedronSize = 105;
68   myTrihedronRelative = true;
69 }
70
71 /*!
72   Destructor
73 */
74 SVTK_Viewer::~SVTK_Viewer() 
75 {
76 }
77
78 /*!
79   \return background color
80 */
81 QColor
82 SVTK_Viewer
83 ::backgroundColor() const
84 {
85   return myBgColor;
86 }
87
88 /*!
89   Changes background color
90   \param theColor - new background color
91 */
92 void
93 SVTK_Viewer
94 ::setBackgroundColor( const QColor& theColor )
95 {
96   if ( !theColor.isValid() )
97     return;
98
99   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
100   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
101     if(SUIT_ViewWindow* aViewWindow = aViews.at(i)){
102       if(TViewWindow* aView = dynamic_cast<TViewWindow*>(aViewWindow)){
103         aView->setBackgroundColor(theColor);
104       }
105     }
106   }
107
108   myBgColor = theColor;
109 }
110
111 /*!Create new instance of view window on desktop \a theDesktop.
112  *\retval SUIT_ViewWindow* - created view window pointer.
113  */
114 SUIT_ViewWindow*
115 SVTK_Viewer::
116 createView( SUIT_Desktop* theDesktop )
117 {
118   TViewWindow* aViewWindow = new TViewWindow(theDesktop);
119   aViewWindow->Initialize(this);
120
121   aViewWindow->setBackgroundColor( backgroundColor() );
122   aViewWindow->SetTrihedronSize( trihedronSize(), trihedronRelative() );
123
124   return aViewWindow;
125 }
126
127 /*!
128   \return trihedron size
129 */
130 int SVTK_Viewer::trihedronSize() const
131 {
132   return myTrihedronSize;
133 }
134
135 /*!
136   \return true if thihedron changes size in accordance with bounding box
137 */
138 bool SVTK_Viewer::trihedronRelative() const
139 {
140   return myTrihedronRelative;
141 }
142
143 /*!
144   Sets trihedron size and relativeness( whether thihedron changes size in accordance with bounding box)
145   \param theSize - new size
146   \param theRelative - new relativeness
147 */
148 void SVTK_Viewer::setTrihedronSize( const int theSize, const bool theRelative )
149 {
150   myTrihedronSize = theSize;
151   myTrihedronRelative = theRelative;
152
153   if (SUIT_ViewManager* aViewManager = getViewManager()) {
154     QPtrVector<SUIT_ViewWindow> aViews = aViewManager->getViews();
155     for (int i = 0; i < aViews.count(); i++) {
156       if (TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at(i))) {
157         aView->SetTrihedronSize(theSize, theRelative);
158       }
159     }
160   }
161 }
162
163 /*!
164   Sets new view manager
165   \param theViewManager - new view manager
166 */
167 void SVTK_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
168 {
169   SUIT_ViewModel::setViewManager(theViewManager);
170
171   if ( !theViewManager )
172     return;
173
174   connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
175           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
176   
177   connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
178           this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
179   
180   connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
181           this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
182 }
183
184 /*!
185   Builds popup for vtk viewer
186 */
187 void
188 SVTK_Viewer
189 ::contextMenuPopup( QPopupMenu* thePopup )
190 {
191   thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
192   thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_CHANGE_BACKGROUD" ), this, SLOT( onChangeBgColor() ) );
193
194   thePopup->insertSeparator();
195
196   if(TViewWindow* aView = dynamic_cast<TViewWindow*>(myViewManager->getActiveView())){
197     if ( !aView->getMainWindow()->getToolBar()->isVisible() ){
198       thePopup->insertItem( VTKViewer_Viewer::tr( "MEN_SHOW_TOOLBAR" ), this, SLOT( onShowToolbar() ) );
199     }
200   }
201 }
202
203 /*!
204   SLOT: called on mouse button press, empty implementation
205 */
206 void 
207 SVTK_Viewer
208 ::onMousePress(SUIT_ViewWindow* vw, QMouseEvent* event)
209 {}
210
211 /*!
212   SLOT: called on mouse move, empty implementation
213 */
214 void 
215 SVTK_Viewer
216 ::onMouseMove(SUIT_ViewWindow* vw, QMouseEvent* event)
217 {}
218
219 /*!
220   SLOT: called on mouse button release, empty implementation
221 */
222 void 
223 SVTK_Viewer
224 ::onMouseRelease(SUIT_ViewWindow* vw, QMouseEvent* event)
225 {}
226
227 /*!
228   Enables/disables selection
229   \param isEnabled - new state
230 */
231 void 
232 SVTK_Viewer
233 ::enableSelection(bool isEnabled)
234 {
235   mySelectionEnabled = isEnabled;
236   //!! To be done for view windows
237 }
238
239 /*!
240   Enables/disables selection of many object
241   \param isEnabled - new state
242 */
243 void
244 SVTK_Viewer
245 ::enableMultiselection(bool isEnable)
246 {
247   myMultiSelectionEnabled = isEnable;
248   //!! To be done for view windows
249 }
250
251 /*!
252   SLOT: called on dump view operation is activated, stores scene to raster file
253 */
254 void
255 SVTK_Viewer
256 ::onDumpView()
257 {
258   if(SUIT_ViewWindow* aView = myViewManager->getActiveView())
259     aView->onDumpView();
260 }
261
262 /*!
263   SLOT: called if background color is to be changed changed, passes new color to view port
264 */
265 void
266 SVTK_Viewer
267 ::onChangeBgColor()
268 {
269   if(SUIT_ViewWindow* aView = myViewManager->getActiveView()){
270     QColor aColor = QColorDialog::getColor( backgroundColor(), aView);
271     setBackgroundColor(aColor);
272   }
273 }
274
275 /*!
276   SLOT: called when popup item "Show toolbar" is activated, shows toolbar of active view window
277 */
278 void
279 SVTK_Viewer
280 ::onShowToolbar() 
281 {
282   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
283   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
284     if(TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at(i))){
285       aView->getMainWindow()->getToolBar()->show();
286     }
287   }
288 }
289
290 /*!
291   Display presentation
292   \param prs - presentation
293 */
294 void
295 SVTK_Viewer
296 ::Display( const SALOME_VTKPrs* prs )
297 {
298   // try do downcast object
299   if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
300     if(aPrs->IsNull())
301       return;
302     if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
303       // get SALOMEDS Study
304       // Temporarily commented to avoid awful dependecy on SALOMEDS
305       // TODO: better mechanism of storing display/erse status in a study
306       // should be provided...
307       // _PTR(Study) aStudy(getStudyDS());
308       anActorCollection->InitTraversal();
309       while(vtkActor* anActor = anActorCollection->GetNextActor()){
310         if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
311           // Set visibility flag
312           // Temporarily commented to avoid awful dependecy on SALOMEDS
313           // TODO: better mechanism of storing display/erse status in a study
314           // should be provided...
315           //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
316           //if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
317           //  ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),true,this);
318           //}
319           // just display the object
320           QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
321           for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
322             if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i))){
323               if(SVTK_View* aView = aViewWindow->getView()){
324                 aView->Display(anAct,false);
325                 if(anAct->IsSetCamera()){
326                   vtkRenderer* aRenderer = aView->getRenderer();
327                   anAct->SetCamera( aRenderer->GetActiveCamera() );
328                 }
329               }
330             }
331           }
332         }
333       }
334     }
335   }
336 }
337
338 /*!
339   Erase presentation
340   \param prs - presentation
341   \param forced - removes object from view
342 */
343 void
344 SVTK_Viewer
345 ::Erase( const SALOME_VTKPrs* prs, const bool forced )
346 {
347   // try do downcast object
348   if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
349     if(aPrs->IsNull())
350       return;
351     if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
352       // get SALOMEDS Study
353       // Temporarily commented to avoid awful dependecy on SALOMEDS
354       // TODO: better mechanism of storing display/erse status in a study
355       // should be provided...
356       //_PTR(Study) aStudy(getStudyDS());
357       anActorCollection->InitTraversal();
358       while(vtkActor* anActor = anActorCollection->GetNextActor())
359         if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
360           // Set visibility flag
361           // Temporarily commented to avoid awful dependecy on SALOMEDS
362           // TODO: better mechanism of storing display/erse status in a study
363           // should be provided...
364           //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
365           //if(!anObj.IsNull() && anObj->hasEntry() && aStudy){
366           //  ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
367           //}
368           // just display the object
369           QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
370           for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
371             if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i)))
372               if(SVTK_View* aView = aViewWindow->getView())
373                 if ( forced )
374                   aView->Remove(anAct,false);
375                 else
376                   aView->Erase(anAct,forced);
377           }
378         }
379     }
380   }
381 }
382
383 /*!
384   Erase all presentations
385   \param forced - removes all objects from view
386 */
387 void
388 SVTK_Viewer
389 ::EraseAll( const bool forced )
390 {
391   // Temporarily commented to avoid awful dependecy on SALOMEDS
392   // TODO: better mechanism of storing display/erse status in a study
393   // should be provided...
394   //_PTR(Study) aStudy(getStudyDS());
395   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
396   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
397     if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i)))
398       if(SVTK_View* aView = aViewWindow->getView()){
399         vtkRenderer* aRenderer =  aView->getRenderer();
400         vtkActorCollection* anActorCollection = aRenderer->GetActors();
401         anActorCollection->InitTraversal();
402         while(vtkActor* anActor = anActorCollection->GetNextActor()){
403           if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
404             // Set visibility flag
405             // Temporarily commented to avoid awful dependecy on SALOMEDS
406             // TODO: better mechanism of storing display/erse status in a study
407             // should be provided...
408             //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
409             //if(!anObj.IsNull() && anObj->hasEntry() && aStudy)
410             //  ToolsGUI::SetVisibility(aStudy,anObj->getEntry(),false,this);
411             if(forced)
412               aRenderer->RemoveActor(anAct);
413             else{
414               // just erase actor
415               anAct->SetVisibility( false );
416               // erase dependent actors
417               vtkActorCollection* aCollection = vtkActorCollection::New();
418               anAct->GetChildActors( aCollection );
419               aCollection->InitTraversal();
420               while(vtkActor* aSubAct = aCollection->GetNextActor())
421                 aSubAct->SetVisibility( false );
422               aCollection->Delete();
423             }
424           }
425         }
426       }
427   }
428   Repaint();
429 }
430
431 /*!
432   Create presentation corresponding to the entry
433   \param entry - entry
434 */
435 SALOME_Prs* 
436 SVTK_Viewer
437 ::CreatePrs( const char* entry )
438 {
439   SVTK_Prs* prs = new SVTK_Prs();
440   if ( entry ) {
441     if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(getViewManager()->getActiveView()))
442       if(SVTK_View* aView = aViewWindow->getView()){
443         vtkRenderer* aRenderer =  aView->getRenderer();
444         vtkActorCollection* theActors = aRenderer->GetActors();
445         theActors->InitTraversal();
446         vtkActor* ac;
447         while( ( ac = theActors->GetNextActor() ) ) {
448           SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
449           if ( anActor && anActor->hasIO() && !strcmp( anActor->getIO()->getEntry(), entry ) ) {
450             prs->AddObject( ac );
451           }
452         }
453       }
454   }
455   return prs;
456 }
457
458 /*!
459   Auxiliary method called before displaying of objects
460 */
461 void
462 SVTK_Viewer
463 ::BeforeDisplay( SALOME_Displayer* d )
464 {
465   d->BeforeDisplay( this, SALOME_VTKViewType() );
466 }
467
468 /*!
469   Auxiliary method called after displaying of objects
470 */
471 void
472 SVTK_Viewer::AfterDisplay( SALOME_Displayer* d )
473 {
474   d->AfterDisplay( this, SALOME_VTKViewType() );
475 }
476
477 /*!
478   \return true if object is displayed in viewer
479   \param obj - object to be checked
480 */
481 bool
482 SVTK_Viewer
483 ::isVisible( const Handle(SALOME_InteractiveObject)& io )
484 {
485   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
486   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
487     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
488       if(TViewWindow* aViewWnd = dynamic_cast<TViewWindow*>(aViewWindow))
489         if(SVTK_View* aView = aViewWnd->getView())
490           if(!aView->isVisible( io ))
491             return false;
492
493   return true;
494 }
495
496 /*!
497   Updates current viewer
498 */
499 void 
500 SVTK_Viewer
501 ::Repaint()
502 {
503 //  if (theUpdateTrihedron) onAdjustTrihedron();
504   QPtrVector<SUIT_ViewWindow> aViews = myViewManager->getViews();
505   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
506     if(TViewWindow* aViewWindow = dynamic_cast<TViewWindow*>(aViews.at(i)))
507       if(SVTK_View* aView = aViewWindow->getView())
508         aView->Repaint();
509 }