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