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