]> SALOME platform Git repositories - modules/kernel.git/blob - src/VTKViewer/VTKViewer_InteractorStyleSALOME.cxx
Salome HOME
d9eb3a75c5b24879222f9f0b458514a4dbf8c951
[modules/kernel.git] / src / VTKViewer / VTKViewer_InteractorStyleSALOME.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_InteractorStyleSALOME.cxx
25 //  Author : Christophe ATTANASIO
26 //  Module : SALOME
27 //  $Header$
28
29 using namespace std;
30 #include "VTKViewer_InteractorStyleSALOME.h"
31 #include "VTKViewer_RenderWindow.h"
32
33 #include <qapplication.h>
34 #include "QAD_Application.h"
35 #include "QAD_Desktop.h"
36
37 #include "SALOME_Selection.h"
38 #include "SALOME_Actor.h"
39 #include "SALOME_ListIteratorOfListIO.hxx"
40
41 #include <vtkObjectFactory.h>
42 #include <vtkMath.h>
43 #include <vtkCommand.h>
44 #include <vtkAssemblyNode.h>
45 #include <vtkPicker.h>
46 #include <vtkPointPicker.h>
47 #include <vtkCellPicker.h>
48 #include <vtkLine.h> 
49 #include <vtkUnstructuredGrid.h> 
50 #include <vtkExtractEdges.h>
51 #include <vtkPolyDataMapper.h>
52 #include <vtkDataSetCollection.h>
53 #include <vtkImageData.h>
54
55
56 //VRV: porting on Qt 3.0.5
57 #if QT_VERSION >= 0x030005
58 #include <qpainter.h>
59 #endif
60 //VRV: porting on Qt 3.0.5
61
62 //----------------------------------------------------------------------------
63 VTKViewer_InteractorStyleSALOME *VTKViewer_InteractorStyleSALOME::New() 
64 {
65   // First try to create the object from the vtkObjectFactory
66   vtkObject* ret = vtkObjectFactory::CreateInstance("VTKViewer_InteractorStyleSALOME");
67   if(ret)
68     {
69       return (VTKViewer_InteractorStyleSALOME*)ret;
70     }
71   // If the factory was unable to create the object, then create it here.
72   return new VTKViewer_InteractorStyleSALOME;
73 }
74
75
76 //----------------------------------------------------------------------------
77 VTKViewer_InteractorStyleSALOME::VTKViewer_InteractorStyleSALOME() 
78 {
79   m_Triedron = 0;
80   this->MotionFactor = 10.0;
81   this->State = VTK_INTERACTOR_STYLE_CAMERA_NONE;
82   this->RadianToDegree = 180.0 / vtkMath::Pi();
83   this->ForcedState = VTK_INTERACTOR_STYLE_CAMERA_NONE;
84   loadCursors();
85 }
86
87 //----------------------------------------------------------------------------
88 VTKViewer_InteractorStyleSALOME::~VTKViewer_InteractorStyleSALOME() 
89 {
90 }
91
92 //----------------------------------------------------------------------------
93 void VTKViewer_InteractorStyleSALOME::setTriedron( vtkActorCollection* triedron )
94 {
95   m_Triedron = triedron;
96 }
97
98 //----------------------------------------------------------------------------
99 void VTKViewer_InteractorStyleSALOME::RotateXY(int dx, int dy)
100 {
101   double rxf;
102   double ryf;
103   vtkCamera *cam;
104   
105   if (this->CurrentRenderer == NULL)
106     {
107       return;
108     }
109   
110   int *size = this->CurrentRenderer->GetRenderWindow()->GetSize();
111   this->DeltaElevation = -20.0 / size[1];
112   this->DeltaAzimuth = -20.0 / size[0];
113   
114   rxf = (double)dx * this->DeltaAzimuth *  this->MotionFactor;
115   ryf = (double)dy * this->DeltaElevation * this->MotionFactor;
116   
117   cam = this->CurrentRenderer->GetActiveCamera();
118   cam->Azimuth(rxf);
119   cam->Elevation(ryf);
120   cam->OrthogonalizeViewUp();
121   this->CurrentRenderer->ResetCameraClippingRange();
122   vtkRenderWindowInteractor *rwi = this->Interactor;
123   /* VSV Light follows camera: if (this->CurrentLight)
124     {
125       // get the first light
126       this->CurrentLight->SetPosition(cam->GetPosition());
127       this->CurrentLight->SetFocalPoint(cam->GetFocalPoint());
128       } */
129   rwi->Render();
130 }
131
132 //----------------------------------------------------------------------------
133 void VTKViewer_InteractorStyleSALOME::PanXY(int x, int y, int oldX, int oldY)
134 {
135   TranslateView(x, y, oldX, oldY);   
136   //vtkRenderWindowInteractor *rwi = this->Interactor;
137   /* VSV Light follows camera: if (this->CurrentLight)
138     {
139       vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
140       this->CurrentLight->SetPosition(cam->GetPosition());
141       this->CurrentLight->SetFocalPoint(cam->GetFocalPoint());
142       }*/
143     
144   this->Interactor->Render();
145 }
146
147 //----------------------------------------------------------------------------
148 void VTKViewer_InteractorStyleSALOME::DollyXY(int dx, int dy)
149 {
150   vtkCamera *cam;
151   double dxf = this->MotionFactor * (double)(dx) / (double)(this->CurrentRenderer->GetCenter()[1]);
152   double dyf = this->MotionFactor * (double)(dy) / (double)(this->CurrentRenderer->GetCenter()[1]);
153
154   double zoomFactor = pow((double)1.1, dxf + dyf);
155   
156   if (this->CurrentRenderer == NULL)
157     {
158       return;
159     }
160   
161   cam = this->CurrentRenderer->GetActiveCamera();
162     if (cam->GetParallelProjection())
163     {
164       cam->SetParallelScale(cam->GetParallelScale()/zoomFactor);
165     }
166   else
167     {
168       cam->Dolly(zoomFactor);
169       this->CurrentRenderer->ResetCameraClippingRange();
170     }
171   
172   /* VSV Light follows camera: if (this->CurrentLight)
173     {      
174       this->CurrentLight->SetPosition(cam->GetPosition());
175       this->CurrentLight->SetFocalPoint(cam->GetFocalPoint());
176       }*/
177   
178   this->Interactor->Render();
179 }
180
181 //----------------------------------------------------------------------------
182 void VTKViewer_InteractorStyleSALOME::SpinXY(int x, int y, int oldX, int oldY)
183 {
184   vtkRenderWindowInteractor *rwi = this->Interactor;
185   vtkCamera *cam;
186
187   if (this->CurrentRenderer == NULL)
188     {
189       return;
190     }
191
192   double newAngle = atan2((double)(y - this->CurrentRenderer->GetCenter()[1]),
193                           (double)(x - this->CurrentRenderer->GetCenter()[0]));
194   double oldAngle = atan2((double)(oldY -this->CurrentRenderer->GetCenter()[1]),
195                           (double)(oldX - this->CurrentRenderer->GetCenter()[0]));
196   
197   newAngle *= this->RadianToDegree;
198   oldAngle *= this->RadianToDegree;
199
200   cam = this->CurrentRenderer->GetActiveCamera();
201   cam->Roll(newAngle - oldAngle);
202   cam->OrthogonalizeViewUp();
203       
204   rwi->Render();
205 }
206
207
208 //----------------------------------------------------------------------------
209 void VTKViewer_InteractorStyleSALOME::OnMouseMove(int vtkNotUsed(ctrl), 
210                                                   int shift,
211                                                   int x, int y) 
212 {
213   myShiftState = shift;
214   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
215     onOperation(QPoint(x, y));
216   else if (ForcedState == VTK_INTERACTOR_STYLE_CAMERA_NONE)
217     onCursorMove(QPoint(x, y));
218 }
219
220
221 //----------------------------------------------------------------------------
222 void VTKViewer_InteractorStyleSALOME::OnLeftButtonDown(int ctrl, int shift, 
223                                                        int x, int y) 
224 {
225   if (this->HasObserver(vtkCommand::LeftButtonPressEvent)) {
226     this->InvokeEvent(vtkCommand::LeftButtonPressEvent,NULL);
227     return;
228   }
229   this->FindPokedRenderer(x, y);
230   if (this->CurrentRenderer == NULL) {
231     return;
232   }
233   myShiftState = shift;
234   // finishing current viewer operation
235   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
236     onFinishOperation();
237     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
238   }
239   myOtherPoint = myPoint = QPoint(x, y);
240   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
241     startOperation(ForcedState);
242   } else {
243     if (ctrl)
244       startOperation(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
245     else
246       startOperation(VTK_INTERACTOR_STYLE_CAMERA_SELECT);
247   }
248   return;
249 }
250 //----------------------------------------------------------------------------
251 void VTKViewer_InteractorStyleSALOME::OnLeftButtonUp(int vtkNotUsed(ctrl),
252                                                      int shift, 
253                                                      int vtkNotUsed(x),
254                                                      int vtkNotUsed(y))
255 {
256   myShiftState = shift;
257   // finishing current viewer operation
258   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
259     onFinishOperation();
260     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
261   }
262 }
263
264 //----------------------------------------------------------------------------
265 void VTKViewer_InteractorStyleSALOME::OnMiddleButtonDown(int ctrl,
266                                                          int shift, 
267                                                          int x, int y) 
268 {
269   if (this->HasObserver(vtkCommand::MiddleButtonPressEvent)) 
270     {
271       this->InvokeEvent(vtkCommand::MiddleButtonPressEvent,NULL);
272       return;
273     }
274   this->FindPokedRenderer(x, y);
275   if (this->CurrentRenderer == NULL)
276     {
277       return;
278     }
279   myShiftState = shift;
280   // finishing current viewer operation
281   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
282     onFinishOperation();
283     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
284   }
285   myOtherPoint = myPoint = QPoint(x, y);
286   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
287     startOperation(ForcedState);
288   }
289   else {
290     if (ctrl)
291       startOperation(VTK_INTERACTOR_STYLE_CAMERA_PAN);
292   }
293 }
294 //----------------------------------------------------------------------------
295 void VTKViewer_InteractorStyleSALOME::OnMiddleButtonUp(int vtkNotUsed(ctrl),
296                                                        int shift, 
297                                                        int vtkNotUsed(x),
298                                                        int vtkNotUsed(y))
299 {
300   myShiftState = shift;
301   // finishing current viewer operation
302   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
303     onFinishOperation();
304     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
305   }
306 }
307
308 //----------------------------------------------------------------------------
309 void VTKViewer_InteractorStyleSALOME::OnRightButtonDown(int ctrl,
310                                                         int shift, 
311                                                         int x, int y) 
312 {
313   if (this->HasObserver(vtkCommand::RightButtonPressEvent)) 
314     {
315       this->InvokeEvent(vtkCommand::RightButtonPressEvent,NULL);
316       return;
317     }
318   this->FindPokedRenderer(x, y);
319   if (this->CurrentRenderer == NULL)
320     {
321       return;
322     }
323   myShiftState = shift;
324   // finishing current viewer operation
325   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
326     onFinishOperation();
327     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
328   }
329   myOtherPoint = myPoint = QPoint(x, y);
330   if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
331     startOperation(ForcedState);
332   }
333   else {
334     if (ctrl)
335       startOperation(VTK_INTERACTOR_STYLE_CAMERA_ROTATE);  
336   }
337 }
338
339 //----------------------------------------------------------------------------
340 void VTKViewer_InteractorStyleSALOME::OnRightButtonUp(int vtkNotUsed(ctrl),
341                                                       int shift, 
342                                                       int vtkNotUsed(x),
343                                                       int vtkNotUsed(y))
344 {
345   myShiftState = shift;
346   // finishing current viewer operation
347   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
348     onFinishOperation();
349     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
350   }
351 }
352
353 //----------------------------------------------------------------------------
354 void VTKViewer_InteractorStyleSALOME::PrintSelf(ostream& os, vtkIndent indent)
355 {
356   vtkInteractorStyle::PrintSelf(os,indent);
357
358 }
359
360 /* XPM */
361 const char* imageZoomCursor[] = { 
362 "32 32 3 1",
363 ". c None",
364 "a c #000000",
365 "# c #ffffff",
366 "................................",
367 "................................",
368 ".#######........................",
369 "..aaaaaaa.......................",
370 "................................",
371 ".............#####..............",
372 "...........##.aaaa##............",
373 "..........#.aa.....a#...........",
374 ".........#.a.........#..........",
375 ".........#a..........#a.........",
376 "........#.a...........#.........",
377 "........#a............#a........",
378 "........#a............#a........",
379 "........#a............#a........",
380 "........#a............#a........",
381 ".........#...........#.a........",
382 ".........#a..........#a.........",
383 ".........##.........#.a.........",
384 "........#####.....##.a..........",
385 ".......###aaa#####.aa...........",
386 "......###aa...aaaaa.......#.....",
387 ".....###aa................#a....",
388 "....###aa.................#a....",
389 "...###aa...............#######..",
390 "....#aa.................aa#aaaa.",
391 ".....a....................#a....",
392 "..........................#a....",
393 "...........................a....",
394 "................................",
395 "................................",
396 "................................",
397 "................................"};
398
399 const char* imageRotateCursor[] = { 
400 "32 32 3 1",
401 ". c None",
402 "a c #000000",
403 "# c #ffffff",
404 "................................",
405 "................................",
406 "................................",
407 "................................",
408 "........#.......................",
409 ".......#.a......................",
410 "......#######...................",
411 ".......#aaaaa#####..............",
412 "........#..##.a#aa##........##..",
413 ".........a#.aa..#..a#.....##.aa.",
414 ".........#.a.....#...#..##.aa...",
415 ".........#a.......#..###.aa.....",
416 "........#.a.......#a..#aa.......",
417 "........#a.........#..#a........",
418 "........#a.........#a.#a........",
419 "........#a.........#a.#a........",
420 "........#a.........#a.#a........",
421 ".........#.........#a#.a........",
422 "........##a........#a#a.........",
423 "......##.a#.......#.#.a.........",
424 "....##.aa..##.....##.a..........",
425 "..##.aa.....a#####.aa...........",
426 "...aa.........aaa#a.............",
427 "................#.a.............",
428 "...............#.a..............",
429 "..............#.a...............",
430 "...............a................",
431 "................................",
432 "................................",
433 "................................",
434 "................................",
435 "................................"};
436
437 // loads cursors for viewer operations - zoom, pan, etc...
438 void VTKViewer_InteractorStyleSALOME::loadCursors()
439 {
440   myDefCursor       = QCursor(ArrowCursor);
441   myHandCursor      = QCursor(PointingHandCursor);
442   myPanCursor       = QCursor(SizeAllCursor);
443   myZoomCursor      = QCursor(QPixmap(imageZoomCursor));
444   myRotateCursor    = QCursor(QPixmap(imageRotateCursor));
445   mySpinCursor      = QCursor(QPixmap(imageRotateCursor)); // temporarly !!!!!!
446   myGlobalPanCursor = QCursor(CrossCursor);
447   myCursorState     = false;
448 }
449
450 // event filter - controls mouse and keyboard events during viewer operations
451 bool VTKViewer_InteractorStyleSALOME::eventFilter(QObject* object, QEvent* event)
452 {
453   if (!myGUIWindow) return false;
454   if ( (event->type() == QEvent::MouseButtonPress || event->type() == QEvent::KeyPress) && object != myGUIWindow)
455   {
456     qApp->removeEventFilter(this);
457     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
458   }
459   return QObject::eventFilter(object, event);
460 }
461
462 // starts Zoom operation (e.g. through menu command)
463 void VTKViewer_InteractorStyleSALOME::startZoom()
464 {
465   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
466   {
467     onFinishOperation();
468     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
469   }
470   setCursor(VTK_INTERACTOR_STYLE_CAMERA_ZOOM);
471   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_ZOOM;
472   qApp->installEventFilter(this);
473 }
474
475 // starts Pan operation (e.g. through menu command)
476 void VTKViewer_InteractorStyleSALOME::startPan()
477 {
478   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
479   {
480     onFinishOperation();
481     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
482   }
483   setCursor(VTK_INTERACTOR_STYLE_CAMERA_PAN);
484   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_PAN;
485   qApp->installEventFilter(this);
486 }
487
488 // starts Rotate operation (e.g. through menu command)
489 void VTKViewer_InteractorStyleSALOME::startRotate()
490 {
491   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
492   {
493     onFinishOperation();
494     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
495   }
496   setCursor(VTK_INTERACTOR_STYLE_CAMERA_ROTATE);
497   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_ROTATE;
498   qApp->installEventFilter(this);
499 }
500
501 // starts Spin operation (e.g. through menu command)
502 void VTKViewer_InteractorStyleSALOME::startSpin()
503 {
504   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
505   {
506     onFinishOperation();
507     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
508   }
509   setCursor(VTK_INTERACTOR_STYLE_CAMERA_SPIN);
510   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_SPIN;
511   qApp->installEventFilter(this);
512 }
513
514
515 // starts Fit Area operation (e.g. through menu command)
516 void VTKViewer_InteractorStyleSALOME::startFitArea()
517 {
518   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
519   {
520     onFinishOperation();
521     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
522   }
523   setCursor(VTK_INTERACTOR_STYLE_CAMERA_FIT);
524   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_FIT;
525   qApp->installEventFilter(this);
526 }
527
528 // starts Global Panning operation (e.g. through menu command)
529 void VTKViewer_InteractorStyleSALOME::startGlobalPan()
530 {
531   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
532   {
533     onFinishOperation();
534     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
535   }
536   setCursor(VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN);
537   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN;
538
539   // store current zoom scale
540   vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
541   myScale = cam->GetParallelScale();
542
543   // make fit all
544   Standard_Boolean TriedronWasVisible = false;
545   if ( m_Triedron ) {
546     m_Triedron->InitTraversal();
547     vtkActor *ac = m_Triedron->GetNextActor();
548     while(!(ac==NULL)) {
549       if(ac->GetVisibility()) {
550         TriedronWasVisible = true;
551         ac->VisibilityOff();
552       }
553       ac = m_Triedron->GetNextActor();
554     }
555   }
556   this->CurrentRenderer->ResetCamera();
557   this->CurrentRenderer->ResetCameraClippingRange();
558   if( m_Triedron && TriedronWasVisible ) {
559     m_Triedron->InitTraversal();
560     vtkActor *ac = m_Triedron->GetNextActor();
561     while(!(ac==NULL)) {
562       ac->VisibilityOn();
563       ac = m_Triedron->GetNextActor();
564     }
565   }
566   //VTKViewer_RenderWindow* aRW = dynamic_cast<VTKViewer_RenderWindow*>(this->Interactor->GetRenderWindow());
567   if (myGUIWindow) myGUIWindow->update();
568   
569   qApp->installEventFilter(this);
570 }
571
572 // returns TRUE if needs redrawing
573 bool VTKViewer_InteractorStyleSALOME::needsRedrawing()
574 {
575   return State == VTK_INTERACTOR_STYLE_CAMERA_ZOOM   ||
576          State == VTK_INTERACTOR_STYLE_CAMERA_PAN    ||
577          State == VTK_INTERACTOR_STYLE_CAMERA_ROTATE ||
578          State == VTK_INTERACTOR_STYLE_CAMERA_SPIN   ||
579          State == VTK_INTERACTOR_STYLE_CAMERA_NONE;
580 }
581
582 // fits viewer contents to rect
583 void VTKViewer_InteractorStyleSALOME::fitRect(const int left, 
584                                        const int top, 
585                                        const int right, 
586                                        const int bottom)
587 {
588   if (this->CurrentRenderer == NULL) {
589     return;
590   }
591   vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
592  
593   // move camera
594   int x = (left + right)/2;
595   int y = (top + bottom)/2;
596   int *aSize = this->CurrentRenderer->GetRenderWindow()->GetSize();
597   int oldX = aSize[0]/2;
598   int oldY = aSize[1]/2;
599   TranslateView(oldX, oldY, x, y);
600
601       
602   // zoom camera
603   double dxf = (double)(aSize[0]) / (double)(abs(right - left));
604   double dyf = (double)(aSize[1]) / (double)(abs(bottom - top));
605   double zoomFactor = (dxf + dyf)/2 ;
606
607   if (cam->GetParallelProjection()) {
608     cam->SetParallelScale(cam->GetParallelScale()/zoomFactor);
609   } else {
610     cam->Dolly(zoomFactor);
611     this->CurrentRenderer->ResetCameraClippingRange();
612   }
613   
614   //vtkRenderWindowInteractor *rwi = this->Interactor;
615   /* VSV Light follows camera: if (this->CurrentLight) {
616     this->CurrentLight->SetPosition(cam->GetPosition());
617     this->CurrentLight->SetFocalPoint(cam->GetFocalPoint());
618     }*/
619   //  rwi->Render();
620   //VTKViewer_RenderWindow* aRW = dynamic_cast<VTKViewer_RenderWindow*>(rwi->GetRenderWindow());
621   myGUIWindow->update();
622 }
623
624
625
626 // starts viewer operation (!internal usage!)
627 void VTKViewer_InteractorStyleSALOME::startOperation(int operation)
628 {
629   switch(operation)
630   { 
631   case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
632   case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
633   case VTK_INTERACTOR_STYLE_CAMERA_PAN:
634   case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
635   case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
636   case VTK_INTERACTOR_STYLE_CAMERA_FIT:
637   case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
638     if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
639       startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
640     State = operation;
641     if (State != VTK_INTERACTOR_STYLE_CAMERA_SELECT)
642       setCursor(operation);
643     onStartOperation();
644     break;
645   case VTK_INTERACTOR_STYLE_CAMERA_NONE:
646   default:
647     setCursor(VTK_INTERACTOR_STYLE_CAMERA_NONE);
648     State = ForcedState = VTK_INTERACTOR_STYLE_CAMERA_NONE;
649     break;
650   }
651 }
652
653 // sets proper cursor for window when viewer operation is activated
654 void VTKViewer_InteractorStyleSALOME::setCursor(const int operation)
655 {
656   if (!myGUIWindow) return;
657   switch (operation)
658   {
659     case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
660       myGUIWindow->setCursor(myZoomCursor); 
661       myCursorState = true;
662       break;
663     case VTK_INTERACTOR_STYLE_CAMERA_PAN:
664       myGUIWindow->setCursor(myPanCursor); 
665       myCursorState = true;
666       break;
667     case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
668       myGUIWindow->setCursor(myRotateCursor); 
669       myCursorState = true;
670       break;
671     case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
672       myGUIWindow->setCursor(mySpinCursor); 
673       myCursorState = true;
674       break;
675     case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
676       myGUIWindow->setCursor(myGlobalPanCursor); 
677       myCursorState = true;
678       break;
679     case VTK_INTERACTOR_STYLE_CAMERA_FIT:
680     case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
681       myGUIWindow->setCursor(myHandCursor); 
682       myCursorState = true;
683       break;
684     case VTK_INTERACTOR_STYLE_CAMERA_NONE:
685     default:
686       myGUIWindow->setCursor(myDefCursor); 
687       myCursorState = false;
688       break;
689   }
690 }
691
692 // called when viewer operation started (!put necessary initialization here!)
693 void VTKViewer_InteractorStyleSALOME::onStartOperation()
694 {
695   if (!myGUIWindow) return;
696   switch (State) {
697     case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
698     case VTK_INTERACTOR_STYLE_CAMERA_FIT:
699     {
700       QPainter p(myGUIWindow);
701       p.setPen(Qt::lightGray);
702       p.setRasterOp(Qt::XorROP);
703       p.drawRect(QRect(myPoint, myOtherPoint));
704       break;
705     }
706     case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
707     case VTK_INTERACTOR_STYLE_CAMERA_PAN:
708     case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
709     case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
710     case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
711       break;
712   }
713 }
714
715 // called when viewer operation finished (!put necessary post-processing here!)
716 void VTKViewer_InteractorStyleSALOME::onFinishOperation() 
717 {
718   if (!myGUIWindow) return;
719
720   QAD_Study* aActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
721   SALOME_Selection* aSel    = SALOME_Selection::Selection( aActiveStudy->getSelection() );
722   vtkRenderWindowInteractor *rwi = this->Interactor;
723
724   int aSelectionMode = aSel->SelectionMode();
725   bool aSelActiveCompOnly = aSel->IsSelectActiveCompOnly();
726   SALOMEDS::SComponent_var aActiveComponent = SALOMEDS::SComponent::_narrow(
727      aActiveStudy->getStudyDocument()->FindObject(QAD_Application::getDesktop()->getActiveComponent()));
728
729   switch (State) {
730     case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
731     case VTK_INTERACTOR_STYLE_CAMERA_FIT:
732     {
733       QPainter p(myGUIWindow);
734       p.setPen(Qt::lightGray);
735       p.setRasterOp(Qt::XorROP);
736       QRect rect(myPoint, myOtherPoint);
737       p.drawRect(rect);
738       rect = rect.normalize();
739       if (State == VTK_INTERACTOR_STYLE_CAMERA_FIT) {
740         // making fit rect opeation 
741         int w, h, x, y;
742         rwi->GetSize(w, h);
743         int x1, y1, x2, y2;
744         x1 = rect.left(); 
745         y1 = h - rect.top() - 1;
746         x2 = rect.right(); 
747         y2 = h - rect.bottom() - 1;
748         fitRect(x1, y1, x2, y2);
749       }
750       else {
751         if (myPoint == myOtherPoint) {
752         // process point selection
753           int w, h, x, y;
754           rwi->GetSize(w, h);
755           x = myPoint.x(); 
756           y = h - myPoint.y() - 1;
757           vtkActorCollection* listactors = NULL;
758           this->FindPokedRenderer(x, y);
759           rwi->StartPickCallback();
760           rwi->GetPicker()->Pick(x, y, 0.0, this->CurrentRenderer);
761     
762           if ( rwi->GetPicker()->IsA("vtkCellPicker") ) {
763             vtkCellPicker* picker;
764             if ( (picker = vtkCellPicker::SafeDownCast(rwi->GetPicker())) ) {
765               MESSAGE ( " CellId : " << picker->GetCellId() );
766               if ( picker->GetCellId() >= 0 ) {
767                 vtkActor* ac = picker->GetActor();
768                 if ( ac->IsA("SALOME_Actor") ) {
769                   SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( ac );
770                   MESSAGE ( " NAME Actor : " << SActor->getName() );
771
772                   //Cell selection //////////////////////////////////// NB
773                   if ( aSelectionMode == 3 ) {
774                     if ( SActor->hasIO() ) {
775                       Handle(SALOME_InteractiveObject) IO = SActor->getIO();
776                       // Look in the current selection
777                       SALOME_ListIteratorOfListIO It(aSel->StoredIObjects());
778                       Standard_Boolean IsSelected = false;
779                       for(;It.More();It.Next()) {
780                         Handle(SALOME_InteractiveObject) IOS = It.Value();
781                         if(IO->isSame(IOS)) {
782                           IsSelected = true;
783                           IO = IOS; //Added by SRN, fix SAL1307
784                           break;
785                         }
786                       }
787                       if(IsSelected) {
788                         // This IO is already in the selection
789                         //if(shift) {
790                           bool add = aSel->AddOrRemoveIndex( IO, picker->GetCellId(), myShiftState, false );
791                           //Sel->RemoveIObject(IO);
792                         //}
793                       } else {
794                         if(!myShiftState) {
795                           this->HighlightProp( NULL );
796                           aSel->ClearIObjects();
797                         }
798                         bool add = aSel->AddOrRemoveIndex( IO, picker->GetCellId(), myShiftState, false );
799                         aSel->AddIObject( IO, false );
800                       }
801                     }
802                   }
803                   //Edge selection ////////////////////////// NB
804                   else if ( aSelectionMode == 2 ) {
805                     if(SActor->hasIO()){
806                       Handle(SALOME_InteractiveObject) IO = SActor->getIO();
807                       float pickPosition[3],pcoords[3],closestPoint[3],weights[3],dist1=1000000.0,dist2=0;
808                       int subId,edgeId=-10,pickedID,result;
809                       pickedID = picker->GetCellId();
810                       picker->GetPickPosition(pickPosition);
811                       if (vtkDataSet* UGrid = SActor->GetMapper()->GetInput()){
812                         if (vtkCell* pickedCell = UGrid->GetCell(pickedID)){
813                           edgeId = -1;
814                           for (int i = 0, iEnd = pickedCell->GetNumberOfEdges(); i < iEnd; i++){
815                             vtkCell* edge = pickedCell->GetEdge(i);
816                             if(vtkLine* line = vtkLine::SafeDownCast(edge)){
817                               result = line->EvaluatePosition(pickPosition,closestPoint,subId,pcoords,dist2,weights);
818                               if (dist2 < dist1) {
819                                 dist1  = dist2;
820                                 edgeId = i;
821                               }
822                             }
823                           }
824                           MESSAGE("edgeID transformed = "<<edgeId);
825                           // Look in the current selection
826                           SALOME_ListIteratorOfListIO It(aSel->StoredIObjects());
827                           Standard_Boolean IsSelected = false;
828                           for(;It.More();It.Next()) {
829                             Handle(SALOME_InteractiveObject) IOS = It.Value();
830                             if(IO->isSame(IOS)) {
831                               IO = IOS; //Added by SRN, fix SAL1307
832                               IsSelected = true;
833                               break;
834                             }
835                           }
836                           if(!myShiftState) {
837                             this->HighlightProp( NULL );
838                             aSel->ClearIObjects();
839                           }
840                           aSel->SetSelectionMode(2, true);
841                           bool add = aSel->AddOrRemoveIndex( IO, pickedID, true, false);
842                           if(edgeId >= 0)
843                             add = aSel->AddOrRemoveIndex( IO, -edgeId-1, true, true );
844                           aSel->AddIObject( IO, false );
845                         }
846                       }
847                     }
848                   }
849                 } else {
850                   this->HighlightProp( NULL );
851                   aSel->ClearIObjects();
852                 }
853               }
854             }
855           } else if ( rwi->GetPicker()->IsA("vtkPointPicker") ) {
856             vtkPointPicker* picker;
857             if ( (picker = vtkPointPicker::SafeDownCast(rwi->GetPicker())) ) {
858               MESSAGE ( " PointId : " << picker->GetPointId() );
859               if ( picker->GetPointId() >= 0 ) {
860                 vtkActor* ac = picker->GetActor();
861                 if ( ac->IsA("SALOME_Actor") ) {
862                   SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( ac );
863                   MESSAGE ( " NAME Actor : " << SActor->getName() );
864                   if ( SActor->hasIO() ) {
865                     Handle(SALOME_InteractiveObject) IO = SActor->getIO();
866 /*                  if (IO.IsNull()) 
867                       break;
868                     if (aSelActiveCompOnly && 
869                         strcmp(aActiveComponent->ComponentDataType(), IO->getComponentDataType()) != 0) {
870                       break;
871                     }*/
872                     // Look in the current selection
873                     SALOME_ListIteratorOfListIO It(aSel->StoredIObjects());
874                     Standard_Boolean IsSelected = false;
875                     for(;It.More();It.Next()) {
876                       Handle(SALOME_InteractiveObject) IOS = It.Value();
877                       if(IO->isSame(IOS)) {
878                         IO = IOS; //Added by SRN, fix SAL1307
879                         IsSelected = true;
880                         break;
881                       }
882                     }
883                     if(IsSelected) {
884                       // This IO is already in the selection
885                       bool add = aSel->AddOrRemoveIndex( IO, picker->GetPointId(), myShiftState, false );
886                     } else {
887                       if(!myShiftState) {
888                         this->HighlightProp( NULL );
889                         aSel->ClearIObjects();
890                       }
891                       bool add = aSel->AddOrRemoveIndex( IO, picker->GetPointId(), myShiftState, false );
892                       aSel->AddIObject( IO, false );
893                     }
894                   }
895                 } 
896               } else {
897                 this->HighlightProp( NULL );
898                 aSel->ClearIObjects();
899               } 
900             }
901           } else {
902             vtkPicker* picker;
903             if ( (picker = vtkPicker::SafeDownCast(rwi->GetPicker())) ) {
904               listactors = picker->GetActors();
905             }
906             if ( listactors->GetNumberOfItems() == 0 ) {
907               // No selection clear all
908               this->PropPicked = 0;
909               this->HighlightProp( NULL );
910               aSel->ClearIObjects();
911             } else {
912               vtkActor* ac;
913               listactors->InitTraversal();
914               ac = listactors->GetNextActor();
915               if ( ac->IsA("SALOME_Actor") ) {
916                 SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( ac );
917                 if ( SActor->hasIO() ) {      
918                   this->PropPicked++;
919                   Handle(SALOME_InteractiveObject) IO = SActor->getIO();
920                   // Look in the current selection
921                   SALOME_ListIteratorOfListIO It(aSel->StoredIObjects());
922                   Standard_Boolean IsSelected = false;
923                   for(;It.More();It.Next()) {
924                     Handle(SALOME_InteractiveObject) IOS = It.Value();
925                     if( IO->isSame(IOS) ) {
926                       IO = IOS; //Added by SRN, fix SAL1307
927                       IsSelected = true;
928                       break;
929                     }
930                   }
931                   if(IsSelected) {
932                     // This IO is already in the selection
933                     if(myShiftState) {
934                       aSel->RemoveIObject(IO);
935                     }
936                   }
937                   else {
938                     if(!myShiftState) {
939                       this->HighlightProp( NULL );
940                       aSel->ClearIObjects();
941                     }
942                     aSel->AddIObject( IO, false );
943                   }
944                 }
945               }
946             }
947             rwi->EndPickCallback();
948           }
949         } else {
950           //processing rectangle selection
951           rwi->StartPickCallback();
952
953           if (!myShiftState) {
954             this->PropPicked = 0;
955             this->HighlightProp( NULL );
956             aSel->ClearIObjects();
957           }
958
959           // Compute bounds
960           vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
961           QRect rect(myPoint, myOtherPoint);
962           rect = rect.normalize();
963           int w, h, x, y;
964           rwi->GetSize(w, h);
965           int x1, y1, x2, y2;
966           x1 = rect.left(); 
967           y1 = h - rect.top() - 1;
968           x2 = rect.right(); 
969           y2 = h - rect.bottom() - 1;
970
971           switch (aSelectionMode) {
972           case 1: // Nodes selection
973             {
974               if (! rwi->GetPicker()->IsA("vtkPointPicker") ) break;
975               vtkPointPicker* aPointPicker = vtkPointPicker::SafeDownCast(rwi->GetPicker());
976               vtkActorCollection* aListActors = this->CurrentRenderer->GetActors();
977               aListActors->InitTraversal();
978               vtkActor* aActor;
979               for (int k = 0; k < aListActors->GetNumberOfItems(); k++) {
980                 aActor = aListActors->GetNextActor();
981                 if (aActor != NULL) {
982                   if (aActor->GetVisibility() == 0) 
983                     continue;
984                   vtkAbstractMapper3D* aMapper3D = aActor->GetMapper();
985                   if ((aMapper3D != NULL) && (aActor->IsA("SALOME_Actor"))) {
986                     SALOME_Actor* SActor = SALOME_Actor::SafeDownCast(aActor);
987
988                     if ((SActor != NULL) && (SActor->hasIO())) {
989                       Handle(SALOME_InteractiveObject) IO = SActor->getIO();
990                       if (IO.IsNull()) 
991                         continue;
992                       if (aSelActiveCompOnly && 
993                               strcmp(aActiveComponent->ComponentDataType(), IO->getComponentDataType()) != 0) {
994                         continue;
995                       }
996
997                       vtkMapper*       aMapper;
998                       vtkVolumeMapper* aVolumeMapper;
999                       vtkDataSet*      aDataSet;
1000                       
1001                       if ( (aMapper = vtkMapper::SafeDownCast(aMapper3D)) != NULL ) {
1002                         aDataSet = aMapper->GetInput();
1003                       } else if ((aVolumeMapper = vtkVolumeMapper::SafeDownCast(aMapper3D)) != NULL ){
1004                         aDataSet = aVolumeMapper->GetInput();
1005                       } else {
1006                         continue;
1007                       }
1008                       if (aDataSet) {
1009                         for (int i=0; i < aDataSet->GetNumberOfPoints(); i++) {
1010                           float* aPoint;
1011                           aPoint = aDataSet->GetPoint(i);
1012                           if (IsInRect(aPoint,  x1, y1, x2, y2)) {
1013                             float aDisp[3];
1014                             ComputeWorldToDisplay(aPoint[0],
1015                                                   aPoint[1],
1016                                                   aPoint[2], aDisp);
1017                             aPointPicker->Pick(aDisp[0], aDisp[1], 0.0, CurrentRenderer);
1018                             if ( aPointPicker->GetPointId() >= 0) { // && (!aSel->IsIndexSelected(IO, aPointPicker->GetPointId()))) {
1019                               aSel->AddOrRemoveIndex(IO, aPointPicker->GetPointId(), true, false);
1020                               aSel->AddIObject(IO, false);
1021                             }
1022                           }
1023                         }
1024                       }
1025                     }
1026                   }
1027                 }
1028               }
1029             }
1030             break;
1031           case 2: // edges selection
1032           case 3: // triangles selection
1033             {
1034               aSel->SetSelectionMode(aSelectionMode,true);
1035               if (!rwi->GetPicker()->IsA("vtkCellPicker") ) break;
1036               vtkCellPicker* aCellPicker = vtkCellPicker::SafeDownCast(rwi->GetPicker());
1037               vtkActorCollection* aListActors = this->CurrentRenderer->GetActors();
1038               aListActors->InitTraversal();
1039               vtkActor* aActor;
1040               for (int k = 0, kEnd = aListActors->GetNumberOfItems(); k < kEnd; k++){
1041                 vtkActor* aActor = aListActors->GetNextActor();
1042                 if (vtkActor* aActor = aListActors->GetNextActor()){
1043                   if (aActor->GetVisibility() == 0) continue;
1044                   if(SALOME_Actor* SActor = SALOME_Actor::SafeDownCast(aActor)) {
1045                     if(SActor->hasIO()) {
1046                       Handle(SALOME_InteractiveObject) IO = SActor->getIO();
1047                       if(IO.IsNull()) continue;
1048                       if(aSelActiveCompOnly)
1049                         if(strcmp(aActiveComponent->ComponentDataType(),IO->getComponentDataType()) != 0)
1050                           continue;
1051                       if(vtkDataSet* aDataSet = SActor->GetMapper()->GetInput()){
1052                         for(int i = 0, iEnd = aDataSet->GetNumberOfCells(); i < iEnd; i++){
1053                           if(vtkCell* aCell = aDataSet->GetCell(i)){
1054                             if(IsInRect(aCell,  x1, y1, x2, y2)){
1055                               float* aBounds = aCell->GetBounds();
1056                               float aCenter[3];
1057                               aCenter[0] =(aBounds[0] + aBounds[1])/2; // Center X
1058                               aCenter[1] =(aBounds[2] + aBounds[3])/2; // Center Y
1059                               aCenter[2] =(aBounds[4] + aBounds[5])/2; // Center Z
1060                               float aDisp[3];
1061                               ComputeWorldToDisplay(aCenter[0],aCenter[1],aCenter[2],aDisp);
1062                               aCellPicker->Pick(aDisp[0], aDisp[1], 0.0, CurrentRenderer);
1063                               if(aCellPicker->GetCellId() >= 0 && !aSel->IsIndexSelected(IO,aCellPicker->GetCellId())){
1064                                 aSel->AddOrRemoveIndex( IO, aCellPicker->GetCellId(), true, false);
1065                                 aSel->AddIObject( IO, false );
1066                               }
1067                             }
1068                           }
1069                         }
1070                       }
1071                     }
1072                   }
1073                 }
1074               }
1075             }
1076             break;
1077           case 4: // objects selection
1078             {
1079               vtkActorCollection* aListActors = this->CurrentRenderer->GetActors();
1080               aListActors->InitTraversal();
1081               vtkActor* aActor;
1082               SALOME_ListIO aListIO;
1083               for (int k = 0; k < aListActors->GetNumberOfItems(); k++) {
1084                 aActor = aListActors->GetNextActor();
1085                 if (aActor) {
1086                   if (aActor->GetVisibility() == 0) 
1087                     continue;
1088                   if ( aActor->IsA("SALOME_Actor") ) {
1089                     SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast(aActor);
1090                     if ( aSActor->hasIO() && IsInRect(aSActor, x1, y1, x2, y2)) {   
1091                       Handle(SALOME_InteractiveObject) aIO = aSActor->getIO();
1092                       if (aIO.IsNull())
1093                         continue;
1094                       if (aSelActiveCompOnly && 
1095                               strcmp(aActiveComponent->ComponentDataType(), aIO->getComponentDataType()) != 0) {
1096                         continue;
1097                       }
1098                       if (aListIO.IsEmpty()) {
1099                         aListIO.Append( aIO );
1100                       } else {
1101                         SALOME_ListIteratorOfListIO It(aListIO);
1102                         bool isStored = false;
1103                         for(;It.More();It.Next()) {
1104                           Handle(SALOME_InteractiveObject) IOS = It.Value();
1105                           if( aIO->isSame(IOS) ) {
1106                             aIO = IOS; //Added by SRN, fix SAL1307
1107                             isStored = true;
1108                             break;
1109                           }
1110                         }
1111                         if (!isStored)
1112                           aListIO.Append( aIO );
1113                       }
1114                     }
1115                   }
1116                 }
1117               }
1118               if (!aListIO.IsEmpty()) {
1119                 SALOME_ListIteratorOfListIO It(aListIO);
1120                 for(;It.More();It.Next()) {
1121                   Handle(SALOME_InteractiveObject) IOS = It.Value();
1122                   this->PropPicked++;
1123                   aSel->AddIObject( IOS, false );
1124                 }
1125               }
1126             } // end case 4
1127           } //end switch
1128           rwi->EndPickCallback();
1129         }
1130         aActiveStudy->update3dViewers();
1131       } 
1132     } 
1133     break;
1134   case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
1135   case VTK_INTERACTOR_STYLE_CAMERA_PAN:
1136   case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
1137   case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
1138     break;
1139   case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN: 
1140     {
1141       int w, h, x, y;
1142       rwi->GetSize(w, h);
1143       x = myPoint.x(); 
1144       y = h - myPoint.y() - 1;
1145       Place(x, y);
1146     }
1147     break;
1148   }
1149 }
1150
1151 // called during viewer operation when user moves mouse (!put necessary processing here!)
1152 void VTKViewer_InteractorStyleSALOME::onOperation(QPoint mousePos) 
1153 {
1154   if (!myGUIWindow) return;
1155   int w, h;
1156   GetInteractor()->GetSize(w, h);
1157   switch (State) {
1158   case VTK_INTERACTOR_STYLE_CAMERA_PAN: 
1159     {
1160       // processing panning
1161       //this->FindPokedCamera(mousePos.x(), mousePos.y());
1162       this->PanXY(mousePos.x(), myPoint.y(), myPoint.x(), mousePos.y());
1163       myPoint = mousePos;
1164       break;
1165     }
1166   case VTK_INTERACTOR_STYLE_CAMERA_ZOOM: 
1167     {    
1168       // processing zooming
1169       //this->FindPokedCamera(mousePos.x(), mousePos.y());
1170       this->DollyXY(mousePos.x() - myPoint.x(), mousePos.y() - myPoint.y());
1171       myPoint = mousePos;
1172       break;
1173     }
1174   case VTK_INTERACTOR_STYLE_CAMERA_ROTATE: 
1175     {
1176       // processing rotation
1177       //this->FindPokedCamera(mousePos.x(), mousePos.y());
1178       this->RotateXY(mousePos.x() - myPoint.x(), myPoint.y() - mousePos.y());
1179       myPoint = mousePos;
1180       break;
1181     }
1182   case VTK_INTERACTOR_STYLE_CAMERA_SPIN: 
1183     {
1184       // processing spinning
1185       //this->FindPokedCamera(mousePos.x(), mousePos.y());
1186       this->SpinXY(mousePos.x(), mousePos.y(), myPoint.x(), myPoint.y());
1187       myPoint = mousePos;
1188       break;
1189     }
1190   case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN: 
1191     {    
1192       break;
1193     }
1194   case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
1195     {
1196       if (!myCursorState)
1197         setCursor(VTK_INTERACTOR_STYLE_CAMERA_SELECT);
1198     }
1199   case VTK_INTERACTOR_STYLE_CAMERA_FIT:
1200     {
1201       QPainter p(myGUIWindow);
1202       p.setPen(Qt::lightGray);
1203       p.setRasterOp(Qt::XorROP);
1204       p.drawRect(QRect(myPoint, myOtherPoint));
1205       myOtherPoint = mousePos;
1206       p.drawRect(QRect(myPoint, myOtherPoint));
1207       break;
1208     }
1209   }
1210   this->LastPos[0] = mousePos.x();
1211   this->LastPos[1] = h - mousePos.y() - 1;
1212 }
1213
1214 // called when user moves mouse inside viewer window and there is no active viewer operation 
1215 // (!put necessary processing here!)
1216 void VTKViewer_InteractorStyleSALOME::onCursorMove(QPoint mousePos) {
1217   // processing highlighting
1218   QAD_Study* myActiveStudy = QAD_Application::getDesktop()->getActiveStudy();
1219   SALOME_Selection* Sel = SALOME_Selection::Selection( myActiveStudy->getSelection() );
1220       
1221   vtkRenderWindowInteractor *rwi = this->Interactor;
1222   int w, h, x, y;
1223   rwi->GetSize(w, h);
1224   x = mousePos.x(); y = h - mousePos.y() - 1;
1225
1226   this->FindPokedRenderer(x,y);
1227   rwi->StartPickCallback();
1228   rwi->GetPicker()->Pick(x, y, 0.0, this->CurrentRenderer);
1229
1230   if ( rwi->GetPicker()->IsA("vtkPicker") ) {
1231     vtkPicker* picker = vtkPicker::SafeDownCast(rwi->GetPicker());
1232     vtkActor* ac = picker->GetActor();
1233     
1234     if ( ac != NULL ) {
1235       if ( ac->IsA("SALOME_Actor") ) {
1236         SALOME_Actor* SActor = SALOME_Actor::SafeDownCast( ac );
1237         if ( preview != SActor ) {
1238           if ( preview != NULL ) {
1239             preview->SetPreSelected( false );
1240           }
1241           preview = SActor;
1242               
1243           if ( SActor->hasIO() ) {
1244             Handle( SALOME_InteractiveObject) IO = SActor->getIO();
1245
1246             SALOME_ListIteratorOfListIO It(Sel->StoredIObjects());
1247             Standard_Boolean IsSelected = false;
1248             for(;It.More();It.Next()) {
1249               Handle(SALOME_InteractiveObject) IOS = It.Value();
1250               if(IO->isSame(IOS)) {
1251                 IsSelected = true;
1252                 break;
1253               }
1254             }
1255
1256             if ( !IsSelected ) {
1257             // Find All actors with same IO
1258               vtkActorCollection* theActors = this->CurrentRenderer->GetActors();
1259               theActors->InitTraversal();
1260               vtkActor *ac = theActors->GetNextActor();
1261               while( ac ) {
1262                 if ( ac->IsA("SALOME_Actor") ) {
1263                   SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
1264                   if ( anActor->hasIO() ) {
1265                     Handle(SALOME_InteractiveObject) IOS = anActor->getIO();
1266                     if(IO->isSame(IOS)) {
1267                       anActor->SetPreSelected( true );
1268                     }
1269                   }
1270                 }
1271                 ac = theActors->GetNextActor();
1272               }
1273               // MESSAGE ( " NAME PREVIEW " << SActor->getName() );
1274             }
1275           }
1276         }
1277       }
1278     } else {
1279       preview = NULL;
1280       vtkActorCollection* theActors = this->CurrentRenderer->GetActors();
1281       theActors->InitTraversal();
1282       vtkActor *ac = theActors->GetNextActor();
1283       while( ac ) {
1284         if ( ac->IsA("SALOME_Actor") ) {
1285           SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac );
1286           anActor->SetPreSelected( false );
1287         }
1288         ac = theActors->GetNextActor();
1289       }
1290     }
1291   }
1292   this->LastPos[0] = x;
1293   this->LastPos[1] = y;
1294 }
1295
1296 // called on finsh GlobalPan operation 
1297 void VTKViewer_InteractorStyleSALOME::Place(const int theX, const int theY) 
1298 {
1299   if (this->CurrentRenderer == NULL) {
1300     return;
1301   }
1302
1303   //translate view
1304   int *aSize = this->CurrentRenderer->GetRenderWindow()->GetSize();
1305   int centerX = aSize[0]/2;
1306   int centerY = aSize[1]/2;
1307
1308   TranslateView(centerX, centerY, theX, theY);
1309
1310   // restore zoom scale
1311   vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
1312   cam->SetParallelScale(myScale);
1313   this->CurrentRenderer->ResetCameraClippingRange();
1314
1315   /* VSV Light follows camera: if (this->CurrentLight) {
1316     this->CurrentLight->SetPosition(cam->GetPosition());
1317     this->CurrentLight->SetFocalPoint(cam->GetFocalPoint());
1318     }*/
1319   //VTKViewer_RenderWindow* aRW = dynamic_cast<VTKViewer_RenderWindow*>(this->Interactor->GetRenderWindow());
1320   if (myGUIWindow) myGUIWindow->update();
1321 }
1322
1323
1324
1325 // Translates view from Point to Point
1326 void VTKViewer_InteractorStyleSALOME::TranslateView(int toX, int toY, int fromX, int fromY)
1327 {
1328   vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
1329   double viewFocus[4], focalDepth, viewPoint[3];
1330   float newPickPoint[4], oldPickPoint[4], motionVector[3];
1331   cam->GetFocalPoint(viewFocus);
1332
1333   this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1],
1334                               viewFocus[2], viewFocus);
1335   focalDepth = viewFocus[2];
1336
1337   this->ComputeDisplayToWorld(double(toX), double(toY),
1338                               focalDepth, newPickPoint);
1339   this->ComputeDisplayToWorld(double(fromX),double(fromY),
1340                               focalDepth, oldPickPoint);
1341   
1342   // camera motion is reversed
1343   motionVector[0] = oldPickPoint[0] - newPickPoint[0];
1344   motionVector[1] = oldPickPoint[1] - newPickPoint[1];
1345   motionVector[2] = oldPickPoint[2] - newPickPoint[2];
1346   
1347   cam->GetFocalPoint(viewFocus);
1348   cam->GetPosition(viewPoint);
1349   cam->SetFocalPoint(motionVector[0] + viewFocus[0],
1350                      motionVector[1] + viewFocus[1],
1351                      motionVector[2] + viewFocus[2]);
1352   cam->SetPosition(motionVector[0] + viewPoint[0],
1353                    motionVector[1] + viewPoint[1],
1354                    motionVector[2] + viewPoint[2]);
1355 }
1356
1357
1358 /// Checks: is the given Actor within display coordinates?
1359 bool VTKViewer_InteractorStyleSALOME::IsInRect(vtkActor* theActor, 
1360                                                const int left, const int top, 
1361                                                const int right, const int bottom)
1362 {
1363   float* aBounds = theActor->GetBounds();
1364   float aMin[3], aMax[3];
1365   ComputeWorldToDisplay(aBounds[0], aBounds[2], aBounds[4], aMin);
1366   ComputeWorldToDisplay(aBounds[1], aBounds[3], aBounds[5], aMax);
1367   if (aMin[0] > aMax[0]) {
1368     float aBuf = aMin[0];
1369     aMin[0] = aMax[0];
1370     aMax[0] = aBuf;
1371   }
1372   if (aMin[1] > aMax[1]) {
1373     float aBuf = aMin[1];
1374     aMin[1] = aMax[1];
1375     aMax[1] = aBuf;    
1376   }
1377
1378   return ((aMin[0]>left) && (aMax[0]<right) && (aMin[1]>bottom) && (aMax[1]<top));
1379 }
1380
1381
1382 /// Checks: is the given Cell within display coordinates?
1383 bool VTKViewer_InteractorStyleSALOME::IsInRect(vtkCell* theCell, 
1384                                                const int left, const int top, 
1385                                                const int right, const int bottom)
1386 {
1387   float* aBounds = theCell->GetBounds();
1388   float aMin[3], aMax[3];
1389   ComputeWorldToDisplay(aBounds[0], aBounds[2], aBounds[4], aMin);
1390   ComputeWorldToDisplay(aBounds[1], aBounds[3], aBounds[5], aMax);
1391   if (aMin[0] > aMax[0]) {
1392     float aBuf = aMin[0];
1393     aMin[0] = aMax[0];
1394     aMax[0] = aBuf;
1395   }
1396   if (aMin[1] > aMax[1]) {
1397     float aBuf = aMin[1];
1398     aMin[1] = aMax[1];
1399     aMax[1] = aBuf;    
1400   }
1401
1402   return ((aMin[0]>left) && (aMax[0]<right) && (aMin[1]>bottom) && (aMax[1]<top));
1403 }
1404
1405
1406 bool VTKViewer_InteractorStyleSALOME::IsInRect(float* thePoint, 
1407                                                const int left, const int top, 
1408                                                const int right, const int bottom)
1409 {
1410   float aPnt[3];
1411   ComputeWorldToDisplay(thePoint[0], thePoint[1], thePoint[2], aPnt);
1412
1413   return ((aPnt[0]>left) && (aPnt[0]<right) && (aPnt[1]>bottom) && (aPnt[1]<top));
1414 }