Salome HOME
updated copyright message
[modules/gui.git] / src / VTKViewer / VTKViewer_InteractorStyle.cxx
1 // Copyright (C) 2007-2023  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, or (at your option) any later version.
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 /*!View fit selection.*/
632 void  VTKViewer_InteractorStyle::ViewFitSelection() {
633
634   vtkActorCollection* aSelectedCollection = vtkActorCollection::New();
635
636   VTK::ActorCollectionCopy aCopy( CurrentRenderer->GetActors() );
637   vtkActorCollection* aCollection = aCopy.GetActors();
638   aCollection->InitTraversal();
639   while ( vtkActor* aProp = aCollection->GetNextActor() )
640     if ( VTKViewer_Actor* anActor = VTKViewer_Actor::SafeDownCast( aProp ) )
641       if ( anActor->isPreselected() )
642         aSelectedCollection->AddItem( aProp );
643
644   double bounds[6];
645   ::ComputeBounds( aSelectedCollection, bounds );
646
647   if ( aSelectedCollection->GetNumberOfItems() && ::isBoundValid( bounds ) ) {
648     CurrentRenderer->ResetCamera( bounds );
649     CurrentRenderer->ResetCameraClippingRange( bounds );
650   }
651 }
652
653
654 /*! starts Global Panning operation (e.g. through menu command)*/
655 void VTKViewer_InteractorStyle::startGlobalPan()
656 {
657   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
658   {
659     onFinishOperation();
660     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
661   }
662   setCursor(VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN);
663   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN;
664
665   // store current zoom scale
666   vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
667   myScale = cam->GetParallelScale();
668
669   ViewFitAll();
670
671   if (myGUIWindow) myGUIWindow->update();
672   
673   qApp->installEventFilter(this);
674 }
675
676
677 /*!\retval \c true if needs redrawing*/
678 bool VTKViewer_InteractorStyle::needsRedrawing()
679 {
680   return State == VTK_INTERACTOR_STYLE_CAMERA_ZOOM   ||
681          State == VTK_INTERACTOR_STYLE_CAMERA_PAN    ||
682          State == VTK_INTERACTOR_STYLE_CAMERA_ROTATE ||
683          State == VTK_INTERACTOR_STYLE_CAMERA_SPIN   ||
684          State == VTK_INTERACTOR_STYLE_CAMERA_NONE;
685 }
686
687
688 /*! fits viewer contents to rectangle
689  *\param left - left side
690  *\param top  - top side
691  *\param right  - right side
692  *\param bottom  - bottom side 
693  */
694 void VTKViewer_InteractorStyle::fitRect(const int left, 
695                                        const int top, 
696                                        const int right, 
697                                        const int bottom)
698 {
699   if (this->CurrentRenderer == NULL) return;
700  
701   // move camera
702   int x = (left + right)/2;
703   int y = (top + bottom)/2;
704   int *aSize = this->CurrentRenderer->GetRenderWindow()->GetSize();
705   int oldX = aSize[0]/2;
706   int oldY = aSize[1]/2;
707   TranslateView(oldX, oldY, x, y);
708
709   // zoom camera
710   double dxf = (double)(aSize[0]) / (double)(abs(right - left));
711   double dyf = (double)(aSize[1]) / (double)(abs(bottom - top));
712   double zoomFactor = (dxf + dyf)/2 ;
713
714   vtkCamera *aCam = this->CurrentRenderer->GetActiveCamera();
715   if(aCam->GetParallelProjection())
716     aCam->SetParallelScale(aCam->GetParallelScale()/zoomFactor);
717   else{
718     aCam->Dolly(zoomFactor);
719     ::ResetCameraClippingRange(this->CurrentRenderer);
720   }
721   
722   myGUIWindow->update();
723 }
724
725
726 /*! starts viewer operation (!internal usage!)*/
727 void VTKViewer_InteractorStyle::startOperation(int operation)
728 {
729   switch(operation)
730   { 
731   case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
732   case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
733   case VTK_INTERACTOR_STYLE_CAMERA_PAN:
734   case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
735   case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
736   case VTK_INTERACTOR_STYLE_CAMERA_FIT:
737   case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
738     if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
739       startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
740     State = operation;
741     if (State != VTK_INTERACTOR_STYLE_CAMERA_SELECT)
742       setCursor(operation);
743     onStartOperation();
744     break;
745   case VTK_INTERACTOR_STYLE_CAMERA_NONE:
746   default:
747     setCursor(VTK_INTERACTOR_STYLE_CAMERA_NONE);
748     State = ForcedState = VTK_INTERACTOR_STYLE_CAMERA_NONE;
749     break;
750   }
751 }
752
753
754 /*! sets proper cursor for window when viewer operation is activated*/
755 void VTKViewer_InteractorStyle::setCursor(const int operation)
756 {
757   if (!myGUIWindow) return;
758   switch (operation)
759   {
760     case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
761       myGUIWindow->setCursor(myZoomCursor); 
762       myCursorState = true;
763       break;
764     case VTK_INTERACTOR_STYLE_CAMERA_PAN:
765       myGUIWindow->setCursor(myPanCursor); 
766       myCursorState = true;
767       break;
768     case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
769       myGUIWindow->setCursor(myRotateCursor); 
770       myCursorState = true;
771       break;
772     case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
773       myGUIWindow->setCursor(mySpinCursor); 
774       myCursorState = true;
775       break;
776     case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
777       myGUIWindow->setCursor(myGlobalPanCursor); 
778       myCursorState = true;
779       break;
780     case VTK_INTERACTOR_STYLE_CAMERA_FIT:
781     case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
782       myGUIWindow->setCursor(myHandCursor); 
783       myCursorState = true;
784       break;
785     case VTK_INTERACTOR_STYLE_CAMERA_NONE:
786     default:
787       myGUIWindow->setCursor(myDefCursor); 
788       myCursorState = false;
789       break;
790   }
791 }
792
793 /*!
794   Draws rectangle by starting and current points
795 */
796 void VTKViewer_InteractorStyle::drawRect()
797 {
798   if ( !myRectBand ) {
799     myRectBand = new QRubberBand( QRubberBand::Rectangle, myGUIWindow );
800     QPalette palette;
801     palette.setColor(myRectBand->foregroundRole(), Qt::white);
802     myRectBand->setPalette(palette);
803   }
804   myRectBand->hide();
805
806   QRect aRect(myPoint, myOtherPoint);
807   myRectBand->setGeometry( aRect );
808   myRectBand->setVisible( aRect.isValid() );
809 }
810
811 /*!
812   \brief Delete rubber band on the end on the dragging operation.
813 */
814 void VTKViewer_InteractorStyle::endDrawRect()
815 {
816   delete myRectBand;
817   myRectBand = 0;
818 }
819
820 /*! called when viewer operation started (!put necessary initialization here!)*/
821 void VTKViewer_InteractorStyle::onStartOperation()
822 {
823   if (!myGUIWindow) return;
824   // VSV: LOD actor activisation
825   //  this->Interactor->GetRenderWindow()->SetDesiredUpdateRate(this->Interactor->GetDesiredUpdateRate());
826   switch (State) {
827     case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
828     case VTK_INTERACTOR_STYLE_CAMERA_FIT:
829     {
830       drawRect();
831       break;
832     }
833     case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
834     case VTK_INTERACTOR_STYLE_CAMERA_PAN:
835     case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
836     case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN:
837     case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
838       break;
839   }
840 }
841
842
843 /*! called when viewer operation finished (!put necessary post-processing here!)*/
844 void VTKViewer_InteractorStyle::onFinishOperation() 
845 {
846   if (!myGUIWindow) return;
847
848
849 //  SUIT_Study* aActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
850 //  SALOME_Selection* aSel    = SALOME_Selection::Selection( aActiveStudy->getSelection() );
851
852   // VSV: LOD actor activisation
853   //  rwi->GetRenderWindow()->SetDesiredUpdateRate(rwi->GetStillUpdateRate());
854
855 //  Selection_Mode aSelectionMode = aSel->SelectionMode();
856 //  bool aSelActiveCompOnly = aSel->IsSelectActiveCompOnly();
857
858 /*  switch (State) {
859     case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
860     case VTK_INTERACTOR_STYLE_CAMERA_FIT:
861     {
862       QPainter p(myGUIWindow);
863       p.setPen(Qt::lightGray);
864       p.setRasterOp(Qt::XorROP);
865       QRect rect(myPoint, myOtherPoint);
866       p.drawRect(rect);
867       rect = rect.normalize();
868       if (State == VTK_INTERACTOR_STYLE_CAMERA_FIT) {
869         // making fit rect opeation 
870         int w, h;
871         m_Interactor->GetSize(w, h);
872         int x1, y1, x2, y2;
873         x1 = rect.left(); 
874         y1 = h - rect.top() - 1;
875         x2 = rect.right(); 
876         y2 = h - rect.bottom() - 1;
877         fitRect(x1, y1, x2, y2);
878       }
879       else {
880         if (myPoint == myOtherPoint) {
881           // process point selection
882           int w, h, x, y;
883           m_Interactor->GetSize(w, h);
884           x = myPoint.x(); 
885           y = h - myPoint.y() - 1;
886
887           this->FindPokedRenderer(x, y);
888           m_Interactor->StartPickCallback();
889
890           vtkPicker* aPicker = vtkPicker::SafeDownCast(m_Interactor->GetPicker());
891           aPicker->Pick(x, y, 0.0, this->CurrentRenderer);
892     
893           SALOME_Actor* SActor = SALOME_Actor::SafeDownCast(aPicker->GetActor());
894
895           if (vtkCellPicker* picker = vtkCellPicker::SafeDownCast(aPicker)) {
896             int aVtkId = picker->GetCellId();
897             if ( aVtkId >= 0 && SActor && SActor->hasIO() && IsValid( SActor, aVtkId ) ) {
898               int anObjId = SActor->GetElemObjId(aVtkId);
899               if(anObjId >= 0){
900                 Handle(SALOME_InteractiveObject) IO = SActor->getIO();
901                 if(aSelectionMode != EdgeOfCellSelection) {
902                   if(CheckDimensionId(aSelectionMode,SActor,anObjId)){
903                     if (IsSelected(IO,aSel)) {
904                       // This IO is already in the selection
905                       aSel->AddOrRemoveIndex( IO, anObjId, myShiftState, false );
906                     } else {
907                       if (!myShiftState) {
908                         this->HighlightProp( NULL );
909                         aSel->ClearIObjects();
910                       }
911                       aSel->AddOrRemoveIndex( IO, anObjId, myShiftState, false );
912                       aSel->AddIObject( IO, false );
913                     }
914                   }
915                 }else{
916                   if (!myShiftState) {
917                     this->HighlightProp( NULL );
918                     aSel->ClearIObjects();
919                   }
920                   int anEdgeId = GetEdgeId(picker,SActor,anObjId);
921                   if (anEdgeId >= 0) {
922                     aSel->AddOrRemoveIndex( IO, anObjId, true, false);
923                     aSel->AddOrRemoveIndex( IO, -anEdgeId-1, true, true );
924                     aSel->AddIObject( IO, false );
925                   } 
926                 }
927               }
928             } else {
929               this->HighlightProp( NULL );
930               aSel->ClearIObjects();
931             }
932           } else if ( vtkPointPicker* picker = vtkPointPicker::SafeDownCast(aPicker) ) {
933             int aVtkId = picker->GetPointId();
934             if ( aVtkId >= 0 && IsValid( SActor, aVtkId, true ) ) {
935               if ( SActor && SActor->hasIO() ) {
936                 int anObjId = SActor->GetNodeObjId(aVtkId);
937                 if(anObjId >= 0){
938                   Handle(SALOME_InteractiveObject) IO = SActor->getIO();
939                   if(IsSelected(IO,aSel)) {
940                     // This IO is already in the selection
941                     aSel->AddOrRemoveIndex( IO, anObjId, myShiftState, false );
942                   } else {
943                     if(!myShiftState) {
944                       this->HighlightProp( NULL );
945                       aSel->ClearIObjects();
946                     }
947                     aSel->AddOrRemoveIndex( IO, anObjId, myShiftState, false );
948                     aSel->AddIObject( IO, false );
949                   }
950                 }
951               }
952             } else {
953               this->HighlightProp( NULL );
954               aSel->ClearIObjects();
955             } 
956           } else {
957             if ( SActor && SActor->hasIO() ) {
958               this->PropPicked++;
959               Handle(SALOME_InteractiveObject) IO = SActor->getIO();
960               if(IsSelected(IO,aSel)) {
961                 // This IO is already in the selection
962                 if(myShiftState) {
963                   aSel->RemoveIObject(IO);
964                 }
965               }
966               else {
967                 if(!myShiftState) {
968                   this->HighlightProp( NULL );
969                   aSel->ClearIObjects();
970                 }
971                 aSel->AddIObject( IO, false );
972               }
973             }else{
974               // No selection clear all
975               this->PropPicked = 0;
976               this->HighlightProp( NULL );
977               aSel->ClearIObjects();
978             }
979           }
980           m_Interactor->EndPickCallback();
981         } else {
982           //processing rectangle selection
983           QString aComponentDataType = SUIT_Application::getDesktop()->getComponentDataType();
984           if(aSelActiveCompOnly && aComponentDataType.isEmpty()) return;
985           m_Interactor->StartPickCallback();
986
987           if (!myShiftState) {
988             this->PropPicked = 0;
989             this->HighlightProp( NULL );
990             aSel->ClearIObjects();
991           }
992
993           // Compute bounds
994           //      vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
995           QRect rect(myPoint, myOtherPoint);
996           rect = rect.normalize();
997           int w, h;
998           m_Interactor->GetSize(w, h);
999           int x1, y1, x2, y2;
1000           x1 = rect.left(); 
1001           y1 = h - rect.top() - 1;
1002           x2 = rect.right(); 
1003           y2 = h - rect.bottom() - 1;
1004
1005           switch (aSelectionMode) {
1006           case NodeSelection: {
1007             if ( vtkPointPicker* aPointPicker = vtkPointPicker::SafeDownCast(m_Interactor->GetPicker()) ) {
1008               vtkActorCollection* aListActors = this->CurrentRenderer->GetActors();
1009               aListActors->InitTraversal();
1010               while (vtkActor* aActor = aListActors->GetNextActor()) {
1011                 if (!aActor->GetVisibility()) 
1012                   continue;
1013                 if(SALOME_Actor* SActor = SALOME_Actor::SafeDownCast(aActor)) {
1014                   if (SActor->hasIO()) {
1015                     Handle(SALOME_InteractiveObject) IO = SActor->getIO();
1016                     if (IO.IsNull()) 
1017                       continue;
1018                     if (aSelActiveCompOnly && aComponentDataType != IO->getComponentDataType())
1019                       continue;
1020                     if (vtkDataSet* aDataSet = SActor->GetInput()) {
1021                       SALOME_Selection::TContainerOfId anIndices;
1022                       for(int i = 0; i < aDataSet->GetNumberOfPoints(); i++) {
1023                         float aPoint[3];
1024                         aDataSet->GetPoint(i,aPoint);
1025                         if (IsInRect(aPoint,x1,y1,x2,y2)){
1026                           float aDisp[3];
1027                           ComputeWorldToDisplay(aPoint[0],aPoint[1],aPoint[2],aDisp);
1028                           if(aPointPicker->Pick(aDisp[0],aDisp[1],0.0,CurrentRenderer)){
1029                             if(vtkActorCollection *anActorCollection = aPointPicker->GetActors()){
1030                               if(anActorCollection->IsItemPresent(SActor)){
1031                                 float aPickedPoint[3];
1032                                 aPointPicker->GetMapperPosition(aPickedPoint);
1033                                 vtkIdType aVtkId = aDataSet->FindPoint(aPickedPoint);
1034                                 if ( aVtkId >= 0 && IsValid( SActor, aVtkId, true ) ){
1035                                   int anObjId = SActor->GetNodeObjId(aVtkId);
1036                                   anIndices.insert(anObjId);
1037                                 }
1038                               }
1039                             }
1040                           }
1041                         }
1042                       }
1043                       if (!anIndices.empty()) {
1044                         aSel->AddOrRemoveIndex(IO, anIndices, true, false);
1045                         aSel->AddIObject(IO, false);
1046                         anIndices.clear();
1047                       }else{
1048                         aSel->RemoveIObject(IO, false);
1049                       }
1050                     }
1051                   }
1052                 }
1053               }
1054             }
1055             break;
1056           }
1057           case CellSelection:
1058           case EdgeOfCellSelection:
1059           case EdgeSelection:
1060           case FaceSelection:
1061           case VolumeSelection: 
1062             {
1063               vtkSmartPointer<VTKViewer_CellAreaPicker> picker = VTKViewer_CellRectPicker::New();
1064               picker->SetTolerance(0.001);
1065               picker->Pick(x1, y1, 0.0, x2, y2, 0.0, this->CurrentRenderer);
1066               
1067               vtkActorCollection* aListActors = picker->GetActors();
1068               aListActors->InitTraversal();
1069               while(vtkActor* aActor = aListActors->GetNextActor()) {
1070                 if (SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast(aActor)) {
1071                   if (aSActor->hasIO()) {
1072                     Handle(SALOME_InteractiveObject) aIO = aSActor->getIO();
1073                     if (aSelActiveCompOnly && aComponentDataType != aIO->getComponentDataType())
1074                       continue;
1075                     VTKViewer_CellDataSet cellList = picker->GetCellData(aActor);
1076                     if ( !cellList.empty() ) {
1077                       SALOME_Selection::TContainerOfId anIndexes;
1078                       VTKViewer_CellDataSet::iterator it;
1079                       for ( it = cellList.begin(); it != cellList.end(); ++it ) {
1080                         int aCellId = (*it).cellId;
1081                         
1082                         if ( !IsValid( aSActor, aCellId ) )
1083                           continue;
1084                         
1085                         int anObjId = aSActor->GetElemObjId(aCellId);
1086                         if (anObjId != -1){
1087                           if ( CheckDimensionId(aSelectionMode,aSActor,anObjId) ) {
1088                             anIndexes.insert(anObjId);
1089                           }
1090                         }
1091                       }
1092                       aSel->AddOrRemoveIndex(aIO, anIndexes, true, false);
1093                       aSel->AddIObject(aIO, false);
1094                     }
1095                   }
1096                 }
1097               }
1098             }
1099             break;          
1100           case ActorSelection: // objects selection
1101             {
1102               vtkSmartPointer<VTKViewer_AreaPicker> picker = VTKViewer_AreaPicker::New();
1103               picker->SetTolerance(0.001);
1104               picker->Pick(x1, y1, 0.0, x2, y2, 0.0, this->CurrentRenderer);
1105
1106               vtkActorCollection* aListActors = picker->GetActors();
1107               SALOME_ListIO aListIO;
1108               aListActors->InitTraversal();
1109               while(vtkActor* aActor = aListActors->GetNextActor()) {
1110                 if (SALOME_Actor* aSActor = SALOME_Actor::SafeDownCast(aActor)) {
1111                   if (aSActor->hasIO()) {
1112                     Handle(SALOME_InteractiveObject) aIO = aSActor->getIO();
1113                     if (!IsStored(aIO,aListIO))
1114                       aListIO.Append(aIO);
1115                   }
1116                 }
1117               }
1118               if (!aListIO.IsEmpty()) {
1119                 SALOME_ListIteratorOfListIO It(aListIO);
1120                 for(;It.More();It.Next()) {
1121                   Handle(SALOME_InteractiveObject) IOS = It.Value();
1122                   this->PropPicked++;
1123                   aSel->AddIObject( IOS, false );
1124                 }
1125               }
1126             } // end case 4
1127           } //end switch
1128           m_Interactor->EndPickCallback();
1129         }
1130         aActiveStudy->update3dViewers();
1131       } 
1132     } 
1133     break;
1134   case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
1135   case VTK_INTERACTOR_STYLE_CAMERA_PAN:
1136   case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
1137   case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
1138     break;
1139   case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN: 
1140     {
1141       int w, h, x, y;
1142       m_Interactor->GetSize(w, h);
1143       x = myPoint.x(); 
1144       y = h - myPoint.y() - 1;
1145       Place(x, y);
1146     }
1147     break;
1148   }
1149   if (myGUIWindow) myGUIWindow->update();
1150 */
1151 }
1152
1153 /*! called during viewer operation when user moves mouse (!put necessary processing here!)*/
1154 void VTKViewer_InteractorStyle::onOperation(QPoint mousePos) 
1155 {
1156   if (!myGUIWindow) return;
1157   int w, h;
1158   GetInteractor()->GetSize(w, h);
1159   switch (State) {
1160   case VTK_INTERACTOR_STYLE_CAMERA_PAN: 
1161     {
1162       // processing panning
1163       //this->FindPokedCamera(mousePos.x(), mousePos.y());
1164       this->PanXY(mousePos.x(), myPoint.y(), myPoint.x(), mousePos.y());
1165       myPoint = mousePos;
1166       break;
1167     }
1168   case VTK_INTERACTOR_STYLE_CAMERA_ZOOM: 
1169     {    
1170       // processing zooming
1171       //this->FindPokedCamera(mousePos.x(), mousePos.y());
1172       this->DollyXY(mousePos.x() - myPoint.x(), mousePos.y() - myPoint.y());
1173       myPoint = mousePos;
1174       break;
1175     }
1176   case VTK_INTERACTOR_STYLE_CAMERA_ROTATE: 
1177     {
1178       // processing rotation
1179       //this->FindPokedCamera(mousePos.x(), mousePos.y());
1180       this->RotateXY(mousePos.x() - myPoint.x(), myPoint.y() - mousePos.y());
1181       myPoint = mousePos;
1182       break;
1183     }
1184   case VTK_INTERACTOR_STYLE_CAMERA_SPIN: 
1185     {
1186       // processing spinning
1187       //this->FindPokedCamera(mousePos.x(), mousePos.y());
1188       this->SpinXY(mousePos.x(), mousePos.y(), myPoint.x(), myPoint.y());
1189       myPoint = mousePos;
1190       break;
1191     }
1192   case VTK_INTERACTOR_STYLE_CAMERA_GLOBAL_PAN: 
1193     {    
1194       break;
1195     }
1196   case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
1197     {
1198       if (!myCursorState)
1199         setCursor(VTK_INTERACTOR_STYLE_CAMERA_SELECT);
1200     } // fall through!
1201   case VTK_INTERACTOR_STYLE_CAMERA_FIT:
1202     {
1203       myOtherPoint = mousePos;
1204       drawRect();
1205       break;
1206     }
1207   }
1208   this->LastPos[0] = mousePos.x();
1209   this->LastPos[1] = h - mousePos.y() - 1;
1210 }
1211
1212 /*! called when selection mode changed (!put necessary initialization here!)*/
1213 void VTKViewer_InteractorStyle::OnSelectionModeChanged()
1214 {
1215   
1216   myPreSelectionActor->SetVisibility(false);
1217   myElemId = myEdgeId = myNodeId = -1;
1218   mySelectedActor = NULL;
1219 }
1220
1221 /*! called when user moves mouse inside viewer window and there is no active viewer operation \n
1222  * (!put necessary processing here!)
1223  */
1224 void VTKViewer_InteractorStyle::onCursorMove(QPoint /*mousePos*/) {
1225   // processing highlighting
1226 //  SUIT_Study* anActiveStudy = SUIT_Application::getDesktop()->getActiveStudy();
1227 //  SALOME_Selection* Sel = SALOME_Selection::Selection( anActiveStudy->getSelection() );
1228 //  Selection_Mode aSelectionMode = Sel->SelectionMode();
1229
1230 /*  int w, h, x, y;
1231   m_Interactor->GetSize(w, h);
1232   x = mousePos.x(); y = h - mousePos.y() - 1;
1233
1234   this->FindPokedRenderer(x,y);
1235   m_Interactor->StartPickCallback();
1236   myPreSelectionActor->SetVisibility(false);
1237
1238   vtkPicker* aPicker = vtkPicker::SafeDownCast(m_Interactor->GetPicker());
1239   aPicker->Pick(x, y, 0.0, this->CurrentRenderer);
1240
1241   SALOME_Actor* SActor = SALOME_Actor::SafeDownCast(aPicker->GetActor());
1242
1243   if (vtkCellPicker* picker = vtkCellPicker::SafeDownCast(aPicker)) {
1244     int aVtkId = picker->GetCellId();
1245     if ( aVtkId >= 0 ) {
1246       int anObjId = SActor->GetElemObjId(aVtkId);
1247       if ( SActor && SActor->hasIO() && IsValid( SActor, aVtkId ) ) {
1248         bool anIsSameObjId = (mySelectedActor == SActor && myElemId == anObjId);
1249         bool aResult = anIsSameObjId;
1250         if(!anIsSameObjId) {
1251           if(aSelectionMode != EdgeOfCellSelection) {
1252             aResult = CheckDimensionId(aSelectionMode,SActor,anObjId);
1253             if(aResult){
1254               mySelectedActor = SActor;
1255               myElemId = anObjId;
1256               m_Interactor->setCellData(anObjId,SActor,myPreSelectionActor);
1257             }
1258           }
1259         }
1260         if(aSelectionMode == EdgeOfCellSelection){
1261           int anEdgeId = GetEdgeId(picker,SActor,anObjId);
1262           bool anIsSameEdgeId = (myEdgeId != anEdgeId) && anIsSameObjId;
1263           aResult = anIsSameEdgeId;
1264           if(!anIsSameEdgeId) {
1265             aResult = (anEdgeId >= 0);
1266             if (aResult) {
1267               mySelectedActor = SActor;
1268               myEdgeId = anEdgeId;
1269               myElemId = anObjId;
1270               m_Interactor->setEdgeData(anObjId,SActor,-anEdgeId-1,myPreSelectionActor);
1271             } 
1272           }
1273         }
1274         if(aResult) {
1275           myPreSelectionActor->GetProperty()->SetRepresentationToSurface();
1276           myPreSelectionActor->SetVisibility(true);
1277         }
1278       }
1279     }
1280   }
1281   else if (vtkPointPicker* picker = vtkPointPicker::SafeDownCast(aPicker)) {
1282     int aVtkId = picker->GetPointId();
1283     if ( aVtkId >= 0 && IsValid( SActor, aVtkId, true ) ) {
1284       if ( SActor && SActor->hasIO() ) {
1285         int anObjId = SActor->GetNodeObjId(aVtkId);
1286         bool anIsSameObjId = (mySelectedActor == SActor && myNodeId == anObjId);
1287         if(!anIsSameObjId) {
1288           mySelectedActor = SActor;
1289           myNodeId = anObjId;
1290           m_Interactor->setPointData(anObjId,SActor,myPreSelectionActor);
1291         }
1292         myPreSelectionActor->GetProperty()->SetRepresentationToSurface();
1293         myPreSelectionActor->SetVisibility(true);
1294       }
1295     }
1296   }
1297   else if ( vtkPicker::SafeDownCast(aPicker) ) {
1298     if ( SActor ) {
1299       if ( myPreViewActor != SActor ) {
1300         if ( myPreViewActor != NULL ) {
1301           myPreViewActor->SetPreSelected( false );
1302         }
1303         myPreViewActor = SActor;
1304               
1305         if ( SActor->hasIO() ) {
1306           Handle( SALOME_InteractiveObject) IO = SActor->getIO();
1307           if ( !IsSelected(IO,Sel) ) {
1308             // Find All actors with same IO
1309             vtkActorCollection* theActors = this->CurrentRenderer->GetActors();
1310             theActors->InitTraversal();
1311             while( vtkActor *ac = theActors->GetNextActor() ) {
1312               if ( SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac ) ) {
1313                 if ( anActor->hasIO() ) {
1314                   Handle(SALOME_InteractiveObject) IOS = anActor->getIO();
1315                   if(IO->isSame(IOS)) {
1316                     anActor->SetPreSelected( true );
1317                   }
1318                 }
1319               }
1320             }
1321           }
1322         }
1323       }
1324     } else {
1325       myPreViewActor = NULL;
1326       vtkActorCollection* theActors = this->CurrentRenderer->GetActors();
1327       theActors->InitTraversal();
1328       while( vtkActor *ac = theActors->GetNextActor() ) {
1329         if ( SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( ac ) ) {
1330           anActor->SetPreSelected( false );
1331         }
1332       }
1333     }
1334   }
1335   m_Interactor->EndPickCallback();
1336   //m_Interactor->Render();
1337   myGUIWindow->update();
1338   
1339   this->LastPos[0] = x;
1340   this->LastPos[1] = y;*/
1341 }
1342
1343 /*! called on finsh GlobalPan operation */
1344 void VTKViewer_InteractorStyle::Place(const int theX, const int theY) 
1345 {
1346   if (this->CurrentRenderer == NULL) {
1347     return;
1348   }
1349
1350   //translate view
1351   int *aSize = this->CurrentRenderer->GetRenderWindow()->GetSize();
1352   int centerX = aSize[0]/2;
1353   int centerY = aSize[1]/2;
1354
1355   TranslateView(centerX, centerY, theX, theY);
1356
1357   // restore zoom scale
1358   vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
1359   cam->SetParallelScale(myScale);
1360   ::ResetCameraClippingRange(this->CurrentRenderer);
1361
1362   if (myGUIWindow) myGUIWindow->update();
1363
1364 }
1365
1366
1367
1368 /*! Translates view from Point to Point*/
1369 void VTKViewer_InteractorStyle::TranslateView(int toX, int toY, int fromX, int fromY)
1370 {
1371   vtkCamera *cam = this->CurrentRenderer->GetActiveCamera();
1372   double viewFocus[4], focalDepth, viewPoint[3];
1373   double newPickPoint[4], oldPickPoint[4], motionVector[3];
1374   cam->GetFocalPoint(viewFocus);
1375
1376   this->ComputeWorldToDisplay(viewFocus[0], viewFocus[1],
1377                               viewFocus[2], viewFocus);
1378   focalDepth = viewFocus[2];
1379
1380   this->ComputeDisplayToWorld(double(toX), double(toY),
1381                               focalDepth, newPickPoint);
1382   this->ComputeDisplayToWorld(double(fromX),double(fromY),
1383                               focalDepth, oldPickPoint);
1384   
1385   // camera motion is reversed
1386   motionVector[0] = oldPickPoint[0] - newPickPoint[0];
1387   motionVector[1] = oldPickPoint[1] - newPickPoint[1];
1388   motionVector[2] = oldPickPoint[2] - newPickPoint[2];
1389   
1390   cam->GetFocalPoint(viewFocus);
1391   cam->GetPosition(viewPoint);
1392   cam->SetFocalPoint(motionVector[0] + viewFocus[0],
1393                      motionVector[1] + viewFocus[1],
1394                      motionVector[2] + viewFocus[2]);
1395   cam->SetPosition(motionVector[0] + viewPoint[0],
1396                    motionVector[1] + viewPoint[1],
1397                    motionVector[2] + viewPoint[2]);
1398 }
1399
1400
1401 /*! Checks: is the given Actor within display coordinates?*/
1402 bool VTKViewer_InteractorStyle::IsInRect(vtkActor* theActor, 
1403                                                const int left, const int top, 
1404                                                const int right, const int bottom)
1405 {
1406   double* aBounds = theActor->GetBounds();
1407   double aMin[3], aMax[3];
1408   ComputeWorldToDisplay(aBounds[0], aBounds[2], aBounds[4], aMin);
1409   ComputeWorldToDisplay(aBounds[1], aBounds[3], aBounds[5], aMax);
1410   if (aMin[0] > aMax[0]) {
1411     double aBuf = aMin[0];
1412     aMin[0] = aMax[0];
1413     aMax[0] = aBuf;
1414   }
1415   if (aMin[1] > aMax[1]) {
1416     double aBuf = aMin[1];
1417     aMin[1] = aMax[1];
1418     aMax[1] = aBuf;    
1419   }
1420
1421   return ((aMin[0]>left) && (aMax[0]<right) && (aMin[1]>bottom) && (aMax[1]<top));
1422 }
1423
1424
1425 /*! Checks: is the given Cell within display coordinates?*/
1426 bool VTKViewer_InteractorStyle::IsInRect(vtkCell* theCell, 
1427                                                const int left, const int top, 
1428                                                const int right, const int bottom)
1429 {
1430   double* aBounds = theCell->GetBounds();
1431   double aMin[3], aMax[3];
1432   ComputeWorldToDisplay(aBounds[0], aBounds[2], aBounds[4], aMin);
1433   ComputeWorldToDisplay(aBounds[1], aBounds[3], aBounds[5], aMax);
1434   if (aMin[0] > aMax[0]) {
1435     double aBuf = aMin[0];
1436     aMin[0] = aMax[0];
1437     aMax[0] = aBuf;
1438   }
1439   if (aMin[1] > aMax[1]) {
1440     double aBuf = aMin[1];
1441     aMin[1] = aMax[1];
1442     aMax[1] = aBuf;    
1443   }
1444
1445   return ((aMin[0]>left) && (aMax[0]<right) && (aMin[1]>bottom) && (aMax[1]<top));
1446 }
1447
1448 /*!Checks: is given point \a thePoint in rectangle*/
1449 bool VTKViewer_InteractorStyle::IsInRect(double* thePoint, 
1450                                          const int left, const int top, 
1451                                          const int right, const int bottom)
1452 {
1453   double aPnt[3];
1454   ComputeWorldToDisplay(thePoint[0], thePoint[1], thePoint[2], aPnt);
1455
1456   return ((aPnt[0]>left) && (aPnt[0]<right) && (aPnt[1]>bottom) && (aPnt[1]<top));
1457 }
1458
1459 /*!Set filter \a theFilter*/
1460 void  VTKViewer_InteractorStyle::SetFilter( const Handle(VTKViewer_Filter)& theFilter )
1461 {
1462   myFilters[ theFilter->GetId() ] = theFilter;
1463 }
1464
1465 /*!Checks: is filter present (with id \a theId)
1466  *\param theId - filter id.
1467  */
1468 bool  VTKViewer_InteractorStyle::IsFilterPresent( const int theId )
1469 {
1470   return myFilters.find( theId ) != myFilters.end();
1471 }
1472
1473 /*!Remove filter with id \a theId.
1474  *\param theId - filter id.
1475  */
1476 void  VTKViewer_InteractorStyle::RemoveFilter( const int theId )
1477 {
1478   if ( IsFilterPresent( theId ) )
1479     myFilters.erase( theId );
1480 }
1481
1482 /*!Checks: is valid cell(node) with id \a theId in actor \a theActor.
1483  *\param theActor - VTKViewer_Actor pointer.
1484  *\param theId    - cell id.
1485  *\param theIsNode - boolean flag, if \c true - node, else - cell.
1486  */
1487 bool VTKViewer_InteractorStyle::IsValid( VTKViewer_Actor* theActor,
1488                                                const vtkIdType theId,
1489                                                const bool    theIsNode )
1490 {
1491   std::map<int, Handle(VTKViewer_Filter)>::const_iterator anIter;
1492   for ( anIter = myFilters.begin(); anIter != myFilters.end(); ++anIter )
1493   {
1494     const Handle(VTKViewer_Filter)& aFilter = anIter->second;
1495     if ( theIsNode == aFilter->IsNodeFilter() &&
1496          !aFilter->IsValid( theActor, theId ) )
1497       return false;
1498   }
1499   return true;
1500 }
1501
1502 /*!Gets filter handle by filter id \a theId.*/
1503 Handle(VTKViewer_Filter) VTKViewer_InteractorStyle::GetFilter( const int theId )
1504 {
1505   return IsFilterPresent( theId ) ? myFilters[ theId ] : Handle(VTKViewer_Filter)();
1506 }
1507
1508 /*!Increment pan.
1509  *\param incrX - X coordinate increment.
1510  *\param incrY - Y coordinate increment.
1511  */
1512 void VTKViewer_InteractorStyle::IncrementalPan( const int incrX, const int incrY )
1513 {
1514   this->PanXY( incrX, incrY, 0, 0 );
1515 }
1516
1517 /*!Increment zoom.
1518  *\param incr - zoom increment.
1519  */
1520 void VTKViewer_InteractorStyle::IncrementalZoom( const int incr )
1521 {
1522   this->DollyXY( incr, incr );
1523 }
1524
1525 /*!Increment rotate.
1526  *\param incrX - X coordinate increment.
1527  *\param incrY - Y coordinate increment.
1528  */
1529 void VTKViewer_InteractorStyle::IncrementalRotate( const int incrX, const int incrY )
1530 {
1531   this->RotateXY( incrX, -incrY );
1532 }
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556