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