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