Salome HOME
Updated copyright comment
[modules/gui.git] / src / SVTK / SVTK_InteractorStyle.cxx
index 831110451d9aeb7c7db6995775059556a55869d1..1701d907e825839b032da7150a0318bfe4c9ca64 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -48,6 +48,7 @@
 #include <vtkCamera.h>
 #include <vtkRenderer.h>
 #include <vtkPointPicker.h>
+#include <vtkCellPicker.h>
 #include <vtkRenderWindow.h>
 #include <vtkRenderWindowInteractor.h>
 #include <vtkCallbackCommand.h>
 #include <vtkPerspectiveTransform.h> 
 #include <vtkMatrix4x4.h>
 
+#include <QtxRubberBand.h>
+
 #include <QPixmap>
 #include <QWidget>
-#include <QRubberBand>
 #include <QPolygon>
 
 #include <algorithm>
@@ -76,24 +78,25 @@ namespace
 }
 
 
-vtkStandardNewMacro(SVTK_InteractorStyle);
+vtkStandardNewMacro(SVTK_InteractorStyle)
 
 
 /*!
   Constructor
 */
 SVTK_InteractorStyle::SVTK_InteractorStyle():
-  mySelectionEvent(new SVTK_SelectionEvent()),
-  myPointPicker(vtkPointPicker::New()),
   myLastHighlitedActor(NULL),
   myLastPreHighlitedActor(NULL),
   myControllerIncrement(SVTK_ControllerIncrement::New()),
   myControllerOnKeyDown(SVTK_ControllerOnKeyDown::New()),
+  mySelectionEvent(new SVTK_SelectionEvent()),
   myHighlightSelectionPointActor(SVTK_Actor::New()),
+  myPointPicker(vtkPointPicker::New()),
   myRectBand(0),
   myPolygonBand(0),
+  myPoligonState(Disable),
   myIsAdvancedZoomingEnabled(false),
-  myPoligonState( Disable )
+  myInteractivePoint{0.0, 0.0, 0.0}
 {
   myPointPicker->Delete();
 
@@ -535,22 +538,61 @@ void SVTK_InteractorStyle::OnLeftButtonDown(int ctrl, int shift,
         {
           if( SALOME_Actor* anActor = SALOME_Actor::SafeDownCast( aVTKActor ) )
           {
-            SVTK::TPickLimiter aPickLimiter( myPointPicker, anActor );
-            myPointPicker->Pick( aSelectionEvent->myX,
-                                 aSelectionEvent->myY, 
-                                 0.0, 
+               Selection_Mode aSelectionMode = GetSelector()->SelectionMode();
+               double* aCoords = NULL;
+               vtkIdType aVtkId;
+               bool isTrueType = false;
+               
+               if( myCurrFocalPointType == SVTK::StartFocalPointSelection ||
+                       ( myCurrRotationPointType == SVTK::StartPointSelection && aSelectionMode == NodeSelection ) )
+               {
+              SVTK::TPickLimiter aPickLimiter( myPointPicker, anActor );
+              myPointPicker->Pick( aSelectionEvent->myX,
+                                   aSelectionEvent->myY,
+                                   0.0,
+                                   GetCurrentRenderer() );
+              aVtkId = myPointPicker->GetPointId();
+              if ( aVtkId >= 0 )
+              {
+                vtkIdType anObjId = anActor->GetNodeObjId( aVtkId );
+                aCoords = anActor->GetNodeCoord(anObjId);
+                isTrueType = true;
+              }
+               }
+
+               if( aSelectionMode == EdgeSelection || aSelectionMode == FaceSelection ||  aSelectionMode == VolumeSelection )
+               {
+              vtkSmartPointer<vtkCellPicker> aCellPicker = vtkCellPicker::New();
+              aCellPicker->SetTolerance( 0.005 );
+              SVTK::TPickLimiter aPickLimiter( aCellPicker, anActor );
+              aCellPicker->Pick( aSelectionEvent->myX,
+                                 aSelectionEvent->myY,
+                                 0.0,
                                  GetCurrentRenderer() );
-            int aVtkId = myPointPicker->GetPointId();
-            if ( aVtkId >= 0 )
-            {
-              int anObjId = anActor->GetNodeObjId( aVtkId );
-              double* aCoords = anActor->GetNodeCoord(anObjId);
-              
+              aVtkId = aCellPicker->GetCellId();
+              vtkIdType aCellId = anActor->GetElemObjId( aVtkId );
+
+              if( aSelectionMode == EdgeSelection )
+               isTrueType = anActor->GetObjDimension( aCellId ) == 1;
+              else if( aSelectionMode == FaceSelection )
+               isTrueType = anActor->GetObjDimension( aCellId ) == 2;
+              else if( aSelectionMode == VolumeSelection )
+               isTrueType = anActor->GetObjDimension( aCellId ) == 3;
+
+              if ( aVtkId >= 0 && isTrueType )
+                aCoords = anActor->GetGravityCenter( aCellId );
+               }
+
+               if( aVtkId >= 0 )
+               {
               if (myCurrRotationPointType == SVTK::StartPointSelection) {
                 myCurrRotationPointType = SVTK::SetRotateSelected;
-                
                 // invoke event for update coordinates in SVTK_SetRotationPointDlg
-                InvokeEvent(SVTK::RotationPointChanged,(void*)aCoords);
+                if( isTrueType )
+                  InvokeEvent(SVTK::RotationPointChanged,(void*)aCoords);
+                else
+                  InvokeEvent(SVTK::RotationPointChanged);
+                GetSelector()->SetSelectionMode(ActorSelection);
               }
               else if (myCurrFocalPointType == SVTK::StartFocalPointSelection) {
                 myCurrFocalPointType = SVTK::SetFocalPointSelected;
@@ -572,6 +614,7 @@ void SVTK_InteractorStyle::OnLeftButtonDown(int ctrl, int shift,
           // invoke event with no data (for SVTK_SetRotationPointDlg)
           InvokeEvent(SVTK::RotationPointChanged,0);
           myCurrRotationPointType = myPrevRotationPointType;
+          GetSelector()->SetSelectionMode(ActorSelection);
         }
         else if (myCurrFocalPointType == SVTK::StartFocalPointSelection) {
           // invoke event with no data (for SVTK_ViewParameterDlg)
@@ -618,7 +661,11 @@ void SVTK_InteractorStyle::OnLeftButtonUp(int vtkNotUsed(ctrl),
   // finishing current viewer operation
   if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
     onFinishOperation();
+    int lastOperation = State;
     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
+    if (lastOperation == VTK_INTERACTOR_STYLE_CAMERA_INTERACTIVE_SELECTION) {
+      InvokeEvent(SVTK::InteractiveSelectionFinished, nullptr);
+    }
   }
 }
 
@@ -643,7 +690,8 @@ void SVTK_InteractorStyle::OnMiddleButtonDown(int ctrl,
     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
   }
   myOtherPoint = myPoint = QPoint(x, y);
-  if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
+  if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE &&
+      ForcedState != VTK_INTERACTOR_STYLE_CAMERA_INTERACTIVE_SELECTION) {
     startOperation(ForcedState);
   }
   else {
@@ -701,7 +749,8 @@ void SVTK_InteractorStyle::OnRightButtonDown(int ctrl,
     startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
   }
   myOtherPoint = myPoint = QPoint(x, y);
-  if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE) {
+  if (ForcedState != VTK_INTERACTOR_STYLE_CAMERA_NONE && 
+      ForcedState != VTK_INTERACTOR_STYLE_CAMERA_INTERACTIVE_SELECTION) {
     startOperation(ForcedState);
   }
   else {
@@ -875,6 +924,20 @@ void SVTK_InteractorStyle::startRotate()
   ForcedState = VTK_INTERACTOR_STYLE_CAMERA_ROTATE;
 }
 
+/*!
+  Starts Interactive Selection operation
+*/
+void SVTK_InteractorStyle::startInteractiveSelection()
+{
+       if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
+       {
+               onFinishOperation();
+               startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
+       }
+       ForcedState = VTK_INTERACTOR_STYLE_CAMERA_INTERACTIVE_SELECTION;
+}
+
+
 /*!
   Set rotation point selected by user
 */
@@ -1016,6 +1079,7 @@ void SVTK_InteractorStyle::startOperation(int operation)
   case VTK_INTERACTOR_STYLE_CAMERA_SPIN:
   case VTK_INTERACTOR_STYLE_CAMERA_FIT:
   case VTK_INTERACTOR_STYLE_CAMERA_SELECT:
+  case VTK_INTERACTOR_STYLE_CAMERA_INTERACTIVE_SELECTION:
     if (State != VTK_INTERACTOR_STYLE_CAMERA_NONE)
       startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
     State = operation;
@@ -1100,6 +1164,11 @@ void SVTK_InteractorStyle::onStartOperation()
         drawRect();
       break;
     }
+    case VTK_INTERACTOR_STYLE_CAMERA_INTERACTIVE_SELECTION:
+    {
+      InteractiveSelection();
+      break;
+    }
     case VTK_INTERACTOR_STYLE_CAMERA_ZOOM:
     case VTK_INTERACTOR_STYLE_CAMERA_PAN:
     case VTK_INTERACTOR_STYLE_CAMERA_ROTATE:
@@ -1159,6 +1228,11 @@ void SVTK_InteractorStyle::onFinishOperation()
 
           if( anActorCollection )
           {
+           if( !myShiftState && 
+               anActorCollection->GetNumberOfItems () > 1 && 
+               myLastHighlitedActor.GetPointer() ) {
+             anActorCollection->RemoveItem ( myLastHighlitedActor.GetPointer() );
+           }
             anActorCollection->InitTraversal();
             while( vtkActor* aVTKActor = anActorCollection->GetNextActor() )
             {
@@ -1279,7 +1353,7 @@ void SVTK_InteractorStyle::onOperation(QPoint mousePos)
     {
       if (!myCursorState)
         setCursor(VTK_INTERACTOR_STYLE_CAMERA_SELECT);
-    }
+    } // fall through!
   case VTK_INTERACTOR_STYLE_CAMERA_FIT:
     {
       myOtherPoint = mousePos;
@@ -1289,6 +1363,8 @@ void SVTK_InteractorStyle::onOperation(QPoint mousePos)
         drawRect();
       break;
     }
+  case VTK_INTERACTOR_STYLE_CAMERA_INTERACTIVE_SELECTION:
+         this->InteractiveSelection();
   }
 }
 
@@ -1296,7 +1372,7 @@ void SVTK_InteractorStyle::onOperation(QPoint mousePos)
   Called when user moves mouse inside viewer window and there is no active viewer operation 
   (!put necessary processing here!)
 */
-void SVTK_InteractorStyle::onCursorMove(QPoint mousePos
+void SVTK_InteractorStyle::onCursorMove(QPoint /*mousePos*/
 {
   if ( !GetSelector()->IsPreSelectionEnabled() ) 
     return;
@@ -1310,8 +1386,7 @@ void SVTK_InteractorStyle::onCursorMove(QPoint mousePos)
   SALOME_Actor* aPreHighlightedActor = NULL;
   vtkActorCollection* anActorCollection = GetSelector()->Pick(aSelectionEvent, GetCurrentRenderer());
 
-  if ( myCurrRotationPointType == SVTK::StartPointSelection ||
-       myCurrFocalPointType == SVTK::StartFocalPointSelection )
+  if ( myCurrFocalPointType == SVTK::StartFocalPointSelection )
   {
     myHighlightSelectionPointActor->SetVisibility( false );
 
@@ -1324,11 +1399,11 @@ void SVTK_InteractorStyle::onCursorMove(QPoint mousePos)
         {
           SVTK::TPickLimiter aPickLimiter( myPointPicker, anActor );
           myPointPicker->Pick( aSelectionEvent->myX, aSelectionEvent->myY, 0.0, GetCurrentRenderer() );
-          int aVtkId = myPointPicker->GetPointId();
+          vtkIdType aVtkId = myPointPicker->GetPointId();
           if ( aVtkId >= 0 ) {
-            int anObjId = anActor->GetNodeObjId( aVtkId );
+            vtkIdType anObjId = anActor->GetNodeObjId( aVtkId );
 
-            TColStd_IndexedMapOfInteger aMapIndex;
+            SVTK_TIndexedMapOfVtkId aMapIndex;
             aMapIndex.Add( anObjId );
             myHighlightSelectionPointActor->MapPoints( anActor, aMapIndex );
 
@@ -1446,6 +1521,23 @@ void SVTK_InteractorStyle::IncrementalRotate( const int incrX, const int incrY )
   this->RotateXY( incrX, -incrY );
 }
 
+void SVTK_InteractorStyle::InteractiveSelection() 
+{
+  if (vtkCamera *cam = GetCurrentRenderer()->GetActiveCamera()) {
+    int posX, posY;
+    this->Interactor->GetEventPosition(posX, posY);
+    double viewFocus[3], z;
+    double* focalPointWorld = cam->GetFocalPoint();
+         this->ComputeWorldToDisplay(focalPointWorld[0], focalPointWorld[1],
+                 focalPointWorld[2], viewFocus);
+         z = viewFocus[2];
+
+         this->ComputeDisplayToWorld(double(posX), double(posY),
+               z, this->myInteractivePoint);
+         InvokeEvent(SVTK::InteractiveSelectionChanged, (void*)this->myInteractivePoint);
+  }
+}
+
 /*!
   Redefined in order to add an observer (callback) for custorm event (space mouse event)
 */
@@ -1492,6 +1584,8 @@ void SVTK_InteractorStyle::SetInteractor( vtkRenderWindowInteractor* theInteract
     theInteractor->AddObserver( SVTK::SetFocalPointGravity, EventCallbackCommand, Priority );
     theInteractor->AddObserver( SVTK::StartFocalPointSelection, EventCallbackCommand, Priority );
     theInteractor->AddObserver( SVTK::SetFocalPointSelected, EventCallbackCommand, Priority );
+         theInteractor->AddObserver( SVTK::StartInteractiveSelection, EventCallbackCommand, Priority);
+    theInteractor->AddObserver( SVTK::StopCurrentOperation, EventCallbackCommand, Priority);
   }
 }
 
@@ -1505,6 +1599,9 @@ void SVTK_InteractorStyle::OnTimer()
   // check if bounding box was changed
   if ( GetCurrentRenderer() )
   {
+#ifdef VGL_WORKAROUND
+    GetCurrentRenderer()->Render();
+#endif
     double aCurrBBCenter[3];
     if ( ComputeBBCenter(GetCurrentRenderer(),aCurrBBCenter) )
     {
@@ -1571,17 +1668,28 @@ void SVTK_InteractorStyle::DominantCombinedSwitch()
 */
 void SVTK_InteractorStyle::drawRect()
 {
-  if ( !myRectBand ) {
-    myRectBand = new QRubberBand( QRubberBand::Rectangle, GetRenderWidget() );
-    QPalette palette;
-    palette.setColor(myRectBand->foregroundRole(), Qt::white);
-    myRectBand->setPalette(palette);
-  }
-  myRectBand->hide();
+  if ( !myRectBand )
+    myRectBand = new QtxRectRubberBand( GetRenderWidget() );
 
+  myRectBand->setUpdatesEnabled ( false );
   QRect aRect = SUIT_Tools::makeRect(myPoint.x(), myPoint.y(), myOtherPoint.x(), myOtherPoint.y());
-  myRectBand->setGeometry( aRect );
-  myRectBand->setVisible( aRect.isValid() );
+  myRectBand->initGeometry( aRect );
+
+  if ( !myRectBand->isVisible() )
+    myRectBand->show();
+
+  myRectBand->setUpdatesEnabled ( true );
+}
+
+/*!
+  \brief Delete rubber band on the end on the dragging operation.
+*/
+void SVTK_InteractorStyle::endDrawRect()
+{
+  if ( myRectBand ) {
+    myRectBand->clearGeometry();
+    myRectBand->hide();
+  }
 }
 
 bool isIntersect( const QPoint& theStart1, const QPoint& theEnd1,
@@ -1649,7 +1757,7 @@ bool isValid( const QPolygon* thePoints, const QPoint& theCurrent )
     return true;
 
   bool res = true;
-  for ( uint i = 0; i < thePoints->count() - 1 && res; i++ )
+  for ( int i = 0; i < thePoints->count() - 1 && res; i++ )
   {
     const QPoint& aStart = thePoints->point( i );
     const QPoint& anEnd  = thePoints->point( i + 1 );
@@ -1709,17 +1817,6 @@ void SVTK_InteractorStyle::drawPolygon()
   }
 }
 
-/*!
-  \brief Delete rubber band on the end on the dragging operation.
-*/
-void SVTK_InteractorStyle::endDrawRect()
-{
-  if ( myRectBand ) myRectBand->hide();
-
-  delete myRectBand;
-  myRectBand = 0;
-}
-
 /*!
   \brief Delete rubber band on the end on the dragging operation.
 */
@@ -1727,7 +1824,9 @@ void SVTK_InteractorStyle::endDrawPolygon()
 {
   if ( myPolygonBand ) myPolygonBand->hide();
 
-  delete myPolygonBand;
+  // RNV fix for : #19204 [CEA][Windows] VTK Viewer - Access violation while right clicking
+  //delete myPolygonBand;
+  myPolygonBand->deleteLater();
   myPolygonBand = 0;
 
   myPolygonPoints.clear();
@@ -1872,7 +1971,9 @@ void SVTK_InteractorStyle::ProcessEvents( vtkObject* object,
       case SVTK::StartFocalPointSelection:
         self->startFocalPointSelection();
         return;
-
+      case SVTK::StartInteractiveSelection:
+        self->startInteractiveSelection();
+        return;
       case SVTK::SetFocalPointSelected:
         if ( self->myCurrFocalPointType == SVTK::StartFocalPointSelection )
         {
@@ -1884,6 +1985,9 @@ void SVTK_InteractorStyle::ProcessEvents( vtkObject* object,
         self->myPrevFocalPointType = self->myCurrFocalPointType;
         self->myCurrFocalPointType = SVTK::SetFocalPointSelected;
         return;
+      case SVTK::StopCurrentOperation:
+        self->onFinishOperation();
+        self->startOperation(VTK_INTERACTOR_STYLE_CAMERA_NONE);
       }
     }
   }
@@ -1978,7 +2082,7 @@ SVTK_ControllerIncrement* SVTK_InteractorStyle::ControllerIncrement()
   return myControllerIncrement.GetPointer();
 }
 
-vtkStandardNewMacro(SVTK_ControllerIncrement);
+vtkStandardNewMacro(SVTK_ControllerIncrement)
 SVTK_ControllerIncrement::SVTK_ControllerIncrement()
 {
   myIncrement=10;
@@ -2007,7 +2111,7 @@ int SVTK_ControllerIncrement::Decrease()
   return myIncrement;
 }
 
-vtkStandardNewMacro(SVTK_GeomControllerIncrement);
+vtkStandardNewMacro(SVTK_GeomControllerIncrement)
 SVTK_GeomControllerIncrement::SVTK_GeomControllerIncrement()
 {
 }
@@ -2028,7 +2132,7 @@ int SVTK_GeomControllerIncrement::Decrease()
   return myIncrement;
 }
 
-vtkStandardNewMacro(SVTK_ControllerOnKeyDown);
+vtkStandardNewMacro(SVTK_ControllerOnKeyDown)
 
 /*!
   Constructor
@@ -2044,7 +2148,7 @@ SVTK_ControllerOnKeyDown::~SVTK_ControllerOnKeyDown()
 {
 }
 
-bool SVTK_ControllerOnKeyDown::OnKeyDown(vtkInteractorStyle* theIS)
+bool SVTK_ControllerOnKeyDown::OnKeyDown(vtkInteractorStyle* /*theIS*/)
 {
   return true;
 }