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