Salome HOME
Merge from V5_1_main 14/05/2010
[modules/geom.git] / src / GEOMGUI / GEOMGUI_OCCSelector.cxx
1 //  Copyright (C) 2007-2010  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 //  Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 //  CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 //  This library is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU Lesser General Public
8 //  License as published by the Free Software Foundation; either
9 //  version 2.1 of the License.
10 //
11 //  This library is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 //  Lesser General Public License for more details.
15 //
16 //  You should have received a copy of the GNU Lesser General Public
17 //  License along with this library; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 //  See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 // File   : GEOMGUI_OCCSelector.cxx
24 // Author : Alexander SOLOVYOV, Open CASCADE S.A.S. (alexander.solovyov@opencascade.com)
25 //
26 #include "GEOMGUI_OCCSelector.h"
27
28 #include <LightApp_DataSubOwner.h>
29
30 #include <OCCViewer_ViewModel.h>
31
32 #include <SUIT_Session.h>
33 #include <SalomeApp_Study.h>
34 #include <SALOME_InteractiveObject.hxx>
35
36 #include <StdSelect_BRepOwner.hxx>
37 #include <SelectMgr_Selection.hxx>
38 #include <SelectMgr_EntityOwner.hxx>
39 #include <SelectMgr_IndexedMapOfOwner.hxx>
40 #include <SelectBasics_SensitiveEntity.hxx>
41
42 #include <AIS_Shape.hxx>
43 #include <AIS_ListOfInteractive.hxx>
44 #include <AIS_ListIteratorOfListOfInteractive.hxx>
45 #include <TopExp.hxx>
46 #include <TopTools_IndexedMapOfShape.hxx>
47 #include <TColStd_ListIteratorOfListOfInteger.hxx>
48 #include <TColStd_IndexedMapOfInteger.hxx>
49 #include <NCollection_DataMap.hxx>
50
51
52 //================================================================
53 // Function : GEOMGUI_OCCSelector
54 // Purpose  :
55 //================================================================
56 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
57 : LightApp_OCCSelector( viewer, mgr )
58 {
59 }
60
61 //================================================================
62 // Function : ~GEOMGUI_OCCSelector
63 // Purpose  :
64 //================================================================
65 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
66 {
67 }
68
69 //================================================================
70 // Function : getSelection
71 // Purpose  :
72 //================================================================
73 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
74 {
75   OCCViewer_Viewer* vw = viewer();
76   if (!vw)
77     return;
78
79   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
80
81   if (ic->HasOpenedContext())
82   {
83     TopoDS_Shape curBigShape;
84     TopTools_IndexedMapOfShape subShapes;
85
86     for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
87     {
88       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
89       if (anOwner.IsNull())
90         continue;
91
92       Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
93
94       QString entryStr = entry(io);
95       int index = -1;
96
97       if (anOwner->ComesFromDecomposition()) // == Local Selection
98       {
99         TopoDS_Shape subShape = anOwner->Shape();
100         Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
101         if (!aisShape.IsNull())
102         {
103           TopoDS_Shape bigShape = aisShape->Shape();
104
105           if (!bigShape.IsEqual(curBigShape))
106           {
107             curBigShape = bigShape;
108             subShapes.Clear();
109             TopExp::MapShapes(bigShape, subShapes);
110           }
111           index = subShapes.FindIndex(subShape);
112         }
113       }
114
115       if (!entryStr.isEmpty())
116       {
117         LightApp_DataOwner* owner;
118         if (index > -1) // Local Selection
119           owner = new LightApp_DataSubOwner (entryStr, index);
120         else // Global Selection
121           owner = new LightApp_DataOwner (entryStr);
122
123         aList.append(SUIT_DataOwnerPtr(owner));
124       }
125     }
126   }
127   else
128   {
129     for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
130     {
131       Handle(AIS_InteractiveObject) io = ic->Current();
132
133       QString entryStr = entry( io );
134
135       if ( !entryStr.isEmpty() )
136       {
137         LightApp_DataOwner* owner = new LightApp_DataOwner( entryStr );
138         aList.append( SUIT_DataOwnerPtr( owner ) );
139       }
140     }
141   }
142
143   // add externally selected objects
144   SUIT_DataOwnerPtrList::const_iterator anExtIter;
145   for (anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++)
146   {
147     aList.append(*anExtIter);
148   }
149 }
150
151 //================================================================
152 // Function : getEntityOwners
153 // Purpose  :
154 //================================================================
155 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
156                              const Handle(AIS_InteractiveContext)& theIC,
157                              SelectMgr_IndexedMapOfOwner& theMap )
158 {
159   if ( theObj.IsNull() || theIC.IsNull() )
160     return;
161
162   TColStd_ListOfInteger modes;
163   theIC->ActivatedModes( theObj, modes );
164
165   TColStd_ListIteratorOfListOfInteger itr( modes );
166   for (; itr.More(); itr.Next() ) {
167     int m = itr.Value();
168     if ( !theObj->HasSelection( m ) )
169       continue;
170
171     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
172
173     for ( sel->Init(); sel->More(); sel->Next() ) {
174       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
175       if ( entity.IsNull() )
176         continue;
177
178       Handle(SelectMgr_EntityOwner) owner =
179         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
180       if ( !owner.IsNull() )
181         theMap.Add( owner );
182     }
183   }
184 }
185
186 //================================================================
187 // Function : setSelection
188 // Purpose  :
189 //================================================================
190 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
191 {
192   OCCViewer_Viewer* vw = viewer();
193   if ( !vw )
194     return;
195
196   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
197
198   // "entry - list_of_int" map for LOCAL selection
199 #ifndef WNT
200   NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
201 #else
202   NCollection_DataMap<Standard_CString, TColStd_IndexedMapOfInteger> indexesMap;
203 #endif
204
205   QMap<QString,int> globalSelMap; // only Key=entry from this map is used.  value(int) is NOT used at all.
206   SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
207
208   AIS_ListOfInteractive aDispList;
209   ic->DisplayedObjects( aDispList );
210
211   // build a map of data owner indexes to be selected.
212   // "entry - to - list_of_ids" map
213   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
214   {
215     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
216     if ( subOwner )
217     {
218       QString entry = subOwner->entry();
219 #ifndef WNT
220       if ( indexesMap.IsBound( TCollection_AsciiString(entry.toLatin1().data())))
221 #else
222       if ( indexesMap.IsBound( entry.toLatin1().data() ) )
223 #endif
224       {
225         TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entry.toLatin1().data());
226         subIndexes.Add( subOwner->index() );
227         //indexesMap.replace( entry, subIndexes );
228       }
229       else
230       {
231         TColStd_IndexedMapOfInteger subIndexes;
232         subIndexes.Add( subOwner->index() );
233         indexesMap.Bind(entry.toLatin1().data(), subIndexes);
234       }
235     }
236     else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
237     {
238       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
239       if ( owner )
240       {
241         SalomeApp_Study* appStudy =
242           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
243         QString anEntry = appStudy->referencedToEntry( owner->entry() );
244
245         globalSelMap[anEntry] = 1;
246       }
247     }
248   }
249
250   // get all owners.  Fill "entry - list_of_owners" map.
251   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
252   {
253     Handle(AIS_InteractiveObject) io = it.Value();
254     QString entryStr = entry( io );
255     if ( !entryStr.isEmpty() )
256     {
257       //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map.  temporary use.
258       SelectMgr_IndexedMapOfOwner owners;
259       getEntityOwners( io, ic, owners ); // get all owners
260
261       int i, n = owners.Extent();
262
263       // 1. Prepare map of shapes for local selection
264       TopTools_IndexedMapOfShape aMapOfShapes;
265       bool isLocal = false;
266
267       Handle(StdSelect_BRepOwner) anOwner;
268       for (i = 1; i <= n && !isLocal; i++)
269       {
270         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
271         if (!anOwner.IsNull() && anOwner->HasShape())
272         {
273           if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
274           {
275             // has a local selection
276             Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
277             if (!aisShape.IsNull() && indexesMap.IsBound(entryStr.toLatin1().data()))
278             {
279               isLocal = true;
280               TopoDS_Shape shape = aisShape->Shape();
281               aMapOfShapes.Clear();
282               TopExp::MapShapes(shape, aMapOfShapes);
283             }
284           }
285         }
286       }
287
288       // 2. Process all owners
289       for (i = 1; i <= n; i++)
290       {
291         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
292
293         if ( anOwner.IsNull() || !anOwner->HasShape() )
294           continue;
295
296         // GLOBAL selection
297         if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
298         {
299           ownersmap.Add( anOwner );
300           globalSelMap[entryStr]++;
301         }
302         // LOCAL selection
303         else
304         {
305           if (isLocal)
306           {
307             const TColStd_IndexedMapOfInteger& subIndexes =
308               indexesMap.ChangeFind(entryStr.toLatin1().data());
309
310             const TopoDS_Shape& aSubShape = anOwner->Shape();
311             int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
312
313             // check if the "sub_shape_index" is found in the "map of indexes for this entry",
314             // which was passes in the parameter
315             if ( subIndexes.Contains( aSubShapeId ) )
316             {
317               ownersmap.Add( anOwner );
318             }
319           }
320         } // end of LOCAL selection
321       } // end of for(owners)
322     } // end of if(entry)
323   } // end of for(AIS_all_ios)
324
325   vw->unHighlightAll( false );
326
327   // DO the selection
328   int i = 1, n = ownersmap.Extent();
329   bool isAutoHilight = ic->AutomaticHilight();
330   ic->SetAutomaticHilight(Standard_False); //Bug 17269: for better performance
331   for  (; i <= n; i++)
332   {
333     Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
334     if ( owner->State() )
335       continue;
336
337     if ( ic->HasOpenedContext() )
338       ic->AddOrRemoveSelected( owner, false );
339     else
340       ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
341   }
342   ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
343   if (n < 3000)
344     ic->HilightSelected(/*updateviewer*/Standard_True);
345   else
346     vw->update();
347
348   // fill extra selected
349   mySelectedExternals.clear();
350   for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 )
351   {
352     const LightApp_DataSubOwner* subOwner =
353       dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
354     if ( !subOwner )
355     {
356       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
357       if ( owner )
358       {
359         SalomeApp_Study* appStudy =
360           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
361         QString anEntry = appStudy->referencedToEntry( owner->entry() );
362         if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);
363       }
364     }
365   }
366 }