Salome HOME
Implementation of NPAL16017 (EDF 415 GEOM : Sketcher Modify the preview color).
[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
21 #include "GEOMGUI_OCCSelector.h"
22
23 #include <LightApp_DataSubOwner.h>
24
25 #include <OCCViewer_ViewModel.h>
26
27 #include <SUIT_Session.h>
28 #include <SalomeApp_Study.h>
29 #include <SALOME_InteractiveObject.hxx>
30
31 #include <StdSelect_BRepOwner.hxx>
32 #include <SelectMgr_Selection.hxx>
33 #include <SelectMgr_EntityOwner.hxx>
34 #include <SelectMgr_IndexedMapOfOwner.hxx>
35 #include <SelectBasics_SensitiveEntity.hxx>
36
37 #include <AIS_Shape.hxx>
38 #include <AIS_ListOfInteractive.hxx>
39 #include <AIS_ListIteratorOfListOfInteractive.hxx>
40 #include <TopExp.hxx>
41 #include <TopTools_IndexedMapOfShape.hxx>
42 #include <TColStd_ListIteratorOfListOfInteger.hxx>
43 #include <TColStd_IndexedMapOfInteger.hxx>
44 #include <NCollection_DataMap.hxx>
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     TopoDS_Shape curBigShape;
79     TopTools_IndexedMapOfShape subShapes;
80
81     for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
82     {
83       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
84       if (anOwner.IsNull())
85         continue;
86
87       Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
88
89       QString entryStr = entry(io);
90       int index = -1;
91
92       if (anOwner->ComesFromDecomposition()) // == Local Selection
93       {
94         TopoDS_Shape subShape = anOwner->Shape();
95         Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
96         if (!aisShape.IsNull())
97         {
98           TopoDS_Shape bigShape = aisShape->Shape();
99
100           if (!bigShape.IsEqual(curBigShape))
101           {
102             curBigShape = bigShape;
103             TopExp::MapShapes(bigShape, subShapes);
104           }
105           index = subShapes.FindIndex(subShape);
106         }
107       }
108
109       if (!entryStr.isEmpty())
110       {
111         LightApp_DataOwner* owner;
112         if (index > -1) // Local Selection
113           owner = new LightApp_DataSubOwner (entryStr, index);
114         else // Global Selection
115           owner = new LightApp_DataOwner (entryStr);
116
117         aList.append(SUIT_DataOwnerPtr(owner));
118       }
119     }
120   }
121   else
122   {
123     for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
124     {
125       Handle(AIS_InteractiveObject) io = ic->Current();
126
127       QString entryStr = entry( io );
128
129       if ( !entryStr.isEmpty() )
130       {
131         LightApp_DataOwner* owner = new LightApp_DataOwner( entryStr );
132         aList.append( SUIT_DataOwnerPtr( owner ) );
133       }
134     }
135   }
136
137   // add externally selected objects
138   SUIT_DataOwnerPtrList::const_iterator anExtIter;
139   for (anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++)
140   {
141     aList.append(*anExtIter);
142   }
143 }
144
145 //================================================================
146 // Function : getEntityOwners
147 // Purpose  : 
148 //================================================================
149 static void getEntityOwners( const Handle(AIS_InteractiveObject)& theObj,
150                              const Handle(AIS_InteractiveContext)& theIC,
151                              SelectMgr_IndexedMapOfOwner& theMap )
152 {
153   if ( theObj.IsNull() || theIC.IsNull() )
154     return;
155
156   TColStd_ListOfInteger modes;
157   theIC->ActivatedModes( theObj, modes );
158
159   TColStd_ListIteratorOfListOfInteger itr( modes );
160   for (; itr.More(); itr.Next() ) {
161     int m = itr.Value();
162     if ( !theObj->HasSelection( m ) )
163       continue;
164
165     Handle(SelectMgr_Selection) sel = theObj->Selection( m );
166
167     for ( sel->Init(); sel->More(); sel->Next() ) {
168       Handle(SelectBasics_SensitiveEntity) entity = sel->Sensitive();
169       if ( entity.IsNull() )
170         continue;
171
172       Handle(SelectMgr_EntityOwner) owner =
173         Handle(SelectMgr_EntityOwner)::DownCast(entity->OwnerId());
174       if ( !owner.IsNull() )
175         theMap.Add( owner );
176     }
177   }
178 }
179
180 //================================================================
181 // Function : setSelection
182 // Purpose  : 
183 //================================================================
184 void GEOMGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
185 {
186   OCCViewer_Viewer* vw = viewer();
187   if ( !vw )
188     return;
189
190   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
191
192   // "entry - list_of_int" map for LOCAL selection
193 #ifndef WNT
194   NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
195 #else
196   NCollection_DataMap<Standard_CString, TColStd_IndexedMapOfInteger> indexesMap;
197 #endif
198
199   QMap<QString,int> globalSelMap; // only Key=entry from this map is used.  value(int) is NOT used at all.
200   SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
201
202   AIS_ListOfInteractive aDispList;
203   ic->DisplayedObjects( aDispList );
204
205   // build a map of data owner indexes to be selected.
206   // "entry - to - list_of_ids" map
207   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
208   {
209     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
210     if ( subOwner )
211     {
212       QString entry = subOwner->entry();
213 #ifndef WNT
214       if ( indexesMap.IsBound( TCollection_AsciiString((char*)entry.latin1())))
215 #else
216           if ( indexesMap.IsBound( (char*)entry.latin1()))
217 #endif
218       {
219         TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entry.latin1());
220         subIndexes.Add( subOwner->index() );
221         //indexesMap.replace( entry, subIndexes );
222       }
223       else
224       {
225         TColStd_IndexedMapOfInteger subIndexes;
226         subIndexes.Add( subOwner->index() );
227         indexesMap.Bind((char*)entry.latin1(), subIndexes);
228       }
229     }
230     else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
231     {
232       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
233       if ( owner )
234       {
235         SalomeApp_Study* appStudy =
236           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
237         QString anEntry = appStudy->referencedToEntry( owner->entry() );
238
239         globalSelMap[anEntry] = 1;
240       }
241     }
242   }
243
244   // get all owners.  Fill "entry - list_of_owners" map.
245   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
246   {
247     Handle(AIS_InteractiveObject) io = it.Value();
248     QString entryStr = entry( io );
249     if ( !entryStr.isEmpty() )
250     {
251       //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map.  temporary use.
252       SelectMgr_IndexedMapOfOwner owners;
253       getEntityOwners( io, ic, owners ); // get all owners
254
255       int i, n = owners.Extent();
256
257       // 1. Prepare map of shapes for local selection
258       TopTools_IndexedMapOfShape aMapOfShapes;
259       bool isLocal = false;
260
261       Handle(StdSelect_BRepOwner) anOwner;
262       for (i = 1; i <= n && !isLocal; i++)
263       {
264         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
265         if (!anOwner.IsNull() && anOwner->HasShape())
266         {
267           if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
268           {
269             // has a local selection
270             Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
271             if (!aisShape.IsNull() && indexesMap.IsBound((char*)entryStr.latin1()))
272             {
273               isLocal = true;
274               TopoDS_Shape shape = aisShape->Shape();
275               TopExp::MapShapes(shape, aMapOfShapes);
276             }
277           }
278         }
279       }
280
281       // 2. Process all owners
282       for (i = 1; i <= n; i++)
283       {
284         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
285
286         if ( anOwner.IsNull() || !anOwner->HasShape() )
287           continue;
288
289         // GLOBAL selection
290         if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
291         {
292           ownersmap.Add( anOwner );
293           globalSelMap[entryStr]++;
294         }
295         // LOCAL selection
296         else
297         {
298           if (isLocal)
299           {
300             const TColStd_IndexedMapOfInteger& subIndexes =
301               indexesMap.ChangeFind((char*)entryStr.latin1());
302
303             const TopoDS_Shape& aSubShape = anOwner->Shape();
304             int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
305
306             // check if the "sub_shape_index" is found in the "map of indexes for this entry",
307             // which was passes in the parameter
308             if ( subIndexes.Contains( aSubShapeId ) )
309             {
310               ownersmap.Add( anOwner );
311             }
312           }
313         } // end of LOCAL selection
314       } // end of for(owners)
315     } // end of if(entry)
316   } // end of for(AIS_all_ios)
317
318   vw->unHighlightAll( false );
319
320   // DO the selection
321   int i = 1, n = ownersmap.Extent();
322   bool isAutoHilight = ic->AutomaticHilight();
323   ic->SetAutomaticHilight(Standard_False); //Bug 17269: for better performance
324   for  (; i <= n; i++)
325   {
326     Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
327     if ( owner->State() )
328       continue;
329
330     if ( ic->HasOpenedContext() )
331       ic->AddOrRemoveSelected( owner, false );
332     else
333       ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
334   }
335   ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
336   if (n < 3000)
337     ic->HilightSelected(/*updateviewer*/Standard_True);
338   else
339     vw->update();
340
341   // fill extra selected
342   mySelectedExternals.clear();
343   for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 )
344   {
345     const LightApp_DataSubOwner* subOwner =
346       dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
347     if ( !subOwner )
348     {
349       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
350       if ( owner )
351       {
352         SalomeApp_Study* appStudy =
353           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
354         QString anEntry = appStudy->referencedToEntry( owner->entry() );
355         if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);
356       }
357     }
358   }
359 }