Salome HOME
Fixed bugs on:
[modules/kernel.git] / src / VTKViewer / VTKViewer_ViewFrame.cxx
1 //  SALOME VTKViewer : build VTK viewer into Salome desktop
2 //
3 //  Copyright (C) 2003  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. 
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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : VTKViewer_ViewFrame.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 #include "VTKViewer_ViewFrame.h"
30 #include "VTKViewer_Utilities.h"
31 #include "VTKViewer_Trihedron.h"
32 #include "SALOME_CubeAxesActor2D.h"
33 #include "VTKViewer_RenderWindow.h"
34 #include "VTKViewer_RenderWindowInteractor.h"
35 #include "VTKViewer_InteractorStyleSALOME.h"
36 #include "VTKViewer_Algorithm.h"
37 #include "VTKViewer_Functor.h"
38 #include "VTKViewer_Prs.h"
39
40 #include "SALOME_Actor.h"
41 #include "SALOME_Transform.h"
42 #include "SALOME_TransformFilter.h"
43 #include "SALOME_GeometryFilter.h"
44 #include "SALOMEGUI.h"
45
46 #include "QAD_Settings.h"
47 #include "QAD_Config.h"
48 #include "QAD_Application.h"
49 #include "QAD_Desktop.h"
50 #include "SALOME_Selection.h"
51 #include "SALOME_InteractiveObject.hxx"
52 #include "ToolsGUI.h"
53 #include "SALOMEDS_Tool.hxx"
54
55 #include "utilities.h"
56
57 //QT Include
58 #include <qlayout.h>
59 #include <qcolordialog.h>
60 #include <qfiledialog.h>
61 #include <qapplication.h>
62
63 // VTK Includes
64 #include <vtkActor.h>
65 #include <vtkCamera.h>
66 #include <vtkRenderer.h>
67 #include <vtkTransform.h>
68 #include <vtkActorCollection.h>
69 #include <vtkTextProperty.h>
70
71 #include <TColStd_IndexedMapOfInteger.hxx>
72
73 #ifdef _DEBUG_
74 static int MYDEBUG = 0;
75 #else
76 static int MYDEBUG = 0;
77 #endif
78
79 using namespace std;
80
81 /*!
82     Constructor
83 */
84 VTKViewer_ViewFrame::VTKViewer_ViewFrame(QWidget* parent, const char* name) 
85   :  QAD_ViewFrame(parent, name)
86 {
87   m_ViewUp[0] = 0; m_ViewUp[1] = 0; m_ViewUp[2] = -1;
88   m_ViewNormal[0] = 0; m_ViewNormal[1] = 0; m_ViewNormal[2] = 1;
89   m_Triedron = VTKViewer_Trihedron::New();
90   m_CubeAxes = SALOME_CubeAxesActor2D::New();
91   m_Transform = SALOME_Transform::New();
92   //m_Renderer = VTKViewer_Renderer::New() ;
93   m_Renderer = vtkRenderer::New() ;
94
95   m_Triedron->AddToRender(m_Renderer);
96   m_Renderer->AddProp(m_CubeAxes);
97   InitialSetup();
98 }  
99
100 void VTKViewer_ViewFrame::InitialSetup() {
101   m_RW = new VTKViewer_RenderWindow(this, "RenderWindow");
102   m_RW->getRenderWindow()->AddRenderer(m_Renderer);
103
104   m_Renderer->GetActiveCamera()->ParallelProjectionOn();
105   m_Renderer->LightFollowCameraOn();
106   m_Renderer->TwoSidedLightingOn();
107
108   // Set BackgroundColor
109   QString BgrColorRed   = QAD_CONFIG->getSetting("VTKViewer:BackgroundColorRed");
110   QString BgrColorGreen = QAD_CONFIG->getSetting("VTKViewer:BackgroundColorGreen");
111   QString BgrColorBlue  = QAD_CONFIG->getSetting("VTKViewer:BackgroundColorBlue");
112
113   if( !BgrColorRed.isEmpty() && !BgrColorGreen.isEmpty() && !BgrColorBlue.isEmpty() ) 
114     m_Renderer->SetBackground( BgrColorRed.toInt()/255., BgrColorGreen.toInt()/255., BgrColorBlue.toInt()/255. );
115   else
116     m_Renderer->SetBackground( 0, 0, 0 );
117   
118   // Create an interactor.
119   m_RWInteractor = VTKViewer_RenderWindowInteractor::New();
120   m_RWInteractor->SetRenderWindow(m_RW->getRenderWindow());
121
122   VTKViewer_InteractorStyleSALOME* RWS = VTKViewer_InteractorStyleSALOME::New();
123   m_RWInteractor->SetInteractorStyle(RWS); 
124   RWS->Delete();
125
126   m_RWInteractor->setGUIWindow(m_RW);
127   RWS->setGUIWindow(m_RW);
128
129   m_RWInteractor->Initialize();
130   m_RWInteractor->setViewFrame(this);
131
132   RWS->setViewFrame(this);
133   //SRN: additional initialization, to init CurrentRenderer of vtkInteractorStyle 
134   RWS->FindPokedRenderer(0, 0);
135
136   vtkTextProperty* tprop = vtkTextProperty::New();
137   tprop->SetColor(1, 1, 1);
138   tprop->ShadowOn();
139   
140   float bnd[6];
141   bnd[0] = bnd[2] = bnd[4] = 0;
142   bnd[1] = bnd[3] = bnd[5] = m_Triedron->GetSize();
143   m_CubeAxes->SetLabelFormat("%6.4g");
144   m_CubeAxes->SetBounds(bnd);
145   m_CubeAxes->SetCamera(m_Renderer->GetActiveCamera());
146   m_CubeAxes->SetFlyModeToOuterEdges(); // ENK remarks: it must bee
147   m_CubeAxes->SetFontFactor(0.8);
148   m_CubeAxes->SetAxisTitleTextProperty(tprop);
149   m_CubeAxes->SetAxisLabelTextProperty(tprop);
150   m_CubeAxes->SetCornerOffset(0);
151   m_CubeAxes->SetScaling(0);
152   m_CubeAxes->SetNumberOfLabels(5);
153   m_CubeAxes->VisibilityOff();
154   m_CubeAxes->SetTransform(m_Transform);
155   tprop->Delete();
156   
157   setCentralWidget( m_RW );
158   onViewReset();
159 }
160
161 VTKViewer_ViewFrame::~VTKViewer_ViewFrame() {
162   // In order to ensure that the interactor unregisters
163   // this RenderWindow, we assign a NULL RenderWindow to 
164   // it before deleting it.
165   m_RWInteractor->SetRenderWindow(NULL) ;
166   m_RWInteractor->Delete() ;
167   
168   m_Transform->Delete() ;
169   //m_RW->Delete() ;
170   m_Renderer->RemoveAllProps();
171   // NRI : BugID 1137:  m_Renderer->Delete() ;
172   m_Triedron->Delete();
173
174   m_CubeAxes->Delete();
175   INFOS("VTKViewer_ViewFrame::~VTKViewer_ViewFrame()");
176 }
177
178 /*!
179   Returns widget containing 3D-Viewer
180 */
181 QWidget* VTKViewer_ViewFrame::getViewWidget(){
182   return m_RW;
183 }
184
185 bool VTKViewer_ViewFrame::isTrihedronDisplayed(){
186   return m_Triedron->GetVisibility() == VTKViewer_Trihedron::eOn;
187 }
188
189 bool VTKViewer_ViewFrame::isCubeAxesDisplayed(){
190   return m_CubeAxes->GetVisibility() == 1;
191 }
192
193 double VTKViewer_ViewFrame::GetTrihedronSize() const
194 {
195   return m_Triedron->GetSize();
196 }
197
198 void VTKViewer_ViewFrame::AdjustTrihedrons(const bool forced)
199 {
200   
201   if ( (!isCubeAxesDisplayed() || !isTrihedronDisplayed()) && forced)
202     return;
203
204   float bnd[ 6 ];
205   float newbnd[6];
206   newbnd[ 0 ] = newbnd[ 2 ] = newbnd[ 4 ] = VTK_LARGE_FLOAT;
207   newbnd[ 1 ] = newbnd[ 3 ] = newbnd[ 5 ] = -VTK_LARGE_FLOAT;
208
209   m_CubeAxes->GetBounds(bnd);
210   if(MYDEBUG)
211     cout << "Bounds: BEFORE\n"
212          << "\txMin=" << bnd[ 0 ] << " xMax=" << bnd[ 1 ] << "\n"
213          << "\tyMin=" << bnd[ 2 ] << " yMax=" << bnd[ 3 ] << "\n"
214          << "\tzMin=" << bnd[ 4 ] << " zMax=" << bnd[ 5 ] << "\n";
215
216   int aVisibleNum = m_Triedron->GetVisibleActorCount( m_Renderer );
217   if ( aVisibleNum )
218   {
219     // if the new trihedron size have sufficient difference, then apply the value
220     double aNewSize = 100, anOldSize=m_Triedron->GetSize();
221     bool aTDisplayed = isTrihedronDisplayed();
222     bool aCDisplayed = isCubeAxesDisplayed();
223     if(aTDisplayed) m_Triedron->VisibilityOff();
224     if(aCDisplayed) m_CubeAxes->VisibilityOff();
225     static float aSizeInPercents = 105;
226     QString aSetting = QAD_CONFIG->getSetting( "Viewer:TrihedronSize" );
227     if ( !aSetting.isEmpty() )
228       aSizeInPercents = aSetting.toFloat();
229     bool isComputeTrihedronSize = ::ComputeTrihedronSize(m_Renderer, aNewSize, anOldSize, aSizeInPercents);
230     
231     m_Triedron->SetSize( aNewSize );
232     // itearte throuh displayed objects and set size if necessary
233     
234     vtkActorCollection* anActors = getRenderer()->GetActors();
235     anActors->InitTraversal();
236     while( vtkActor* anActor = anActors->GetNextActor() )
237       {
238         if( SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast( anActor ) )
239           {
240             if ( aSActor->IsResizable() )
241               aSActor->SetSize( 0.5 * aNewSize );
242             if( aSActor->GetVisibility() && !aSActor->IsInfinitive()){
243               float *abounds = aSActor->GetBounds();
244               if(MYDEBUG)
245                 cout << "Bounds: Actor="<<aSActor<<"\n"
246                      << "\txMin=" << abounds[ 0 ] << " xMax=" << abounds[ 1 ] << "\n"
247                      << "\tyMin=" << abounds[ 2 ] << " yMax=" << abounds[ 3 ] << "\n"
248                      << "\tzMin=" << abounds[ 4 ] << " zMax=" << abounds[ 5 ] << "\n";
249               if (  abounds[0] > -VTK_LARGE_FLOAT && abounds[1] < VTK_LARGE_FLOAT &&
250                     abounds[2] > -VTK_LARGE_FLOAT && abounds[3] < VTK_LARGE_FLOAT &&
251                     abounds[4] > -VTK_LARGE_FLOAT && abounds[5] < VTK_LARGE_FLOAT)
252                 for(int i=0;i<5;i=i+2){
253                   if(abounds[i]<newbnd[i]) newbnd[i]=abounds[i];
254                   if(abounds[i+1]>newbnd[i+1]) newbnd[i+1]=abounds[i+1];
255                   
256                 }
257             }
258           }
259       }
260     if(aTDisplayed) m_Triedron->VisibilityOn();
261     if(aCDisplayed) m_CubeAxes->VisibilityOn();
262     
263   } else {
264      double aSize = m_Triedron->GetSize();
265      newbnd[0] = newbnd[2] = newbnd[4] = 0;
266      newbnd[1] = newbnd[3] = newbnd[5] = aSize;
267   }
268   
269   if( newbnd[0]<VTK_LARGE_FLOAT && newbnd[2]<VTK_LARGE_FLOAT && newbnd[4]<VTK_LARGE_FLOAT &&
270       newbnd[1]>-VTK_LARGE_FLOAT && newbnd[3]>-VTK_LARGE_FLOAT && newbnd[5]>-VTK_LARGE_FLOAT){
271     for(int i=0;i<6;i++) bnd[i] = newbnd[i];
272     m_CubeAxes->SetBounds(bnd);
273   }
274   if(MYDEBUG)
275     cout << "Bounds AFTER: VisibleActors="<<aVisibleNum<<"\n"
276          << "\txMin=" << bnd[ 0 ] << " xMax=" << bnd[ 1 ] << "\n"
277          << "\tyMin=" << bnd[ 2 ] << " yMax=" << bnd[ 3 ] << "\n"
278          << "\tzMin=" << bnd[ 4 ] << " zMax=" << bnd[ 5 ] << "\n";
279   
280   m_CubeAxes->SetBounds(bnd);
281
282   ::ResetCameraClippingRange(m_Renderer);
283 }
284
285 void VTKViewer_ViewFrame::onAdjustTrihedron()
286 {
287   AdjustTrihedrons(false);
288 }
289
290 void VTKViewer_ViewFrame::onAdjustCubeAxes()
291 {   
292   AdjustTrihedrons(false);
293 }
294
295 /*!
296   Display/hide Trihedron
297 */
298 void VTKViewer_ViewFrame::onViewTrihedron(){
299   if(!m_Triedron) return;
300   if(isTrihedronDisplayed()) m_Triedron->VisibilityOff();
301   else m_Triedron->VisibilityOn();
302   Repaint();
303 }
304
305 void VTKViewer_ViewFrame::onViewCubeAxes(){
306   if(!m_CubeAxes) return;
307   if(isCubeAxesDisplayed()) m_CubeAxes->VisibilityOff();
308   else m_CubeAxes->VisibilityOn();
309   Repaint();
310 }
311
312
313 /*!
314   Provides top projection of the active view
315 */
316 void VTKViewer_ViewFrame::onViewTop(){
317   vtkCamera* camera = m_Renderer->GetActiveCamera();
318   camera->SetPosition(0,0,1);
319   camera->SetViewUp(0,1,0);
320   camera->SetFocalPoint(0,0,0);
321   onViewFitAll();
322 }
323
324 /*!
325   Provides bottom projection of the active view
326 */
327 void VTKViewer_ViewFrame::onViewBottom(){
328   vtkCamera* camera = m_Renderer->GetActiveCamera();
329   camera->SetPosition(0,0,-1);
330   camera->SetViewUp(0,1,0);
331   camera->SetFocalPoint(0,0,0);
332   onViewFitAll();
333 }
334
335 /*!
336   Provides left projection of the active view
337 */
338 void VTKViewer_ViewFrame::onViewLeft(){
339   vtkCamera* camera = m_Renderer->GetActiveCamera(); 
340   camera->SetPosition(0,-1,0);
341   camera->SetViewUp(0,0,1);
342   camera->SetFocalPoint(0,0,0);
343   onViewFitAll();
344 }
345
346 /*!
347   Provides right projection of the active view
348 */
349 void VTKViewer_ViewFrame::onViewRight(){
350   vtkCamera* camera = m_Renderer->GetActiveCamera();
351   camera->SetPosition(0,1,0);
352   camera->SetViewUp(0,0,1);
353   camera->SetFocalPoint(0,0,0);
354   onViewFitAll();
355 }
356
357 /*!
358   Provides back projection of the active view
359 */
360 void VTKViewer_ViewFrame::onViewBack(){
361   vtkCamera* camera = m_Renderer->GetActiveCamera();
362   camera->SetPosition(-1,0,0);
363   camera->SetViewUp(0,0,1);
364   camera->SetFocalPoint(0,0,0);
365   onViewFitAll();
366 }
367
368 /*!
369   Provides front projection of the active view
370 */
371 void VTKViewer_ViewFrame::onViewFront(){
372   vtkCamera* camera = m_Renderer->GetActiveCamera();
373   camera->SetPosition(1,0,0);
374   camera->SetViewUp(0,0,1);
375   camera->SetFocalPoint(0,0,0);
376   onViewFitAll();
377 }
378
379 /*!
380   Fits all objects in the active view
381 */
382 void VTKViewer_ViewFrame::onViewFitAll(){
383
384   int aTriedronWasVisible = false;
385   int aCubeAxesWasVisible = false;
386   if(m_Triedron){
387     aTriedronWasVisible = isTrihedronDisplayed();
388     if(aTriedronWasVisible) m_Triedron->VisibilityOff();
389   }
390   if(m_CubeAxes){
391     aCubeAxesWasVisible = isCubeAxesDisplayed();
392     if(aCubeAxesWasVisible) m_CubeAxes->VisibilityOff();
393   }
394
395   if(m_Triedron->GetVisibleActorCount(m_Renderer)){
396     m_Triedron->VisibilityOff();
397     m_CubeAxes->VisibilityOff();
398     ::ResetCamera(m_Renderer);
399   }else{
400     m_Triedron->SetVisibility(VTKViewer_Trihedron::eOnlyLineOn);
401     m_CubeAxes->SetVisibility(2);
402     ::ResetCamera(m_Renderer,true);
403   }
404   if(aTriedronWasVisible) m_Triedron->VisibilityOn();
405   else m_Triedron->VisibilityOff();
406   if(aCubeAxesWasVisible) m_CubeAxes->VisibilityOn();
407   else m_CubeAxes->VisibilityOff();
408   ::ResetCameraClippingRange(m_Renderer);
409
410   Repaint();
411 }
412
413 /*!
414   Reset the active view
415 */
416 void VTKViewer_ViewFrame::onViewReset(){
417   int aTriedronIsVisible = isTrihedronDisplayed();
418   int aCubeAxesIsVisible = isCubeAxesDisplayed();
419   
420   m_Triedron->SetVisibility(VTKViewer_Trihedron::eOnlyLineOn);
421   m_CubeAxes->SetVisibility(0);
422   
423   ::ResetCamera(m_Renderer,true);  
424   vtkCamera* aCamera = m_Renderer->GetActiveCamera();
425   aCamera->SetPosition(1,-1,1);
426   aCamera->SetViewUp(0,0,1);
427   ::ResetCamera(m_Renderer,true);  
428   
429   if(aTriedronIsVisible) m_Triedron->VisibilityOn();
430   else m_Triedron->VisibilityOff();
431   
432   if(aCubeAxesIsVisible) m_CubeAxes->VisibilityOn();
433   else m_CubeAxes->VisibilityOff();
434
435   static float aCoeff = 3.0;
436   aCamera->SetParallelScale(aCoeff*aCamera->GetParallelScale());
437   Repaint();
438 }
439
440 /*!
441   Rotates the active view
442 */
443 void VTKViewer_ViewFrame::onViewRotate(){
444   m_RWInteractor->GetInteractorStyleSALOME()->startRotate();
445 }
446
447 /*!
448   Sets a new center of the active view
449 */
450 void VTKViewer_ViewFrame::onViewGlobalPan(){
451   if(m_Triedron->GetVisibleActorCount(m_Renderer))
452     m_RWInteractor->GetInteractorStyleSALOME()->startGlobalPan();
453 }
454
455 /*!
456   Zooms the active view
457 */
458 void VTKViewer_ViewFrame::onViewZoom(){
459   m_RWInteractor->GetInteractorStyleSALOME()->startZoom();
460 }
461
462 /*!
463   Moves the active view
464 */
465 void VTKViewer_ViewFrame::onViewPan(){
466   m_RWInteractor->GetInteractorStyleSALOME()->startPan();
467 }
468
469 /*!
470   Fits all obejcts within a rectangular area of the active view
471 */
472 void VTKViewer_ViewFrame::onViewFitArea(){
473   m_RWInteractor->GetInteractorStyleSALOME()->startFitArea();
474 }
475
476 /*!
477     Set background of the viewport
478 */
479 void VTKViewer_ViewFrame::setBackgroundColor( const QColor& color)
480 {
481   if ( m_Renderer )
482     m_Renderer->SetBackground( color.red()/255., color.green()/255., color.blue()/255. );
483 }
484
485 /*!
486     Returns background of the viewport
487 */
488 QColor VTKViewer_ViewFrame::backgroundColor() const
489 {
490   float backint[3];
491   if ( m_Renderer ) {
492     m_Renderer->GetBackground(backint);
493     return QColorDialog::getColor ( QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255)), NULL );
494   }
495   return QMainWindow::backgroundColor();
496 }
497
498
499 void VTKViewer_ViewFrame::SetSelectionMode( Selection_Mode mode )
500 {
501   m_RWInteractor->SetSelectionMode( mode );
502 }
503
504 void VTKViewer_ViewFrame::rename( const Handle(SALOME_InteractiveObject)& IObject, QString newName )
505 {
506   m_RWInteractor->rename(IObject, newName);
507 }
508
509 void VTKViewer_ViewFrame::unHighlightAll() 
510 {
511   m_RWInteractor->unHighlightAll();
512 }
513
514 void VTKViewer_ViewFrame::highlight( const Handle(SALOME_InteractiveObject)& IObject, 
515                                      bool highlight, 
516                                      bool update ) 
517 {
518   QAD_Study* ActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
519   SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
520   m_RWInteractor->highlight(IObject, highlight, update);
521
522   if(Sel->HasIndex(IObject) && IObject->hasEntry()){
523     TColStd_IndexedMapOfInteger MapIndex;
524     Sel->GetIndex(IObject,MapIndex);
525     using namespace SALOME::VTK;
526     const char* anEntry = IObject->getEntry();
527     vtkActorCollection* aCollection = getRenderer()->GetActors();
528     if(SALOME_Actor* anActor = Find<SALOME_Actor>(aCollection,TIsSameEntry<SALOME_Actor>(anEntry))){
529       switch (Sel->SelectionMode()) {
530       case NodeSelection:
531         m_RWInteractor->highlightPoint(MapIndex,anActor,highlight,update);
532         break;
533       case EdgeOfCellSelection:
534         m_RWInteractor->highlightEdge(MapIndex,anActor,highlight,update);
535         break;
536       case CellSelection:
537       case EdgeSelection:
538       case FaceSelection:
539       case VolumeSelection:
540         m_RWInteractor->highlightCell(MapIndex,anActor,highlight,update);
541         break;
542       }
543     }
544   }else{
545     m_RWInteractor->unHighlightSubSelection();
546   }
547 }
548
549 bool VTKViewer_ViewFrame::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
550 {
551   return m_RWInteractor->isInViewer( IObject );
552 }
553
554 bool VTKViewer_ViewFrame::isVisible( const Handle(SALOME_InteractiveObject)& IObject ) 
555 {
556   return m_RWInteractor->isVisible( IObject );
557 }
558
559 void VTKViewer_ViewFrame::setPopupServer( QAD_Application* App )
560 {
561   m_RW->setPopupServer( App );
562 }
563
564 /* selection */
565 Handle(SALOME_InteractiveObject) VTKViewer_ViewFrame::FindIObject(const char* theEntry)
566 {
567   using namespace SALOME::VTK;
568   SALOME_Actor* anActor = 
569     Find<SALOME_Actor>(getRenderer()->GetActors(),
570                        TIsSameEntry<SALOME_Actor>(theEntry));
571   if(anActor)
572     return anActor->getIO();
573
574   return Handle(SALOME_InteractiveObject)();
575 }
576
577 /* display */           
578 void VTKViewer_ViewFrame::Display(const Handle(SALOME_InteractiveObject)& theIObject, bool update)
579 {
580   QAD_Study*          aQADStudy = QAD_Application::getDesktop()->getActiveStudy();
581   SALOME_Selection*   aSel      = SALOME_Selection::Selection( aQADStudy->getSelection() );
582   SALOMEDS::Study_var aStudy    = aQADStudy->getStudyDocument();
583
584   m_RWInteractor->Display(theIObject,false);
585   ToolsGUI::SetVisibility( aStudy, theIObject->getEntry(), true, this );
586   aSel->AddIObject(theIObject,false);
587
588   if(update)
589     Repaint();
590 }
591
592
593 struct TDisplayAction{
594   SALOME_Selection* mySel;
595   Handle(SALOME_InteractiveObject) myIO;
596   TDisplayAction(SALOME_Selection* theSel,
597                  Handle(SALOME_InteractiveObject) theIO): 
598     mySel(theSel), myIO(theIO)
599   {}
600   void operator()(SALOME_Actor* theActor){
601     theActor->SetVisibility(true);
602     mySel->AddIObject(myIO,false);
603   }
604 };
605
606 void VTKViewer_ViewFrame::DisplayOnly(const Handle(SALOME_InteractiveObject)& theIObject)
607 {
608   QAD_Study* aStudy = QAD_Application::getDesktop()->getActiveStudy();
609   SALOME_Selection* aSel = SALOME_Selection::Selection(aStudy->getSelection());
610
611   aSel->ClearIObjects();
612   //m_RWInteractor->EraseAll();
613   EraseAll();
614
615   using namespace SALOME::VTK;
616   ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
617                           TIsSameIObject<SALOME_Actor>(theIObject),
618                           TDisplayAction(aSel,theIObject));
619
620   ToolsGUI::SetVisibility(
621     aStudy->getStudyDocument(), theIObject->getEntry(), true, this );
622
623   Repaint();
624 }
625
626
627 struct TEraseAction: TDisplayAction{
628   VTKViewer_RenderWindowInteractor* myRWInteractor;
629   TEraseAction(SALOME_Selection* theSel,
630                Handle(SALOME_InteractiveObject) theIO,
631                VTKViewer_RenderWindowInteractor* theRWInteractor): 
632     TDisplayAction(theSel,theIO),
633     myRWInteractor(theRWInteractor)
634   {}
635   void operator()(SALOME_Actor* theActor){
636     myRWInteractor->Erase(myIO,false);
637     mySel->RemoveIObject(myIO,false);
638   }
639 };
640
641 void VTKViewer_ViewFrame::Erase(const Handle(SALOME_InteractiveObject)& theIObject, bool update)
642 {
643   QAD_Study* aStudy = QAD_Application::getDesktop()->getActiveStudy();
644   SALOME_Selection* aSel = SALOME_Selection::Selection(aStudy->getSelection());
645
646   using namespace SALOME::VTK;
647   ForEachIf<SALOME_Actor>(getRenderer()->GetActors(),
648                           TIsSameIObject<SALOME_Actor>(theIObject),
649                           TEraseAction(aSel,theIObject,m_RWInteractor));
650
651   ToolsGUI::SetVisibility(
652     aStudy->getStudyDocument(), theIObject->getEntry(), false, this );
653
654   if(update)
655     Repaint();
656 }
657
658
659 void VTKViewer_ViewFrame::DisplayAll()
660 {
661   m_RWInteractor->DisplayAll();
662
663   // update flag of visibility
664   QAD_Study*               aQADStudy  = QAD_Application::getDesktop()->getActiveStudy();
665   SALOMEDS::Study_var      aStudy     = aQADStudy->getStudyDocument();
666   QAD_Desktop*             aDesktop   = QAD_Application::getDesktop();
667   const QString&           aCompName  = aDesktop->getComponentDataType();
668   SALOMEDS::SObject_var    aComponent =
669     SALOMEDS::SObject::_narrow( aStudy->FindComponent ( aCompName.latin1() ) );
670     
671   std::list<SALOMEDS::SObject_var> aList;
672   SALOMEDS_Tool::GetAllChildren( aStudy, aComponent, aList );
673
674   std::list<SALOMEDS::SObject_var>::iterator anIter = aList.begin();
675   for ( ; anIter != aList.end(); ++anIter )
676     ToolsGUI::SetVisibility( aStudy, (*anIter)->GetID(), true, this );
677 }
678
679
680 void VTKViewer_ViewFrame::EraseAll()
681 {
682   m_RWInteractor->EraseAll();
683
684   // update flag of visibility
685   QAD_Study*               aQADStudy  = QAD_Application::getDesktop()->getActiveStudy();
686   SALOMEDS::Study_var      aStudy     = aQADStudy->getStudyDocument();
687   QAD_Desktop*             aDesktop   = QAD_Application::getDesktop();
688   const QString&           aCompName  = aDesktop->getComponentDataType();
689   SALOMEDS::SObject_var    aComponent =
690     SALOMEDS::SObject::_narrow( aStudy->FindComponent ( aCompName.latin1() ) );
691
692   std::list<SALOMEDS::SObject_var> aList;
693   SALOMEDS_Tool::GetAllChildren( aStudy, aComponent, aList );
694
695   std::list<SALOMEDS::SObject_var>::iterator anIter = aList.begin();
696   for ( ; anIter != aList.end(); ++anIter )
697     ToolsGUI::SetVisibility( aStudy, (*anIter)->GetID(), false, this );
698 }
699
700
701 void VTKViewer_ViewFrame::Repaint(bool theUpdateTrihedron)
702 {
703   if (theUpdateTrihedron) onAdjustTrihedron();
704   //if (theUpdateCubeAxes) onAdjustCubeAxes();
705   m_RW->update();
706 }
707
708 void VTKViewer_ViewFrame::GetScale(double theScale[3]){
709   m_Transform->GetMatrixScale(theScale);
710 }
711
712 void VTKViewer_ViewFrame::SetScale(double theScale[3]){
713   m_Transform->SetMatrixScale(theScale[0], theScale[1], theScale[2]);
714   m_RWInteractor->Render();
715   Repaint();
716 }
717
718 void VTKViewer_ViewFrame::InsertActor( SALOME_Actor* theActor, bool theMoveInternalActors ){
719   theActor->AddToRender(m_Renderer);
720   theActor->SetTransform(m_Transform);
721   if(theMoveInternalActors) 
722     m_RWInteractor->MoveInternalActors();
723 }
724
725 void VTKViewer_ViewFrame::AddActor( SALOME_Actor* theActor, bool theUpdate /*=false*/ ){
726   InsertActor(theActor);
727   if(theUpdate) 
728     Repaint();
729 }
730
731 void VTKViewer_ViewFrame::RemoveActor( SALOME_Actor* theActor, bool theUpdate /*=false*/ ){
732   theActor->RemoveFromRender(m_Renderer);
733   if(theUpdate) 
734     Repaint();
735 }
736
737 void VTKViewer_ViewFrame::MoveActor(SALOME_Actor* theActor)
738 {
739   RemoveActor(theActor);
740   InsertActor(theActor,true);
741 }
742
743 //==========================================================
744 /*!
745  *  VTKViewer_ViewFrame::Display
746  *  Display presentation
747  */
748 //==========================================================
749 void VTKViewer_ViewFrame::Display( const SALOME_VTKPrs* prs )
750 {
751   // try do downcast object
752   const VTKViewer_Prs* aVTKPrs = dynamic_cast<const VTKViewer_Prs*>( prs );
753   if ( !aVTKPrs || aVTKPrs->IsNull() )
754     return;
755
756   vtkActorCollection* actors = aVTKPrs->GetObjects();
757   if ( !actors )
758     return;
759
760   actors->InitTraversal();
761   vtkActor* actor;
762   while( ( actor = actors->GetNextActor() ) )
763   {
764     SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor );
765     if ( salomeActor )                      
766     {
767       // just display the object
768       m_RWInteractor->Display( salomeActor, false );
769       
770       // Set visibility flag
771       Handle(SALOME_InteractiveObject) anObj = salomeActor->getIO();
772       if ( !anObj.IsNull() && anObj->hasEntry() )
773       {
774         SALOMEDS::Study_var aStudy =
775           QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
776         ToolsGUI::SetVisibility( aStudy, anObj->getEntry(), true, this );
777       }
778
779       if ( salomeActor->IsSetCamera() )
780         salomeActor->SetCamera( getRenderer()->GetActiveCamera() );
781     }
782   }
783 }
784
785 //==========================================================
786 /*!
787  *  VTKViewer_ViewFrame::Erase
788  *  Erase presentation
789  */
790 //==========================================================
791 void VTKViewer_ViewFrame::Erase( const SALOME_VTKPrs* prs, const bool forced )
792 {
793   // try do downcast object
794   const VTKViewer_Prs* aVTKPrs = dynamic_cast<const VTKViewer_Prs*>( prs );
795   if ( !aVTKPrs || aVTKPrs->IsNull() )
796     return;
797
798   vtkActorCollection* actors = aVTKPrs->GetObjects();
799   if ( !actors )
800     return;
801
802   SALOMEDS::Study_var aStudy =
803     QAD_Application::getDesktop()->getActiveStudy()->getStudyDocument();
804
805   actors->InitTraversal();
806   vtkActor* actor;
807   while( ( actor = actors->GetNextActor() ) ) {
808     SALOME_Actor* salomeActor = SALOME_Actor::SafeDownCast( actor );
809     if ( salomeActor ) {
810       // just erase the object
811       if ( forced )
812         m_RWInteractor->Remove( salomeActor, false );
813       else
814         m_RWInteractor->Erase( salomeActor, forced );
815
816       // Set visibility flag if necessary
817       if ( !forced )
818       {
819         Handle(SALOME_InteractiveObject) anObj = salomeActor->getIO();
820         if ( !anObj.IsNull() && anObj->hasEntry() )
821           ToolsGUI::SetVisibility( aStudy, anObj->getEntry(), true, this );
822       }
823     }
824   }
825 }
826   
827 //==========================================================
828 /*!
829  *  VTKViewer_ViewFrame::CreatePrs
830  *  Create presentation by entry
831  */
832 //==========================================================
833 SALOME_Prs* VTKViewer_ViewFrame::CreatePrs( const char* entry )
834 {
835   VTKViewer_Prs* prs = new VTKViewer_Prs();
836   if ( entry ) {
837     vtkActorCollection* theActors = m_Renderer->GetActors();
838     theActors->InitTraversal();
839     vtkActor* ac;
840     while( ( ac = theActors->GetNextActor() ) ) {
841       SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
842       if ( anActor && anActor->hasIO() && !strcmp( anActor->getIO()->getEntry(), entry ) ) {
843         prs->AddObject( ac );
844       }
845     }
846   }
847   return prs;
848 }
849
850 //==========================================================
851 /*!
852  *  VTKViewer_ViewFrame::BeforeDisplay
853  *  Axiluary method called before displaying of objects
854  */
855 //==========================================================
856 void  VTKViewer_ViewFrame::BeforeDisplay( SALOME_Displayer* d )
857 {
858   d->BeforeDisplay( this, SALOME_VTKViewType() );
859 }
860
861 //==========================================================
862 /*!
863  *  VTKViewer_ViewFrame::AfterDisplay
864  *  Axiluary method called after displaying of objects
865  */
866 //==========================================================
867 void  VTKViewer_ViewFrame::AfterDisplay( SALOME_Displayer* d )
868 {
869   d->AfterDisplay( this, SALOME_VTKViewType() );
870 }
871
872 //==========================================================
873 /*!
874  *  VTKViewer_ViewFrame::undo
875  *  Redisplay all objects in viewer
876  */
877 //==========================================================
878 void VTKViewer_ViewFrame::undo( QAD_Study* theQADStudy, const char* /*StudyFrameEntry*/ )
879 {
880   redisplayAll( theQADStudy, true );
881 }
882
883 //==========================================================
884 /*!
885  *  VTKViewer_ViewFrame::redo
886  *  Redisplay all objects in viewer
887  */
888 //==========================================================
889 void VTKViewer_ViewFrame::redo( QAD_Study* theQADStudy, const char* /*StudyFrameEntry*/ )
890 {
891   redisplayAll( theQADStudy, true );
892 }
893
894 //==========================================================
895 /*!
896  *  VTKViewer_ViewFrame::redisplayAll
897  *  Redisplay all objects in viewer
898  */
899 //==========================================================
900 void VTKViewer_ViewFrame::redisplayAll( QAD_Study* theQADStudy, const bool theToUpdate )
901 {
902   SALOMEDS::Study_var      aStudy     = theQADStudy->getStudyDocument();
903   SALOME_Selection*        aSel       = SALOME_Selection::Selection( theQADStudy->getSelection() );
904   QAD_Desktop*             aDesktop   = QAD_Application::getDesktop();
905   SALOMEGUI*               aGUI       = aDesktop->getActiveGUI();
906   const QString&           aCompName  = aDesktop->getComponentDataType();
907   SALOMEDS::SObject_var    aComponent =
908     SALOMEDS::SObject::_narrow( aStudy->FindComponent ( aCompName.latin1() ) );
909
910   if ( aComponent->_is_nil() )
911     return;
912
913   m_RWInteractor->RemoveAll( false );
914   //m_RWInteractor->EraseAll();
915
916   aSel->ClearIObjects();
917
918   /*  
919    //   ENK commented, already added to renderer in 
920    //   VTKViewer_ViewFrame::VTKViewer_ViewFrame(...)
921   if ( isTrhDisplayed )
922       m_Triedron->AddToRender( m_Renderer );
923   */
924
925   std::list<SALOMEDS::SObject_var> aList;
926   SALOMEDS_Tool::GetAllChildren( aStudy, aComponent, aList );
927
928   std::list<SALOMEDS::SObject_var>::iterator anIter = aList.begin();
929   for ( ; anIter != aList.end(); ++anIter )
930   {
931     SALOMEDS::SObject_var anObj = (*anIter);
932     if ( ToolsGUI::GetVisibility( aStudy, anObj, this ) )
933     {
934       Handle(SALOME_InteractiveObject) anIObj = new SALOME_InteractiveObject();
935       anIObj->setEntry( anObj->GetID() );
936       aGUI->BuildPresentation( anIObj, this );
937     }
938     
939   }
940
941   if ( theToUpdate )
942     Repaint();
943 }
944
945 #define INCREMENT_FOR_OP 10
946
947 //=======================================================================
948 // name    : onPanLeft
949 // Purpose : Performs incremental panning to the left
950 //=======================================================================
951 void VTKViewer_ViewFrame::onPanLeft()
952 {
953   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalPan( -INCREMENT_FOR_OP, 0 );
954 }
955
956 //=======================================================================
957 // name    : onPanRight
958 // Purpose : Performs incremental panning to the right
959 //=======================================================================
960 void VTKViewer_ViewFrame::onPanRight()
961 {
962   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalPan( INCREMENT_FOR_OP, 0 );
963 }
964
965 //=======================================================================
966 // name    : onPanUp
967 // Purpose : Performs incremental panning to the top
968 //=======================================================================
969 void VTKViewer_ViewFrame::onPanUp()
970 {
971   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalPan( 0, INCREMENT_FOR_OP );
972 }
973
974 //=======================================================================
975 // name    : onPanDown
976 // Purpose : Performs incremental panning to the bottom
977 //=======================================================================
978 void VTKViewer_ViewFrame::onPanDown()
979 {
980   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalPan( 0, -INCREMENT_FOR_OP );
981 }
982
983 //=======================================================================
984 // name    : onZoomIn
985 // Purpose : Performs incremental zooming in
986 //=======================================================================
987 void VTKViewer_ViewFrame::onZoomIn()
988 {
989   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalZoom( INCREMENT_FOR_OP );
990 }
991
992 //=======================================================================
993 // name    : onZoomOut
994 // Purpose : Performs incremental zooming out
995 //=======================================================================
996 void VTKViewer_ViewFrame::onZoomOut()
997 {
998   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalZoom( -INCREMENT_FOR_OP );
999 }
1000
1001 //=======================================================================
1002 // name    : onRotateLeft
1003 // Purpose : Performs incremental rotating to the left
1004 //=======================================================================
1005 void VTKViewer_ViewFrame::onRotateLeft()
1006 {
1007   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalRotate( -INCREMENT_FOR_OP, 0 );
1008 }
1009
1010 //=======================================================================
1011 // name    : onRotateRight
1012 // Purpose : Performs incremental rotating to the right
1013 //=======================================================================
1014 void VTKViewer_ViewFrame::onRotateRight()
1015 {
1016   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalRotate( INCREMENT_FOR_OP, 0 );
1017 }
1018
1019 //=======================================================================
1020 // name    : onRotateUp
1021 // Purpose : Performs incremental rotating to the top
1022 //=======================================================================
1023 void VTKViewer_ViewFrame::onRotateUp()
1024 {
1025   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalRotate( 0, -INCREMENT_FOR_OP );
1026 }
1027
1028 //=======================================================================
1029 // name    : onRotateDown
1030 // Purpose : Performs incremental rotating to the bottom
1031 //=======================================================================
1032 void VTKViewer_ViewFrame::onRotateDown()
1033 {
1034   m_RWInteractor->GetInteractorStyleSALOME()->IncrementalRotate( 0, INCREMENT_FOR_OP );
1035 }