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