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