]> SALOME platform Git repositories - modules/yacs.git/blob - src/VTKViewer/VTKViewer_ViewFrame.cxx
Salome HOME
sources v1.2
[modules/yacs.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 using namespace std;
30 #include "VTKViewer_ViewFrame.h"
31 #include "VTKViewer_RenderWindow.h"
32 //#include "VTKViewer_InteractorStyleSALOME.h"
33
34 #include "QAD_Settings.h"
35 #include "QAD_Config.h"
36 #include "QAD_Application.h"
37 #include "QAD_Desktop.h"
38 #include "SALOME_Selection.h"
39 #include "SALOME_InteractiveObject.hxx"
40 #include "VTKViewer_InteractorStyleSALOME.h"
41
42 #include "utilities.h"
43
44 //QT Include
45 #include <qlayout.h>
46 #include <qcolordialog.h>
47 #include <qfiledialog.h>
48 #include <qapplication.h>
49
50 // VTK Includes
51 #include <vtkActor.h>
52 #include <vtkRenderer.h>
53 #include <vtkTransform.h>
54 #include <vtkPolyDataMapper.h> 
55
56 #include <vtkMath.h>
57 #include <vtkTextSource.h>
58 #include <vtkLine.h>
59 #include <vtkConeSource.h>
60 #include <vtkTextMapper.h>
61 #include <vtkMapper2D.h>
62 #include <vtkActor2D.h>
63 #include <vtkWindowToImageFilter.h>
64 #include <vtkTIFFWriter.h>
65 #include <vtkVectorText.h>
66 #include <vtkFollower.h>
67
68 /*!
69     Constructor
70 */
71 VTKViewer_ViewFrame::VTKViewer_ViewFrame(QWidget* parent, const char* name) 
72   :  QAD_ViewFrame(parent, name)
73 {
74   m_ViewUp[0] = 0; m_ViewUp[1] = 0; m_ViewUp[2] = -1;
75   m_ViewNormal[0] = 0; m_ViewNormal[1] = 0; m_ViewNormal[2] = 1;
76   m_Transform = SALOME_Transform::New();
77
78   //  m_InitialSetupDone = false ;
79   InitialSetup();
80 }
81
82
83 vtkFollower* CreateTextActor(char *text, float aSize) {
84   vtkVectorText* aTxt = vtkVectorText::New();
85   aTxt->SetText(text);
86   vtkPolyDataMapper* textMapper = vtkPolyDataMapper::New();
87   textMapper->SetInput(aTxt->GetOutput());
88   vtkFollower* textActor = vtkFollower::New();
89   textActor->SetMapper(textMapper);
90   float aScale = 6 * aSize/100;
91   textActor->SetScale(aScale, aScale, aScale);
92   return textActor;
93 }
94
95 void VTKViewer_ViewFrame::AddVector(float* o,float* p,vtkRenderer* renderer, float aSize) {
96   vtkPoints* myPoints = vtkPoints::New();
97   vtkLine* myLine = vtkLine::New();
98
99   myPoints->InsertNextPoint(o);
100   myPoints->InsertNextPoint(p);
101
102   (myLine->GetPointIds())->InsertNextId(0);
103   (myLine->GetPointIds())->InsertNextId(1);
104
105   vtkActor* lineActor = vtkActor::New();
106
107   vtkCellArray* cell = vtkCellArray::New();
108
109   cell->InsertNextCell(myLine);
110
111   vtkPolyData* output = vtkPolyData::New();
112   
113   output->SetPoints(myPoints);
114   output->SetLines(cell);
115  
116   vtkPolyDataMapper* mapper = vtkPolyDataMapper::New();
117
118   mapper->SetInput(output);
119
120   lineActor->SetMapper(mapper);
121
122   // Create CONE
123
124   vtkConeSource* acone =  vtkConeSource::New();
125
126   float dim = aSize;
127
128   acone->SetResolution(2);
129   //  acone->SetAngle(70);
130   acone->SetRadius(0.02*dim);
131   acone->SetHeight(0.08*dim);
132
133   vtkActor* coneActor = vtkActor::New();
134  
135   vtkPolyDataMapper* coneMapper = vtkPolyDataMapper::New();
136   coneMapper->SetInput(acone->GetOutput());
137
138   coneActor->SetMapper(coneMapper);
139   float rot[3];
140   rot[0]=0; rot[1]=0; rot[2]=0;
141
142   vtkFollower* aTextActor;
143
144   coneActor->AddPosition(p);
145   if(p[0]!=0) {
146     // x
147     aTextActor = CreateTextActor("X", dim);
148   } else if(p[1]!=0) {
149     // y
150     rot[2]=90;
151     coneActor->AddOrientation(rot);
152     aTextActor = CreateTextActor("Y", dim);
153   } else if(p[2]!=0) {
154     // z
155     rot[1]=-90;
156     coneActor->AddOrientation(rot);
157     aTextActor = CreateTextActor("Z", dim);
158   }
159   aTextActor->AddPosition(p);
160   aTextActor->SetCamera(renderer->GetActiveCamera());
161
162   coneActor->GetProperty()->SetInterpolation(1);
163   coneActor->GetProperty()->SetRepresentationToSurface();
164   coneActor->GetProperty()->SetAmbient(1);
165   coneActor->GetProperty()->SetAmbientColor(1,1,1);
166   coneActor->GetProperty()->SetDiffuseColor(0.7,0.7,0.7);
167   coneActor->GetProperty()->SetSpecularColor(0.7,0.7,0.7);
168
169   lineActor->GetProperty()->SetInterpolation(1);
170   lineActor->GetProperty()->SetRepresentationToSurface();
171   lineActor->GetProperty()->SetAmbient(1);
172   lineActor->GetProperty()->SetAmbientColor(1,1,1);
173   lineActor->GetProperty()->SetDiffuseColor(0.7,0.7,0.7);
174   lineActor->GetProperty()->SetSpecularColor(0.7,0.7,0.7);
175
176   aTextActor->GetProperty()->SetAmbient(1);
177   aTextActor->GetProperty()->SetAmbientColor(1,1,1);
178   aTextActor->GetProperty()->SetDiffuseColor(0.7,0.7,0.7);
179   aTextActor->GetProperty()->SetSpecularColor(0.7,0.7,0.7);
180      
181   coneActor->PickableOff();
182   lineActor->PickableOff();
183   aTextActor->PickableOff();
184   
185   m_Triedron->AddItem(coneActor);
186   m_Triedron->AddItem(lineActor);
187   m_Triedron->AddItem(aTextActor);
188
189   renderer->AddActor(coneActor);
190   renderer->AddActor(lineActor);
191   renderer->AddActor(aTextActor);
192 }  
193
194 bool VTKViewer_ViewFrame::isTrihedronDisplayed() {
195   m_Triedron->InitTraversal();
196   vtkActor *ac = m_Triedron->GetNextActor();
197   while(!(ac==NULL)) {
198     if(ac->GetVisibility()) return true;
199     ac = m_Triedron->GetNextActor();
200   }
201   return false;
202 }
203
204 void VTKViewer_ViewFrame::SetTrihedronSize(int size)
205 {
206   m_Triedron->InitTraversal();
207   vtkActor* anActor = m_Triedron->GetNextActor();
208   while(!(anActor==NULL)) {  
209     m_Renderer->RemoveActor( anActor );
210     anActor = m_Triedron->GetNextActor();
211   }
212
213   m_Triedron->RemoveAllItems();
214   AddAxis(m_Renderer);
215   m_RW->update();
216 }
217
218
219 void VTKViewer_ViewFrame::AddAxis(vtkRenderer* renderer) {  
220   float origine[3];
221   float X[3];
222   float Y[3];
223   float Z[3];
224   float dim;
225
226   QString Size = QAD_CONFIG->getSetting("Viewer:TrihedronSize");
227   if( Size.isEmpty() ){
228     dim = 100;
229   } else {
230     dim = Size.toFloat();
231   }
232
233   origine[0]=0;        origine[1]=0;        origine[2]=0;
234   X[0]=origine[0]+dim; X[1]=origine[0];     X[2]=origine[0];
235   Y[0]=origine[0];     Y[1]=origine[0]+dim; Y[2]=origine[0];
236   Z[0]=origine[0];     Z[1]=origine[0];     Z[2]=origine[0]+dim;
237
238   AddVector(origine,X,renderer, dim);
239   AddVector(origine,Y,renderer, dim);
240   AddVector(origine,Z,renderer, dim);
241  
242 }
243
244 /*!
245   Returns widget containing 3D-Viewer
246 */
247 QWidget* VTKViewer_ViewFrame::getViewWidget() 
248 {
249   return m_RW;
250 }
251
252
253 void VTKViewer_ViewFrame::InitialSetup() {
254   m_Renderer = vtkRenderer::New() ;
255
256   m_RW = new VTKViewer_RenderWindow(this, "RenderWindow");
257   m_RW->getRenderWindow()->AddRenderer(m_Renderer);
258
259   m_Renderer->GetActiveCamera()->ParallelProjectionOn();
260   m_Renderer->LightFollowCameraOn();
261
262   // Set BackgroundColor
263   QString BgrColorRed   = QAD_CONFIG->getSetting("VTKViewer:BackgroundColorRed");
264   QString BgrColorGreen = QAD_CONFIG->getSetting("VTKViewer:BackgroundColorGreen");
265   QString BgrColorBlue  = QAD_CONFIG->getSetting("VTKViewer:BackgroundColorBlue");
266
267   if( !BgrColorRed.isEmpty() && !BgrColorGreen.isEmpty() && !BgrColorBlue.isEmpty() ) 
268     m_Renderer->SetBackground( BgrColorRed.toInt()/255., BgrColorGreen.toInt()/255., BgrColorBlue.toInt()/255. );
269   else
270     m_Renderer->SetBackground( 0, 0, 0 );
271  
272   // CREATE AXIS
273   m_Triedron = vtkActorCollection::New();
274   AddAxis(m_Renderer);
275  
276   // Create an interactor.
277   m_RWInteractor = VTKViewer_RenderWindowInteractor::New();
278   m_RWInteractor->setGUIWindow(m_RW);
279   m_RWInteractor->SetRenderWindow(m_RW->getRenderWindow());
280
281   VTKViewer_InteractorStyleSALOME* RWS = VTKViewer_InteractorStyleSALOME::New();
282   RWS->setGUIWindow(m_RW);
283   m_RWInteractor->SetInteractorStyle(RWS); 
284
285   m_RWInteractor->Initialize();
286   RWS->setTriedron( m_Triedron );
287   //SRN: additional initialization, to init CurrentRenderer of vtkInteractorStyle 
288   RWS->FindPokedRenderer(0, 0);
289
290   setCentralWidget( m_RW );
291   onViewReset();
292 }
293
294 VTKViewer_ViewFrame::~VTKViewer_ViewFrame() {
295   //
296   // In order to ensure that the interactor unregisters
297   // this RenderWindow, we assign a NULL RenderWindow to 
298   // it before deleting it.
299   //
300   m_Transform->Delete() ;
301     
302   m_RWInteractor->SetRenderWindow(NULL) ;
303   m_RWInteractor->Delete() ;
304   
305   //m_RW->Delete() ;
306
307   // NRI : BugID 1137:  m_Renderer->Delete() ;
308 }
309
310
311 /*!
312   Display/hide Trihedron
313 */
314 void VTKViewer_ViewFrame::onViewTrihedron()
315 {
316   if (isTrihedronDisplayed()) {
317     m_Triedron->InitTraversal();
318     vtkActor *ac = m_Triedron->GetNextActor();
319     while(!(ac==NULL)) {
320       ac->VisibilityOff();
321       ac = m_Triedron->GetNextActor();
322     }
323   }
324   else {
325     m_Triedron->InitTraversal();
326     vtkActor *ac = m_Triedron->GetNextActor();
327     while(!(ac==NULL)) {
328       ac->VisibilityOn();
329       ac = m_Triedron->GetNextActor();
330     }
331     m_TriedronVisible = true;
332   }  
333   m_RW->update();
334 }
335
336 /*!
337   Provides top projection of the active view
338 */
339 void VTKViewer_ViewFrame::onViewTop() {
340   vtkCamera* camera = m_Renderer->GetActiveCamera();
341   camera->SetFocalPoint(0,0,0);
342   camera->SetPosition(0,0,1);
343   camera->SetViewUp(0,1,0);
344   m_Renderer->ResetCamera();  
345   onViewFitAll();
346   m_RW->update();
347 }
348
349 /*!
350   Provides bottom projection of the active view
351 */
352 void VTKViewer_ViewFrame::onViewBottom()
353 {
354   vtkCamera* camera = m_Renderer->GetActiveCamera();
355   camera->SetFocalPoint(0,0,0);
356   camera->SetPosition(0,0,-1);
357   camera->SetViewUp(0,1,0);
358   m_Renderer->ResetCamera();  
359   onViewFitAll();
360   m_RW->update();
361 }
362
363 /*!
364   Provides left projection of the active view
365 */
366 void VTKViewer_ViewFrame::onViewLeft()    
367 {
368   vtkCamera* camera = m_Renderer->GetActiveCamera(); 
369   camera->SetFocalPoint(0,0,0);
370   camera->SetPosition(0,1,0);
371   camera->SetViewUp(0,0,1);
372   m_Renderer->ResetCamera();  
373   onViewFitAll();
374   m_RW->update(); 
375 }
376
377 /*!
378   Provides right projection of the active view
379 */
380 void VTKViewer_ViewFrame::onViewRight()
381 {
382   vtkCamera* camera = m_Renderer->GetActiveCamera();
383   camera->SetFocalPoint(0,0,0);
384   camera->SetPosition(0,-1,0);
385   camera->SetViewUp(0,0,1);
386   m_Renderer->ResetCamera();  
387   onViewFitAll();
388   m_RW->update();
389 }
390
391 /*!
392   Provides back projection of the active view
393 */
394 void VTKViewer_ViewFrame::onViewBack()
395 {
396   vtkCamera* camera = m_Renderer->GetActiveCamera();
397   camera->SetPosition(-1,0,0);
398   camera->SetFocalPoint(0,0,0);
399   camera->SetViewUp(0,0,1);
400   m_Renderer->ResetCamera();  
401   onViewFitAll();
402   m_RW->update();
403 }
404
405 /*!
406   Provides front projection of the active view
407 */
408 void VTKViewer_ViewFrame::onViewFront()
409 {
410   vtkCamera* camera = m_Renderer->GetActiveCamera();
411   camera->SetPosition(1,0,0);
412   camera->SetFocalPoint(0,0,0);
413   camera->SetViewUp(0,0,1);
414   m_Renderer->ResetCamera();  
415   onViewFitAll();
416   m_RW->update();
417 }
418
419 /*!
420   Reset the active view
421 */
422 void VTKViewer_ViewFrame::onViewReset()    
423 {
424   vtkCamera* camera = m_Renderer->GetActiveCamera();
425   camera->SetPosition(1,-1,1);
426   camera->SetFocalPoint(0,0,0);
427   camera->SetViewUp(0,0,1);
428   m_Renderer->ResetCamera();  
429   camera->SetParallelScale(500);
430   m_Renderer->ResetCameraClippingRange();
431   m_RW->update();
432 }
433
434 /*!
435   Rotates the active view
436 */
437 void VTKViewer_ViewFrame::onViewRotate()
438 {
439   VTKViewer_InteractorStyleSALOME* RWS = dynamic_cast<VTKViewer_InteractorStyleSALOME*>(getRWInteractor()->GetInteractorStyle());
440   if (RWS)
441     RWS->startRotate();
442 }
443
444 /*!
445   Sets a new center of the active view
446 */
447 void VTKViewer_ViewFrame::onViewGlobalPan()
448 {
449   VTKViewer_InteractorStyleSALOME* RWS = dynamic_cast<VTKViewer_InteractorStyleSALOME*>(getRWInteractor()->GetInteractorStyle());
450   if (RWS)
451     RWS->startGlobalPan();
452 }
453
454 /*!
455   Zooms the active view
456 */
457 void VTKViewer_ViewFrame::onViewZoom()
458 {
459   VTKViewer_InteractorStyleSALOME* RWS = dynamic_cast<VTKViewer_InteractorStyleSALOME*>(getRWInteractor()->GetInteractorStyle());
460   if (RWS)
461     RWS->startZoom();
462 }
463
464 /*!
465   Moves the active view
466 */
467 void VTKViewer_ViewFrame::onViewPan()
468 {
469   VTKViewer_InteractorStyleSALOME* RWS = dynamic_cast<VTKViewer_InteractorStyleSALOME*>(getRWInteractor()->GetInteractorStyle());
470   if (RWS)
471     RWS->startPan();
472 }
473
474 /*!
475   Fits all obejcts within a rectangular area of the active view
476 */
477 void VTKViewer_ViewFrame::onViewFitArea()
478 {
479   VTKViewer_InteractorStyleSALOME* RWS = dynamic_cast<VTKViewer_InteractorStyleSALOME*>(getRWInteractor()->GetInteractorStyle());
480   if (RWS)
481     RWS->startFitArea();
482 }
483
484 /*!
485   Fits all objects in the active view
486 */
487 // Reset the camera clipping range to include this entire bounding box
488 static void ResetCameraClippingRange(vtkRenderer* theRenderer, float bounds[6] )
489 {
490   //see vtkRenderer::ResetCameraClippingRange(float bounds[6]) method
491   double  vn[3], position[3], a, b, c, d;
492   double  range[2], dist;
493   int     i, j, k;
494   float center[3];
495   float distance;
496   float width;
497
498   vtkCamera* anActiveCamera = theRenderer->GetActiveCamera();
499   if ( anActiveCamera == NULL )
500     {
501       //vtkErrorMacro(<< "Trying to reset clipping range of non-existant camera");
502     return;
503     }
504   
505   // Find the plane equation for the camera view plane
506   anActiveCamera->GetViewPlaneNormal(vn);
507   anActiveCamera->GetPosition(position);
508 //  a = -vn[0];
509 //  b = -vn[1];
510 //  c = -vn[2];
511 //  d = -(a*position[0] + b*position[1] + c*position[2]);
512
513   // Set the max near clipping plane and the min far clipping plane
514 //  range[0] = a*bounds[0] + b*bounds[2] + c*bounds[4] + d;
515 //  range[1] = 1e-18;
516
517   // Find the closest / farthest bounding box vertex
518 //  for ( k = 0; k < 2; k++ )
519 //    {
520 //    for ( j = 0; j < 2; j++ )
521 //        {
522 //        for ( i = 0; i < 2; i++ )
523 //          {
524 //          dist = a*bounds[i] + b*bounds[2+j] + c*bounds[4+k] + d;
525 //          range[0] = (dist<range[0])?(dist):(range[0]);
526 //          range[1] = (dist>range[1])?(dist):(range[1]);
527 //          }
528 //        }
529 //    }
530   
531   center[0] = (bounds[0] + bounds[1])/2.0;
532   center[1] = (bounds[2] + bounds[3])/2.0;
533   center[2] = (bounds[4] + bounds[5])/2.0;
534   width = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
535                (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
536                (bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
537   distance = sqrt((position[0]-center[0])*(position[0]-center[0]) +
538                   (position[1]-center[1])*(position[1]-center[1]) +
539                   (position[2]-center[2])*(position[2]-center[2]));
540   range[0] = distance - width/2.0;
541   range[1] = distance + width/2.0;
542
543   // Give ourselves a little breathing room
544   range[0] = 0.99*range[0] - (range[1] - range[0])*0.5;
545   range[1] = 1.01*range[1] + (range[1] - range[0])*0.5;
546
547   // Make sure near is not bigger than far
548   range[0] = (range[0] >= range[1])?(0.01*range[1]):(range[0]);
549
550   // Make sure near is at least some fraction of far - this prevents near
551   // from being behind the camera or too close in front. How close is too
552   // close depends on the resolution of the depth buffer
553   int ZBufferDepth = 16;
554   vtkRenderWindow* aRenderWindow = theRenderer->GetRenderWindow();
555   if (aRenderWindow)
556     {
557       ZBufferDepth = aRenderWindow->GetDepthBufferSize();
558     }
559   //
560   if ( ZBufferDepth <= 16 )
561     {
562     range[0] = (range[0] < 0.01*range[1])?(0.01*range[1]):(range[0]);
563     }
564   else if ( ZBufferDepth <= 24 )
565     {
566     range[0] = (range[0] < 0.01*range[1])?(0.01*range[1]):(range[0]);
567     }
568   else
569     {
570     range[0] = (range[0] < 0.01*range[1])?(0.01*range[1]):(range[0]);
571     }
572   anActiveCamera->SetClippingRange( range );
573 }
574
575 static void ResetCamera(vtkRenderer* theRenderer){  
576   //see vtkRenderer::ResetCamera(float bounds[6]) method
577   float      bounds[6];
578   if(!theRenderer) return;
579   theRenderer->ComputeVisiblePropBounds( bounds );
580
581   float center[3];
582   float distance;
583   float width;
584   double vn[3], *vup;
585   
586   if ( theRenderer->GetActiveCamera() != NULL )
587     {
588     theRenderer->GetActiveCamera()->GetViewPlaneNormal(vn);
589     }
590   else
591     {
592     MESSAGE("Trying to reset non-existant camera");
593     return;
594     }
595
596   center[0] = (bounds[0] + bounds[1])/2.0;
597   center[1] = (bounds[2] + bounds[3])/2.0;
598   center[2] = (bounds[4] + bounds[5])/2.0;
599   width = sqrt((bounds[1]-bounds[0])*(bounds[1]-bounds[0]) +
600                (bounds[3]-bounds[2])*(bounds[3]-bounds[2]) +
601                (bounds[5]-bounds[4])*(bounds[5]-bounds[4]));
602   double ang = theRenderer->GetActiveCamera()->GetViewAngle();
603   distance = 2.0*width/tan(ang*vtkMath::Pi()/360.0);
604   // check view-up vector against view plane normal
605   vup = theRenderer->GetActiveCamera()->GetViewUp();
606   if ( fabs(vtkMath::Dot(vup,vn)) > 0.999 )
607     {
608     MESSAGE("Resetting view-up since view plane normal is parallel");
609     theRenderer->GetActiveCamera()->SetViewUp(-vup[2], vup[0], vup[1]);
610     }
611
612   // update the camera
613   theRenderer->GetActiveCamera()->SetFocalPoint(center[0],center[1],center[2]);
614  theRenderer->GetActiveCamera()->SetPosition(center[0]+distance*vn[0],
615                                   center[1]+distance*vn[1],
616                                   center[2]+distance*vn[2]);
617   // setup default parallel scale
618   theRenderer->GetActiveCamera()->SetParallelScale(width/2.0);
619   //workaround on VTK
620   //theRenderer->ResetCameraClippingRange(bounds);
621   ResetCameraClippingRange(theRenderer,bounds);
622 }
623
624 void VTKViewer_ViewFrame::onViewFitAll()
625 {
626   Standard_Boolean TriedronWasVisible = false;
627   if (isTrihedronDisplayed()) {
628     m_Triedron->InitTraversal();
629     vtkActor *ac = m_Triedron->GetNextActor();
630     while(!(ac==NULL)) {
631       ac->VisibilityOff();
632       ac = m_Triedron->GetNextActor();
633     }
634     TriedronWasVisible = true;
635   }
636   bool hasVisibleActors = m_Renderer->VisibleActorCount() > 0;
637   if ( hasVisibleActors )    // if there are visible actors, not to take into account Trihedron
638     ResetCamera(m_Renderer); 
639   if(TriedronWasVisible) {
640     m_Triedron->InitTraversal();
641     vtkActor *ac = m_Triedron->GetNextActor();
642     while(!(ac==NULL)) {
643       ac->VisibilityOn();
644       ac = m_Triedron->GetNextActor();
645     }
646     if ( !hasVisibleActors ) // if there are NO visible actors, fit view to see only Trihedron
647       ResetCamera(m_Renderer); 
648   }
649   //m_Renderer->ResetCameraClippingRange();
650   m_RW->update();
651 }
652
653 /*!
654     Set background of the viewport
655 */
656 void VTKViewer_ViewFrame::setBackgroundColor( const QColor& color)
657 {
658   if ( m_Renderer )
659     m_Renderer->SetBackground( color.red()/255., color.green()/255., color.blue()/255. );
660 }
661
662 /*!
663     Returns background of the viewport
664 */
665 QColor VTKViewer_ViewFrame::backgroundColor() const
666 {
667   float backint[3];
668   if ( m_Renderer ) {
669     m_Renderer->GetBackground(backint);
670     return QColorDialog::getColor ( QColor(int(backint[0]*255), int(backint[1]*255), int(backint[2]*255)), NULL );
671   }
672   return QMainWindow::backgroundColor();
673 }
674
675
676 void VTKViewer_ViewFrame::SetSelectionMode( int mode )
677 {
678   m_RWInteractor->SetSelectionMode( mode );
679 }
680
681 void VTKViewer_ViewFrame::rename( const Handle(SALOME_InteractiveObject)& IObject, QString newName )
682 {
683   m_RWInteractor->rename(IObject, newName);
684 }
685
686 void VTKViewer_ViewFrame::unHighlightAll() 
687 {
688   m_RWInteractor->unHighlightAll();
689 }
690
691 void VTKViewer_ViewFrame::highlight( const Handle(SALOME_InteractiveObject)& IObject, 
692                                      bool highlight, 
693                                      bool update ) 
694 {
695   QAD_Study* ActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
696   SALOME_Selection* Sel = SALOME_Selection::Selection( ActiveStudy->getSelection() );
697   if ( Sel->SelectionMode() == 4 )
698     m_RWInteractor->highlight(IObject, highlight, update);
699   else if ( Sel->SelectionMode() == 3 ) {
700     m_RWInteractor->highlight(IObject, highlight, update);
701     if ( Sel->HasIndex( IObject ) ) {
702       TColStd_MapOfInteger MapIndex;
703       Sel->GetIndex( IObject, MapIndex );
704       m_RWInteractor->highlightCell(IObject, highlight, MapIndex, update);
705     }
706   } 
707   else if ( Sel->SelectionMode() == 2 ) {
708     m_RWInteractor->highlight(IObject, highlight, update);
709     if ( Sel->HasIndex( IObject ) ) {
710       TColStd_MapOfInteger MapIndex;
711       Sel->GetIndex( IObject, MapIndex );
712       m_RWInteractor->highlightEdge(IObject, highlight, MapIndex, update);
713     }
714   }
715   else if ( Sel->SelectionMode() == 1 ) {
716     m_RWInteractor->highlight(IObject, highlight, update);
717     if ( Sel->HasIndex( IObject ) ) {
718       TColStd_MapOfInteger MapIndex;
719       Sel->GetIndex( IObject, MapIndex );
720       m_RWInteractor->highlightPoint(IObject, highlight, MapIndex, update);
721     }
722   }
723 }
724
725 bool VTKViewer_ViewFrame::isInViewer( const Handle(SALOME_InteractiveObject)& IObject ) 
726 {
727   return m_RWInteractor->isInViewer( IObject );
728 }
729
730 bool VTKViewer_ViewFrame::isVisible( const Handle(SALOME_InteractiveObject)& IObject ) 
731 {
732   return m_RWInteractor->isVisible( IObject );
733 }
734
735 void VTKViewer_ViewFrame::setPopupServer( QAD_Application* App )
736 {
737   m_RW->setPopupServer( App );
738 }
739
740 void VTKViewer_ViewFrame::undo(SALOMEDS::Study_var aStudy,
741                                const char* StudyFrameEntry)
742 {
743   vtkActorCollection* theActors = m_Renderer->GetActors();
744   theActors->InitTraversal();
745   vtkActor *ac = theActors->GetNextActor();
746   while(!(ac==NULL)) {
747     if ( ac->IsA("SALOME_Actor") ) {
748       SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
749       if ( anActor->hasIO() ) {
750         Handle(SALOME_InteractiveObject) IO = anActor->getIO();
751         if ( IO->hasEntry() ) { 
752           /*if (!QAD_ViewFrame::isInViewer(aStudy, IO->getEntry(), StudyFrameEntry)) {
753             m_RWInteractor->Erase(IO);
754             }*/
755         }
756       }
757     }
758     ac = theActors->GetNextActor();
759   }
760 }
761
762 void VTKViewer_ViewFrame::redo(SALOMEDS::Study_var aStudy,
763                                const char* StudyFrameEntry)
764 {
765   SALOMEDS::SObject_var RefSO;
766   SALOMEDS::SObject_var SO = aStudy->FindObjectID( StudyFrameEntry );
767   SALOMEDS::ChildIterator_var it = aStudy->NewChildIterator(SO);
768   for (; it->More();it->Next()){
769     SALOMEDS::SObject_var CSO= it->Value();
770     if (CSO->ReferencedObject(RefSO)) {
771       vtkActorCollection* theActors = m_Renderer->GetActors();
772       theActors->InitTraversal();
773       vtkActor *ac = theActors->GetNextActor();
774       while(!(ac==NULL)) {
775         if ( ac->IsA("SALOME_Actor") ) {
776           SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
777           if ( anActor->hasIO() ) {
778             Handle(SALOME_InteractiveObject) IO = anActor->getIO();
779             if ( IO->hasEntry() ) { 
780               /*if ( strcmp(IO->getEntry(),RefSO->GetID()) == 0 )
781                 m_RWInteractor->Display(IO);*/
782             }
783           }
784         }
785         ac = theActors->GetNextActor();
786       }
787     }
788   }
789 }
790
791
792 /* selection */
793 Handle(SALOME_InteractiveObject) VTKViewer_ViewFrame::FindIObject(const char* Entry)
794 {
795   Handle(SALOME_InteractiveObject) IO;
796   vtkActorCollection* theActors = m_Renderer->GetActors();
797   theActors->InitTraversal();
798   vtkActor *ac = theActors->GetNextActor();
799   while(!(ac==NULL)) {
800     if ( ac->IsA("SALOME_Actor") ) {
801       SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
802       if ( anActor->hasIO() ) {
803         IO = anActor->getIO();
804         if ( IO->hasEntry() ) {
805           if ( strcmp( IO->getEntry(), Entry ) == 0 ) {
806             return IO;
807           }
808         }
809       }
810     }
811     ac = theActors->GetNextActor();
812   }
813   return IO;
814 }
815
816 /* display */           
817 void VTKViewer_ViewFrame::Display(const Handle(SALOME_InteractiveObject)& IObject, bool update)
818 {
819   QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
820   SALOME_Selection* Sel
821     = SALOME_Selection::Selection( myStudy->getSelection() );
822
823   vtkActorCollection* theActors = m_Renderer->GetActors();
824   theActors->InitTraversal();
825   vtkActor *ac = theActors->GetNextActor();
826   while(!(ac==NULL))
827     {
828       if ( ac->IsA("SALOME_Actor") )
829         {
830           SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
831           if ( anActor->hasIO() ) 
832             {
833               Handle(SALOME_InteractiveObject) IO = anActor->getIO();
834               if ( IO->isSame(IObject) )
835                 {
836                   m_RWInteractor->Display(IO, false);
837                   Sel->AddIObject(IO, false);
838                   break;
839                 }
840             }
841         }
842       ac = theActors->GetNextActor();
843     }
844   if (update)
845     Repaint();
846 }
847
848
849 void VTKViewer_ViewFrame::DisplayOnly(const Handle(SALOME_InteractiveObject)& IObject)
850 {
851   QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
852   SALOME_Selection* Sel
853     = SALOME_Selection::Selection( myStudy->getSelection() );
854
855   vtkActorCollection* theActors = m_Renderer->GetActors();
856   theActors->InitTraversal();
857   vtkActor *ac = theActors->GetNextActor();
858   while(!(ac==NULL)) {
859     if ( ac->IsA("SALOME_Actor") ) {
860       SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
861       if ( anActor->hasIO() ) {
862         Handle(SALOME_InteractiveObject) IO = anActor->getIO();
863         if ( !IO->isSame(IObject) ) {
864           m_RWInteractor->Erase(IO, false);
865           Sel->RemoveIObject(IO, false);
866         } else {
867           anActor->SetVisibility(true);
868           Sel->AddIObject(IO, false);
869         }
870       }
871     }
872     ac = theActors->GetNextActor();
873   }
874   Repaint();
875 }
876
877 void VTKViewer_ViewFrame::Erase(const Handle(SALOME_InteractiveObject)& IObject, bool update)
878 {
879   QAD_Study* myStudy = QAD_Application::getDesktop()->getActiveStudy();
880   SALOME_Selection* Sel
881     = SALOME_Selection::Selection( myStudy->getSelection() );
882
883   vtkActorCollection* theActors = m_Renderer->GetActors();
884   theActors->InitTraversal();
885   vtkActor *ac = theActors->GetNextActor();
886   while(!(ac==NULL)) 
887     {
888       if ( ac->IsA("SALOME_Actor") )
889         {
890           SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
891           if ( anActor->hasIO() )
892             {
893               Handle(SALOME_InteractiveObject) IO = anActor->getIO();
894               if ( IO->isSame( IObject ) ) {
895                 m_RWInteractor->Erase(IO, false);
896                 Sel->RemoveIObject(IO, false);
897                 break;
898               }
899             }
900         }
901       ac = theActors->GetNextActor();
902     }
903   if (update)
904     Repaint();
905 }
906
907
908 void VTKViewer_ViewFrame::DisplayAll()
909 {
910   m_RWInteractor->DisplayAll();
911 }
912
913
914 void VTKViewer_ViewFrame::EraseAll()
915 {
916   m_RWInteractor->EraseAll();
917 }
918
919
920 void VTKViewer_ViewFrame::Repaint()
921 {
922   // m_RWInteractor->Render();
923   m_RW->update();
924 }
925
926 void VTKViewer_ViewFrame::GetScale(double theScale[3]){
927   m_Transform->GetScale(theScale);
928 }
929
930 void VTKViewer_ViewFrame::SetScale(double theScale[3]){
931   m_Transform->SetScale(theScale[0], theScale[1], theScale[2]);
932   m_Transform->Modified();
933   Repaint();
934 }
935
936 void VTKViewer_ViewFrame::AddActor( SALOME_Actor* theActor, bool update /*=false*/ ){
937   theActor->SetVisibility(true);
938   theActor->AddToRender(m_Renderer);
939   theActor->SetTransform(m_Transform);
940   if(update){
941     m_Renderer->ResetCameraClippingRange();
942     m_RWInteractor->Render();
943   }
944 }
945
946 void VTKViewer_ViewFrame::RemoveActor( SALOME_Actor* theActor, bool update /*=false*/ ){
947   theActor->RemoveFromRender(m_Renderer);
948   if(update){
949     m_Renderer->ResetCameraClippingRange();
950     m_RWInteractor->Render();
951   }
952 }
953
954