Salome HOME
a6e6d76e3536689eeda10bc4b1f435a9c92b0fc8
[modules/gui.git] / src / SVTK / SVTK_ViewModel.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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 #include <QMenu>
24 #include <QColorDialog>
25 #include <QToolBar>
26 #include <QTimer>
27
28 #include <vtkCamera.h>
29 #include <vtkRenderer.h>
30 #include <vtkActorCollection.h>
31
32 //#include "SUIT_Session.h"
33 #include "SVTK_ViewModel.h"
34 #include "SVTK_ViewWindow.h"
35 #include "SVTK_View.h"
36 #include "SVTK_Renderer.h"
37 //#include "SVTK_MainWindow.h"
38 #include "SVTK_Prs.h"
39
40 #include "VTKViewer_Algorithm.h"
41 #include "VTKViewer_ViewModel.h"
42
43 #include "SUIT_ViewModel.h"
44 #include "SUIT_ViewManager.h"
45
46 #include "SALOME_Actor.h"
47
48 #include "QtxActionToolMgr.h"
49 #include "QtxBackgroundTool.h"
50
51 // VSR: Uncomment below line to allow texture background support in VTK viewer
52 #define VTK_ENABLE_TEXTURED_BACKGROUND
53
54 /*!
55   Constructor
56 */
57 SVTK_Viewer::SVTK_Viewer()
58 {
59   myTrihedronSize = 105;
60   myTrihedronRelative = true;
61   myIsStaticTrihedronVisible = true;
62   myIncrementSpeed = 10;
63   myIncrementMode = 0;
64   myProjMode = 0;
65   myStereoType = 0;
66   myAnaglyphFilter = 0;
67   myStyle = 0;
68   myZoomingStyle = 0;
69   mySelectionEnabled = true;
70   myPreSelectionMode = Standard_Preselection;
71   mySpaceBtn[0] = 1;
72   mySpaceBtn[1] = 2;
73   mySpaceBtn[2] = 9;
74   myDefaultBackground = Qtx::BackgroundData( Qt::black );
75   myQuadBufferSupport = false;
76 }
77
78 /*!
79   Destructor
80 */
81 SVTK_Viewer::~SVTK_Viewer() 
82 {
83 }
84
85 /*! Get data for supported background modes: gradient types, identifiers and supported image formats */
86 QString SVTK_Viewer::backgroundData( QStringList& gradList, QIntList& idList, QIntList& txtList )
87 {
88   gradList << tr( "GT_HORIZONTALGRADIENT" )
89            << tr( "GT_VERTICALGRADIENT" )
90            << tr( "GT_FIRSTDIAGONALGRADIENT" )
91            << tr( "GT_SECONDDIAGONALGRADIENT" )
92            << tr( "GT_FIRSTCORNERGRADIENT" )
93            << tr( "GT_SECONDCORNERGRADIENT" )
94            << tr( "GT_THIRDCORNERGRADIENT" )
95            << tr( "GT_FOURTHCORNERGRADIENT" );
96   idList   << HorizontalGradient
97            << VerticalGradient
98            << FirstDiagonalGradient
99            << SecondDiagonalGradient
100            << FirstCornerGradient
101            << SecondCornerGradient
102            << ThirdCornerGradient
103            << FourthCornerGradient;
104 #ifdef VTK_ENABLE_TEXTURED_BACKGROUND
105   txtList  << Qtx::CenterTexture << Qtx::TileTexture << Qtx::StretchTexture;
106 #endif
107   return tr("BG_IMAGE_FILES");
108 }
109
110 /*! Get data for supported background modes: gradient types, identifiers and supported image formats */
111 void SVTK_Viewer::stereoData( QStringList& typeList, QIntList& idList )
112 {
113   typeList << tr("ST_CRYSTALEYES") << tr("ST_REDBLUE")      <<
114               tr("ST_INTERLACED")  << tr("ST_LEFT")         <<
115               tr("ST_RIGHT")       << tr("ST_DRESDEN")      <<
116               tr("ST_ANAGLYPH")    << tr("ST_CHECKERBOARD") <<
117               tr("ST_SPLITVIEWPORTHORIZONTAL");
118   idList   << CrystalEyesType << RedBlueType      <<
119               InterlacedType  << LeftType         <<
120               RightType       << DresdenType      <<
121               AnaglyphType    << CheckerboardType <<
122               SplitViewPortHorizontalType;
123 }
124
125 //! Get background color of the viewer [obsolete]
126 QColor SVTK_Viewer::backgroundColor() const
127 {
128   return background().color();
129 }
130
131 //! Set background color to the viewer [obsolete]
132 void SVTK_Viewer::setBackgroundColor( const QColor& c )
133 {
134   Qtx::BackgroundData bg = background();
135   bg.setColor( c );
136   setBackground( bg );
137 }
138
139 /*!
140   \return background data
141 */
142 Qtx::BackgroundData SVTK_Viewer::background() const
143 {
144   return myDefaultBackground;
145 }
146
147 /*!
148   Changes background
149   \param theBackground - new background data
150 */
151 void SVTK_Viewer::setBackground( const Qtx::BackgroundData& theBackground )
152 {
153   myDefaultBackground = theBackground.isValid() ? theBackground : Qtx::BackgroundData( Qt::black );
154   QVector<SUIT_ViewWindow*> aViews = myViewManager->getViews();
155   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
156     if(SUIT_ViewWindow* aViewWindow = aViews.at(i)){
157       if(TViewWindow* aView = dynamic_cast<TViewWindow*>(aViewWindow)){
158         aView->setBackground(myDefaultBackground);
159       }
160     }
161   }  
162 }
163
164 /*!Create new instance of view window on desktop \a theDesktop.
165  *\retval SUIT_ViewWindow* - created view window pointer.
166  */
167 SUIT_ViewWindow* SVTK_Viewer::createView( SUIT_Desktop* theDesktop )
168 {
169   TViewWindow* aViewWindow = new TViewWindow(theDesktop);
170   aViewWindow->Initialize(this);
171
172   aViewWindow->setBackground( background() );
173   aViewWindow->SetTrihedronSize( trihedronSize(), trihedronRelative() );
174   aViewWindow->SetStaticTrihedronVisible( isStaticTrihedronVisible() );
175   aViewWindow->SetProjectionMode( projectionMode() );
176   aViewWindow->SetStereoType( stereoType() );
177   aViewWindow->SetAnaglyphFilter( anaglyphFilter() );
178   aViewWindow->SetQuadBufferSupport( isQuadBufferSupport() );
179   aViewWindow->SetInteractionStyle( interactionStyle() );
180   aViewWindow->SetZoomingStyle( zoomingStyle() );
181   aViewWindow->SetPreSelectionMode( preSelectionMode() );
182   aViewWindow->SetSelectionEnabled( isSelectionEnabled() );
183   aViewWindow->SetIncrementalSpeed( incrementalSpeed(), incrementalSpeedMode() );
184   aViewWindow->SetSpacemouseButtons( spacemouseBtn(1), spacemouseBtn(2), spacemouseBtn(3) );
185
186   connect(aViewWindow, SIGNAL( actorAdded(VTKViewer_Actor*) ), 
187           this,  SLOT(onActorAdded(VTKViewer_Actor*)));
188   connect(aViewWindow, SIGNAL( actorRemoved(VTKViewer_Actor*) ), 
189           this,  SLOT(onActorRemoved(VTKViewer_Actor*)));
190   
191   return aViewWindow;
192 }
193
194 /*!
195   \return trihedron size
196 */
197 double SVTK_Viewer::trihedronSize() const
198 {
199   return myTrihedronSize;
200 }
201
202 /*!
203   \return true if thihedron changes size in accordance with bounding box
204 */
205 bool SVTK_Viewer::trihedronRelative() const
206 {
207   return myTrihedronRelative;
208 }
209
210 /*!
211   Sets trihedron size and relativeness( whether thihedron changes size in accordance with bounding box)
212   \param theSize - new size
213   \param theRelative - new relativeness
214 */
215 void SVTK_Viewer::setTrihedronSize( const double theSize, const bool theRelative )
216 {
217   myTrihedronSize = theSize;
218   myTrihedronRelative = theRelative;
219
220   if (SUIT_ViewManager* aViewManager = getViewManager()) {
221     QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
222     for ( int i = 0; i < aViews.count(); i++ )
223     {
224       if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
225               aView->SetTrihedronSize( theSize, theRelative );
226     }
227   }
228 }
229
230 /*!
231   \return visibility status of the static trihedron
232 */
233 bool SVTK_Viewer::isStaticTrihedronVisible() const
234 {
235   return myIsStaticTrihedronVisible;
236 }
237
238 /*!
239   Sets visibility status of the static trihedron
240   \param theIsVisible - new visibility status
241 */
242 void SVTK_Viewer::setStaticTrihedronVisible( const bool theIsVisible )
243 {
244   myIsStaticTrihedronVisible = theIsVisible;
245
246   if (SUIT_ViewManager* aViewManager = getViewManager()) {
247     QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
248     for ( int i = 0; i < aViews.count(); i++ )
249     {
250       if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
251         aView->SetStaticTrihedronVisible( theIsVisible );
252     }
253   }
254 }
255
256 /*!
257   \return projection mode
258 */
259 int SVTK_Viewer::projectionMode() const
260 {
261   return myProjMode;
262 }
263
264
265 /*!
266   Sets projection mode: 0 - orthogonal, 1 - perspective projection
267   \param theMode - new projection mode
268 */
269 void SVTK_Viewer::setProjectionMode( const int theMode )
270 {
271   if ( myProjMode != theMode ) {
272     if ( theMode != SVTK_ViewWindow::Stereo )
273       myProjMode = theMode;
274     if (SUIT_ViewManager* aViewManager = getViewManager()) {
275       QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
276       for ( int i = 0; i < aViews.count(); i++ )
277       {
278         if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
279           aView->SetProjectionMode( theMode );
280       }
281     }
282   }
283 }
284
285 /*!
286   \return stereo type
287 */
288 int SVTK_Viewer::stereoType() const
289 {
290   return myStereoType;
291 }
292
293 /*!
294   Sets stereo type
295   \param theType - new stereo type
296 */
297 void SVTK_Viewer::setStereoType( const int theType )
298 {
299   if ( myStereoType != theType ) {
300     myStereoType = theType;
301
302     if (SUIT_ViewManager* aViewManager = getViewManager()) {
303       QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
304       for ( int i = 0; i < aViews.count(); i++ )
305       {
306         if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
307           aView->SetStereoType( theType );
308       }
309     }
310   }
311 }
312
313 /*!
314   \return anaglyph filter
315 */
316 int SVTK_Viewer::anaglyphFilter() const
317 {
318   return myAnaglyphFilter;
319 }
320
321 /*!
322   Sets anaglyph filter
323   \param theFilter - new anaglyph filter
324 */
325 void SVTK_Viewer::setAnaglyphFilter( const int theFilter )
326 {
327   if ( myAnaglyphFilter != theFilter ) {
328           myAnaglyphFilter = theFilter;
329
330     if (SUIT_ViewManager* aViewManager = getViewManager()) {
331       QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
332       for ( int i = 0; i < aViews.count(); i++ )
333       {
334         if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
335           aView->SetAnaglyphFilter( theFilter );
336       }
337     }
338   }
339 }
340
341 /*!
342   \return support quad-buffered stereo
343 */
344 bool SVTK_Viewer::isQuadBufferSupport() const
345 {
346   return myQuadBufferSupport;
347 }
348
349 /*!
350   Set support quad-buffered stereo
351   \param theEnable - enable/disable support quad-buffered stereo
352 */
353 void SVTK_Viewer::setQuadBufferSupport( const bool theEnable )
354 {
355   if ( myQuadBufferSupport != theEnable ) {
356     myQuadBufferSupport = theEnable;
357
358     if (SUIT_ViewManager* aViewManager = getViewManager()) {
359       QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
360       for ( int i = 0; i < aViews.count(); i++ )
361       {
362         if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
363           aView->SetQuadBufferSupport( theEnable );
364       }
365     }
366   }
367 }
368 /*!
369   \return interaction style
370 */
371 int SVTK_Viewer::interactionStyle() const
372 {
373   return myStyle;
374 }
375
376 /*!
377   Sets interaction style: 0 - standard, 1 - keyboard free interaction
378   \param theStyle - new interaction style
379 */
380 void SVTK_Viewer::setInteractionStyle( const int theStyle )
381 {
382   myStyle = theStyle;
383   
384   if (SUIT_ViewManager* aViewManager = getViewManager()) {
385     QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
386     for ( int i = 0; i < aViews.count(); i++ )
387     {
388       if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
389         aView->SetInteractionStyle( theStyle );
390     }
391   }
392 }
393
394 /*!
395   \return zooming style
396 */
397 int SVTK_Viewer::zoomingStyle() const
398 {
399   return myZoomingStyle;
400 }
401
402 /*!
403   Sets zooming style: 0 - standard, 1 - advanced (at cursor)
404   \param theStyle - new zooming style
405 */
406 void SVTK_Viewer::setZoomingStyle( const int theStyle )
407 {
408   myZoomingStyle = theStyle;
409   
410   if (SUIT_ViewManager* aViewManager = getViewManager()) {
411     QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
412     for ( int i = 0; i < aViews.count(); i++ )
413     {
414       if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
415         aView->SetZoomingStyle( theStyle );
416     }
417   }
418 }
419
420 /*!
421   \return current preselection mode
422 */
423 Preselection_Mode SVTK_Viewer::preSelectionMode() const
424 {
425   return myPreSelectionMode;
426 }
427
428 /*!
429   Sets preselection mode
430   \param theMode - new preselection mode
431 */
432 void SVTK_Viewer::setPreSelectionMode( Preselection_Mode theMode )
433 {
434   myPreSelectionMode = theMode;
435   
436   if (SUIT_ViewManager* aViewManager = getViewManager()) {
437     QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
438     for ( int i = 0; i < aViews.count(); i++ )
439     {
440       if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
441         aView->SetPreSelectionMode( theMode );
442     }
443   }
444 }
445
446 /*!
447   \return incremental speed value
448 */
449 int SVTK_Viewer::incrementalSpeed() const
450 {
451   return myIncrementSpeed;
452 }
453
454 /*!
455   \return modification mode of the incremental speed 
456 */
457 int SVTK_Viewer::incrementalSpeedMode() const
458 {
459   return myIncrementMode;
460 }
461
462 /*!
463   Set the incremental speed value and modification mode
464   \param theValue - new value
465   \param theMode - new mode: 0 - arithmetic, 1 - geometrical progression
466 */
467 void SVTK_Viewer::setIncrementalSpeed( const int theValue, const int theMode )
468 {
469   myIncrementSpeed = theValue;
470   myIncrementMode = theMode;
471
472   if (SUIT_ViewManager* aViewManager = getViewManager()) {
473     QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
474     for ( int i = 0; i < aViews.count(); i++ )
475     {
476       if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
477         aView->SetIncrementalSpeed( theValue, theMode );
478     }
479   }
480 }
481
482 /*!
483   \return spacemouse button assigned to the specified function
484   \param theIndex - function by number (from 1 to 3)
485 */
486 int SVTK_Viewer::spacemouseBtn( const int theIndex ) const
487 {
488   if ( theIndex < 1 || theIndex > 3 ) 
489     return -1;
490   return mySpaceBtn[theIndex-1];
491 }
492
493 /*!
494   Set the spacemouse buttons
495   \param theBtn1, theBtn2, theBtn3 - new buttons
496 */
497 void SVTK_Viewer::setSpacemouseButtons( const int theBtn1, const int theBtn2, const int theBtn3 )
498 {
499   mySpaceBtn[0] = theBtn1;
500   mySpaceBtn[1] = theBtn2;
501   mySpaceBtn[2] = theBtn3;
502
503   if (SUIT_ViewManager* aViewManager = getViewManager()) {
504     QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
505     for ( int i = 0; i < aViews.count(); i++ )
506     {
507       if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
508         aView->SetSpacemouseButtons( theBtn1, theBtn2, theBtn3 );
509     }
510   }
511 }
512
513 /*!
514   Sets new view manager
515   \param theViewManager - new view manager
516 */
517 void SVTK_Viewer::setViewManager(SUIT_ViewManager* theViewManager)
518 {
519   SUIT_ViewModel::setViewManager(theViewManager);
520
521   if ( !theViewManager )
522     return;
523
524   connect(theViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
525           this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
526   
527   connect(theViewManager, SIGNAL(mouseMove(SUIT_ViewWindow*, QMouseEvent*)), 
528           this, SLOT(onMouseMove(SUIT_ViewWindow*, QMouseEvent*)));
529   
530   connect(theViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
531           this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
532
533   connect(theViewManager, SIGNAL(viewCreated(SUIT_ViewWindow*)), 
534           this, SLOT(onViewCreated(SUIT_ViewWindow*)));
535 }
536
537 /*!
538   Builds popup for vtk viewer
539 */
540 void SVTK_Viewer::contextMenuPopup( QMenu* thePopup )
541 {
542   thePopup->addAction( VTKViewer_Viewer::tr( "MEN_DUMP_VIEW" ), this, SLOT( onDumpView() ) );
543   thePopup->addAction( VTKViewer_Viewer::tr( "MEN_CHANGE_BACKGROUND" ), this, SLOT( onChangeBackground() ) );
544
545   thePopup->addSeparator();
546
547   if(TViewWindow* aView = dynamic_cast<TViewWindow*>(myViewManager->getActiveView())){
548     //Support of several toolbars in the popup menu
549     QList<QToolBar*> lst = aView->findChildren<QToolBar*>();
550     QList<QToolBar*>::const_iterator it = lst.begin(), last = lst.end();
551     for( ; it!=last; it++ )
552       thePopup->addAction( (*it)->toggleViewAction() );
553     aView->RefreshDumpImage();
554   }
555 }
556
557 /*!
558   SLOT: called on mouse button press, empty implementation
559 */
560 void SVTK_Viewer::onMousePress(SUIT_ViewWindow* /*vw*/, QMouseEvent* /*event*/)
561 {}
562
563 /*!
564   SLOT: called on mouse move, empty implementation
565 */
566 void SVTK_Viewer::onMouseMove(SUIT_ViewWindow* /*vw*/, QMouseEvent* /*event*/)
567 {}
568
569 /*!
570   SLOT: called on mouse button release, empty implementation
571 */
572 void SVTK_Viewer::onMouseRelease(SUIT_ViewWindow* /*vw*/, QMouseEvent* /*event*/)
573 {}
574
575 /*!
576   Enables/disables selection
577   \param isEnabled - new state
578 */
579 void SVTK_Viewer::enableSelection(bool isEnabled)
580 {
581   mySelectionEnabled = isEnabled;
582   //!! To be done for view windows
583    
584   if (SUIT_ViewManager* aViewManager = getViewManager()) {
585     QVector<SUIT_ViewWindow*> aViews = aViewManager->getViews();
586     for ( int i = 0; i < aViews.count(); i++ )
587     {
588       if ( TViewWindow* aView = dynamic_cast<TViewWindow*>(aViews.at( i )) )
589         aView->SetSelectionEnabled( isEnabled );
590     }
591   }
592
593   if(!isEnabled) {
594     //clear current selection in the viewer
595     bool blocked = blockSignals( true );
596     if ( SUIT_ViewManager* aViewMgr = getViewManager() ) {
597       if( SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>( aViewMgr->getActiveView() ) ){
598         if( SVTK_Selector* aSelector = aViewWindow->GetSelector() ) {
599           if(SVTK_View* aView = aViewWindow->getView()){
600             aSelector->ClearIObjects();
601             aView->onSelectionChanged();
602           }
603         }
604       }
605     }
606     blockSignals( blocked );  
607   }
608
609 }
610
611 /*!
612   Enables/disables selection of many object
613   \param isEnabled - new state
614 */
615 void SVTK_Viewer::enableMultiselection(bool isEnable)
616 {
617   myMultiSelectionEnabled = isEnable;
618   //!! To be done for view windows
619 }
620
621 /*!
622   SLOT: called on dump view operation is activated, stores scene to raster file
623 */
624 void SVTK_Viewer::onDumpView()
625 {
626   if(SUIT_ViewWindow* aView = myViewManager->getActiveView())
627     aView->onDumpView();
628 }
629
630 /*!
631   SLOT: called if background color is to be changed changed, passes new color to view port
632 */
633 void SVTK_Viewer::onChangeBackground()
634 {
635   SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(myViewManager->getActiveView());
636   if ( !aView )
637     return;
638
639   // get supported gradient types
640   QStringList gradList;
641   QIntList    idList, txtList;
642   QString     formats = backgroundData( gradList, idList, txtList );
643
644   // invoke dialog box
645   Qtx::BackgroundData bgData = QtxBackgroundDialog::getBackground( aView->background(),  // initial background
646                                                                    aView,                // parent for dialog box
647                                                                    txtList,              // allowed texture modes
648                                                                    true,                 // enable solid color mode
649                                                                    true,                 // enable gradient mode
650                                                                    false,                // disable custom gradient mode
651                                                                    !txtList.isEmpty(),   // enable texture mode
652                                                                    gradList,             // gradient names
653                                                                    idList,               // gradient identifiers
654                                                                    formats );            // image formats
655
656   // set chosen background data to the viewer
657   if ( bgData.isValid() )
658     aView->setBackground( bgData );
659 }
660
661 /*!
662   Display presentation
663   \param prs - presentation
664 */
665 void SVTK_Viewer::Display( const SALOME_VTKPrs* prs )
666 {
667   // try do downcast object
668   if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
669     if(aPrs->IsNull())
670       return;
671     if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
672       anActorCollection->InitTraversal();
673       while(vtkActor* anActor = anActorCollection->GetNextActor()){
674         if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
675           if(!anAct->ShouldBeDisplayed())
676             continue;
677           // Set visibility flag
678           // Temporarily commented to avoid awful dependecy on SALOMEDS
679           // TODO: better mechanism of storing display/erse status in a study
680           // should be provided...
681           //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
682           //if(!anObj.IsNull() && anObj->hasEntry()){
683           //  ToolsGUI::SetVisibility(anObj->getEntry(),true,this);
684           //}
685           // just display the object
686           QVector<SUIT_ViewWindow*> aViews = myViewManager->getViews();
687           for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
688             if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i))){
689               if(SVTK_View* aView = aViewWindow->getView()){
690                 aView->Display(anAct,false);
691                 if(anAct->IsSetCamera()){
692                   vtkRenderer* aRenderer = aView->getRenderer();
693                   anAct->SetCamera( aRenderer->GetActiveCamera() );
694                 }
695               }
696             }
697           }
698         }
699       }
700     }
701   }
702 }
703
704 /*!
705   Erase presentation
706   \param prs - presentation
707   \param forced - removes object from view
708 */
709 void SVTK_Viewer::Erase( const SALOME_VTKPrs* prs, const bool forced )
710 {
711   // try do downcast object
712   if(const SVTK_Prs* aPrs = dynamic_cast<const SVTK_Prs*>( prs )){
713     if(aPrs->IsNull())
714       return;
715     if(vtkActorCollection* anActorCollection = aPrs->GetObjects()){
716       anActorCollection->InitTraversal();
717       while(vtkActor* anActor = anActorCollection->GetNextActor())
718         if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
719           // Set visibility flag
720           // Temporarily commented to avoid awful dependecy on SALOMEDS
721           // TODO: better mechanism of storing display/erase status in a study
722           // should be provided...
723           //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
724           //if(!anObj.IsNull() && anObj->hasEntry()){
725           //  ToolsGUI::SetVisibility(anObj->getEntry(),false,this);
726           //}
727           // just display the object
728           QVector<SUIT_ViewWindow*> aViews = myViewManager->getViews();
729           for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
730             if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i)))
731               if(SVTK_View* aView = aViewWindow->getView())
732               {
733                 if ( forced )
734                   aView->Remove(anAct,false);
735                 else
736                   aView->Erase(anAct,forced);
737               }
738           }
739         }
740     }
741   }
742 }
743
744 /*!
745   Erase all presentations
746   \param forced - removes all objects from view
747 */
748 void SVTK_Viewer::EraseAll( SALOME_Displayer* d, const bool forced )
749 {
750   QVector<SUIT_ViewWindow*> aViews = myViewManager->getViews();
751   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++){
752     if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(aViews.at(i)))
753       if(SVTK_View* aView = aViewWindow->getView()){
754         vtkRenderer* aRenderer =  aView->getRenderer();
755         VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
756         vtkActorCollection* anActorCollection = aCopy.GetActors();
757         anActorCollection->InitTraversal();
758         while(vtkActor* anActor = anActorCollection->GetNextActor()){
759           if(SALOME_Actor* anAct = SALOME_Actor::SafeDownCast(anActor)){
760             // Set visibility flag
761             // Temporarily commented to avoid awful dependecy on SALOMEDS
762             // TODO: better mechanism of storing display/erse status in a study
763             // should be provided...
764             //Handle(SALOME_InteractiveObject) anObj = anAct->getIO();
765             //if(!anObj.IsNull() && anObj->hasEntry())
766             //  ToolsGUI::SetVisibility(anObj->getEntry(),false,this);
767             if(forced){
768               if(SVTK_Renderer* aRnd = aView->GetRenderer())
769                 aRnd->RemoveActor(anAct);
770             }else{
771               // just erase actor
772               anAct->SetVisibility( false );
773               // erase dependent actors
774               vtkActorCollection* aCollection = vtkActorCollection::New();
775               anAct->GetChildActors( aCollection );
776               aCollection->InitTraversal();
777               while(vtkActor* aSubAct = aCollection->GetNextActor())
778                 aSubAct->SetVisibility( false );
779               aCollection->Delete();
780             }
781           }
782         }
783       }
784   }
785
786   SALOME_View::EraseAll( d, forced );
787
788   Repaint();
789 }
790
791 /*!
792   Create presentation corresponding to the entry
793   \param entry - entry
794 */
795 SALOME_Prs* SVTK_Viewer::CreatePrs( const char* entry )
796 {
797   SVTK_Prs* prs = new SVTK_Prs( entry );
798   if ( entry ) {
799     if(SVTK_ViewWindow* aViewWindow = dynamic_cast<SVTK_ViewWindow*>(getViewManager()->getActiveView()))
800       if(SVTK_View* aView = aViewWindow->getView()){
801         vtkRenderer* aRenderer =  aView->getRenderer();
802         VTK::ActorCollectionCopy aCopy(aRenderer->GetActors());
803         vtkActorCollection* theActors = aCopy.GetActors();
804         theActors->InitTraversal();
805         vtkActor* ac;
806         while( ( ac = theActors->GetNextActor() ) ) {
807           SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
808           if ( anActor && anActor->hasIO() && !strcmp( anActor->getIO()->getEntry(), entry ) ) {
809             prs->AddObject( ac );
810           }
811         }
812       }
813   }
814   return prs;
815 }
816
817 /*!
818   \return true if object is displayed in viewer
819   \param obj - object to be checked
820 */
821 bool SVTK_Viewer::isVisible( const Handle(SALOME_InteractiveObject)& io )
822 {
823   QVector<SUIT_ViewWindow*> aViews = myViewManager->getViews();
824   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
825     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
826       if(TViewWindow* aViewWnd = dynamic_cast<TViewWindow*>(aViewWindow))
827         if(SVTK_View* aView = aViewWnd->getView())
828           if(!aView->isVisible( io ))
829             return false;
830
831   return true;
832 }
833
834 /*!
835   \Collect objects visible in viewer
836   \param theList - visible objects collection
837 */
838 void SVTK_Viewer::GetVisible( SALOME_ListIO& theList )
839 {
840   // here we collect object if it is disaplaed even one view
841   QVector<SUIT_ViewWindow*> aViews = myViewManager->getViews();
842   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
843     if(SUIT_ViewWindow* aViewWindow = aViews.at(i))
844       if(TViewWindow* aViewWnd = dynamic_cast<TViewWindow*>(aViewWindow))
845         if(SVTK_View* aView = aViewWnd->getView())
846           aView->GetVisible( theList );
847 }
848
849 /*!
850   Updates current viewer
851 */
852 void SVTK_Viewer::Repaint()
853 {
854 //  if (theUpdateTrihedron) onAdjustTrihedron();
855   QVector<SUIT_ViewWindow*> aViews = myViewManager->getViews();
856   for(int i = 0, iEnd = aViews.size(); i < iEnd; i++)
857     if(TViewWindow* aViewWindow = dynamic_cast<TViewWindow*>(aViews.at(i)))
858       if(SVTK_View* aView = aViewWindow->getView())
859         aView->Repaint();
860 }
861  
862
863 void SVTK_Viewer::onActorAdded(VTKViewer_Actor* theActor)
864 {
865   emit actorAdded((SVTK_ViewWindow*)sender(), theActor);
866 }
867
868 void SVTK_Viewer::onActorRemoved(VTKViewer_Actor* theActor)
869 {
870   emit actorRemoved((SVTK_ViewWindow*)sender(), theActor);
871 }
872
873 void SVTK_Viewer::onViewCreated( SUIT_ViewWindow* view) {
874 #ifdef VGL_WORKAROUND
875   if ( SVTK_ViewWindow* svw = dynamic_cast<SVTK_ViewWindow*>( view ) )
876     QTimer::singleShot(500, [svw] () { svw->Repaint(); } );
877 #endif
878 }