Salome HOME
Delete unused files
[modules/hexablock.git] / src / HEXABLOCKGUI / HEXABLOCKGUI_OCCSelector.cxx
1 // Copyright (C) 2007-2013  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   : HEXABLOCKGUI_OCCSelector.cxx
24 //
25
26 #include "HEXABLOCKGUI_OCCSelector.h"
27 #include "HEXABLOCKGUI_SalomeTools.hxx"
28
29 #include <LightApp_DataSubOwner.h>
30
31 #include <OCCViewer_ViewModel.h>
32
33 #include <SUIT_Session.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 using namespace HEXABLOCK::GUI;
54
55 //================================================================
56 // Function : HEXABLOCKGUI_OCCSelector
57 // Purpose  :
58 //================================================================
59 HEXABLOCKGUI_OCCSelector::HEXABLOCKGUI_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
60 : LightApp_OCCSelector( viewer, mgr )
61 {
62 }
63
64 //================================================================
65 // Function : ~HEXABLOCKGUI_OCCSelector
66 // Purpose  :
67 //================================================================
68 HEXABLOCKGUI_OCCSelector::~HEXABLOCKGUI_OCCSelector()
69 {
70 }
71
72 //================================================================
73 // Function : getSelection
74 // Purpose  :
75 //================================================================
76 void HEXABLOCKGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
77 {
78   OCCViewer_Viewer* vw = viewer();
79   if (!vw)
80     return;
81
82   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
83
84   if (ic->HasOpenedContext())
85   {
86     TopoDS_Shape curBigShape;
87     TopTools_IndexedMapOfShape subShapes;
88
89     for (ic->InitSelected(); ic->MoreSelected(); ic->NextSelected())
90     {
91       Handle(StdSelect_BRepOwner) anOwner = Handle(StdSelect_BRepOwner)::DownCast(ic->SelectedOwner());
92       if (anOwner.IsNull())
93         continue;
94
95       Handle(AIS_InteractiveObject) io = Handle(AIS_InteractiveObject)::DownCast(anOwner->Selectable());
96
97       QString entryStr = entry(io);
98       int index = -1;
99
100       if (anOwner->ComesFromDecomposition()) // == Local Selection
101       {
102         TopoDS_Shape subShape = anOwner->Shape();
103         Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast(io);
104         if (!aisShape.IsNull())
105         {
106           TopoDS_Shape bigShape = aisShape->Shape();
107
108           if (!bigShape.IsEqual(curBigShape))
109           {
110             curBigShape = bigShape;
111             subShapes.Clear();
112             TopExp::MapShapes(bigShape, subShapes);
113           }
114           index = subShapes.FindIndex(subShape);
115         }
116       }
117
118       if (!entryStr.isEmpty())
119       {
120         Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner()); 
121         LightApp_DataOwner* owner;
122         if (index > -1) // Local Selection
123           owner = new LightApp_DataSubOwner (entryStr, index);
124         else if ( !anIO.IsNull() ) // Global Selection
125           owner = new LightApp_DataOwner( anIO );
126
127         aList.append(SUIT_DataOwnerPtr(owner));
128       }
129     }
130   }
131   else
132   {
133     for (ic->InitCurrent(); ic->MoreCurrent(); ic->NextCurrent())
134     {
135       Handle(AIS_InteractiveObject) io = ic->Current();
136
137       QString entryStr = entry( io );
138
139       if ( !entryStr.isEmpty() )
140       {
141         Handle(SALOME_InteractiveObject) anIO = Handle(SALOME_InteractiveObject)::DownCast(io->GetOwner()); 
142         if ( !anIO.IsNull() ) {
143           LightApp_DataOwner* owner = new LightApp_DataOwner( anIO );
144           aList.append( SUIT_DataOwnerPtr( owner ) );
145         }
146       }
147     }
148   }
149
150   // add externally selected objects
151   SUIT_DataOwnerPtrList::const_iterator anExtIter, itEnd;
152   for (anExtIter = mySelectedExternals.begin(), itEnd = mySelectedExternals.end(); anExtIter != itEnd; anExtIter++)
153   {
154     aList.append(*anExtIter);
155   }
156 }
157
158 //================================================================
159 // Function : setSelection
160 // Purpose  :
161 //================================================================
162 void HEXABLOCKGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
163 {
164   OCCViewer_Viewer* vw = viewer();
165   if ( !vw )
166     return;
167
168   Handle(AIS_InteractiveContext) ic = vw->getAISContext();
169
170   // "entry - list_of_int" map for LOCAL selection
171   NCollection_DataMap<TCollection_AsciiString, TColStd_IndexedMapOfInteger> indexesMap;
172
173   QMap<QString,int> globalSelMap; // only Key=entry from this map is used.  value(int) is NOT used at all.
174   SelectMgr_IndexedMapOfOwner ownersmap; // map of owners to be selected
175
176   AIS_ListOfInteractive aDispList;
177   ic->DisplayedObjects( aDispList );
178
179   // build a map of data owner indexes to be selected.
180   // "entry - to - list_of_ids" map
181   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(), itEnd = aList.end(); itr != itEnd; ++itr )
182   {
183     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
184     if ( subOwner )
185     {
186       QString entry = subOwner->entry();
187       if ( indexesMap.IsBound( TCollection_AsciiString(entry.toLatin1().data())))
188       {
189         TColStd_IndexedMapOfInteger& subIndexes = indexesMap.ChangeFind(entry.toLatin1().data());
190         subIndexes.Add( subOwner->index() );
191         //indexesMap.replace( entry, subIndexes );
192       }
193       else
194       {
195         TColStd_IndexedMapOfInteger subIndexes;
196         subIndexes.Add( subOwner->index() );
197         indexesMap.Bind(entry.toLatin1().data(), subIndexes);
198       }
199     }
200     else // the owner is NOT a sub owner, maybe it is a DataOwner == GLOBAL selection
201     {
202       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
203       if ( owner )
204       {
205         SalomeApp_Study* appStudy =
206           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
207         QString anEntry = appStudy->referencedToEntry( owner->entry() );
208
209         globalSelMap[anEntry] = 1;
210       }
211     }
212   }
213
214   // get all owners.  Fill "entry - list_of_owners" map.
215   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
216   {
217     Handle(AIS_InteractiveObject) io = it.Value();
218     QString entryStr = entry( io );
219     if ( !entryStr.isEmpty() )
220     {
221       //EntryToListOfOwnerMap entryOwnersMap; // "entry - list_of_owners" map.  temporary use.
222       SelectMgr_IndexedMapOfOwner owners;
223       getEntityOwners( io, ic, owners ); // get all owners
224
225       int i, n = owners.Extent();
226
227       // 1. Prepare map of shapes for local selection
228       TopTools_IndexedMapOfShape aMapOfShapes;
229       bool isLocal = false;
230
231       Handle(StdSelect_BRepOwner) anOwner;
232       for (i = 1; i <= n && !isLocal; i++)
233       {
234         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
235         if (!anOwner.IsNull() && anOwner->HasShape())
236         {
237           if (anOwner->ComesFromDecomposition() || !globalSelMap.contains(entryStr))
238           {
239             // has a local selection
240             Handle(AIS_Shape) aisShape = Handle(AIS_Shape)::DownCast( io );
241             if (!aisShape.IsNull() && indexesMap.IsBound(entryStr.toLatin1().data()))
242             {
243               isLocal = true;
244               TopoDS_Shape shape = aisShape->Shape();
245               aMapOfShapes.Clear();
246               TopExp::MapShapes(shape, aMapOfShapes);
247             }
248           }
249         }
250       }
251
252       // 2. Process all owners
253       for (i = 1; i <= n; i++)
254       {
255         anOwner = Handle(StdSelect_BRepOwner)::DownCast(owners( i ));
256
257         if ( anOwner.IsNull() || !anOwner->HasShape() )
258           continue;
259
260         // GLOBAL selection
261         if ( !anOwner->ComesFromDecomposition() && globalSelMap.contains( entryStr ) )
262         {
263           ownersmap.Add( anOwner );
264           globalSelMap[entryStr]++;
265         }
266         // LOCAL selection
267         else
268         {
269           if (isLocal)
270           {
271             const TColStd_IndexedMapOfInteger& subIndexes =
272               indexesMap.ChangeFind(entryStr.toLatin1().data());
273
274             const TopoDS_Shape& aSubShape = anOwner->Shape();
275             int aSubShapeId = aMapOfShapes.FindIndex( aSubShape );
276
277             // check if the "sub_shape_index" is found in the "map of indexes for this entry",
278             // which was passes in the parameter
279             if ( subIndexes.Contains( aSubShapeId ) )
280             {
281               ownersmap.Add( anOwner );
282             }
283           }
284         } // end of LOCAL selection
285       } // end of for(owners)
286     } // end of if(entry)
287   } // end of for(AIS_all_ios)
288
289   vw->unHighlightAll( false );
290
291   // DO the selection
292   int i = 1, n = ownersmap.Extent();
293   bool isAutoHilight = ic->AutomaticHilight();
294   ic->SetAutomaticHilight(Standard_False); //Bug 17269: for better performance
295   for  (; i <= n; i++)
296   {
297     Handle(SelectMgr_EntityOwner) owner = ownersmap( i );
298     if ( owner->State() )
299       continue;
300
301     if ( ic->HasOpenedContext() )
302       ic->AddOrRemoveSelected( owner, false );
303     else
304       ic->AddOrRemoveSelected( Handle(AIS_InteractiveObject)::DownCast(owner->Selectable()), false );
305   }
306   ic->SetAutomaticHilight(isAutoHilight); //Bug 17269: restore mode
307   if (n < 3000)
308     ic->HilightSelected(/*updateviewer*/Standard_True);
309   else
310     vw->update();
311
312   // fill extra selected
313   mySelectedExternals.clear();
314   for ( SUIT_DataOwnerPtrList::const_iterator itr2 = aList.begin(), itEnd = aList.end(); itr2 != itEnd; ++itr2 )
315   {
316     const LightApp_DataSubOwner* subOwner =
317       dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
318     if ( !subOwner )
319     {
320       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr2).operator->() );
321       if ( owner )
322       {
323         SalomeApp_Study* appStudy =
324           dynamic_cast<SalomeApp_Study*>( SUIT_Session::session()->activeApplication()->activeStudy() );
325         QString anEntry = appStudy->referencedToEntry( owner->entry() );
326         if (globalSelMap[anEntry] == 1) mySelectedExternals.append(*itr2);
327       }
328     }
329   }
330 }