Salome HOME
Merge from OCC_development_generic_2006
[modules/gui.git] / src / LightApp / LightApp_VTKSelector.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/
18 //
19 #include "LightApp_VTKSelector.h"
20 #include "LightApp_DataOwner.h"
21
22 #include "SVTK_ViewModelBase.h"
23 #include "SVTK_Selector.h"
24 #include "SVTK_ViewWindow.h"
25 #include "SVTK_Functor.h"
26
27 #include "SALOME_Actor.h"
28 #include "SALOME_ListIteratorOfListIO.hxx"
29
30 #include "VTKViewer_Algorithm.h"
31
32 #include <vtkRenderer.h>
33
34 /*!
35   Constructor.
36 */
37 LightApp_SVTKDataOwner
38 ::LightApp_SVTKDataOwner( const Handle(SALOME_InteractiveObject)& theIO,
39                           const TColStd_IndexedMapOfInteger& theIds,
40                           Selection_Mode theMode,
41                           SALOME_Actor* theActor):
42   LightApp_DataOwner( theIO ),
43   mySelectionMode(theMode),
44   myActor(theActor)
45 {
46   myIds = theIds; // workaround - there is no constructor copy for the container
47 }
48
49 /*!
50   Destuctor.
51 */
52 LightApp_SVTKDataOwner
53 ::~LightApp_SVTKDataOwner()
54 {
55 }
56
57 /*!
58   Gets actor pointer.
59 */
60 SALOME_Actor* 
61 LightApp_SVTKDataOwner
62 ::GetActor() const
63 {
64   return myActor.GetPointer();
65 }
66
67 /*!
68   Constructor.
69 */
70 LightApp_VTKSelector
71 ::LightApp_VTKSelector( SVTK_ViewModelBase* viewer, 
72                         SUIT_SelectionMgr* mgr ): 
73   SUIT_Selector( mgr, viewer ),
74   myViewer( viewer )
75 {
76   if ( myViewer )
77     connect( myViewer, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
78 }
79
80 /*!
81   Destructor.
82 */
83 LightApp_VTKSelector
84 ::~LightApp_VTKSelector()
85 {
86 }
87
88 /*!
89   Gets viewer.
90 */
91 SVTK_ViewModelBase* 
92 LightApp_VTKSelector
93 ::viewer() const
94 {
95   return myViewer;
96 }
97
98 /*!
99   Gets type of salome vtk viewer.
100 */
101 QString
102 LightApp_VTKSelector
103 ::type() const
104
105   return myViewer->getType(); 
106 }
107
108 /*!
109   On selection changed.
110 */
111 void
112 LightApp_VTKSelector
113 ::onSelectionChanged()
114 {
115   selectionChanged();
116 }
117
118 /*!
119   Gets list of selected data owners.(output \a aList).
120 */
121 void
122 LightApp_VTKSelector
123 ::getSelection( SUIT_DataOwnerPtrList& aList ) const
124 {
125   if(myViewer){
126     if(SUIT_ViewManager* aViewMgr = myViewer->getViewManager()){
127       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewMgr->getActiveView())){
128         if(SVTK_Selector* aSelector = aView->GetSelector()){
129           Selection_Mode aMode = aSelector->SelectionMode();
130           const SALOME_ListIO& aListIO = aSelector->StoredIObjects();
131           SALOME_ListIteratorOfListIO anIter(aListIO);
132           for(; anIter.More(); anIter.Next()){
133             Handle(SALOME_InteractiveObject) anIO = anIter.Value();
134             if(anIO->hasEntry()){
135               TColStd_IndexedMapOfInteger anIds;
136               aSelector->GetIndex(anIO,anIds);
137               SALOME_Actor* anActor = aSelector->GetActor(anIO);
138               if( !anActor ){
139                 using namespace SVTK;
140                 anActor = Find<SALOME_Actor>(aView->getRenderer()->GetActors(),TIsSameIObject<SALOME_Actor>(anIO));
141               }
142               aList.append(new LightApp_SVTKDataOwner(anIO,anIds,aMode,anActor));
143             }
144           }
145         }
146       }
147     }
148   }
149 }
150
151 /*!
152   Sets selection to selector from data owner list \a theList.
153 */
154 void 
155 LightApp_VTKSelector
156 ::setSelection( const SUIT_DataOwnerPtrList& theList )
157 {
158   if(myViewer){
159     if(SUIT_ViewManager* aViewMgr = myViewer->getViewManager()){
160       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewMgr->getActiveView())){
161         if(SVTK_Selector* aSelector = aView->GetSelector()){
162           SALOME_ListIO anAppendList;
163           const SALOME_ListIO& aStoredList = aSelector->StoredIObjects();
164           SUIT_DataOwnerPtrList::const_iterator anIter = theList.begin();
165           for(; anIter != theList.end(); ++anIter){
166             const SUIT_DataOwner* aDataOwner = (*anIter).get();
167             if(const LightApp_SVTKDataOwner* anOwner = dynamic_cast<const LightApp_SVTKDataOwner*>(aDataOwner)){
168               aSelector->SetSelectionMode(anOwner->GetMode());
169               Handle(SALOME_InteractiveObject) anIO = anOwner->IO();
170
171               if( anOwner->GetActor() )
172                 aSelector->AddIObject( anOwner->GetActor() );
173               else
174                 aSelector->AddIObject(anIO);
175
176               anAppendList.Append(anIO);
177               aSelector->AddOrRemoveIndex(anIO,anOwner->GetIds(),false);
178             }else if(const LightApp_DataOwner* anOwner = dynamic_cast<const LightApp_DataOwner*>(aDataOwner)){
179               Handle(SALOME_InteractiveObject) anIO = 
180                 new SALOME_InteractiveObject(anOwner->entry().latin1(),"");
181               aSelector->AddIObject(anIO);
182               anAppendList.Append(anIO);
183             }
184           }
185           // To remove IOs, which is not selected.
186           QMap< QString, Handle( SALOME_InteractiveObject )> toRemove;
187           SALOME_ListIteratorOfListIO anIt( aStoredList );
188           for( ; anIt.More(); anIt.Next() )
189             if( !anIt.Value().IsNull() )
190               toRemove[ anIt.Value()->getEntry() ] = anIt.Value();
191
192           anIt = SALOME_ListIteratorOfListIO(anAppendList);
193           for( ; anIt.More(); anIt.Next() )
194             toRemove.remove( anIt.Value()->getEntry() );
195
196           QMap< QString, Handle( SALOME_InteractiveObject )>::const_iterator RIt = toRemove.begin(),
197                                                                              REnd = toRemove.end();
198           for( ; RIt!=REnd; RIt++ )
199             aSelector->RemoveIObject( RIt.data() );
200           
201           aView->onSelectionChanged();
202         }
203       }
204     }
205   }
206 }