Salome HOME
Join modifications from branch OCC_debug_for_3_2_0b1
[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 #ifndef DISABLE_VTKVIEWER
23   #include "SVTK_ViewModelBase.h"
24   #include "SVTK_ViewManager.h"
25   #include "SVTK_Selector.h"
26   #include "SVTK_ViewWindow.h"
27   #include "SVTK_Functor.h"
28   #include "VTKViewer_Algorithm.h"
29   #include <vtkRenderer.h>
30 #endif
31
32 #ifndef DISABLE_SALOMEOBJECT
33   #include "SALOME_Actor.h"
34   #include "SALOME_ListIteratorOfListIO.hxx"
35 #endif
36
37
38
39 #ifndef DISABLE_VTKVIEWER
40 #ifndef DISABLE_SALOMEOBJECT
41 /*!
42   Constructor.
43 */
44 LightApp_SVTKDataOwner
45 ::LightApp_SVTKDataOwner( const Handle(SALOME_InteractiveObject)& theIO,
46                           SUIT_Desktop* theDesktop ):
47   LightApp_DataOwner( theIO ),
48   myDesktop( theDesktop )
49 {}
50 #endif
51
52 /*!
53   Destuctor.
54 */
55 LightApp_SVTKDataOwner
56 ::~LightApp_SVTKDataOwner()
57 {
58 }
59
60 /*!
61   \return active SVTK view window
62 */
63 SVTK_ViewWindow* 
64 LightApp_SVTKDataOwner
65 ::GetActiveViewWindow() const
66 {
67   if(SUIT_ViewWindow* aViewWindow = myDesktop->activeWindow())
68     return dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
69
70   return NULL;
71 }
72
73 /*!
74   Gets dataowners ids list.
75 */
76 const TColStd_IndexedMapOfInteger& 
77 LightApp_SVTKDataOwner
78 ::GetIds() const
79 {
80   if(SVTK_ViewWindow* aViewWindow = GetActiveViewWindow()){
81     if(SVTK_Selector* aSelector = aViewWindow->GetSelector()){
82       aSelector->GetIndex(IO(),myIds);
83     }
84   }
85
86   return myIds;
87 }
88
89 /*!
90   Gets selection mode.
91 */
92 Selection_Mode
93 LightApp_SVTKDataOwner
94 ::GetMode() const
95 {
96   if(SVTK_ViewWindow* aViewWindow = GetActiveViewWindow()){
97     if(SVTK_Selector* aSelector = aViewWindow->GetSelector()){
98       return aSelector->SelectionMode();
99     }
100   }
101   
102   return -1;
103 }
104
105 /*!
106   Gets actor pointer.
107 */
108 SALOME_Actor* 
109 LightApp_SVTKDataOwner
110 ::GetActor() const
111 {
112   if(SVTK_ViewWindow* aViewWindow = GetActiveViewWindow()){
113     using namespace SVTK;
114     return Find<SALOME_Actor>(aViewWindow->getRenderer()->GetActors(),TIsSameIObject<SALOME_Actor>(IO()));
115   }
116
117   return NULL;
118 }
119
120 #endif
121
122
123 #ifndef DISABLE_VTKVIEWER
124 /*!
125   Constructor.
126 */
127 LightApp_VTKSelector
128 ::LightApp_VTKSelector( SVTK_ViewModelBase* viewer, 
129                         SUIT_SelectionMgr* mgr ): 
130   SUIT_Selector( mgr, viewer ),
131   myViewer( viewer )
132 {
133   if ( myViewer )
134     connect( myViewer, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
135 }
136
137 /*!
138   Destructor.
139 */
140 LightApp_VTKSelector
141 ::~LightApp_VTKSelector()
142 {
143 }
144
145 /*!
146   Gets viewer.
147 */
148 SVTK_ViewModelBase* 
149 LightApp_VTKSelector
150 ::viewer() const
151 {
152   return myViewer;
153 }
154
155 /*!
156   Gets type of salome vtk viewer.
157 */
158 QString
159 LightApp_VTKSelector
160 ::type() const
161
162   return myViewer->getType(); 
163 }
164
165 #endif
166 /*!
167   On selection changed.
168 */
169 void
170 LightApp_VTKSelector
171 ::onSelectionChanged()
172 {
173   selectionChanged();
174 }
175
176 #ifndef DISABLE_VTKVIEWER
177
178 /*!
179   Gets list of selected data owners.(output \a aList).
180 */
181 void
182 LightApp_VTKSelector
183 ::getSelection( SUIT_DataOwnerPtrList& aList ) const
184 {
185   if(myViewer){
186     if(SUIT_ViewManager* aViewManager = myViewer->getViewManager()){
187       if(SVTK_ViewManager* aViewMgr = dynamic_cast<SVTK_ViewManager*>(aViewManager)){
188         if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewMgr->getActiveView())){
189           if(SVTK_Selector* aSelector = aView->GetSelector()){
190             const SALOME_ListIO& aListIO = aSelector->StoredIObjects();
191             SALOME_ListIteratorOfListIO anIter(aListIO);
192             for(; anIter.More(); anIter.Next()){
193               Handle(SALOME_InteractiveObject) anIO = anIter.Value();
194               if(anIO->hasEntry())
195                 aList.append(new LightApp_SVTKDataOwner(anIO,aViewMgr->getDesktop()));
196             }
197           }
198         }
199       }
200     }
201   }
202 }
203
204 /*!
205   Sets selection to selector from data owner list \a theList.
206 */
207 void 
208 LightApp_VTKSelector
209 ::setSelection( const SUIT_DataOwnerPtrList& theList )
210 {
211   if(myViewer){
212     if(SUIT_ViewManager* aViewMgr = myViewer->getViewManager()){
213       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewMgr->getActiveView())){
214         if(SVTK_Selector* aSelector = aView->GetSelector()){
215           SALOME_ListIO anAppendList;
216           const SALOME_ListIO& aStoredList = aSelector->StoredIObjects();
217           SUIT_DataOwnerPtrList::const_iterator anIter = theList.begin();
218           for(; anIter != theList.end(); ++anIter){
219             const SUIT_DataOwner* aDataOwner = (*anIter).get();
220             if(const LightApp_SVTKDataOwner* anOwner = dynamic_cast<const LightApp_SVTKDataOwner*>(aDataOwner)){
221               aSelector->SetSelectionMode(anOwner->GetMode());
222               Handle(SALOME_InteractiveObject) anIO = anOwner->IO();
223
224               aSelector->AddIObject(anIO);
225
226               anAppendList.Append(anIO);
227               aSelector->AddOrRemoveIndex(anIO,anOwner->GetIds(),false);
228             }else if(const LightApp_DataOwner* anOwner = dynamic_cast<const LightApp_DataOwner*>(aDataOwner)){
229               Handle(SALOME_InteractiveObject) anIO = 
230                 new SALOME_InteractiveObject(anOwner->entry().latin1(),"");
231               aSelector->AddIObject(anIO);
232               anAppendList.Append(anIO);
233             }
234           }
235           // To remove IOs, which is not selected.
236           QMap< QString, Handle( SALOME_InteractiveObject )> toRemove;
237           SALOME_ListIteratorOfListIO anIt( aStoredList );
238           for( ; anIt.More(); anIt.Next() )
239             if( !anIt.Value().IsNull() )
240               toRemove[ anIt.Value()->getEntry() ] = anIt.Value();
241
242           anIt = SALOME_ListIteratorOfListIO(anAppendList);
243           for( ; anIt.More(); anIt.Next() )
244             toRemove.remove( anIt.Value()->getEntry() );
245
246           QMap< QString, Handle( SALOME_InteractiveObject )>::const_iterator RIt = toRemove.begin(),
247                                                                              REnd = toRemove.end();
248           for( ; RIt!=REnd; RIt++ )
249             aSelector->RemoveIObject( RIt.data() );
250           
251           aView->onSelectionChanged();
252         }
253       }
254     }
255   }
256 }
257
258 #endif