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