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>
42 #include <SUIT_Session.h>
43 #include <SalomeApp_Study.h>
46 //================================================================
47 // Function : GEOMGUI_OCCSelector
49 //================================================================
50 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
51 : LightApp_OCCSelector( viewer, mgr )
55 //================================================================
56 // Function : ~GEOMGUI_OCCSelector
58 //================================================================
59 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
63 //================================================================
64 // Function : getSelection
66 //================================================================
67 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
69 OCCViewer_Viewer* vw = viewer();
73 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
75 if ( ic->HasOpenedContext() )
77 for ( ic->InitSelected(); ic->MoreSelected(); ic->NextSelected() )
79 Handle(SelectMgr_EntityOwner) anOwner = ic->SelectedOwner();
80 if ( anOwner.IsNull() )
83 Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
85 QString entryStr = entry( io );
88 if ( anOwner->ComesFromDecomposition() ) // == Local Selection
90 TopoDS_Shape subShape = anOwner->Shape();
91 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
92 if ( !aisShape.IsNull() )
94 TopoDS_Shape bigShape = aisShape->Shape();
96 TopTools_IndexedMapOfShape subShapes;
97 TopExp::MapShapes( bigShape, subShapes );
98 index = subShapes.FindIndex( subShape );
102 if ( !entryStr.isEmpty() )
104 LightApp_DataOwner* owner;
105 if ( index > -1 ) // Local Selection
106 owner = new LightApp_DataSubOwner( entryStr, index );
107 else // Global Selection
108 owner = new LightApp_DataOwner( entryStr );
110 aList.append( SUIT_DataOwnerPtr( owner ) );
116 for ( ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent() )
118 Handle(AIS_InteractiveObject) io = ic->Current();
120 QString entryStr = entry( io );
122 if ( !entryStr.isEmpty() )
124 LightApp_DataOwner* owner = new LightApp_DataOwner( entryStr );
125 aList.append( SUIT_DataOwnerPtr( owner ) );
131 //================================================================
132 // Function : getEntityOwners
134 //================================================================
135 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
136 const Handle(AIS_InteractiveContext)& theIC,
137 SelectMgr_IndexedMapOfOwner& theMap )
139 if ( theObj.IsNull() || theIC.IsNull() )
142 TColStd_ListOfInteger modes;
143 theIC->ActivatedModes( theObj, modes );
145 TColStd_ListIteratorOfListOfInteger itr( modes );
146 for (; itr.More(); itr.Next() ) {
148 if ( !theObj->HasSelection( m ) )
151 Handle(SelectMgr_Selection) sel = theObj->Selection( m );
153 for ( sel->Init(); sel->More(); sel->Next() ) {
154 Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
155 if ( entity.IsNull() )
158 Handle(SelectMgr_EntityOwner) owner =
159 Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
160 if ( !owner.IsNull() )
166 //================================================================
167 // Function : setSelection
169 //================================================================
170 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
172 OCCViewer_Viewer* vw = viewer();
176 Handle(AIS_InteractiveContext) ic = vw->getAISContext();
179 NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap; // "entry - list_of_int" map for LOCAL selection
181 NCollection_DataMap<Standard_CString, TColStd_IndexedMapOfInteger> indexesMap; // "entry - list_of_int" map for LOCAL selection
183 QMap<QString,int> globalSelMap; // only Key=entry from this map is used. value(int) is NOT used at all.
184 SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
186 AIS_ListOfInteractive aDispList;
187 ic->DisplayedObjects( aDispList );
189 // build a map of data owner indexes to be selected.
190 // "entry - to - list_of_ids" map
191 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
193 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
196 QString entry = subOwner->entry();
198 if ( indexesMap.IsBound( TCollection_AsciiString((char*)entry.latin1())))
200 if ( indexesMap.IsBound( (char*)entry.latin1()))
203 TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entry.latin1());
204 subIndexes.Add( subOwner->index() );
205 //indexesMap.replace( entry, subIndexes );
209 TColStd_IndexedMapOfInteger subIndexes;
210 subIndexes.Add( subOwner->index() );
211 indexesMap.Bind((char*)entry.latin1(), subIndexes);
214 else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
216 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
219 SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
220 QString anEntry = appStudy->referencedToEntry( owner->entry() );
222 globalSelMap[anEntry] = 1;
227 // get all owners. Fill "entry - list_of_owners" map.
228 for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
230 Handle(AIS_InteractiveObject) io = it.Value();
231 QString entryStr = entry( io );
232 if ( !entryStr.isEmpty() )
234 //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map. temporary use.
235 SelectMgr_IndexedMapOfOwner owners;
236 getEntityOwners( io, ic, owners ); // get all owners
238 for ( int i = 1, n = owners.Extent(); i <= n; i++ )
240 Handle(SelectMgr_EntityOwner) anOwner = owners( i );
241 if ( anOwner.IsNull() || !anOwner->HasShape() )
245 if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
247 ownersmap.Add( anOwner );
252 Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
254 if ( !aisShape.IsNull() && indexesMap.IsBound( (char*)entryStr.latin1() ) )
256 TopoDS_Shape shape = aisShape->Shape();
257 TopTools_IndexedMapOfShape aMapOfShapes;
258 TopExp::MapShapes( shape, aMapOfShapes );
259 const TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entryStr.latin1());
261 const TopoDS_Shape& aSubShape = anOwner->Shape();
262 int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
264 // check if the "sub_shape_index" is found in the "map of indexes for this entry",
265 // which was passes in the parameter
266 if ( subIndexes.Contains( aSubShapeId ) )
268 ownersmap.Add( anOwner );
271 } // end of local selection
272 } // end of for(owners)
274 }// end of for(AIS_all_ios)
276 vw->unHighlightAll( false );
279 for ( int i = 1, n = ownersmap.Extent(); i <= n; i++ )
281 Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
282 if ( owner->State() )
285 if ( ic->HasOpenedContext() )
286 ic->AddOrRemoveSelected( owner, false );
288 ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );