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