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