]> SALOME platform Git repositories - modules/gui.git/blob - src/SVTK/SVTK_Selector.cxx
Salome HOME
bc6dad1480e5ee99953a82b0e9588e0dba014f5b
[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 "SVTK_SelectorDef.h"
31
32 #include "SALOME_Actor.h"
33
34 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
35 #include <TColStd_IndexedMapOfInteger.hxx>
36
37 #include <vtkCallbackCommand.h>
38
39 SVTK_Selector* 
40 SVTK_Selector
41 ::New()
42 {
43   return new SVTK_SelectorDef();
44 }
45
46 //----------------------------------------------------------------------------
47 SVTK_SelectorDef
48 ::SVTK_SelectorDef()
49 {
50   mySelectionMode = ActorSelection;
51 }
52
53 SVTK_SelectorDef
54 ::~SVTK_SelectorDef()
55 {
56 }
57
58 //----------------------------------------------------------------------------
59 void 
60 SVTK_SelectorDef
61 ::StartPickCallback()
62 {
63   this->InvokeEvent(vtkCommand::StartPickEvent,NULL);
64 }
65
66 //----------------------------------------------------------------------------
67 void 
68 SVTK_SelectorDef
69 ::EndPickCallback()
70 {
71   this->InvokeEvent(vtkCommand::EndPickEvent,NULL);
72 }
73
74 //----------------------------------------------------------------------------
75 void 
76 SVTK_SelectorDef
77 ::SetSelectionMode(Selection_Mode theMode)
78 {
79   if(mySelectionMode != theMode){
80     mySelectionMode = theMode;
81     myMapIOSubIndex.clear();
82     this->EndPickCallback();
83   }
84 }
85
86 void 
87 SVTK_SelectorDef
88 ::ClearIObjects() 
89 {
90   myIO2Actors.clear();
91   myIObjects.clear();
92   myMapIOSubIndex.clear();
93 }
94
95 //----------------------------------------------------------------------------
96 bool
97 SVTK_SelectorDef
98 ::IsSelected(const Handle(SALOME_InteractiveObject)& theIO) const
99 {
100   return !theIO.IsNull() && (myIObjects.find(theIO) != myIObjects.end());
101 }
102
103 bool
104 SVTK_SelectorDef
105 ::IsSelected(SALOME_Actor* theActor) const
106 {
107   const Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
108   return IsSelected(anIO) && myIO2Actors.find(anIO) != myIO2Actors.end();
109 }
110
111 SALOME_Actor*
112 SVTK_SelectorDef
113 ::GetActor(const Handle(SALOME_InteractiveObject)& theIO) const
114 {
115   TIO2Actors::const_iterator anIter = myIO2Actors.find(theIO);
116   if(anIter != myIO2Actors.end())
117     return anIter->second.GetPointer();
118   return NULL;
119 }
120
121 //----------------------------------------------------------------------------
122 bool 
123 SVTK_SelectorDef
124 ::AddIObject(const Handle(SALOME_InteractiveObject)& theIO) 
125 {
126   if(!IsSelected(theIO)){
127     myIObjects.insert(theIO);
128     return true;
129   }
130   return false;
131 }
132
133 bool 
134 SVTK_SelectorDef
135 ::AddIObject(SALOME_Actor* theActor) 
136 {
137   const Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
138
139   bool anIsIOBound = IsSelected(anIO);
140   if(!anIsIOBound)
141     myIObjects.insert(anIO);
142
143   bool anIsActorBound = myIO2Actors.find(anIO) != myIO2Actors.end();
144   if(!anIsActorBound)
145     myIO2Actors[anIO] = theActor;
146   
147   return !anIsIOBound || !anIsActorBound;
148 }
149
150 //----------------------------------------------------------------------------
151 bool 
152 SVTK_SelectorDef
153 ::RemoveIObject(const Handle(SALOME_InteractiveObject)& theIO) 
154 {
155   bool anIsIOBound = myIObjects.find(theIO) != myIObjects.end();
156
157   myIObjects.erase(theIO);
158   myIO2Actors.erase(theIO);
159   myMapIOSubIndex.erase(theIO);
160
161   return anIsIOBound;
162 }
163
164 bool 
165 SVTK_SelectorDef
166 ::RemoveIObject(SALOME_Actor* theActor) 
167 {
168   const Handle(SALOME_InteractiveObject) anIO = theActor->getIO();
169
170   bool anIsActorBound = myIO2Actors.find(anIO) != myIO2Actors.end();
171   if(anIsActorBound)
172     myIO2Actors.erase(anIO);
173
174   return RemoveIObject(anIO) || anIsActorBound;
175 }
176
177 //----------------------------------------------------------------------------
178 const SALOME_ListIO& 
179 SVTK_SelectorDef
180 ::StoredIObjects() const
181 {
182   myIObjectList.Clear();
183   TIObjects::const_iterator anIter = myIObjects.begin();
184   TIObjects::const_iterator anIterEnd = myIObjects.end();
185   for(; anIter != anIterEnd; anIter++)
186     myIObjectList.Append(*anIter);
187
188   return myIObjectList;
189 }
190
191 int
192 SVTK_SelectorDef
193 ::IObjectCount() const
194 {
195   return myIObjects.size();
196 }
197
198 bool 
199 SVTK_SelectorDef
200 ::HasIndex( const Handle(SALOME_InteractiveObject)& theIO) const
201 {
202   return myMapIOSubIndex.find(theIO) != myMapIOSubIndex.end();
203 }
204
205 void 
206 SVTK_SelectorDef
207 ::GetIndex( const Handle(SALOME_InteractiveObject)& theIO, 
208             TColStd_IndexedMapOfInteger& theIndex)
209 {
210   TMapIOSubIndex::const_iterator anIter = myMapIOSubIndex.find(theIO);
211   if(anIter != myMapIOSubIndex.end())
212     theIndex = anIter->second.myMap;
213   else
214     theIndex.Clear();
215 }
216
217 bool
218 SVTK_SelectorDef
219 ::IsIndexSelected(const Handle(SALOME_InteractiveObject)& theIO, 
220                   int theIndex) const
221 {
222   TMapIOSubIndex::const_iterator anIter = myMapIOSubIndex.find(theIO);
223   if(anIter != myMapIOSubIndex.end()){
224     const TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
225     return aMapIndex.Contains(theIndex);
226   }
227
228   return false;
229 }
230
231 static 
232 bool
233 removeIndex(TColStd_IndexedMapOfInteger& theMapIndex,
234             const int theIndex)
235 {
236   int anId = theMapIndex.FindIndex(theIndex); // i==0 if Index is not in the MapIndex
237   if(anId){
238     // only the last key can be removed
239     int aLastId = theMapIndex.FindKey(theMapIndex.Extent());
240     if(aLastId == anId)
241       theMapIndex.RemoveLast();
242     else{
243       TColStd_IndexedMapOfInteger aNewMap;
244       aNewMap.ReSize(theMapIndex.Extent()-1);
245       for(int j = 1; j <= theMapIndex.Extent(); j++){
246         int anIndex = theMapIndex(j);
247         if ( anIndex != theIndex )
248           aNewMap.Add( anIndex );
249       }
250       theMapIndex = aNewMap;
251     }
252   }
253   return anId;
254 }
255
256
257 bool
258 SVTK_SelectorDef
259 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
260                     const TColStd_IndexedMapOfInteger& 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   for(int i = 1, iEnd = theIndices.Extent(); i <= iEnd; i++)
275     aMapIndex.Add(theIndices(i));
276   
277   if(aMapIndex.IsEmpty()) {
278     myMapIOSubIndex.erase(theIO);
279     return false;
280   }
281
282   return true;
283 }
284
285
286 bool
287 SVTK_SelectorDef
288 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
289                     const TColStd_MapOfInteger& theIndices, 
290                     bool theIsModeShift)
291 {
292   TMapIOSubIndex::iterator aMapIter = myMapIOSubIndex.find(theIO);
293   if(aMapIter == myMapIOSubIndex.end()){
294     TIndexedMapOfInteger anEmpty;
295     aMapIter = myMapIOSubIndex.
296       insert(TMapIOSubIndex::value_type(theIO,anEmpty)).first;
297   }
298   TColStd_IndexedMapOfInteger& aMapIndex = aMapIter->second.myMap;
299
300   if(!theIsModeShift)
301     aMapIndex.Clear();
302   
303   TColStd_MapIteratorOfMapOfInteger anIter(theIndices);
304   for(; anIter.More(); anIter.Next())
305     aMapIndex.Add(anIter.Key());
306   
307   if(aMapIndex.IsEmpty()) {
308     myMapIOSubIndex.erase(theIO);
309     return false;
310   }
311
312   return true;
313 }
314
315
316 bool 
317 SVTK_SelectorDef
318 ::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
319                     int theIndex, 
320                     bool theIsModeShift)
321 {
322   TMapIOSubIndex::iterator anIter = myMapIOSubIndex.find(theIO);
323   if(anIter == myMapIOSubIndex.end()){
324     TIndexedMapOfInteger anEmpty;
325     anIter = myMapIOSubIndex.
326       insert(TMapIOSubIndex::value_type(theIO,anEmpty)).first;
327   }
328   TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
329
330   bool anIsConatains = aMapIndex.Contains(theIndex);
331   if(anIsConatains)
332     removeIndex(aMapIndex,theIndex);
333   
334   if(!theIsModeShift)
335     aMapIndex.Clear();
336   
337   if(!anIsConatains)
338     aMapIndex.Add( theIndex );
339
340   if( aMapIndex.IsEmpty())
341     myMapIOSubIndex.erase(theIO);
342
343   return false;
344 }
345
346
347 void
348 SVTK_SelectorDef
349 ::RemoveIndex( const Handle(SALOME_InteractiveObject)& theIO, 
350                int theIndex)
351 {
352   if(IsIndexSelected(theIO,theIndex)){
353     TMapIOSubIndex::iterator anIter = myMapIOSubIndex.find(theIO);
354     TColStd_IndexedMapOfInteger& aMapIndex = anIter->second.myMap;
355     removeIndex(aMapIndex,theIndex);
356   }
357 }
358
359 void 
360 SVTK_SelectorDef
361 ::ClearIndex()
362 {
363   myMapIOSubIndex.clear();  
364 }
365
366 //----------------------------------------------------------------------------
367 void
368 SVTK_SelectorDef
369 ::SetFilter(const Handle(VTKViewer_Filter)& theFilter)
370 {
371   myFilters.insert(TFilters::value_type(theFilter->GetId(),theFilter));
372 }
373
374 //----------------------------------------------------------------------------
375 bool
376 SVTK_SelectorDef
377 ::IsFilterPresent(const TFilterID theId) const
378 {
379   return myFilters.find(theId) != myFilters.end();
380 }
381
382 //----------------------------------------------------------------------------
383 void  
384 SVTK_SelectorDef
385 ::RemoveFilter(const TFilterID theId)
386 {
387   if(IsFilterPresent(theId))
388     myFilters.erase(theId);
389 }
390
391 //----------------------------------------------------------------------------
392 bool
393 SVTK_SelectorDef
394 ::IsValid(SALOME_Actor* theActor,
395           const TFilterID theId,
396           const bool theIsNode) const
397 {
398   TFilters::const_iterator anIter = myFilters.begin();
399   for(; anIter != myFilters.end(); ++anIter){
400     const Handle(VTKViewer_Filter)& aFilter = anIter->second;
401     if(theIsNode == aFilter->IsNodeFilter() &&
402        !aFilter->IsValid(theActor,theId))
403       return false;
404   }
405   return true;
406 }
407
408 //----------------------------------------------------------------------------
409 Handle(VTKViewer_Filter) 
410 SVTK_SelectorDef
411 ::GetFilter(const TFilterID theId) const
412 {
413   TFilters::const_iterator anIter = myFilters.find(theId);
414   if(anIter != myFilters.end()){
415     const Handle(VTKViewer_Filter)& aFilter = anIter->second;
416     return aFilter;
417   }
418   return Handle(VTKViewer_Filter)();
419 }
420