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