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