Salome HOME
471c3b8ec5924bf3595d0e0b848a46789ef1e14d
[modules/gui.git] / src / SVTK / SVTK_Selector.cxx
1 //  SALOME SALOMEGUI : implementation of desktop and GUI kernel
2 //
3 //  Copyright (C) 2003  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.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org 
21 //
22 //
23 //
24 //  File   : SALOME_Selection.cxx
25 //  Author : Nicolas REJNERI
26 //  Module : SALOME
27 //  $Header$
28
29
30 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
31 #include <TColStd_IndexedMapOfInteger.hxx>
32
33
34 #include "SALOME_Actor.h"
35 #include "SVTK_ViewModel.h"
36 #include "SVTK_ViewWindow.h"
37
38 #include "SVTK_SelectorDef.h"
39
40 SVTK_SelectorDef
41 ::SVTK_SelectorDef()
42 {
43 }
44
45 SVTK_SelectorDef
46 ::~SVTK_SelectorDef()
47 {
48 }
49
50 void 
51 SVTK_SelectorDef
52 ::SetSelectionMode(Selection_Mode theMode)
53 {
54   mySelectionMode = theMode;
55 }
56
57 void 
58 SVTK_SelectorDef
59 ::ClearIObjects() 
60 {
61   myIO2Actors.clear();
62   myIObjects.clear();
63   myMapIOSubIndex.clear();
64 }
65
66 //----------------------------------------------------------------------------
67 bool
68 SVTK_SelectorDef
69 ::IsSelected(const Handle(SALOME_InteractiveObject)& theIO) const
70 {
71   return myIObjects.find(theIO) != myIObjects.end();
72 }
73
74 bool
75 SVTK_SelectorDef
76 ::IsSelected(SALOME_Actor* theActor) const
77 {
78   const Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
79   return IsSelected(anIO) && myIO2Actors.find(anIO) != myIO2Actors.end();
80 }
81
82 SALOME_Actor*
83 SVTK_SelectorDef
84 ::GetActor(const Handle(SALOME_InteractiveObject)& theIO) const
85 {
86   TIO2Actors::const_iterator anIter = myIO2Actors.find(theIO);
87   if(anIter != myIO2Actors.end())
88     return anIter->second.GetPointer();
89   return NULL;
90 }
91
92 //----------------------------------------------------------------------------
93 bool 
94 SVTK_SelectorDef
95 ::AddIObject(const Handle(SALOME_InteractiveObject)& theIO) 
96 {
97   if(!IsSelected(theIO)){
98     myIObjects.insert(theIO);
99     return true;
100   }
101   return false;
102 }
103
104 bool 
105 SVTK_SelectorDef
106 ::AddIObject(SALOME_Actor* theActor) 
107 {
108   const Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
109
110   bool anIsIOBound = IsSelected(anIO);
111   if(!anIsIOBound)
112     myIObjects.insert(anIO);
113
114   bool anIsActorBound = myIO2Actors.find(anIO) != myIO2Actors.end();
115   if(!anIsActorBound)
116     myIO2Actors[anIO] = theActor;
117   
118   return !anIsIOBound || !anIsActorBound;
119 }
120
121 //----------------------------------------------------------------------------
122 bool 
123 SVTK_SelectorDef
124 ::RemoveIObject(const Handle(SALOME_InteractiveObject)& theIO) 
125 {
126   bool anIsIOBound = myIObjects.find(theIO) != myIObjects.end();
127
128   myIObjects.erase(theIO);
129   myIO2Actors.erase(theIO);
130   myMapIOSubIndex.erase(theIO);
131
132   return anIsIOBound;
133 }
134
135 bool 
136 SVTK_SelectorDef
137 ::RemoveIObject(SALOME_Actor* theActor) 
138 {
139   const Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
140
141   bool anIsActorBound = myIO2Actors.find(anIO) != myIO2Actors.end();
142   if(anIsActorBound)
143     myIO2Actors.erase(anIO);
144
145   return RemoveIObject(anIO) || anIsActorBound;
146 }
147
148 //----------------------------------------------------------------------------
149 const SALOME_ListIO& 
150 SVTK_SelectorDef
151 ::StoredIObjects() const
152 {
153   myIObjectList.Clear();
154   TIObjects::const_iterator anIter = myIObjects.begin();
155   TIObjects::const_iterator anIterEnd = myIObjects.end();
156   for(; anIter != anIterEnd; anIter++)
157     myIObjectList.Append(*anIter);
158
159   return myIObjectList;
160 }
161
162 int
163 SVTK_SelectorDef
164 ::IObjectCount() const
165 {
166   return myIObjects.size();
167 }
168
169 bool 
170 SVTK_SelectorDef
171 ::HasIndex( const Handle(SALOME_InteractiveObject)& theIO) const
172 {
173   return myMapIOSubIndex.find(theIO) != myMapIOSubIndex.end();
174 }
175
176 void 
177 SVTK_SelectorDef
178 ::GetIndex( const Handle(SALOME_InteractiveObject)& theIO, 
179             TColStd_IndexedMapOfInteger& theIndex)
180 {
181   TMapIOSubIndex::const_iterator anIter = myMapIOSubIndex.find(theIO);
182   if(anIter != myMapIOSubIndex.end())
183     theIndex = anIter->second.myMap;
184   else
185     theIndex.Clear();
186 }
187
188 bool
189 SVTK_SelectorDef
190 ::IsIndexSelected(const Handle(SALOME_InteractiveObject)& theIO, 
191                   int theIndex) const
192 {
193   TMapIOSubIndex::const_iterator anIter = myMapIOSubIndex.find(theIO);
194   if(anIter != myMapIOSubIndex.end()){
195     const TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
196     return aMapIndex.Contains(theIndex);
197   }
198
199   return false;
200 }
201
202 static 
203 bool
204 removeIndex(TColStd_IndexedMapOfInteger& theMapIndex,
205             const int theIndex)
206 {
207   int anId = theMapIndex.FindIndex(theIndex); // i==0 if Index is not in the MapIndex
208   if(anId){
209     // only the last key can be removed
210     int aLastId = theMapIndex.FindKey(theMapIndex.Extent());
211     if(aLastId == anId)
212       theMapIndex.RemoveLast();
213     else{
214       TColStd_IndexedMapOfInteger aNewMap;
215       aNewMap.ReSize(theMapIndex.Extent()-1);
216       for(int j = 1; j <= theMapIndex.Extent(); j++){
217         int anIndex = theMapIndex(j);
218         if ( anIndex != theIndex )
219           aNewMap.Add( anIndex );
220       }
221       theMapIndex = aNewMap;
222     }
223   }
224   return anId;
225 }
226
227
228 bool
229 SVTK_SelectorDef
230 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
231                     const TColStd_IndexedMapOfInteger& theIndices, 
232                     bool theIsModeShift)
233 {
234   TMapIOSubIndex::iterator aMapIter = myMapIOSubIndex.find(theIO);
235   if(aMapIter == myMapIOSubIndex.end()){
236     TIndexedMapOfInteger anEmpty;
237     aMapIter = myMapIOSubIndex.
238       insert(TMapIOSubIndex::value_type(theIO,anEmpty)).first;
239   }
240   TColStd_IndexedMapOfInteger& aMapIndex = aMapIter->second.myMap;
241
242   if(!theIsModeShift)
243     aMapIndex.Clear();
244   
245   for(int i = 1, iEnd = theIndices.Extent(); i <= iEnd; i++)
246     aMapIndex.Add(theIndices(i));
247   
248   if(aMapIndex.IsEmpty()) {
249     myMapIOSubIndex.erase(theIO);
250     return false;
251   }
252
253   return true;
254 }
255
256
257 bool
258 SVTK_SelectorDef
259 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
260                     const TColStd_MapOfInteger& theIndices, 
261                     bool theIsModeShift)
262 {
263   TMapIOSubIndex::iterator aMapIter = myMapIOSubIndex.find(theIO);
264   if(aMapIter == myMapIOSubIndex.end()){
265     TIndexedMapOfInteger anEmpty;
266     aMapIter = myMapIOSubIndex.
267       insert(TMapIOSubIndex::value_type(theIO,anEmpty)).first;
268   }
269   TColStd_IndexedMapOfInteger& aMapIndex = aMapIter->second.myMap;
270
271   if(!theIsModeShift)
272     aMapIndex.Clear();
273   
274   TColStd_MapIteratorOfMapOfInteger anIter(theIndices);
275   for(; anIter.More(); anIter.Next())
276     aMapIndex.Add(anIter.Key());
277   
278   if(aMapIndex.IsEmpty()) {
279     myMapIOSubIndex.erase(theIO);
280     return false;
281   }
282
283   return true;
284 }
285
286
287 bool 
288 SVTK_SelectorDef
289 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
290                     int theIndex, 
291                     bool theIsModeShift)
292 {
293   TMapIOSubIndex::iterator anIter = myMapIOSubIndex.find(theIO);
294   if(anIter == myMapIOSubIndex.end()){
295     TIndexedMapOfInteger anEmpty;
296     anIter = myMapIOSubIndex.
297       insert(TMapIOSubIndex::value_type(theIO,anEmpty)).first;
298   }
299   TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
300
301   bool anIsConatains = aMapIndex.Contains(theIndex);
302   if(anIsConatains)
303     removeIndex(aMapIndex,theIndex);
304   
305   if(!theIsModeShift)
306     aMapIndex.Clear();
307   
308   if(!anIsConatains)
309     aMapIndex.Add( theIndex );
310
311   if( aMapIndex.IsEmpty())
312     myMapIOSubIndex.erase(theIO);
313
314   return false;
315 }
316
317
318 void
319 SVTK_SelectorDef
320 ::RemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
321                int theIndex)
322 {
323   if(IsIndexSelected(theIO,theIndex)){
324     TMapIOSubIndex::iterator anIter = myMapIOSubIndex.find(theIO);
325     TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
326     removeIndex(aMapIndex,theIndex);
327   }
328 }
329
330 void 
331 SVTK_SelectorDef
332 ::ClearIndex()
333 {
334   myMapIOSubIndex.clear();  
335 }