Salome HOME
PR : merge branch V1_2c dans branche principale pour V1_3_0_b1
[modules/kernel.git] / src / VTKViewer / VTKViewer_RenderWindowInteractor.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_RenderWindowInteractor.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include "VTKViewer_RenderWindowInteractor.h"
31 #include "VTKViewer_RenderWindow.h"
32 #include "VTKViewer_InteractorStyleSALOME.h"
33
34 #include "QAD_Application.h"
35 #include "QAD_Desktop.h"
36
37 #include "SALOME_Selection.h"
38 #include "SALOME_Actor.h"
39
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <math.h>
44
45 // VTK Includes
46 #include <vtkAssemblyNode.h>
47 #include <vtkActor.h>
48 #include <vtkInteractorStyle.h>
49 #include <vtkObjectFactory.h>
50 #include <vtkPicker.h>
51 #include <vtkCellPicker.h>
52 #include <vtkPointPicker.h>
53 #include <vtkUnstructuredGrid.h>
54 #include <vtkPolyDataMapper.h>
55 #include <vtkSphereSource.h>
56 #include <vtkDataSet.h>
57 #include <vtkMaskPoints.h>
58 #include <vtkVertex.h>
59 #include <vtkRendererCollection.h>
60 #include <vtkPolyDataWriter.h>
61
62
63 // QT Includes
64 #include <qkeycode.h>
65
66 VTKViewer_RenderWindowInteractor* VTKViewer_RenderWindowInteractor::New() {
67   vtkObject *ret = vtkObjectFactory::CreateInstance("VTKViewer_RenderWindowInteractor") ;
68   if( ret ) {
69     return dynamic_cast<VTKViewer_RenderWindowInteractor *>(ret) ;
70   }
71   return new VTKViewer_RenderWindowInteractor;
72 }
73
74 VTKViewer_RenderWindowInteractor::VTKViewer_RenderWindowInteractor() {
75   this->Enabled = 0 ;
76   this->mTimer = new QTimer( this ) ;
77   this->displaymode = 0;
78   myGUIWindow = 0;
79   vtkPicker* thepicker = vtkPicker::New();
80   thepicker->SetTolerance(0);
81   this->SetPicker(thepicker);
82
83   connect(mTimer, SIGNAL(timeout()), this, SLOT(TimerFunc())) ;
84 }
85
86
87 VTKViewer_RenderWindowInteractor::~VTKViewer_RenderWindowInteractor() {
88   delete mTimer ;
89 }
90
91 void VTKViewer_RenderWindowInteractor::PrintSelf(ostream& os, vtkIndent indent) {
92   vtkRenderWindowInteractor::PrintSelf(os, indent) ;
93   //
94   // :NOTE: Fri Apr 21 21:51:05 2000 Pagey
95   // QGL specific stuff goes here. One should add output 
96   // lines here if any protected members are added to
97   // the class. 
98   //
99 }
100
101 //
102 // We never allow the VTKViewer_RenderWindowInteractor to control 
103 // the event loop. The application always has the control. 
104 //
105 void VTKViewer_RenderWindowInteractor::Initialize() {
106   //
107   // We cannot do much unless there is a render window 
108   // associated with this interactor. 
109   //
110   if( ! RenderWindow ) {
111     vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize(): No render window attached!") ;
112     return ;
113   }
114
115   //
116   // We cannot hand a render window which is not a VTKViewer_RenderWindow. 
117   // One way to force this is to use dynamic_cast and hope that 
118   // it works. If the dynamic_cast does not work, we flag an error
119   // and get the hell out.
120   //
121   vtkRenderWindow *my_render_win = dynamic_cast<vtkRenderWindow *>(RenderWindow) ;
122   if( !my_render_win ) {
123     vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize() can only handle VTKViewer_RenderWindow.") ;
124     return ;
125   }
126
127   //
128   // If the render window has zero size, then set it to a default 
129   // value of 300x300.
130   // 
131   int* aSize = my_render_win->GetSize();
132   this->Size[0] = ((aSize[0] > 0) ? aSize[0] : 300);
133   this->Size[1] = ((aSize[1] > 0) ? aSize[1] : 300);
134
135   //
136   // Enable the interactor. 
137   //
138   this->Enable() ;
139
140   //
141   // Start the rendering of the window. 
142   //
143   my_render_win->Start() ;
144
145   //
146   // The interactor has been initialized.
147   //
148   this->Initialized = 1 ;
149
150   Cell_Actor = vtkActor::New(); 
151   Cell_Actor->PickableOff();
152   Cell_Actor->GetProperty()->SetColor(1,1,0);
153   Cell_Actor->GetProperty()->SetLineWidth(5);
154   Cell_Actor->GetProperty()->SetRepresentationToSurface();
155
156   Edge_Actor = vtkActor::New(); 
157   Edge_Actor->PickableOff();
158   Edge_Actor->GetProperty()->SetColor(1,0,0);
159   Edge_Actor->GetProperty()->SetLineWidth(5);
160   Edge_Actor->GetProperty()->SetRepresentationToWireframe();
161
162   Point_Actor = vtkActor::New(); 
163   Point_Actor->PickableOff();
164   Point_Actor->GetProperty()->SetColor(1,1,0);
165   Point_Actor->GetProperty()->SetPointSize(5);
166   Point_Actor->GetProperty()->SetRepresentationToPoints();
167
168   return ;
169 }
170
171 void VTKViewer_RenderWindowInteractor::SetSelectionMode(int mode)
172 {
173   Cell_Actor->VisibilityOff();
174   Edge_Actor->VisibilityOff();
175   Point_Actor->VisibilityOff();
176
177   selectionmode = mode;
178   if ( mode == 1 ) {
179     vtkPointPicker* thepicker = vtkPointPicker::New();
180     //    thepicker->SetTolerance(0.001);
181     this->SetPicker(thepicker);
182   } else if ( mode == 2 ) {
183     vtkCellPicker* thepicker = vtkCellPicker::New();
184     thepicker->SetTolerance(0.001);
185     this->SetPicker(thepicker);
186   } else if ( mode == 3 ) {
187     vtkCellPicker* thepicker = vtkCellPicker::New();
188     thepicker->SetTolerance(0.001); 
189     this->SetPicker(thepicker);
190   } else if ( mode == 4 ) {
191     vtkPicker* thepicker = vtkPicker::New();
192     thepicker->SetTolerance(0.001);
193     this->SetPicker(thepicker);
194   }
195 }
196
197
198 void VTKViewer_RenderWindowInteractor::Enable() {
199   //
200   // Do not need to do anything if already enabled.
201   //
202   if( this->Enabled ) {
203     return ;
204   }
205   
206   //
207   // Attach slots to every useful signal of the render window. 
208   //
209   this->ConnectSlots() ;
210   
211   this->Enabled = 1 ;
212   this->Modified() ;
213 }
214
215 void VTKViewer_RenderWindowInteractor::Disable() {
216   if( ! this->Enabled ) {
217     return ;
218   }
219
220   this->DisconnectSlots() ;
221   this->Enabled = 0 ;
222   this->Modified() ;
223 }
224
225 // ================================== 
226 void VTKViewer_RenderWindowInteractor::Start() {
227   //
228   // We do not allow this interactor to control the 
229   // event loop. Only the QtApplication objects are
230   // allowed to do that. 
231   //
232   vtkErrorMacro(<<"VTKViewer_RenderWindowInteractor::Start() not allowed to start event loop.") ;
233   return ;
234 }
235
236 void VTKViewer_RenderWindowInteractor::UpdateSize(int w, int h) {
237   // if the size changed send this on to the RenderWindow
238   if ((w != this->Size[0])||(h != this->Size[1])) {
239     this->Size[0] = w;
240     this->Size[1] = h;
241     this->RenderWindow->SetSize(w,h);
242   }
243 }
244
245 int VTKViewer_RenderWindowInteractor::CreateTimer(int vtkNotUsed(timertype)) {
246   //
247   // Start a one-shot timer for 10ms. 
248   //
249   mTimer->start(10, TRUE) ;
250   return 1 ;
251 }
252
253 int VTKViewer_RenderWindowInteractor::DestroyTimer(void) {
254   //
255   // :TRICKY: Tue May  2 00:17:32 2000 Pagey
256   //
257   // QTimer will automatically expire after 10ms. So 
258   // we do not need to do anything here. In fact, we 
259   // should not even Stop() the QTimer here because doing 
260   // this will skip some of the processing that the TimerFunc()
261   // does and will result in undesirable effects. For 
262   // example, this will result in vtkLODActor to leave
263   // the models in low-res mode after the mouse stops
264   // moving. 
265   //
266   return 1 ;
267 }
268
269 void VTKViewer_RenderWindowInteractor::TimerFunc() {
270   if( ! this->Enabled ) {
271     return ;
272   }
273
274   ((vtkInteractorStyle*)this->InteractorStyle)->OnTimer() ;
275   emit RenderWindowModified() ;
276 }
277
278 void VTKViewer_RenderWindowInteractor::ConnectSlots() {
279   ProcessSlotConnections(true) ;
280 }
281
282 void VTKViewer_RenderWindowInteractor::DisconnectSlots() {
283   ProcessSlotConnections(false) ;
284 }
285
286 void VTKViewer_RenderWindowInteractor::ProcessSlotConnections(bool conn) {
287   //
288   // We cannot do much unless there is a render window 
289   // associated with this interactor. 
290   //
291   if( ! myGUIWindow ) {
292     vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize(): No render window attached!") ;
293     return ;
294   }
295
296   
297   bool (*slot_func) ( const QObject * sender, 
298                       const char * signal, 
299                       const QObject * receiver, 
300                       const char * member ) ;
301   if( conn ) {
302     slot_func = &QObject::connect ;
303   } else {
304     slot_func = &QObject::disconnect ;
305   }
306   //
307   // We cannot hand a render window which is not a VTKViewer_RenderWindow. 
308   // One way to force this is to use dynamic_cast and hope that 
309   // it works. If the dynamic_cast does not work, we flag an error
310   // and get the hell out.
311   //
312   if( ! myGUIWindow ) {
313     vtkErrorMacro(<< "VTKViewer_RenderWindowInteractor::Initialize() can only handle VTKViewer_RenderWindow.") ;
314     return ;
315   }
316
317   slot_func ( myGUIWindow, SIGNAL(ButtonPressed(const QMouseEvent *)), 
318               this,          SLOT(ButtonPressed(const QMouseEvent *)) ) ;
319   slot_func( myGUIWindow, SIGNAL(ButtonReleased(const QMouseEvent *)), 
320              this,          SLOT(ButtonReleased(const QMouseEvent *)) ) ;
321   
322   
323   slot_func( myGUIWindow, SIGNAL(LeftButtonPressed(const QMouseEvent *)), 
324              this,          SLOT(LeftButtonPressed(const QMouseEvent *)) ) ;
325   slot_func( myGUIWindow, SIGNAL(LeftButtonReleased(const QMouseEvent *)), 
326              this,          SLOT(LeftButtonReleased(const QMouseEvent *)) ) ;
327   
328   slot_func( myGUIWindow, SIGNAL(MiddleButtonPressed(const QMouseEvent *)), 
329              this,          SLOT(MiddleButtonPressed(const QMouseEvent *)) ) ;
330   slot_func( myGUIWindow, SIGNAL(MiddleButtonReleased(const QMouseEvent *)), 
331              this,          SLOT(MiddleButtonReleased(const QMouseEvent *)) ) ;
332   
333   slot_func( myGUIWindow, SIGNAL(RightButtonPressed(const QMouseEvent *)), 
334              this,          SLOT(RightButtonPressed(const QMouseEvent *)) ) ;
335   slot_func( myGUIWindow, SIGNAL(RightButtonReleased(const QMouseEvent *)), 
336              this,          SLOT(RightButtonReleased(const QMouseEvent *)) ) ;
337   
338   slot_func( myGUIWindow, SIGNAL(MouseMove(const QMouseEvent *)), 
339              this,          SLOT(MouseMove(const QMouseEvent *)) ) ;
340   
341   slot_func( myGUIWindow, SIGNAL(KeyPressed(QKeyEvent *)),
342              this,          SLOT(KeyPressed(QKeyEvent *)) ) ;
343   
344   slot_func( this,          SIGNAL(RenderWindowModified()), 
345              myGUIWindow, SLOT(update()) ) ;
346
347 }
348
349 void VTKViewer_RenderWindowInteractor::MouseMove(const QMouseEvent *event) {
350   if( ! this->Enabled ) {
351     return ;
352   }
353   VTKViewer_InteractorStyleSALOME* Style = 0;
354   if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
355     Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
356   else {
357     emit RenderWindowModified() ; 
358     return;
359   }
360   Style->OnMouseMove(0, 0, event->x(), event->y()/*this->Size[1] - event->y() - 1*/) ;
361   if (Style->needsRedrawing() )
362     emit RenderWindowModified() ; 
363 }
364
365 void VTKViewer_RenderWindowInteractor::LeftButtonPressed(const QMouseEvent *event) {
366   if( ! this->Enabled ) {
367     return ;
368   }
369   VTKViewer_InteractorStyleSALOME* Style = 0;
370   if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
371     Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
372   else 
373     return;
374   Style->OnLeftButtonDown((event->state() & ControlButton), 
375                           (event->state() & ShiftButton), 
376                           event->x(), event->y());
377 }
378
379 void VTKViewer_RenderWindowInteractor::LeftButtonReleased(const QMouseEvent *event) {
380   if( ! this->Enabled ) {
381     return ;
382   }
383   VTKViewer_InteractorStyleSALOME* Style = 0;
384   if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
385     Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
386   else 
387     return;
388   Style->OnLeftButtonUp( (event->state() & ControlButton), 
389                          (event->state() & ShiftButton), 
390                          event->x(), event->y() ) ;
391 }
392
393 void VTKViewer_RenderWindowInteractor::MiddleButtonPressed(const QMouseEvent *event) {
394   if( ! this->Enabled ) {
395     return ;
396   }
397   VTKViewer_InteractorStyleSALOME* Style = 0;
398   if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
399     Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
400   else 
401     return;
402   Style->OnMiddleButtonDown((event->state() & ControlButton), 
403                             (event->state() & ShiftButton), 
404                             event->x(), event->y() ) ;
405 }
406
407 void VTKViewer_RenderWindowInteractor::MiddleButtonReleased(const QMouseEvent *event) {
408   if( ! this->Enabled ) {
409     return ;
410   }
411   VTKViewer_InteractorStyleSALOME* Style = 0;
412   if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
413     Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
414   else 
415     return;
416   Style->OnMiddleButtonUp( (event->state() & ControlButton), 
417                            (event->state() & ShiftButton), 
418                            event->x(), event->y() ) ;
419 }
420
421 void VTKViewer_RenderWindowInteractor::RightButtonPressed(const QMouseEvent *event) {
422   if( ! this->Enabled ) {
423     return ;
424   }
425   VTKViewer_InteractorStyleSALOME* Style = 0;
426   if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
427     Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
428   else 
429     return;
430   Style->OnRightButtonDown( (event->state() & ControlButton), 
431                             (event->state() & ShiftButton), 
432                             event->x(), event->y() ) ;
433 }
434
435 void VTKViewer_RenderWindowInteractor::RightButtonReleased(const QMouseEvent *event) {
436   if( ! this->Enabled ) {
437     return ;
438   }
439   VTKViewer_InteractorStyleSALOME* Style = 0;
440   if (this->InteractorStyle->IsA("VTKViewer_InteractorStyleSALOME"))
441     Style = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
442   else 
443     return;
444   Style->OnRightButtonUp( (event->state() & ControlButton), 
445                           (event->state() & ShiftButton), 
446                           event->x(), event->y() ) ;
447 }
448
449 void VTKViewer_RenderWindowInteractor::ButtonPressed(const QMouseEvent *event) {
450   return ;
451 }
452
453 void VTKViewer_RenderWindowInteractor::ButtonReleased(const QMouseEvent *event) {
454   return ;
455 }
456
457 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToWireframe(vtkActorCollection* ac)
458 {
459   vtkActor* anActor;
460
461   for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
462     if ( anActor->IsA("GEOM_Actor") ) {
463       SALOME_Actor* SALOMEactor = SALOME_Actor::SafeDownCast(anActor);
464       // Specific for GEOM actor
465       if ( SALOMEactor->getDisplayMode() == 1 )
466         SALOMEactor->setDisplayMode(0);
467     } else {
468       anActor->GetProperty()->SetRepresentationToWireframe();
469     }
470   }
471   this->RenderWindow->Render();
472   emit RenderWindowModified() ;
473 }
474
475 int VTKViewer_RenderWindowInteractor::GetDisplayMode() {
476   return displaymode;
477 }
478
479 void VTKViewer_RenderWindowInteractor::SetDisplayMode(int mode) {
480   if(mode==0) ChangeRepresentationToWireframe();
481   else ChangeRepresentationToSurface();
482   displaymode = mode;
483 }
484
485 void VTKViewer_RenderWindowInteractor::SwitchRepresentation(const Handle(SALOME_InteractiveObject)& IObject,
486                                                             bool update) {  
487   vtkRenderer* aren;
488   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
489        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
490     vtkActorCollection* theActors = aren->GetActors();
491     theActors->InitTraversal();
492     vtkActor *ac = theActors->GetNextActor();
493     while(!(ac==NULL)) {
494       if ( ac->IsA("SALOME_Actor") ) {
495         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
496         if ( anActor->hasIO() ) {
497           if ( IObject->isSame( anActor->getIO() ) ) {
498             if ( anActor->IsA("GEOM_Actor") ) {
499               // GEOM actor
500               if ( anActor->getDisplayMode() == 0 ) anActor->setDisplayMode(1);
501               else anActor->setDisplayMode(0);
502             }
503             else {
504               if(anActor->GetProperty()->GetRepresentation() <= 1)
505                 anActor->GetProperty()->SetRepresentationToSurface();
506               else
507                 anActor->GetProperty()->SetRepresentationToWireframe(); 
508             }
509           }
510         }
511       }
512       ac = theActors->GetNextActor();
513     }
514   } 
515   if (update) {
516     this->RenderWindow->Render();
517     emit RenderWindowModified() ;
518   }
519 }
520
521
522
523 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToWireframe()
524   // change all actors to wireframe
525 {
526   vtkActorCollection *ac;
527   vtkActor *anActor, *aPart;
528   
529   ac = GetRenderer()->GetActors();
530   ChangeRepresentationToWireframe(ac);
531 }
532
533
534
535 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToSurface(vtkActorCollection* ac)
536 {
537   vtkActor *anActor, *aPart;
538   
539   for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
540     if ( anActor->IsA("GEOM_Actor") ) {
541       SALOME_Actor* SALOMEactor = SALOME_Actor::SafeDownCast(anActor);
542       // Specific for GEOM actor
543       if ( SALOMEactor->getDisplayMode() == 0 )
544         SALOMEactor->setDisplayMode(1);
545     } else {
546       anActor->GetProperty()->SetRepresentationToSurface();
547     }
548   }
549   this->RenderWindow->Render();
550   emit RenderWindowModified() ;
551 }
552
553 void VTKViewer_RenderWindowInteractor::ChangeRepresentationToSurface()
554   // change all actors to "surface" or solid
555 {
556   vtkActorCollection *ac;
557   vtkActor *anActor, *aPart;
558   
559   ac = GetRenderer()->GetActors();
560   ChangeRepresentationToSurface(ac);
561 }
562
563 vtkRenderer* VTKViewer_RenderWindowInteractor::GetRenderer()
564 {
565   vtkRendererCollection * theRenderers =  this->RenderWindow->GetRenderers();
566   theRenderers->InitTraversal();
567   return theRenderers->GetNextItem();
568 }
569
570 void VTKViewer_RenderWindowInteractor::EraseAll()
571 {   
572   vtkActorCollection *ac;
573   vtkActor *anActor, *aPart;
574   
575   ac = GetRenderer()->GetActors();
576   for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
577     if ( anActor->IsA("SALOME_Actor") ) {
578       SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( anActor );
579       SActor->SetVisibility(false);
580     }
581   }
582   emit RenderWindowModified() ;
583 }
584
585 void VTKViewer_RenderWindowInteractor::DisplayAll()
586 {   
587   vtkActorCollection *ac;
588   vtkActor *anActor, *aPart;
589   
590   ac = GetRenderer()->GetActors();
591   for (ac->InitTraversal(); (anActor = ac->GetNextActor()); ) {
592     if ( anActor->IsA("SALOME_Actor") ) {
593       SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( anActor );
594       SActor->SetVisibility( true );
595     }
596   }
597   emit RenderWindowModified() ;
598 }
599
600 void VTKViewer_RenderWindowInteractor::Erase(const Handle(SALOME_InteractiveObject)& IObject, bool update)
601 {
602   vtkRenderer* aren;
603   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
604        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
605     vtkActorCollection* theActors = aren->GetActors();
606     vtkActorCollection* theChildActors = vtkActorCollection::New(); 
607     theActors->InitTraversal();
608     vtkActor *ac = theActors->GetNextActor();
609     while(!(ac==NULL)) {
610       if ( ac->IsA("SALOME_Actor") ) {
611         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
612         if ( anActor->hasIO() ) {
613           if ( IObject->isSame( anActor->getIO() ) ) {
614             anActor->SetVisibility( false );
615             anActor->GetChildActors(theChildActors);
616           }
617         }
618       }
619       ac = theActors->GetNextActor();
620     }
621
622     // Erase dependent actors
623     theChildActors->InitTraversal();
624     ac = theChildActors->GetNextActor();
625     while(!(ac==NULL)) {
626       ac->SetVisibility( false );
627       ac = theChildActors->GetNextActor();
628     }
629   } 
630   if (update)
631     emit RenderWindowModified() ;
632 }
633
634 void VTKViewer_RenderWindowInteractor::Remove(const Handle(SALOME_InteractiveObject)& IObject, bool update)
635 {
636   vtkRenderer* aren;
637   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
638        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
639     vtkActorCollection* theActors = aren->GetActors();
640     theActors->InitTraversal();
641     vtkActor *ac = theActors->GetNextActor();
642     while(!(ac==NULL)) {
643       if ( ac->IsA("SALOME_Actor") ) {
644         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
645         if ( anActor->hasIO() ) {
646           if ( IObject->isSame( anActor->getIO() ) ) {
647             aren->RemoveActor(anActor);
648           }
649         }
650       }
651       ac = theActors->GetNextActor();
652     }
653   } 
654   if (update)
655     emit RenderWindowModified() ;
656 }
657
658 float VTKViewer_RenderWindowInteractor::GetTransparency(const Handle(SALOME_InteractiveObject)& IObject) {
659   vtkRenderer* aren;
660   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
661        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
662     vtkActorCollection* theActors = aren->GetActors();
663     theActors->InitTraversal();
664     vtkActor *ac = theActors->GetNextActor();
665     while(!(ac==NULL)) {
666       if ( ac->IsA("SALOME_Actor") ) {
667         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
668         if ( anActor->hasIO() ) {
669           if ( IObject->isSame( anActor->getIO() ) ) {
670             if ( anActor->IsA("GEOM_Actor") ) {
671               // GEOM actor
672               return (1-(anActor->GetOpacity()));
673             }
674             else return (1-anActor->GetProperty()->GetOpacity());
675           }
676         }
677       }
678       ac = theActors->GetNextActor();
679     }
680   } 
681   return -1;
682 }
683
684 void VTKViewer_RenderWindowInteractor::SetTransparency(const Handle(SALOME_InteractiveObject)& IObject, float trans) {
685   vtkRenderer* aren;
686   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
687        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
688     vtkActorCollection* theActors = aren->GetActors();
689     theActors->InitTraversal();
690     vtkActor *ac = theActors->GetNextActor();
691     while(!(ac==NULL)) {
692       if ( ac->IsA("SALOME_Actor") ) {
693         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
694         if ( anActor->hasIO() ) {
695           if ( IObject->isSame( anActor->getIO() ) ) {
696             if ( anActor->IsA("GEOM_Actor") ) {
697               // GEOM actor
698               anActor->SetOpacity(1-trans);
699             }
700             else anActor->GetProperty()->SetOpacity(1-trans);
701           }
702         }
703       }
704       ac = theActors->GetNextActor();
705     }
706   } 
707 }
708
709 void VTKViewer_RenderWindowInteractor::Display( SALOME_Actor* SActor, bool update)
710 {
711   vtkRenderer* aren;
712   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
713        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
714     aren->AddActor( SActor );
715     SActor->SetVisibility( true );
716   }
717
718   if (update) {
719     Render();
720     emit RenderWindowModified();
721   }
722 }
723
724 void VTKViewer_RenderWindowInteractor::Display(const Handle(SALOME_InteractiveObject)& IObject, bool update)
725 {
726   Standard_Boolean isalreadydisplayed = false;
727
728   vtkRenderer* aren;
729   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
730        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
731     vtkActorCollection* theActors = aren->GetActors();
732     theActors->InitTraversal();
733     vtkActor *ac = theActors->GetNextActor();
734     while(!(ac==NULL)) {
735       if ( ac->IsA("SALOME_Actor") ) {
736         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
737         if ( anActor->hasIO() ) {
738           if ( IObject->isSame( anActor->getIO() ) ) {
739             anActor->SetVisibility( true );
740           }
741         }
742       }
743       ac = theActors->GetNextActor();
744     }
745   }
746   if (update)
747     emit RenderWindowModified() ;
748 }
749
750 void VTKViewer_RenderWindowInteractor::KeyPressed(QKeyEvent *event) {
751 //    MESSAGE ( " vtkQGLRenderWindowInteractor::KeyPressed " )
752
753 //    if (!QAD_Application::getDesktop()->getActiveComponent().isEmpty()) {
754 //      QAD_Application::getDesktop()->onKeyPress( event );
755 //    }
756 }
757
758 bool VTKViewer_RenderWindowInteractor::highlight( const Handle(SALOME_InteractiveObject)& IObject, 
759                                                   bool hilight, 
760                                                   bool update)
761 {
762   VTKViewer_InteractorStyleSALOME* aStyle = VTKViewer_InteractorStyleSALOME::SafeDownCast(this->InteractorStyle);
763   if (!aStyle) return false;
764
765   vtkRenderer* aren;
766   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
767        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
768     vtkActorCollection* theActors = aren->GetActors();
769     theActors->InitTraversal();
770     vtkActor *ac = theActors->GetNextActor();
771     while(!(ac==NULL)) {
772       if ( ac->IsA("SALOME_Actor") ) {
773         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
774         if ( anActor->hasIO() ) {
775           if ( IObject->isSame( anActor->getIO() ) ) {
776             if ( anActor->GetMapper() == NULL ) {
777               return false;
778             }
779             //highlight or unhilight actors
780             if ( anActor->hasHighlight() ) {
781               anActor->highlight(hilight);
782             } else {
783               if ( anActor->GetVisibility() == 1 ) {
784                 if (hilight) {
785                   vtkActor2D *actor2D;
786                   vtkProp3D *prop3D;
787                   if ( (prop3D=vtkProp3D::SafeDownCast(anActor)) != NULL ) {
788                     aStyle->HighlightProp3D(prop3D);
789                   } else if ( (actor2D=vtkActor2D::SafeDownCast(anActor)) != NULL ) {
790                     aStyle->HighlightActor2D(actor2D);
791                   }
792                   //this->InteractorStyle->HighlightProp(anActor);
793                 } else {
794                   aStyle->HighlightProp3D(NULL);
795                   aStyle->HighlightActor2D(NULL);
796                   //this->InteractorStyle->HighlightProp(NULL);
797                 }
798               }
799             }
800           }
801         }
802       }
803       ac = theActors->GetNextActor();
804     }
805   }
806   if (update) {
807     Render();
808     emit RenderWindowModified();
809   }
810
811   return false;
812 }
813
814 void VTKViewer_RenderWindowInteractor::Update() {
815   vtkRenderer* aren;
816   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
817        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
818     vtkActorCollection* theActors = aren->GetActors();
819     theActors->InitTraversal();
820     vtkActor *ac = theActors->GetNextActor();
821
822     while(!(ac==NULL)) {
823       ac->ApplyProperties();
824       //VSV: No in VTK: ac->Update();
825       ac = theActors->GetNextActor();
826     }
827     aren->ResetCamera();
828     emit RenderWindowModified() ;  
829   }
830 }
831
832
833 bool VTKViewer_RenderWindowInteractor::unHighlightAll()
834 {
835   vtkRenderer* aren;
836   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
837        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
838     vtkActorCollection* theActors = aren->GetActors();
839     theActors->InitTraversal();
840
841     if ( theActors->IsItemPresent(Point_Actor) != 0 ) 
842       aren->RemoveActor( Point_Actor );
843     if ( theActors->IsItemPresent( Edge_Actor ) != 0 ) 
844       aren->RemoveActor( Edge_Actor ); 
845     if ( theActors->IsItemPresent( Cell_Actor ) != 0 ) 
846       aren->RemoveActor( Cell_Actor );
847
848     vtkActor *ac = theActors->GetNextActor();
849
850     while(!(ac==NULL)) {
851       if ( ac->IsA("SALOME_Actor") ) {
852         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
853         if ( anActor->hasIO() ) {
854           //highlight or unhilight actors
855           if ( anActor->hasHighlight() )
856             anActor->highlight(false);
857         }
858       }
859       ac = theActors->GetNextActor();
860     }
861   }
862
863   emit RenderWindowModified() ;
864   return false;
865 }
866
867 //-----------------
868 // Color methods
869 //-----------------
870
871 void VTKViewer_RenderWindowInteractor::SetColor(const Handle(SALOME_InteractiveObject)& IObject,QColor thecolor) {
872   vtkRenderer* aren;
873   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
874        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
875     vtkActorCollection* theActors = aren->GetActors();
876     theActors->InitTraversal();
877     vtkActor *ac = theActors->GetNextActor();
878     while(!(ac==NULL)) {
879       if ( ac->IsA("SALOME_Actor") ) {
880         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
881         if ( anActor->hasIO() ) {
882           if ( IObject->isSame( anActor->getIO() ) ) {
883             if ( anActor->IsA("GEOM_Actor") ) {
884               // GEOM actor
885               anActor->SetColor(float(thecolor.red())/255,float(thecolor.green())/255,float(thecolor.blue())/255);
886             }
887             else {
888               anActor->GetProperty()->SetColor(float(thecolor.red())/255,float(thecolor.green())/255,float(thecolor.blue())/255);
889             }
890           }
891         }
892       }
893       ac = theActors->GetNextActor();
894     }
895   } 
896 }
897
898 QColor VTKViewer_RenderWindowInteractor::GetColor(const Handle(SALOME_InteractiveObject)& IObject) {
899   vtkRenderer* aren;
900   QColor emptycolor(0,0,0);
901   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
902        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
903     vtkActorCollection* theActors = aren->GetActors();
904     theActors->InitTraversal();
905     vtkActor *ac = theActors->GetNextActor();
906     while(!(ac==NULL)) {
907       if ( ac->IsA("SALOME_Actor") ) {
908         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
909         if ( anActor->hasIO() ) {
910           if ( IObject->isSame( anActor->getIO() ) ) {
911             if ( anActor->IsA("GEOM_Actor") ) {
912               // GEOM actor
913               float r,g,b;
914               anActor->GetColor(r,g,b);
915               return QColor(int(r*255),int(g*255),int(b*255));
916             }
917             else {
918               float color[3];
919               anActor->GetProperty()->GetColor(color);   
920               return QColor(int(color[0]*255),int(color[1]*255),int(color[2]*255));
921             }
922           }
923         }
924       }
925       ac = theActors->GetNextActor();
926     }
927   } 
928   return emptycolor;
929 }
930
931
932
933
934 bool VTKViewer_RenderWindowInteractor::isInViewer(const Handle(SALOME_InteractiveObject)& IObject)
935 {
936   vtkRenderer* aren;
937   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
938        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
939     vtkActorCollection* theActors = aren->GetActors();
940     theActors->InitTraversal();
941     vtkActor *ac = theActors->GetNextActor();
942     while(!(ac==NULL)) {
943       if ( ac->IsA("SALOME_Actor") ) {
944         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
945         if ( anActor->hasIO() ) {
946           if ( IObject->isSame( anActor->getIO() ) ) {
947             //MESSAGE ( " VTKViewer_RenderWindowInteractor::isInViewer = TRUE" )
948             return true;
949           }
950         }
951       }
952       ac = theActors->GetNextActor();
953     }
954   }
955   //MESSAGE ( " VTKViewer_RenderWindowInteractor::isInViewer = FALSE" )
956   return false;
957 }
958
959 bool VTKViewer_RenderWindowInteractor::isVisible(const Handle(SALOME_InteractiveObject)& IObject)
960 {
961   vtkRenderer* aren;
962   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
963        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
964     vtkActorCollection* theActors = aren->GetActors();
965     theActors->InitTraversal();
966     vtkActor *ac = theActors->GetNextActor();
967     while(!(ac==NULL)) {
968       if ( ac->IsA("SALOME_Actor") ) {
969         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
970         if ( anActor->hasIO() ) {
971           if ( IObject->isSame( anActor->getIO() ) ) {
972             return anActor->GetVisibility();
973           }
974         }
975       }
976       ac = theActors->GetNextActor();
977     }
978   }
979   return false;
980 }
981
982 void VTKViewer_RenderWindowInteractor::rename(const Handle(SALOME_InteractiveObject)& IObject, QString newName)
983 {
984   vtkRenderer* aren;
985   for (this->RenderWindow->GetRenderers()->InitTraversal(); 
986        (aren = this->RenderWindow->GetRenderers()->GetNextItem()); ) {
987     vtkActorCollection* theActors = aren->GetActors();
988     theActors->InitTraversal();
989     vtkActor *ac = theActors->GetNextActor();
990     while(!(ac==NULL)) {
991       if ( ac->IsA("SALOME_Actor") ) {
992         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
993         if ( anActor->hasIO() ) {
994           if ( IObject->isSame( anActor->getIO() ) ) {
995             anActor->setName(strdup(newName));
996           }
997         }
998       }
999       ac = theActors->GetNextActor();
1000     }
1001   }  
1002
1003   emit RenderWindowModified() ;
1004 }
1005
1006 bool VTKViewer_RenderWindowInteractor::highlight(const Handle(SALOME_InteractiveObject)& IObject, 
1007                                                  const TColStd_MapOfInteger& MapIndex, 
1008                                                  VTKViewer_RenderWindowInteractor::TCreateMapperFun theFun,
1009                                                  vtkActor *theActor, bool hilight, bool update )
1010 {
1011   if(MapIndex.Extent() == 0) return false;
1012   vtkRenderer* aRen;
1013   this->RenderWindow->GetRenderers()->InitTraversal();
1014   for (; aRen = this->RenderWindow->GetRenderers()->GetNextItem(); ) {
1015     vtkActorCollection* anActorColl = aRen->GetActors();
1016     if ( anActorColl->IsItemPresent(theActor) != 0 ) 
1017       aRen->RemoveActor(theActor);
1018     anActorColl->InitTraversal();
1019     vtkActor *ac = NULL;
1020     for(; (ac = anActorColl->GetNextActor()) != NULL; ){
1021       if(ac->IsA("SALOME_Actor")){
1022         SALOME_Actor* anActor = SALOME_Actor::SafeDownCast(ac);
1023         if(anActor->hasIO()){
1024           if(IObject->isSame(anActor->getIO())){
1025             if(vtkPolyData* aSourcePolyData = anActor->GetPolyDataInput()){
1026               vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
1027               (*theFun)(aSourcePolyData,aMapper,MapIndex);
1028               theActor->SetMapper(aMapper);   
1029               aMapper->Delete();
1030               if(anActorColl->IsItemPresent(theActor) == 0) 
1031                 aRen->AddActor(theActor);             
1032               if(hilight)
1033                 theActor->VisibilityOn();
1034               else
1035                 theActor->VisibilityOff();
1036               break;
1037             }
1038           }
1039         }
1040       }
1041     }
1042   }
1043   if(update){
1044     this->RenderWindow->Render();  
1045     emit RenderWindowModified() ;
1046   }
1047   return false;
1048 }
1049
1050 static void CellCreateMapper(vtkPolyData *theSourcePolyData, vtkPolyDataMapper* theMapper, 
1051                       const TColStd_MapOfInteger& theMapIndex)
1052 {
1053   vtkPolyData* aPolyData = vtkPolyData::New();
1054   aPolyData->Allocate();
1055
1056   vtkIdList *ptIds = vtkIdList::New(); 
1057   ptIds->Allocate(theSourcePolyData->GetMaxCellSize());
1058   vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
1059   aPolyData->SetPoints(theSourcePolyData->GetPoints());
1060   TColStd_MapIteratorOfMapOfInteger ite(theMapIndex);
1061   int aNbOfParts = theSourcePolyData->GetNumberOfCells();
1062   for(; ite.More(); ite.Next()){
1063     int aPartId = ite.Key();
1064     if(0 > aPartId || aPartId >= aNbOfParts) break;
1065     theSourcePolyData->GetCellPoints(aPartId,ptIds);
1066     vtkCell* aCell = theSourcePolyData->GetCell(aPartId);
1067     aPolyData->InsertNextCell(aCell->GetCellType(),ptIds);
1068     for (int i = 0, iEnd = aCell->GetNumberOfEdges(); i < iEnd; i++){
1069       vtkCell* anEdgeCell = aCell->GetEdge(i);
1070       aPolyData->InsertNextCell(VTK_LINE,anEdgeCell->GetPointIds());
1071     }
1072   }
1073   ptIds->Delete();
1074   theMapper->SetInput(aPolyData);  
1075   aPolyData->Delete();
1076 }
1077   
1078 bool VTKViewer_RenderWindowInteractor::highlightCell(const Handle(SALOME_InteractiveObject)& IObject, 
1079                                                      bool hilight, 
1080                                                      const TColStd_MapOfInteger& MapIndex, 
1081                                                      bool update )
1082 {
1083   return highlight(IObject,MapIndex,&CellCreateMapper,Cell_Actor,hilight,update);
1084 }
1085
1086
1087 static void PointCreateMapper(vtkPolyData *theSourcePolyData, vtkPolyDataMapper* theMapper, 
1088                        const TColStd_MapOfInteger& theMapIndex)
1089 {
1090   vtkPolyData* aPolyData = vtkPolyData::New();
1091   aPolyData->Allocate();
1092
1093   vtkPoints *aPoints = vtkPoints::New();
1094   vtkMaskPoints* aMaskPoints = vtkMaskPoints::New();
1095   TColStd_MapIteratorOfMapOfInteger ite(theMapIndex);
1096   aPoints->SetNumberOfPoints(theMapIndex.Extent());
1097   int aNbOfParts = theSourcePolyData->GetNumberOfPoints();
1098   for(int i = 0; ite.More(); ite.Next(),i++){
1099     int aPartId = ite.Key();
1100     if(0 > aPartId || aPartId >= aNbOfParts) break;
1101     aPoints->SetPoint(i,theSourcePolyData->GetPoint(aPartId));
1102   }
1103   aPolyData->SetPoints(aPoints);
1104   aPoints->Delete();
1105   aMaskPoints->SetInput(aPolyData);
1106   aMaskPoints->SetGenerateVertices(1);
1107   aMaskPoints->SetOnRatio(1);
1108   theMapper->SetInput(aMaskPoints->GetOutput());  
1109   aMaskPoints->Delete();
1110 }
1111   
1112 bool VTKViewer_RenderWindowInteractor::highlightPoint(const Handle(SALOME_InteractiveObject)& IObject, 
1113                                                       bool hilight, const TColStd_MapOfInteger& MapIndex, 
1114                                                       bool update)
1115 {
1116   return highlight(IObject,MapIndex,&PointCreateMapper,Point_Actor,hilight,update);
1117 }
1118
1119   
1120 static void EdgeCreateMapper(vtkPolyData *theSourcePolyData, vtkPolyDataMapper* theMapper, 
1121                       const TColStd_MapOfInteger& theMapIndex)
1122 {
1123   vtkPolyData* aPolyData = vtkPolyData::New();
1124   aPolyData->Allocate();
1125
1126   vtkIdList *ptIds = vtkIdList::New(); 
1127   ptIds->Allocate(theSourcePolyData->GetMaxCellSize());
1128   vtkPolyDataMapper* aMapper = vtkPolyDataMapper::New();
1129   aPolyData->SetPoints(theSourcePolyData->GetPoints());
1130   int aCellId = -1, aCellCounter = 0;
1131   for(TColStd_MapIteratorOfMapOfInteger ite(theMapIndex); ite.More(); ite.Next()){
1132     if(ite.Key() > 0){
1133       aCellId = ite.Key();
1134       aCellCounter++;
1135     }
1136   }
1137   if(aCellCounter == 1){
1138     vtkCell* aCell = theSourcePolyData->GetCell(aCellId);
1139     if(aCell->GetCellType() <= VTK_LINE){
1140       aPolyData->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
1141     }else{
1142       TColStd_MapIteratorOfMapOfInteger ite(theMapIndex);
1143       int aNbOfParts = aCell->GetNumberOfEdges();
1144       for(; ite.More(); ite.Next()){
1145         if(ite.Key() < 0){
1146           int aPartId = -ite.Key()-1;
1147           if(0 > aPartId || aPartId >= aNbOfParts) break;
1148           vtkCell* anEdgeCell = aCell->GetEdge(aPartId);
1149           aPolyData->InsertNextCell(VTK_LINE,anEdgeCell->GetPointIds());
1150         }
1151       }
1152     }
1153   }else{
1154     int aNbOfParts = theSourcePolyData->GetNumberOfCells();
1155     for(TColStd_MapIteratorOfMapOfInteger ite(theMapIndex); ite.More(); ite.Next()){
1156       if(ite.Key() > 0){
1157         int aPartId = ite.Key();
1158         if(0 > aPartId || aPartId >= aNbOfParts) break;
1159         vtkCell* aCell = theSourcePolyData->GetCell(aPartId);
1160         aPolyData->InsertNextCell(aCell->GetCellType(),aCell->GetPointIds());
1161       }
1162     }
1163   }
1164   ptIds->Delete();
1165   theMapper->SetInput(aPolyData);  
1166   aPolyData->Delete();
1167 }
1168   
1169
1170 bool VTKViewer_RenderWindowInteractor::highlightEdge( const Handle(SALOME_InteractiveObject)& IObject, 
1171                                                       bool hilight, 
1172                                                       const TColStd_MapOfInteger& MapIndex, 
1173                                                       bool update )
1174 {
1175   return highlight(IObject,MapIndex,&EdgeCreateMapper,Edge_Actor,hilight,update);
1176 }