Salome HOME
Synchronize adm files
[modules/geom.git] / src / GEOMGUI / GEOMGUI_OCCSelector.cxx
1 // Copyright (C) 2007-2014  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, or (at your option) any later version.
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 <SUIT_ResourceMgr.h>
34 #include <SalomeApp_Study.h>
35 #include <SALOME_InteractiveObject.hxx>
36
37 #include <StdSelect_BRepOwner.hxx>
38 #include <SelectMgr_Selection.hxx>
39 #include <SelectMgr_EntityOwner.hxx>
40 #include <SelectMgr_IndexedMapOfOwner.hxx>
41 #include <SelectBasics_SensitiveEntity.hxx>
42
43 #include <AIS_Shape.hxx>
44 #include <AIS_ListOfInteractive.hxx>
45 #include <AIS_ListIteratorOfListOfInteractive.hxx>
46 #include <TopExp.hxx>
47 #include <TopTools_IndexedMapOfShape.hxx>
48 #include <TColStd_ListIteratorOfListOfInteger.hxx>
49 #include <TColStd_IndexedMapOfInteger.hxx>
50 #include <NCollection_DataMap.hxx>
51
52
53 //================================================================
54 // Function : GEOMGUI_OCCSelector
55 // Purpose  :
56 //================================================================
57 GEOMGUI_OCCSelector::GEOMGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
58 : LightApp_OCCSelector( viewer, mgr )
59 {
60 }
61
62 //================================================================
63 // Function : ~GEOMGUI_OCCSelector
64 // Purpose  :
65 //================================================================
66 GEOMGUI_OCCSelector::~GEOMGUI_OCCSelector()
67 {
68 }
69
70 //================================================================
71 // Function : getSelection
72 // Purpose  :
73 //================================================================
74 void GEOMGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
75 {
76   OCCViewer_Viewer* vw = viewer();
77   if (!vw)
78     return;
79
80   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
81
82   if (ic->HasOpenedContext())
83   {
84     TopoDS_Shape curBigShape;
85     TopTools_IndexedMapOfShape subShapes;
86
87     for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
88     {
89       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
90       if (anOwner.IsNull())
91         continue;
92
93       Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
94
95       QString entryStr = entry(io);
96       int index = -1;
97
98       if (anOwner->ComesFromDecomposition()) // == Local Selection
99       {
100         TopoDS_Shape subShape = anOwner->Shape();
101         Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
102         if (!aisShape.IsNull())
103         {
104           TopoDS_Shape bigShape = aisShape->Shape();
105
106           if (!bigShape.IsEqual(curBigShape))
107           {
108             curBigShape = bigShape;
109             subShapes.Clear();
110             TopExp::MapShapes(bigShape, subShapes);
111           }
112           index = subShapes.FindIndex(subShape);
113         }
114       }
115
116       if (!entryStr.isEmpty())
117       {
118         Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner()); 
119         LightApp_DataOwner* owner;
120         if (index > -1) // Local Selection
121           owner = new LightApp_DataSubOwner (entryStr, index);
122         else if ( !anIO.IsNull() ) // Global Selection
123           owner = new LightApp_DataOwner( anIO );
124
125         aList.append(SUIT_DataOwnerPtr(owner));
126       }
127     }
128   }
129   else
130   {
131     for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
132     {
133       Handle(AIS_InteractiveObject) io = ic->Current();
134
135       QString entryStr = entry( io );
136
137       if ( !entryStr.isEmpty() )
138       {
139         Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner()); 
140         if ( !anIO.IsNull() ) {
141           LightApp_DataOwner* owner = new LightApp_DataOwner( anIO );
142           aList.append( SUIT_DataOwnerPtr( owner ) );
143         }
144       }
145     }
146   }
147
148   // add externally selected objects
149   SUIT_DataOwnerPtrList::const_iterator anExtIter;
150   for (anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++)
151   {
152     aList.append(*anExtIter);
153   }
154 }
155
156 //================================================================
157 // Function : getEntityOwners
158 // Purpose  :
159 //================================================================
160 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
161                              const Handle(AIS_InteractiveContext)& theIC,
162                              SelectMgr_IndexedMapOfOwner& theMap )
163 {
164   if ( theObj.IsNull() || theIC.IsNull() )
165     return;
166
167   TColStd_ListOfInteger modes;
168   theIC->ActivatedModes( theObj, modes );
169
170   TColStd_ListIteratorOfListOfInteger itr( modes );
171   for (; itr.More(); itr.Next() ) {
172     int m = itr.Value();
173     if ( !theObj->HasSelection( m ) )
174       continue;
175
176     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
177
178     for ( sel->Init(); sel->More(); sel->Next() ) {
179       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
180       if ( entity.IsNull() )
181         continue;
182
183       Handle(SelectMgr_EntityOwner) owner =
184         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
185       if ( !owner.IsNull() )
186         theMap.Add( owner );
187     }
188   }
189 }
190
191 //================================================================
192 // Function : setSelection
193 // Purpose  :
194 //================================================================
195 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
196 {
197   OCCViewer_Viewer* vw = viewer();
198   if ( !vw || !vw->isSelectionEnabled())
199     return;
200
201   SUIT_ResourceMgr* aResourceMgr = SUIT_Session::session()->resourceMgr();
202   bool anAutoBringToFront = aResourceMgr->booleanValue( "Geometry", "auto_bring_to_front", false );
203
204   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
205
206   // "entry - list_of_int" map for LOCAL selection
207   NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
208
209   QMap<QString,int> globalSelMap; // only Key=entry from this map is used.  value(int) is NOT used at all.
210   SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
211
212   AIS_ListOfInteractive aDispList;
213   ic->DisplayedObjects( aDispList );
214
215   // build a map of data owner indexes to be selected.
216   // "entry - to - list_of_ids" map
217   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
218   {
219     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
220     if ( subOwner )
221     {
222       QString entry = subOwner->entry();
223       if ( indexesMap.IsBound( TCollection_AsciiString(entry.toLatin1().data())))
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
343   ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
344
345   //rnv: In case Automatic Bring To Front viewer will be updated later
346   if(!anAutoBringToFront) {
347     if (n < 3000)
348       ic->HilightSelected(Standard_True);
349     else
350       vw->update();
351   }
352
353   // fill extra selected
354   mySelectedExternals.clear();
355   for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 )
356   {
357     const LightApp_DataSubOwner* subOwner =
358       dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
359     if ( !subOwner )
360     {
361       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
362       if ( owner )
363       {
364         SalomeApp_Study* appStudy =
365           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
366         QString anEntry = appStudy->referencedToEntry( owner->entry() );
367         if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);
368       }
369     }
370   }
371 }