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