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