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