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