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