]> SALOME platform Git repositories - modules/gui.git/blob - src/SVTK/SVTK_Selector.cxx
Salome HOME
625593eca556c2f8d4d0ab0fb55ec86c98feffe1
[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
251   return !aMapIndex.IsEmpty();
252 }
253
254
255 bool
256 SVTK_SelectorDef
257 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
258                     const TColStd_MapOfInteger& theIndices, 
259                     bool theIsModeShift)
260 {
261   TMapIOSubIndex::iterator aMapIter = myMapIOSubIndex.find(theIO);
262   if(aMapIter == myMapIOSubIndex.end()){
263     TIndexedMapOfInteger anEmpty;
264     aMapIter = myMapIOSubIndex.
265       insert(TMapIOSubIndex::value_type(theIO,anEmpty)).first;
266   }
267   TColStd_IndexedMapOfInteger& aMapIndex = aMapIter->second.myMap;
268
269   if(!theIsModeShift)
270     aMapIndex.Clear();
271   
272   TColStd_MapIteratorOfMapOfInteger anIter(theIndices);
273   for(; anIter.More(); anIter.Next())
274     aMapIndex.Add(anIter.Key());
275   
276   if(aMapIndex.IsEmpty())
277     myMapIOSubIndex.erase(theIO);
278
279   return !aMapIndex.IsEmpty();
280 }
281
282
283 bool 
284 SVTK_SelectorDef
285 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
286                     int theIndex, 
287                     bool theIsModeShift)
288 {
289   TMapIOSubIndex::iterator anIter = myMapIOSubIndex.find(theIO);
290   if(anIter == myMapIOSubIndex.end()){
291     TIndexedMapOfInteger anEmpty;
292     anIter = myMapIOSubIndex.
293       insert(TMapIOSubIndex::value_type(theIO,anEmpty)).first;
294   }
295   TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
296
297   bool anIsConatains = aMapIndex.Contains(theIndex);
298   if(anIsConatains)
299     removeIndex(aMapIndex,theIndex);
300   
301   if(!theIsModeShift)
302     aMapIndex.Clear();
303   
304   if(!anIsConatains)
305     aMapIndex.Add( theIndex );
306
307   if( aMapIndex.IsEmpty())
308     myMapIOSubIndex.erase(theIO);
309
310   return false;
311 }
312
313
314 void
315 SVTK_SelectorDef
316 ::RemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
317                int theIndex)
318 {
319   if(IsIndexSelected(theIO,theIndex)){
320     TMapIOSubIndex::iterator anIter = myMapIOSubIndex.find(theIO);
321     TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
322     removeIndex(aMapIndex,theIndex);
323   }
324 }
325
326 void 
327 SVTK_SelectorDef
328 ::ClearIndex()
329 {
330   myMapIOSubIndex.clear();  
331 }