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