Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / SVTK / SVTK_Selector.cxx
index bc6dad1480e5ee99953a82b0e9588e0dba014f5b..fe5a930470c1bbf2c9585722dacbe98c23e1624a 100644 (file)
@@ -17,7 +17,7 @@
 //  License along with this library; if not, write to the Free Software 
 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA 
 // 
-//  See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
 //
 //
 #include <TColStd_IndexedMapOfInteger.hxx>
 
 #include <vtkCallbackCommand.h>
+#include <vtkActorCollection.h>
+#include <vtkCellPicker.h>
 
+
+/*!
+  Find first SALOME_Actor from the end of actors collection
+*/
+inline
+SALOME_Actor* 
+GetLastSALOMEActor(vtkActorCollection* theCollection)
+{
+  if (theCollection) {
+    for (int i = theCollection->GetNumberOfItems() - 1; i >= 0; i--) {
+      if (SALOME_Actor* anActor = dynamic_cast<SALOME_Actor*>(theCollection->GetItemAsObject(i)))
+       if (anActor->hasIO())
+         return anActor;
+    }
+  }
+  return NULL;
+}
+
+
+/*!
+  \return new SVTK_Selector
+*/
 SVTK_Selector* 
 SVTK_Selector
 ::New()
@@ -43,19 +67,31 @@ SVTK_Selector
   return new SVTK_SelectorDef();
 }
 
-//----------------------------------------------------------------------------
+/*!
+  Default constructor
+*/
 SVTK_SelectorDef
-::SVTK_SelectorDef()
+::SVTK_SelectorDef():
+  myPicker(vtkPicker::New()),
+  myCellPicker(vtkCellPicker::New())
 {
   mySelectionMode = ActorSelection;
+
+  myPicker->Delete();
+  myCellPicker->Delete();
 }
 
+/*!
+  Destructor
+*/
 SVTK_SelectorDef
 ::~SVTK_SelectorDef()
 {
 }
 
-//----------------------------------------------------------------------------
+/*!
+  To invoke selectionChanged signals
+*/
 void 
 SVTK_SelectorDef
 ::StartPickCallback()
@@ -63,7 +99,9 @@ SVTK_SelectorDef
   this->InvokeEvent(vtkCommand::StartPickEvent,NULL);
 }
 
-//----------------------------------------------------------------------------
+/*!
+  To invoke selectionChanged signals
+*/
 void 
 SVTK_SelectorDef
 ::EndPickCallback()
@@ -71,7 +109,9 @@ SVTK_SelectorDef
   this->InvokeEvent(vtkCommand::EndPickEvent,NULL);
 }
 
-//----------------------------------------------------------------------------
+/*!
+  To change current Selection_Mode (as outside effect, it invokes selectionChange signal)
+*/
 void 
 SVTK_SelectorDef
 ::SetSelectionMode(Selection_Mode theMode)
@@ -83,6 +123,9 @@ SVTK_SelectorDef
   }
 }
 
+/*!
+  Clear selection
+*/
 void 
 SVTK_SelectorDef
 ::ClearIObjects() 
@@ -92,7 +135,9 @@ SVTK_SelectorDef
   myMapIOSubIndex.clear();
 }
 
-//----------------------------------------------------------------------------
+/*!
+  \return true if the SALOME_InteractiveObject presents into selection
+*/
 bool
 SVTK_SelectorDef
 ::IsSelected(const Handle(SALOME_InteractiveObject)& theIO) const
@@ -100,6 +145,9 @@ SVTK_SelectorDef
   return !theIO.IsNull() && (myIObjects.find(theIO) != myIObjects.end());
 }
 
+/*!
+  \return true if the SALOME_Actor presents into selection
+*/
 bool
 SVTK_SelectorDef
 ::IsSelected(SALOME_Actor* theActor) const
@@ -108,6 +156,10 @@ SVTK_SelectorDef
   return IsSelected(anIO) && myIO2Actors.find(anIO) != myIO2Actors.end();
 }
 
+/*!
+  \return corresponding SALOME_Actor for SALOME_InteractiveObject
+  \param theIO - SALOME_InteractiveObject
+*/
 SALOME_Actor*
 SVTK_SelectorDef
 ::GetActor(const Handle(SALOME_InteractiveObject)& theIO) const
@@ -118,7 +170,10 @@ SVTK_SelectorDef
   return NULL;
 }
 
-//----------------------------------------------------------------------------
+/*!
+  Adds SALOME_InteractiveObject into selection
+  \param theIO - SALOME_InteractiveObject
+*/
 bool 
 SVTK_SelectorDef
 ::AddIObject(const Handle(SALOME_InteractiveObject)& theIO) 
@@ -130,6 +185,10 @@ SVTK_SelectorDef
   return false;
 }
 
+/*!
+  Adds SALOME_Actor into selection
+  \param theActor - SALOME_Actor
+*/
 bool 
 SVTK_SelectorDef
 ::AddIObject(SALOME_Actor* theActor) 
@@ -147,7 +206,10 @@ SVTK_SelectorDef
   return !anIsIOBound || !anIsActorBound;
 }
 
-//----------------------------------------------------------------------------
+/*!
+  Removes SALOME_InteractiveObject from selection
+  \param theIO - SALOME_InteractiveObject
+*/
 bool 
 SVTK_SelectorDef
 ::RemoveIObject(const Handle(SALOME_InteractiveObject)& theIO) 
@@ -161,6 +223,10 @@ SVTK_SelectorDef
   return anIsIOBound;
 }
 
+/*!
+  Removes SALOME_Actor from selection
+  \param theActor - SALOME_Actor
+*/
 bool 
 SVTK_SelectorDef
 ::RemoveIObject(SALOME_Actor* theActor) 
@@ -174,7 +240,9 @@ SVTK_SelectorDef
   return RemoveIObject(anIO) || anIsActorBound;
 }
 
-//----------------------------------------------------------------------------
+/*!
+  \return list of all SALOME_InteractiveObject presenting in selection
+*/
 const SALOME_ListIO& 
 SVTK_SelectorDef
 ::StoredIObjects() const
@@ -188,6 +256,9 @@ SVTK_SelectorDef
   return myIObjectList;
 }
 
+/*!
+  \return number of selected objects
+*/
 int
 SVTK_SelectorDef
 ::IObjectCount() const
@@ -195,6 +266,10 @@ SVTK_SelectorDef
   return myIObjects.size();
 }
 
+/*!
+  \return true if the SALOME_InteractiveObject has a subselection
+  \param theIO - SALOME_InteractiveObject
+*/
 bool 
 SVTK_SelectorDef
 ::HasIndex( const Handle(SALOME_InteractiveObject)& theIO) const
@@ -202,6 +277,10 @@ SVTK_SelectorDef
   return myMapIOSubIndex.find(theIO) != myMapIOSubIndex.end();
 }
 
+/*!
+  Gets indices of subselection for SALOME_InteractiveObject
+  \param theIO - SALOME_InteractiveObject
+*/
 void 
 SVTK_SelectorDef
 ::GetIndex( const Handle(SALOME_InteractiveObject)& theIO, 
@@ -214,6 +293,11 @@ SVTK_SelectorDef
     theIndex.Clear();
 }
 
+/*!
+  \return true if the index presents in subselection 
+  \param theIO - SALOME_InteractiveObject
+  \param theIndex - index
+*/
 bool
 SVTK_SelectorDef
 ::IsIndexSelected(const Handle(SALOME_InteractiveObject)& theIO, 
@@ -222,16 +306,13 @@ SVTK_SelectorDef
   TMapIOSubIndex::const_iterator anIter = myMapIOSubIndex.find(theIO);
   if(anIter != myMapIOSubIndex.end()){
     const TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
-    return aMapIndex.Contains(theIndex);
+    return aMapIndex.Contains( theIndex ) == Standard_True;
   }
 
   return false;
 }
 
-static 
-bool
-removeIndex(TColStd_IndexedMapOfInteger& theMapIndex,
-           const int theIndex)
+static bool removeIndex(TColStd_IndexedMapOfInteger& theMapIndex, const int theIndex)
 {
   int anId = theMapIndex.FindIndex(theIndex); // i==0 if Index is not in the MapIndex
   if(anId){
@@ -250,10 +331,15 @@ removeIndex(TColStd_IndexedMapOfInteger& theMapIndex,
       theMapIndex = aNewMap;
     }
   }
-  return anId;
+  return anId != 0;
 }
 
-
+/*!
+  Changes indices of subselection for SALOME_InteractiveObject
+  \param theIO - SALOME_InteractiveObject
+  \param theIndices - indices
+  \param theIsModeShift - if it is false, then map will be cleared before indices are added
+*/
 bool
 SVTK_SelectorDef
 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
@@ -283,6 +369,12 @@ SVTK_SelectorDef
 }
 
 
+/*!
+  Changes indices of subselection for SALOME_InteractiveObject
+  \param theIO - SALOME_InteractiveObject
+  \param theIndices - indices
+  \param theIsModeShift - if it is false, then map will be cleared before indices are added
+*/
 bool
 SVTK_SelectorDef
 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
@@ -313,6 +405,12 @@ SVTK_SelectorDef
 }
 
 
+/*!
+  Changes indices of subselection for SALOME_InteractiveObject
+  \param theIO - SALOME_InteractiveObject
+  \param theIndex - index
+  \param theIsModeShift - if it is false, then map will be cleared before indices are added
+*/
 bool 
 SVTK_SelectorDef
 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
@@ -327,23 +425,28 @@ SVTK_SelectorDef
   }
   TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
 
-  bool anIsConatains = aMapIndex.Contains(theIndex);
-  if(anIsConatains)
-    removeIndex(aMapIndex,theIndex);
+  bool anIsConatains = aMapIndex.Contains( theIndex ) == Standard_True;
+  if ( anIsConatains )
+    removeIndex( aMapIndex, theIndex );
   
-  if(!theIsModeShift)
+  if ( !theIsModeShift )
     aMapIndex.Clear();
   
-  if(!anIsConatains)
+  if ( !anIsConatains )
     aMapIndex.Add( theIndex );
 
-  if( aMapIndex.IsEmpty())
-    myMapIOSubIndex.erase(theIO);
+  if ( aMapIndex.IsEmpty() )
+    myMapIOSubIndex.erase( theIO );
 
   return false;
 }
 
 
+/*!
+  Removes index of subselection for SALOME_InteractiveObject
+  \param theIO - SALOME_InteractiveObject
+  \param theIndex - index
+*/
 void
 SVTK_SelectorDef
 ::RemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
@@ -356,6 +459,9 @@ SVTK_SelectorDef
   }
 }
 
+/*!
+  Clears all indices of subselection
+*/
 void 
 SVTK_SelectorDef
 ::ClearIndex()
@@ -363,7 +469,10 @@ SVTK_SelectorDef
   myMapIOSubIndex.clear();  
 }
 
-//----------------------------------------------------------------------------
+/*!
+  To apply a filter on the selection
+  \param theFilter - new filter
+*/
 void
 SVTK_SelectorDef
 ::SetFilter(const Handle(VTKViewer_Filter)& theFilter)
@@ -371,7 +480,10 @@ SVTK_SelectorDef
   myFilters.insert(TFilters::value_type(theFilter->GetId(),theFilter));
 }
 
-//----------------------------------------------------------------------------
+/*!
+  \return true if filter with given number is applyed
+  \param theId - filter id
+*/
 bool
 SVTK_SelectorDef
 ::IsFilterPresent(const TFilterID theId) const
@@ -379,7 +491,10 @@ SVTK_SelectorDef
   return myFilters.find(theId) != myFilters.end();
 }
 
-//----------------------------------------------------------------------------
+/*!
+  To remove a filter from the selection
+  \param theId - filter id
+*/
 void  
 SVTK_SelectorDef
 ::RemoveFilter(const TFilterID theId)
@@ -388,7 +503,12 @@ SVTK_SelectorDef
     myFilters.erase(theId);
 }
 
-//----------------------------------------------------------------------------
+/*!
+  \return true if the index satisfy installed filters
+  \param theActor - actor
+  \param theId - filter id
+  \param theIsNode - whether it is node
+*/
 bool
 SVTK_SelectorDef
 ::IsValid(SALOME_Actor* theActor,
@@ -405,7 +525,10 @@ SVTK_SelectorDef
   return true;
 }
 
-//----------------------------------------------------------------------------
+/*!
+  \return filter by it's id
+  \param theId - filter id
+*/
 Handle(VTKViewer_Filter) 
 SVTK_SelectorDef
 ::GetFilter(const TFilterID theId) const
@@ -418,3 +541,34 @@ SVTK_SelectorDef
   return Handle(VTKViewer_Filter)();
 }
 
+SALOME_Actor*
+SVTK_SelectorDef
+::Pick(const SVTK_SelectionEvent* theEvent, vtkRenderer* theRenderer) const
+{
+  myCellPicker->Pick(theEvent->myX,
+                    theEvent->myY, 
+                    0.0,
+                    theRenderer);
+  
+  vtkActorCollection* aListActors = myCellPicker->GetActors();
+  SALOME_Actor* anActor = GetLastSALOMEActor(aListActors);
+  
+  if (! anActor) {
+    myPicker->Pick(theEvent->myX,
+                  theEvent->myY, 
+                  0.0,
+                  theRenderer);
+    aListActors = myPicker->GetActors();
+    anActor = GetLastSALOMEActor(aListActors);
+  }
+  
+  return anActor;
+}
+
+void
+SVTK_SelectorDef
+::SetTolerance(const double& theTolerance) 
+{
+  myPicker->SetTolerance(theTolerance);        
+  myCellPicker->SetTolerance(theTolerance);
+}