Salome HOME
Join modifications from V3_2_0_maintainance (V3_2_6pre4 - T32x_16Aug2007_16h00m)
[modules/geom.git] / src / GEOMGUI / GEOMGUI_OCCSelector.cxx
1 // Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
3 // 
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.
8 // 
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.
13 //
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
17 //
18 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
19 //
20 #include "GEOMGUI_OCCSelector.h"
21
22 #include <LightApp_DataSubOwner.h>
23
24 #include <OCCViewer_ViewModel.h>
25
26 #include <SALOME_InteractiveObject.hxx>
27
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>
33 #include <TopExp.hxx>
34 #include <SelectMgr_IndexedMapOfOwner.hxx>
35 #include <TColStd_ListIteratorOfListOfInteger.hxx>
36 #include <SelectMgr_Selection.hxx>
37 #include <SelectBasics_SensitiveEntity.hxx>
38 #include <StdSelect_BRepOwner.hxx>
39 #include <TColStd_IndexedMapOfInteger.hxx>
40 #include <SelectMgr_IndexedMapOfOwner.hxx>
41 #include <NCollection_DataMap.hxx>
42
43 #include <SUIT_Session.h>
44 #include <SalomeApp_Study.h>
45
46
47 //================================================================
48 // Function : GEOMGUI_OCCSelector
49 // Purpose  : 
50 //================================================================
51 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
52 : LightApp_OCCSelector( viewer, mgr )
53 {
54 }
55
56 //================================================================
57 // Function : ~GEOMGUI_OCCSelector
58 // Purpose  : 
59 //================================================================
60 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
61 {
62 }
63
64 //================================================================
65 // Function : getSelection
66 // Purpose  : 
67 //================================================================
68 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
69 {
70   OCCViewer_Viewer* vw = viewer();
71   if ( !vw )
72     return;
73
74   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
75   
76   if ( ic->HasOpenedContext() )
77     {
78       for ( ic->InitSelected(); ic->MoreSelected(); ic->NextSelected() )
79         {
80           Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
81           if ( anOwner.IsNull() )
82             continue;
83           
84           Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast( anOwner->Selectable() );
85           
86           QString entryStr = entry( io );
87           int index = -1; 
88           
89           if ( anOwner->ComesFromDecomposition() ) // == Local Selection
90             {
91               TopoDS_Shape subShape = anOwner->Shape();
92               Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
93               if ( !aisShape.IsNull() )
94                 {
95                   TopoDS_Shape bigShape = aisShape->Shape();
96                   
97                   TopTools_IndexedMapOfShape subShapes;
98                   TopExp::MapShapes( bigShape, subShapes );
99                   index = subShapes.FindIndex( subShape );
100                 }
101             }
102           
103           if ( !entryStr.isEmpty() )
104             {
105               LightApp_DataOwner* owner;
106               if ( index > -1 ) // Local Selection
107                 owner = new LightApp_DataSubOwner( entryStr, index );
108               else // Global Selection
109                 owner = new LightApp_DataOwner( entryStr );
110
111               aList.append( SUIT_DataOwnerPtr( owner ) );
112             }
113         }
114     }
115   else
116     {
117       for ( ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent() )
118         {
119           Handle(AIS_InteractiveObject) io = ic->Current();
120           
121           QString entryStr = entry( io );
122           
123           if ( !entryStr.isEmpty() )
124             {
125               LightApp_DataOwner* owner = new LightApp_DataOwner( entryStr );
126               aList.append( SUIT_DataOwnerPtr( owner ) );
127             }
128         }
129     }
130   // add externally selected objects
131   SUIT_DataOwnerPtrList::const_iterator anExtIter;
132   for(anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++) {
133     aList.append(*anExtIter);
134   }
135   
136 }
137
138 //================================================================
139 // Function : getEntityOwners
140 // Purpose  : 
141 //================================================================
142 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
143                              const Handle(AIS_InteractiveContext)& theIC,
144                              SelectMgr_IndexedMapOfOwner& theMap )
145 {
146   if ( theObj.IsNull() || theIC.IsNull() )
147     return;
148
149   TColStd_ListOfInteger modes;
150   theIC->ActivatedModes( theObj, modes );
151
152   TColStd_ListIteratorOfListOfInteger itr( modes );
153   for (; itr.More(); itr.Next() ) {
154     int m = itr.Value();
155     if ( !theObj->HasSelection( m ) )
156       continue;
157
158     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
159
160     for ( sel->Init(); sel->More(); sel->Next() ) {
161       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
162       if ( entity.IsNull() )
163         continue;
164
165       Handle(SelectMgr_EntityOwner) owner =
166         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
167       if ( !owner.IsNull() )
168         theMap.Add( owner );
169     }
170   }
171 }
172
173 //================================================================
174 // Function : setSelection
175 // Purpose  : 
176 //================================================================
177 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
178 {
179   OCCViewer_Viewer* vw = viewer();
180   if ( !vw )
181     return;
182
183   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
184
185   NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap; // "entry - list_of_int" map for LOCAL selection
186   QMap<QString,int> globalSelMap; // only Key=entry from this map is used.  value(int) is NOT used at all.
187   SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
188   
189   AIS_ListOfInteractive aDispList;
190   ic->DisplayedObjects( aDispList );
191
192   // build a map of data owner indexes to be selected.
193   // "entry - to - list_of_ids" map
194   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
195   {
196     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
197     if ( subOwner )
198     {
199       QString entry = subOwner->entry();
200 #ifndef WNT
201       if ( indexesMap.IsBound( TCollection_AsciiString((char*)entry.latin1())))
202 #else
203           if ( indexesMap.IsBound( (char*)entry.latin1()))
204 #endif
205       {
206         TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entry.latin1());
207         subIndexes.Add( subOwner->index() );
208         //indexesMap.replace( entry, subIndexes );
209       }
210       else
211       {
212         TColStd_IndexedMapOfInteger subIndexes;
213         subIndexes.Add( subOwner->index() );
214         indexesMap.Bind((char*)entry.latin1(), subIndexes);
215       }
216     } 
217     else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
218     {
219       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
220       if ( owner )
221       {
222         SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
223         QString anEntry = appStudy->referencedToEntry( owner->entry() );
224         
225         globalSelMap[anEntry] = 1;
226       }
227     }
228   }
229
230   // get all owners.  Fill "entry - list_of_owners" map.
231   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
232   {
233     Handle(AIS_InteractiveObject) io = it.Value();
234     QString entryStr = entry( io );
235     if ( !entryStr.isEmpty() )
236     {
237       //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map.  temporary use.
238       SelectMgr_IndexedMapOfOwner owners;
239       getEntityOwners( io, ic, owners ); // get all owners
240
241       for  ( int i = 1, n = owners.Extent(); i <= n; i++ ) 
242       {
243
244         Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
245
246         if ( anOwner.IsNull() || !anOwner->HasShape() )
247           continue;
248
249         // GLOBAL selection
250         if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) ) 
251         {
252           ownersmap.Add( anOwner );
253           globalSelMap[entryStr]++;
254         }
255         // LOCAL selection
256         else
257         {
258           Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
259
260           if ( !aisShape.IsNull() && indexesMap.IsBound( (char*)entryStr.latin1() ) )
261           {
262             TopoDS_Shape shape = aisShape->Shape();
263             TopTools_IndexedMapOfShape aMapOfShapes;
264             TopExp::MapShapes( shape, aMapOfShapes );
265             const TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entryStr.latin1());
266
267             const TopoDS_Shape& aSubShape = anOwner->Shape();
268             int  aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
269
270             // check if the "sub_shape_index" is found in the "map of indexes for this entry",
271             // which was passes in the parameter
272             if ( subIndexes.Contains( aSubShapeId ) )
273             {
274               ownersmap.Add( anOwner );
275             }
276           }
277         } // end of local selection
278       } // end of for(owners)
279     }// end of if(entry)
280   }// end of for(AIS_all_ios)
281
282   vw->unHighlightAll( false );
283
284   // DO the selection
285   for  ( int i = 1, n = ownersmap.Extent(); i <= n; i++ ) 
286   {
287     Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
288     if ( owner->State() )
289       continue;
290
291     if ( ic->HasOpenedContext() )
292       ic->AddOrRemoveSelected( owner, false );
293     else
294       ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
295   }
296
297   vw->update();
298   
299   // fill extra selected
300   mySelectedExternals.clear();
301   for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 ) {
302     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
303     if ( !subOwner )
304     {
305       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
306       if ( owner )
307       {
308         SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
309         QString anEntry = appStudy->referencedToEntry( owner->entry() );
310         if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);
311       }
312     }
313   }
314 }