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