Salome HOME
Following methods are added to have possibility specify display mode for preview
[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 #ifndef WNT
186   NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap; // "entry - list_of_int" map for LOCAL selection
187 #else
188   NCollection_DataMap<Standard_CString, TColStd_IndexedMapOfInteger> indexesMap; // "entry - list_of_int" map for LOCAL selection
189 #endif
190   QMap<QString,int> globalSelMap; // only Key=entry from this map is used.  value(int) is NOT used at all.
191   SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
192   
193   AIS_ListOfInteractive aDispList;
194   ic->DisplayedObjects( aDispList );
195
196   // build a map of data owner indexes to be selected.
197   // "entry - to - list_of_ids" map
198   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
199   {
200     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
201     if ( subOwner )
202     {
203       QString entry = subOwner->entry();
204 #ifndef WNT
205       if ( indexesMap.IsBound( TCollection_AsciiString((char*)entry.latin1())))
206 #else
207           if ( indexesMap.IsBound( (char*)entry.latin1()))
208 #endif
209       {
210         TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entry.latin1());
211         subIndexes.Add( subOwner->index() );
212         //indexesMap.replace( entry, subIndexes );
213       }
214       else
215       {
216         TColStd_IndexedMapOfInteger subIndexes;
217         subIndexes.Add( subOwner->index() );
218         indexesMap.Bind((char*)entry.latin1(), subIndexes);
219       }
220     } 
221     else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
222     {
223       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
224       if ( owner )
225       {
226         SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
227         QString anEntry = appStudy->referencedToEntry( owner->entry() );
228         
229         globalSelMap[anEntry] = 1;
230       }
231     }
232   }
233
234   // get all owners.  Fill "entry - list_of_owners" map.
235   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
236   {
237     Handle(AIS_InteractiveObject) io = it.Value();
238     QString entryStr = entry( io );
239     if ( !entryStr.isEmpty() )
240     {
241       //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map.  temporary use.
242       SelectMgr_IndexedMapOfOwner owners;
243       getEntityOwners( io, ic, owners ); // get all owners
244
245       for  ( int i = 1, n = owners.Extent(); i <= n; i++ ) 
246       {
247
248         Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
249
250         if ( anOwner.IsNull() || !anOwner->HasShape() )
251           continue;
252
253         // GLOBAL selection
254         if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) ) 
255         {
256           ownersmap.Add( anOwner );
257           globalSelMap[entryStr]++;
258         }
259         // LOCAL selection
260         else
261         {
262           Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
263
264           if ( !aisShape.IsNull() && indexesMap.IsBound( (char*)entryStr.latin1() ) )
265           {
266             TopoDS_Shape shape = aisShape->Shape();
267             TopTools_IndexedMapOfShape aMapOfShapes;
268             TopExp::MapShapes( shape, aMapOfShapes );
269             const TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind((char*)entryStr.latin1());
270
271             const TopoDS_Shape& aSubShape = anOwner->Shape();
272             int  aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
273
274             // check if the "sub_shape_index" is found in the "map of indexes for this entry",
275             // which was passes in the parameter
276             if ( subIndexes.Contains( aSubShapeId ) )
277             {
278               ownersmap.Add( anOwner );
279             }
280           }
281         } // end of local selection
282       } // end of for(owners)
283     }// end of if(entry)
284   }// end of for(AIS_all_ios)
285
286   vw->unHighlightAll( false );
287
288   // DO the selection
289   for  ( int i = 1, n = ownersmap.Extent(); i <= n; i++ ) 
290   {
291     Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
292     if ( owner->State() )
293       continue;
294
295     if ( ic->HasOpenedContext() )
296       ic->AddOrRemoveSelected( owner, false );
297     else
298       ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
299   }
300
301   vw->update();
302   
303   // fill extra selected
304   mySelectedExternals.clear();
305   for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(); itr2 != aList.end(); ++itr2 ) {
306     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
307     if ( !subOwner )
308     {
309       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
310       if ( owner )
311       {
312         SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
313         QString anEntry = appStudy->referencedToEntry( owner->entry() );
314         if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);
315       }
316     }
317   }
318 }