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