Salome HOME
Merge branch 'master' into agy/NewPV1_For_850
[modules/gui.git] / src / SVTK / SVTK_Selector.cxx
index b2813a41c4ecb9086e101674626632457dd85d3d..885f3485d4aa11c7cded36ece172caabfe9786a7 100644 (file)
@@ -1,40 +1,44 @@
-//  SALOME SALOMEGUI : implementation of desktop and GUI kernel
+// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// 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, or (at your option) any later version.
 //
-//  Copyright (C) 2003  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
-//  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS 
-// 
-//  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. 
-// 
-//  This library is distributed in the hope that it will be useful, 
-//  but WITHOUT ANY WARRANTY; without even the implied warranty of 
-//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
-//  Lesser General Public License for more details. 
-// 
-//  You should have received a copy of the GNU Lesser General Public 
-//  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 
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
 //
+// You should have received a copy of the GNU Lesser General Public
+// 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.salome-platform.org/ or email : webmaster.salome@opencascade.com
 //
+
+//  SALOME SALOMEGUI : implementation of desktop and GUI kernel
 //  File   : SALOME_Selection.cxx
 //  Author : Nicolas REJNERI
-//  Module : SALOME
-//  $Header$
-
-
 #include "SVTK_SelectorDef.h"
 
+#include <VTKViewer_Filter.h>
+
 #include "SALOME_Actor.h"
 
+#include <SUIT_Session.h>
+#include <SUIT_ResourceMgr.h>
+
 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
 #include <TColStd_IndexedMapOfInteger.hxx>
 
 #include <vtkCallbackCommand.h>
+#include <vtkActorCollection.h>
+#include <vtkCellPicker.h>
+
 
 /*!
   \return new SVTK_Selector
@@ -50,9 +54,17 @@ SVTK_Selector
   Default constructor
 */
 SVTK_SelectorDef
-::SVTK_SelectorDef()
+::SVTK_SelectorDef():
+  myPicker(vtkPicker::New()),
+  myCellPicker(vtkCellPicker::New())
 {
   mySelectionMode = ActorSelection;
+  myDynamicPreselection = true;
+  myPreselectionEnabled = true;
+  mySelectionEnabled = true;
+
+  myPicker->Delete();
+  myCellPicker->Delete();
 }
 
 /*!
@@ -93,6 +105,7 @@ SVTK_SelectorDef
   if(mySelectionMode != theMode){
     mySelectionMode = theMode;
     myMapIOSubIndex.clear();
+    myMapIOSubCompositeIndex.clear();
     this->EndPickCallback();
   }
 }
@@ -107,6 +120,7 @@ SVTK_SelectorDef
   myIO2Actors.clear();
   myIObjects.clear();
   myMapIOSubIndex.clear();
+  myMapIOSubCompositeIndex.clear();
 }
 
 /*!
@@ -193,6 +207,7 @@ SVTK_SelectorDef
   myIObjects.erase(theIO);
   myIO2Actors.erase(theIO);
   myMapIOSubIndex.erase(theIO);
+  myMapIOSubCompositeIndex.erase(theIO);
 
   return anIsIOBound;
 }
@@ -258,7 +273,7 @@ SVTK_SelectorDef
 void 
 SVTK_SelectorDef
 ::GetIndex( const Handle(SALOME_InteractiveObject)& theIO, 
-           TColStd_IndexedMapOfInteger& theIndex)
+            TColStd_IndexedMapOfInteger& theIndex)
 {
   TMapIOSubIndex::const_iterator anIter = myMapIOSubIndex.find(theIO);
   if(anIter != myMapIOSubIndex.end())
@@ -275,21 +290,18 @@ SVTK_SelectorDef
 bool
 SVTK_SelectorDef
 ::IsIndexSelected(const Handle(SALOME_InteractiveObject)& theIO, 
-                 int theIndex) const
+                  int theIndex) const
 {
   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){
@@ -308,9 +320,32 @@ removeIndex(TColStd_IndexedMapOfInteger& theMapIndex,
       theMapIndex = aNewMap;
     }
   }
-  return anId;
+  return anId != 0;
+}
+
+static bool removeCompositeIndex( SVTK_IndexedMapOfIds& theMapIndex, const SVTK_ListOfInteger theIds )
+{
+  int anId = theMapIndex.FindIndex( theIds ); // i==0 if Index is not in the MapIndex
+  if( anId ) {
+    // only the last key can be removed
+    SVTK_ListOfInteger aLastIds = theMapIndex.FindKey( theMapIndex.Extent() );
+    if( aLastIds == theIds )
+      theMapIndex.RemoveLast();
+    else {
+      SVTK_IndexedMapOfIds aNewMap;
+      aNewMap.ReSize(theMapIndex.Extent()-1);
+      for( int j = 1; j <= theMapIndex.Extent(); j++ ){
+        SVTK_ListOfInteger anIds = theMapIndex( j );
+        if ( anIds != theIds )
+          aNewMap.Add( anIds );
+      }
+      theMapIndex = aNewMap;
+    }
+  }
+  return anId != 0;
 }
 
+
 /*!
   Changes indices of subselection for SALOME_InteractiveObject
   \param theIO - SALOME_InteractiveObject
@@ -320,8 +355,8 @@ removeIndex(TColStd_IndexedMapOfInteger& theMapIndex,
 bool
 SVTK_SelectorDef
 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
-                   const TColStd_IndexedMapOfInteger& theIndices, 
-                   bool theIsModeShift)
+                    const TColStd_IndexedMapOfInteger& theIndices, 
+                    bool theIsModeShift)
 {
   TMapIOSubIndex::iterator aMapIter = myMapIOSubIndex.find(theIO);
   if(aMapIter == myMapIOSubIndex.end()){
@@ -355,8 +390,8 @@ SVTK_SelectorDef
 bool
 SVTK_SelectorDef
 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
-                   const TColStd_MapOfInteger& theIndices, 
-                   bool theIsModeShift)
+                    const TColStd_MapOfInteger& theIndices, 
+                    bool theIsModeShift)
 {
   TMapIOSubIndex::iterator aMapIter = myMapIOSubIndex.find(theIO);
   if(aMapIter == myMapIOSubIndex.end()){
@@ -391,8 +426,8 @@ SVTK_SelectorDef
 bool 
 SVTK_SelectorDef
 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
-                   int theIndex, 
-                   bool theIsModeShift)
+                    int theIndex, 
+                    bool theIsModeShift)
 {
   TMapIOSubIndex::iterator anIter = myMapIOSubIndex.find(theIO);
   if(anIter == myMapIOSubIndex.end()){
@@ -402,18 +437,18 @@ 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;
 }
@@ -427,7 +462,7 @@ SVTK_SelectorDef
 void
 SVTK_SelectorDef
 ::RemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
-              int theIndex)
+               int theIndex)
 {
   if(IsIndexSelected(theIO,theIndex)){
     TMapIOSubIndex::iterator anIter = myMapIOSubIndex.find(theIO);
@@ -446,6 +481,147 @@ SVTK_SelectorDef
   myMapIOSubIndex.clear();  
 }
 
+/*!
+  \return true if the SALOME_InteractiveObject has a composite index subselection
+  \param theIO - SALOME_InteractiveObject
+*/
+bool 
+SVTK_SelectorDef
+::HasCompositeIndex( const Handle(SALOME_InteractiveObject)& theIO ) const
+{
+  return myMapIOSubCompositeIndex.find( theIO ) != myMapIOSubCompositeIndex.end();
+}
+
+/*!
+  Gets composite indices of subselection for SALOME_InteractiveObject
+  \param theIO - SALOME_InteractiveObject
+*/
+void 
+SVTK_SelectorDef
+::GetCompositeIndex( const Handle(SALOME_InteractiveObject)& theIO, 
+                    SVTK_IndexedMapOfIds& theIds )
+{
+  TMapIOSubCompositeIndex::const_iterator anIter = myMapIOSubCompositeIndex.find( theIO );
+  if( anIter != myMapIOSubCompositeIndex.end() )
+    theIds = anIter->second;
+  else
+    theIds.Clear();
+}
+
+/*!
+  Changes composite indices of subselection for SALOME_InteractiveObject
+  \param theIO - SALOME_InteractiveObject
+  \param theIndices - composite id
+  \param theIsModeShift - if it is false, then map will be cleared before indices are added
+*/
+bool
+SVTK_SelectorDef
+::AddOrRemoveCompositeIndex( const Handle( SALOME_InteractiveObject )& theIO, 
+                    const SVTK_IndexedMapOfIds& theIds, 
+                    bool theIsModeShift)
+{
+  TMapIOSubCompositeIndex::iterator aMapIter = myMapIOSubCompositeIndex.find( theIO );
+  if( aMapIter == myMapIOSubCompositeIndex.end() ) {
+    SVTK_IndexedMapOfIds anEmpty;
+    aMapIter = myMapIOSubCompositeIndex.insert( TMapIOSubCompositeIndex::value_type( theIO, anEmpty ) ).first;
+  }
+  SVTK_IndexedMapOfIds& aMapIndex = aMapIter->second;
+
+  if( !theIsModeShift )
+    aMapIndex.Clear();
+  
+  for( int i = 1, iEnd = theIds.Extent(); i <= iEnd; i++ )
+    aMapIndex.Add( theIds( i ) );
+  
+  if( aMapIndex.IsEmpty() ) {
+    myMapIOSubCompositeIndex.erase( theIO );
+    return false;
+  }
+  return true;
+}
+
+/*!
+  Changes indices of subselection for SALOME_InteractiveObject
+  \param theIO - SALOME_InteractiveObject
+  \param theIds - composite ids
+  \param theIsModeShift - if it is false, then map will be cleared before indices are added
+*/
+bool 
+SVTK_SelectorDef
+::AddOrRemoveCompositeIndex( const Handle(SALOME_InteractiveObject)& theIO, 
+                            SVTK_ListOfInteger theIds,
+                            bool theIsModeShift)
+{
+  TMapIOSubCompositeIndex::iterator anIter = myMapIOSubCompositeIndex.find( theIO );
+  if( anIter == myMapIOSubCompositeIndex.end() ) {
+    SVTK_IndexedMapOfIds anEmpty;
+    anIter = myMapIOSubCompositeIndex.insert(TMapIOSubCompositeIndex::value_type( theIO,anEmpty ) ).first;
+  }
+
+  SVTK_IndexedMapOfIds& aMapIndex = anIter->second;
+
+  bool anIsContains = aMapIndex.Contains( theIds ) == Standard_True;
+  if ( anIsContains )
+    removeCompositeIndex( aMapIndex, theIds );
+  
+  if ( !theIsModeShift )
+    aMapIndex.Clear();
+  
+  if ( !anIsContains )
+    aMapIndex.Add( theIds );
+
+  if ( aMapIndex.IsEmpty() )
+    myMapIOSubIndex.erase( theIO );
+
+  return false;
+}
+
+/*!
+  Removes composite index of subselection for SALOME_InteractiveObject
+  \param theIO - SALOME_InteractiveObject
+  \param theIds - index
+*/
+void
+SVTK_SelectorDef
+::RemoveCompositeIndex( const Handle(SALOME_InteractiveObject)& theIO, 
+                       SVTK_ListOfInteger theIds )
+{
+  if(IsCompositeIndexSelected( theIO, theIds ) ) {
+    TMapIOSubCompositeIndex::iterator anIter = myMapIOSubCompositeIndex.find( theIO );
+    SVTK_IndexedMapOfIds& aMapIndex = anIter->second;
+    removeCompositeIndex( aMapIndex,theIds );
+  }
+}
+
+/*!
+  \return true if the composite index presents in subselection 
+  \param theIO - SALOME_InteractiveObject
+  \param theIds - index
+*/
+bool
+SVTK_SelectorDef
+::IsCompositeIndexSelected( const Handle(SALOME_InteractiveObject)& theIO, 
+                           SVTK_ListOfInteger theIds ) const
+{
+  TMapIOSubCompositeIndex::const_iterator anIter = myMapIOSubCompositeIndex.find( theIO );
+  if( anIter != myMapIOSubCompositeIndex.end() ) {
+    const SVTK_IndexedMapOfIds& aMapIndex = anIter->second;
+    return aMapIndex.Contains( theIds ) == Standard_True;
+  }
+  return false;
+}
+
+/*!
+  Clears all composite indices of subselection
+*/
+void 
+SVTK_SelectorDef
+::ClearCompositeIndex()
+{
+  myMapIOSubCompositeIndex.clear();  
+}
+
+
 /*!
   To apply a filter on the selection
   \param theFilter - new filter
@@ -489,8 +665,8 @@ SVTK_SelectorDef
 bool
 SVTK_SelectorDef
 ::IsValid(SALOME_Actor* theActor,
-         const TFilterID theId,
-         const bool theIsNode) const
+          const TFilterID theId,
+          const bool theIsNode) const
 {
   TFilters::const_iterator anIter = myFilters.begin();
   for(; anIter != myFilters.end(); ++anIter){
@@ -518,3 +694,78 @@ SVTK_SelectorDef
   return Handle(VTKViewer_Filter)();
 }
 
+vtkActorCollection*
+SVTK_SelectorDef
+::Pick(const SVTK_SelectionEvent* theEvent, vtkRenderer* theRenderer) const
+{
+  vtkActorCollection* aListActors = NULL;
+
+  if ( GetDynamicPreSelection() ) {
+    myCellPicker->Pick(theEvent->myX,
+                       theEvent->myY, 
+                       0.0,
+                       theRenderer);
+  
+    aListActors = myCellPicker->GetActors();
+  }
+
+  if ( !aListActors || !aListActors->GetNumberOfItems() ) {
+    myPicker->Pick(theEvent->myX,
+                   theEvent->myY, 
+                   0.0,
+                   theRenderer);
+    aListActors = myPicker->GetActors();
+  }
+  
+  return aListActors;
+}
+
+void
+SVTK_SelectorDef
+::SetTolerance(const double& theTolerance) 
+{
+  myPicker->SetTolerance(theTolerance);         
+  myCellPicker->SetTolerance(theTolerance);
+}
+
+void
+SVTK_SelectorDef
+::SetDynamicPreSelection( bool theIsDynPreselect )
+{
+  myDynamicPreselection = theIsDynPreselect;
+}
+
+bool
+SVTK_SelectorDef
+::GetDynamicPreSelection() const
+{
+  return myDynamicPreselection;
+}
+
+void
+SVTK_SelectorDef
+::SetPreSelectionEnabled( bool theEnabled )
+{
+  myPreselectionEnabled = theEnabled;
+}
+
+bool
+SVTK_SelectorDef
+::IsPreSelectionEnabled() const
+{
+  return mySelectionEnabled && myPreselectionEnabled;
+}
+
+void
+SVTK_SelectorDef
+::SetSelectionEnabled( bool theEnabled )
+{
+  mySelectionEnabled = theEnabled;
+}
+
+bool
+SVTK_SelectorDef
+::IsSelectionEnabled() const
+{
+  return mySelectionEnabled;
+}