1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
20 #include "GEOMGUI_OCCSelector.h"
22 #include <LightApp_DataSubOwner.h>
24 #include <OCCViewer_ViewModel.h>
26 #include <SALOME_InteractiveObject.hxx>
28 #include <AIS_ListOfInteractive.hxx>
29 #include <AIS_ListIteratorOfListOfInteractive.hxx>
30 #include <SelectMgr_EntityOwner.hxx>
31 #include <AIS_Shape.hxx>
32 #include <TopTools_IndexedMapOfShape.hxx>
34 #include <SelectMgr_IndexedMapOfOwner.hxx>
35 #include <TColStd_ListIteratorOfListOfInteger.hxx>
36 #include <SelectMgr_Selection.hxx>
37 #include <SelectBasics_SensitiveEntity.hxx>
38 #include <TColStd_IndexedMapOfInteger.hxx>
39 #include <SelectMgr_IndexedMapOfOwner.hxx>
40 #include <NCollection_DataMap.hxx>
43 //================================================================
44 // Function : GEOMGUI_OCCSelector
46 //================================================================
47 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
48 : LightApp_OCCSelector( viewer, mgr )
52 //================================================================
53 // Function : ~GEOMGUI_OCCSelector
55 //================================================================
56 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
60 //================================================================
61 // Function : getSelection
63 //================================================================
64 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
66 OCCViewer_Viewer* vw = viewer();
70 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
72 if ( ic->HasOpenedContext() )
74 for ( ic->InitSelected(); ic->MoreSelected(); ic->NextSelected() )
76 Handle(SelectMgr_EntityOwner) anOwner = ic->SelectedOwner();
77 if ( anOwner.IsNull() )
80 Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
82 QString entryStr = entry( io );
85 if ( anOwner->ComesFromDecomposition() ) // == Local Selection
87 TopoDS_Shape subShape = anOwner->Shape();
88 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
89 if ( !aisShape.IsNull() )
91 TopoDS_Shape bigShape = aisShape->Shape();
93 TopTools_IndexedMapOfShape subShapes;
94 TopExp::MapShapes( bigShape, subShapes );
95 index = subShapes.FindIndex( subShape );
99 if ( !entryStr.isEmpty() )
101 LightApp_DataOwner* owner;
102 if ( index > -1 ) // Local Selection
103 owner = new LightApp_DataSubOwner( entryStr, index );
104 else // Global Selection
105 owner = new LightApp_DataOwner( entryStr );
107 aList.append( SUIT_DataOwnerPtr( owner ) );
113 for ( ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent() )
115 Handle(AIS_InteractiveObject) io = ic->Current();
117 QString entryStr = entry( io );
119 if ( !entryStr.isEmpty() )
121 LightApp_DataOwner* owner = new LightApp_DataOwner( entryStr );
122 aList.append( SUIT_DataOwnerPtr( owner ) );
128 //================================================================
129 // Function : getEntityOwners
131 //================================================================
132 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
133 const Handle(AIS_InteractiveContext)& theIC,
134 SelectMgr_IndexedMapOfOwner& theMap )
136 if ( theObj.IsNull() || theIC.IsNull() )
139 TColStd_ListOfInteger modes;
140 theIC->ActivatedModes( theObj, modes );
142 TColStd_ListIteratorOfListOfInteger itr( modes );
143 for (; itr.More(); itr.Next() ) {
145 if ( !theObj->HasSelection( m ) )
148 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
150 for ( sel->Init(); sel->More(); sel->Next() ) {
151 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
152 if ( entity.IsNull() )
155 Handle(SelectMgr_EntityOwner) owner =
156 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
157 if ( !owner.IsNull() )
163 //================================================================
164 // Function : setSelection
166 //================================================================
167 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
169 OCCViewer_Viewer* vw = viewer();
173 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
176 NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap; // "entry - list_of_int" map for LOCAL selection
178 NCollection_DataMap<Standard_CString, TColStd_IndexedMapOfInteger> indexesMap; // "entry - list_of_int" map for LOCAL selection
180 QMap<QString,int> globalSelMap; // only Key=entry from this map is used. value(int) is NOT used at all.
181 SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
183 AIS_ListOfInteractive aDispList;
184 ic->DisplayedObjects( aDispList );
186 // build a map of data owner indexes to be selected.
187 // "entry - to - list_of_ids" map
188 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
190 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
193 QString entry = subOwner->entry();
195 if ( indexesMap.IsBound( TCollection_AsciiString((char*)entry.latin1())))
197 if ( indexesMap.IsBound( (char*)entry.latin1()))
200 TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entry.latin1());
201 subIndexes.Add( subOwner->index() );
202 //indexesMap.replace( entry, subIndexes );
206 TColStd_IndexedMapOfInteger subIndexes;
207 subIndexes.Add( subOwner->index() );
208 indexesMap.Bind((char*)entry.latin1(), subIndexes);
211 else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
213 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
216 globalSelMap[owner->entry()] = 1;
221 // get all owners. Fill "entry - list_of_owners" map.
222 for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
224 Handle(AIS_InteractiveObject) io = it.Value();
225 QString entryStr = entry( io );
226 if ( !entryStr.isEmpty() )
228 //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map. temporary use.
229 SelectMgr_IndexedMapOfOwner owners;
230 getEntityOwners( io, ic, owners ); // get all owners
232 for ( int i = 1, n = owners.Extent(); i <= n; i++ )
234 Handle(SelectMgr_EntityOwner) anOwner = owners( i );
235 if ( anOwner.IsNull() || !anOwner->HasShape() )
239 if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
241 ownersmap.Add( anOwner );
246 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
248 if ( !aisShape.IsNull() && indexesMap.IsBound( (char*)entryStr.latin1() ) )
250 TopoDS_Shape shape = aisShape->Shape();
251 TopTools_IndexedMapOfShape aMapOfShapes;
252 TopExp::MapShapes( shape, aMapOfShapes );
253 const TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entryStr.latin1());
255 const TopoDS_Shape& aSubShape = anOwner->Shape();
256 int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
258 // check if the "sub_shape_index" is found in the "map of indexes for this entry",
259 // which was passes in the parameter
260 if ( subIndexes.Contains( aSubShapeId ) )
262 ownersmap.Add( anOwner );
265 } // end of local selection
266 } // end of for(owners)
268 }// end of for(AIS_all_ios)
270 vw->unHighlightAll( false );
273 for ( int i = 1, n = ownersmap.Extent(); i <= n; i++ )
275 Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
276 if ( owner->State() )
279 if ( ic->HasOpenedContext() )
280 ic->AddOrRemoveSelected( owner, false );
282 ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );