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