Salome HOME
7a18425d58d7af151154e68f6f7a24a6c06cab1b
[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
131   return anIsIOBound;
132 }
133
134 bool 
135 SVTK_SelectorDef
136 ::RemoveIObject(SALOME_Actor* theActor) 
137 {
138   const Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
139
140   bool anIsActorBound = myIO2Actors.find(anIO) != myIO2Actors.end();
141   if(anIsActorBound)
142     myIO2Actors.erase(anIO);
143
144   return RemoveIObject(anIO) || anIsActorBound;
145 }
146
147 //----------------------------------------------------------------------------
148 const SALOME_ListIO& 
149 SVTK_SelectorDef
150 ::StoredIObjects() const
151 {
152   myIObjectList.Clear();
153   TIObjects::const_iterator anIter = myIObjects.begin();
154   TIObjects::const_iterator anIterEnd = myIObjects.end();
155   for(; anIter != anIterEnd; anIter++)
156     myIObjectList.Append(*anIter);
157
158   return myIObjectList;
159 }
160
161 int
162 SVTK_SelectorDef
163 ::IObjectCount() const
164 {
165   return myIObjects.size();
166 }
167
168 bool 
169 SVTK_SelectorDef
170 ::HasIndex( const Handle(SALOME_InteractiveObject)& theIO) const
171 {
172   return myMapIOSubIndex.find(theIO) != myMapIOSubIndex.end();
173 }
174
175 void 
176 SVTK_SelectorDef
177 ::GetIndex( const Handle(SALOME_InteractiveObject)& theIO, 
178             TColStd_IndexedMapOfInteger& theIndex)
179 {
180   TMapIOSubIndex::const_iterator anIter = myMapIOSubIndex.find(theIO);
181   if(anIter != myMapIOSubIndex.end())
182     theIndex = anIter->second.myMap;
183   else
184     theIndex.Clear();
185 }
186
187 bool
188 SVTK_SelectorDef
189 ::IsIndexSelected(const Handle(SALOME_InteractiveObject)& theIO, 
190                   int theIndex) const
191 {
192   TMapIOSubIndex::const_iterator anIter = myMapIOSubIndex.find(theIO);
193   if(anIter != myMapIOSubIndex.end()){
194     const TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
195     return aMapIndex.Contains(theIndex);
196   }
197
198   return false;
199 }
200
201 static 
202 bool
203 removeIndex(TColStd_IndexedMapOfInteger& theMapIndex,
204             const int theIndex)
205 {
206   int anId = theMapIndex.FindIndex(theIndex); // i==0 if Index is not in the MapIndex
207   if(anId){
208     // only the last key can be removed
209     int aLastId = theMapIndex.FindKey(theMapIndex.Extent());
210     if(aLastId == anId)
211       theMapIndex.RemoveLast();
212     else{
213       TColStd_IndexedMapOfInteger aNewMap;
214       aNewMap.ReSize(theMapIndex.Extent()-1);
215       for(int j = 1; j <= theMapIndex.Extent(); j++){
216         int anIndex = theMapIndex(j);
217         if ( anIndex != theIndex )
218           aNewMap.Add( anIndex );
219       }
220       theMapIndex = aNewMap;
221     }
222   }
223   return anId;
224 }
225
226
227 bool
228 SVTK_SelectorDef
229 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
230                     const TColStd_IndexedMapOfInteger& theIndices, 
231                     bool theIsModeShift)
232 {
233   TMapIOSubIndex::iterator aMapIter = myMapIOSubIndex.find(theIO);
234   if(aMapIter == myMapIOSubIndex.end()){
235     TIndexedMapOfInteger anEmpty;
236     aMapIter = myMapIOSubIndex.
237       insert(TMapIOSubIndex::value_type(theIO,anEmpty)).first;
238   }
239   TColStd_IndexedMapOfInteger& aMapIndex = aMapIter->second.myMap;
240
241   if(!theIsModeShift)
242     aMapIndex.Clear();
243   
244   for(int i = 1, iEnd = theIndices.Extent(); i <= iEnd; i++)
245     aMapIndex.Add(theIndices(i));
246   
247   if(aMapIndex.IsEmpty())
248     myMapIOSubIndex.erase(theIO);
249
250   return !aMapIndex.IsEmpty();
251 }
252
253
254 bool
255 SVTK_SelectorDef
256 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
257                     const TColStd_MapOfInteger& theIndices, 
258                     bool theIsModeShift)
259 {
260   TMapIOSubIndex::iterator aMapIter = myMapIOSubIndex.find(theIO);
261   if(aMapIter == myMapIOSubIndex.end()){
262     TIndexedMapOfInteger anEmpty;
263     aMapIter = myMapIOSubIndex.
264       insert(TMapIOSubIndex::value_type(theIO,anEmpty)).first;
265   }
266   TColStd_IndexedMapOfInteger& aMapIndex = aMapIter->second.myMap;
267
268   if(!theIsModeShift)
269     aMapIndex.Clear();
270   
271   TColStd_MapIteratorOfMapOfInteger anIter(theIndices);
272   for(; anIter.More(); anIter.Next())
273     aMapIndex.Add(anIter.Key());
274   
275   if(aMapIndex.IsEmpty())
276     myMapIOSubIndex.erase(theIO);
277
278   return !aMapIndex.IsEmpty();
279 }
280
281
282 bool 
283 SVTK_SelectorDef
284 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
285                     int theIndex, 
286                     bool theIsModeShift)
287 {
288   TMapIOSubIndex::iterator anIter = myMapIOSubIndex.find(theIO);
289   if(anIter == myMapIOSubIndex.end()){
290     TIndexedMapOfInteger anEmpty;
291     anIter = myMapIOSubIndex.
292       insert(TMapIOSubIndex::value_type(theIO,anEmpty)).first;
293   }
294   TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
295
296   bool anIsConatains = aMapIndex.Contains(theIndex);
297   if(anIsConatains)
298     removeIndex(aMapIndex,theIndex);
299   
300   if(!theIsModeShift)
301     aMapIndex.Clear();
302   
303   if(!anIsConatains)
304     aMapIndex.Add( theIndex );
305
306   if( aMapIndex.IsEmpty())
307     myMapIOSubIndex.erase(theIO);
308
309   return false;
310 }
311
312
313 void
314 SVTK_SelectorDef
315 ::RemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
316                int theIndex)
317 {
318   if(IsIndexSelected(theIO,theIndex)){
319     TMapIOSubIndex::iterator anIter = myMapIOSubIndex.find(theIO);
320     TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
321     removeIndex(aMapIndex,theIndex);
322   }
323 }
324
325 void 
326 SVTK_SelectorDef
327 ::ClearIndex()
328 {
329   myMapIOSubIndex.clear();  
330 }