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