Salome HOME
Copyright update 2022
[modules/gui.git] / src / LightApp / LightApp_VTKSelector.cxx
1 // Copyright (C) 2007-2022  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "LightApp_VTKSelector.h"
24 #include "LightApp_DataOwner.h"
25
26 #include "SUIT_Desktop.h"
27
28 #ifndef DISABLE_VTKVIEWER
29   #include "SVTK_ViewModelBase.h"
30   #include "SVTK_ViewManager.h"
31   #include "SVTK_Selector.h"
32   #include "SVTK_ViewWindow.h"
33   #include "SVTK_Functor.h"
34   #include "VTKViewer_Algorithm.h"
35   #include <vtkRenderer.h>
36   #include "SVTK_ViewModel.h"
37 #endif
38
39 #ifndef DISABLE_SALOMEOBJECT
40   #include "SALOME_Actor.h"
41   #include "SALOME_ListIO.hxx"
42 #endif
43
44 #include <utilities.h>
45
46 #ifndef DISABLE_VTKVIEWER
47 #ifndef DISABLE_SALOMEOBJECT
48 /*!
49   Constructor.
50 */
51 LightApp_SVTKDataOwner::LightApp_SVTKDataOwner( const Handle(SALOME_InteractiveObject)& theIO,
52                                                                   SUIT_Desktop* theDesktop )
53 : LightApp_DataOwner( theIO ),
54 myDesktop( theDesktop )
55 {
56 }
57 #else
58 LightApp_SVTKDataOwner::LightApp_SVTKDataOwner( const QString& theEntry )
59 : LightApp_DataOwner( theEntry )
60 {
61 }
62 #endif
63
64 /*!
65   \return active SVTK view window
66 */
67 SVTK_ViewWindow* 
68 LightApp_SVTKDataOwner
69 ::GetActiveViewWindow() const
70 {
71   if(SUIT_ViewWindow* aViewWindow = myDesktop->activeWindow())
72     return dynamic_cast<SVTK_ViewWindow*>(aViewWindow);
73
74   return NULL;
75 }
76
77 /*!
78   Gets dataowners ids list.
79 */
80 const SVTK_TIndexedMapOfVtkId& 
81 LightApp_SVTKDataOwner
82 ::GetIds() const
83 {
84   if(SVTK_ViewWindow* aViewWindow = GetActiveViewWindow()){
85     if(SVTK_Selector* aSelector = aViewWindow->GetSelector()){
86       aSelector->GetIndex(IO(),myIds);
87     }
88   }
89
90   return myIds;
91 }
92
93 /*!
94   Gets selection mode.
95 */
96 Selection_Mode
97 LightApp_SVTKDataOwner
98 ::GetMode() const
99 {
100   if(SVTK_ViewWindow* aViewWindow = GetActiveViewWindow()){
101     if(SVTK_Selector* aSelector = aViewWindow->GetSelector()){
102       return aSelector->SelectionMode();
103     }
104   }
105   
106   return ActorSelection; // workaround for embedded VTK viewer in a module : is it safe ?
107 }
108
109 /*!
110   Gets actor pointer.
111 */
112 SALOME_Actor* 
113 LightApp_SVTKDataOwner
114 ::GetActor() const
115 {
116   if(SVTK_ViewWindow* aViewWindow = GetActiveViewWindow()){
117     using namespace SVTK;
118     VTK::ActorCollectionCopy aCopy(aViewWindow->getRenderer()->GetActors());
119     return Find<SALOME_Actor>(aCopy.GetActors(),TIsSameIObject<SALOME_Actor>(IO()));
120   }
121
122   return NULL;
123 }
124
125 #endif
126
127 /*!
128   Destuctor.
129 */
130 /*
131 LightApp_SVTKDataOwner::~LightApp_SVTKDataOwner()
132 {
133 }
134 */
135
136 #ifndef DISABLE_VTKVIEWER
137 /*!
138   Constructor.
139 */
140 LightApp_VTKSelector
141 ::LightApp_VTKSelector( SVTK_ViewModelBase* viewer, 
142                         SUIT_SelectionMgr* mgr ): 
143   SUIT_Selector( mgr, viewer ),
144   myViewer( viewer )
145 {
146   if ( myViewer )
147     connect( myViewer, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
148 }
149
150 /*!
151   Gets viewer.
152 */
153 SVTK_ViewModelBase* 
154 LightApp_VTKSelector
155 ::viewer() const
156 {
157   return myViewer;
158 }
159
160 /*!
161   Gets type of salome vtk viewer.
162 */
163 QString
164 LightApp_VTKSelector
165 ::type() const
166
167   return myViewer->getType(); 
168 }
169
170 #endif
171 /*!
172   On selection changed.
173 */
174 void
175 LightApp_VTKSelector
176 ::onSelectionChanged()
177 {
178   selectionChanged();
179 }
180
181 #ifndef DISABLE_VTKVIEWER
182
183 /*!
184   Gets list of selected data owners.(output \a aList).
185 */
186 void
187 LightApp_VTKSelector
188 ::getSelection( SUIT_DataOwnerPtrList& aList ) const
189 {
190   if( myViewer ) {
191     if( SVTK_Viewer* aSViewer = dynamic_cast<SVTK_Viewer*>(myViewer) ) {
192       if( !aSViewer->isSelectionEnabled() )
193         return;
194     }
195     if(SUIT_ViewManager* aViewManager = myViewer->getViewManager()){
196       if(SVTK_ViewManager* aViewMgr = dynamic_cast<SVTK_ViewManager*>(aViewManager)){
197         if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewMgr->getActiveView())){
198           if(SVTK_Selector* aSelector = aView->GetSelector()){
199             const SALOME_ListIO& aListIO = aSelector->StoredIObjects();
200             SALOME_ListIteratorOfListIO anIter(aListIO);
201             for(; anIter.More(); anIter.Next()){
202               Handle(SALOME_InteractiveObject) anIO = anIter.Value();
203               if(anIO->hasEntry())
204                 aList.append(new LightApp_SVTKDataOwner(anIO,aViewMgr->getDesktop()));
205             }
206           }
207         }
208       }
209     }
210   }
211 }
212
213 /*!
214   Sets selection to selector from data owner list \a theList.
215 */
216 void
217 LightApp_VTKSelector
218 ::setSelection( const SUIT_DataOwnerPtrList& theList )
219 {
220   SVTK_Viewer* aViewer = dynamic_cast<SVTK_Viewer*>(myViewer);
221   if ( myViewer && aViewer && ( theList.isEmpty() || aViewer->isSelectionEnabled() )) {
222     if(SUIT_ViewManager* aViewMgr = myViewer->getViewManager()){
223       if(SVTK_ViewWindow* aView = dynamic_cast<SVTK_ViewWindow*>(aViewMgr->getActiveView())){
224         if(SVTK_Selector* aSelector = aView->GetSelector()){
225           SALOME_ListIO anAppendList;
226           const SALOME_ListIO& aStoredList = aSelector->StoredIObjects();
227           SUIT_DataOwnerPtrList::const_iterator anIter = theList.begin();
228           for(; anIter != theList.end(); ++anIter) {
229             const SUIT_DataOwner* aDataOwner = (*anIter).get();
230             if(const LightApp_SVTKDataOwner* anOwner = dynamic_cast<const LightApp_SVTKDataOwner*>(aDataOwner))
231             {
232               MESSAGE("aSelector->SetSelectionMode("<<anOwner->GetMode()<<");");
233               aSelector->SetSelectionMode(anOwner->GetMode());
234               Handle(SALOME_InteractiveObject) anIO = anOwner->IO();
235
236               aSelector->AddIObject(anIO);
237
238               anAppendList.Append(anIO);
239               aSelector->AddOrRemoveIndex(anIO,anOwner->GetIds(),false);
240             }
241             else if(const LightApp_DataOwner* anOwner = dynamic_cast<const LightApp_DataOwner*>(aDataOwner))
242             {
243               Handle(SALOME_InteractiveObject) anIO =
244                 new SALOME_InteractiveObject(anOwner->entry().toUtf8(),"");
245               aSelector->AddIObject(anIO);
246               anAppendList.Append(anIO);
247             }
248           }
249           // To remove IOs, which is not selected.
250           QMap< QString, Handle( SALOME_InteractiveObject )> toRemove;
251           SALOME_ListIteratorOfListIO anIt( aStoredList );
252           for( ; anIt.More(); anIt.Next() )
253             if( !anIt.Value().IsNull() )
254               toRemove[ anIt.Value()->getEntry() ] = anIt.Value();
255
256           anIt = SALOME_ListIteratorOfListIO(anAppendList);
257           for( ; anIt.More(); anIt.Next() )
258             toRemove.remove( anIt.Value()->getEntry() );
259
260           QMap< QString, Handle( SALOME_InteractiveObject )>::const_iterator RIt = toRemove.begin(),
261             REnd = toRemove.end();
262           for( ; RIt!=REnd; RIt++ )
263             aSelector->RemoveIObject( RIt.value() );
264
265           aView->onSelectionChanged();
266         }
267       }
268     }
269   }
270 }
271
272 #endif