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