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