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