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