Salome HOME
Initial version
[modules/gui.git] / src / SalomeApp / SalomeApp_SelectionMgr.cxx
1 #include "SalomeApp_SelectionMgr.h"
2
3 #include "SalomeApp_Study.h"
4 #include "SalomeApp_DataOwner.h"
5 #include "SalomeApp_DataSubOwner.h"
6 #include "SalomeApp_Application.h"
7
8 #include <SUIT_Session.h>
9
10 #include <SALOME_ListIO.hxx>
11 #include <SALOME_ListIteratorOfListIO.hxx>
12
13 // Open CASCADE Include
14 #include <TColStd_MapOfInteger.hxx>
15 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
16 #include <TColStd_IndexedMapOfInteger.hxx>
17
18 #include "SALOMEDSClient.hxx"
19
20 SalomeApp_SelectionMgr::SalomeApp_SelectionMgr( SalomeApp_Application* app, const bool fb )
21 : SUIT_SelectionMgr( fb ),
22 myApp( app )
23 {
24 }
25
26 SalomeApp_SelectionMgr::~SalomeApp_SelectionMgr()
27 {
28 }
29
30 SalomeApp_Application* SalomeApp_SelectionMgr::application() const
31 {
32   return myApp;
33 }
34
35 /*
36   get all selected objects from selection manager
37
38 */
39 void SalomeApp_SelectionMgr::selectedObjects( SALOME_ListIO& lst ) const
40 {
41   lst.Clear();
42
43   if ( !application() )
44     return;
45
46   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( application()->activeStudy() );
47   if ( !appStudy )
48     return;
49
50   _PTR(Study) aStudy ( appStudy->studyDS() );
51   if ( !aStudy )
52     return;
53
54   SUIT_DataOwnerPtrList aList;
55   selected( aList );
56
57   QValueList< QString > anEntryList;
58   Handle(SALOME_InteractiveObject) anIO;
59
60   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
61   {
62     const SalomeApp_DataOwner* owner = dynamic_cast<const SalomeApp_DataOwner*>( (*itr).operator->() );
63
64     if ( owner && dynamic_cast<const SalomeApp_DataSubOwner*>( owner ) ) 
65     { //get only subowners, insert into lst unique subowners (subowners with different entries)
66       if ( !anEntryList.contains( owner->entry() ) )
67       { 
68         anEntryList.append( owner->entry() );
69
70         //construct SALOME_InteractiveObject with predefined entry
71         _PTR(SObject) anObj ( aStudy->FindObjectID( owner->entry().latin1() ) );
72         if ( anObj )
73         {
74           _PTR(SComponent) aFC (anObj->GetFatherComponent());
75           if ( aFC )
76           {
77             anIO = new SALOME_InteractiveObject( anObj->GetID().c_str(), aFC->ComponentDataType().c_str(), anObj->Name().c_str() );
78             if ( anIO ) lst.Append( anIO );
79           }
80         }
81       }
82     }
83     else if ( owner )
84     { //get not subowners data owners
85       _PTR(SObject) anObj ( aStudy->FindObjectID( owner->entry().latin1() ) );
86       if ( anObj )
87       {
88         _PTR(SComponent) aFC (anObj->GetFatherComponent());
89         if ( aFC )
90         {
91           anIO = new SALOME_InteractiveObject( anObj->GetID().c_str(), aFC->ComponentDataType().c_str(), anObj->Name().c_str() );
92           if ( anIO ) lst.Append( anIO );
93         }
94       }
95     }
96   }
97 }
98
99 void SalomeApp_SelectionMgr::setSelectedObjects( const SALOME_ListIO& lst, const bool append )
100 {
101   if ( !application() )
102     return;
103
104   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( application()->activeStudy() );
105   if ( !appStudy )
106     return;
107
108   _PTR(Study) aStudy ( appStudy->studyDS() );
109   if ( !aStudy )
110     return;
111
112   SUIT_DataOwnerPtrList owners;
113   for ( SALOME_ListIteratorOfListIO it( lst ); it.More(); it.Next() )
114   {
115     if ( it.Value()->hasEntry() )
116       owners.append( new SalomeApp_DataOwner( it.Value()->getEntry() ) );
117   }
118   setSelected( owners, append );
119 }
120
121 void SalomeApp_SelectionMgr::selectionChanged( SUIT_Selector* theSel )
122 {
123   SUIT_SelectionMgr::selectionChanged( theSel );
124
125   emit currentSelectionChanged();
126 }
127
128 /*
129   get map of indexes for the given SALOME_InteractiveObject
130
131 */
132 void SalomeApp_SelectionMgr::GetIndexes( const Handle(SALOME_InteractiveObject)& IObject, 
133                                          TColStd_IndexedMapOfInteger& theIndex)
134 {
135   theIndex.Clear();
136
137   SUIT_DataOwnerPtrList aList;
138   selected( aList );
139
140   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
141   {
142     const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( (*itr).operator->() );
143     if ( subOwner )
144       if ( subOwner->entry() == QString(IObject->getEntry()) )
145         theIndex.Add( subOwner->index() );
146   }
147   
148 }
149
150 /*
151   get map of indexes for the given entry of SALOME_InteractiveObject
152
153 */
154 void SalomeApp_SelectionMgr::GetIndexes( const QString& theEntry, TColStd_IndexedMapOfInteger& theIndex )
155 {
156   theIndex.Clear();
157
158   SUIT_DataOwnerPtrList aList;
159   selected( aList );
160
161   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
162   {
163     const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( (*itr).operator->() );
164     if ( subOwner )
165       if ( subOwner->entry() == theEntry )
166         theIndex.Add( subOwner->index() );
167   }
168
169 }
170
171 bool SalomeApp_SelectionMgr::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject, 
172                                                const TColStd_MapOfInteger& theIndexes, 
173                                                bool modeShift)
174 {
175   SUIT_DataOwnerPtrList remainsOwners;
176   
177   SUIT_DataOwnerPtrList aList;
178   selected( aList );
179
180   if ( !modeShift ) {
181     for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
182     {
183       const SalomeApp_DataOwner* owner = dynamic_cast<const SalomeApp_DataOwner*>( (*itr).operator->() );
184       if ( owner ) 
185       {
186         if ( owner->entry() != QString(IObject->getEntry()) ) 
187         {         
188           const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( owner );
189           if ( subOwner )
190             remainsOwners.append( new SalomeApp_DataSubOwner( subOwner->entry(), subOwner->index() ) );
191           else
192             remainsOwners.append( new SalomeApp_DataOwner( owner->entry() ) );
193         }
194       }
195     }
196   }
197   else
198     remainsOwners = aList;
199
200   TColStd_MapIteratorOfMapOfInteger It;
201   It.Initialize(theIndexes);
202   for(;It.More();It.Next())
203     remainsOwners.append( new SalomeApp_DataSubOwner( QString(IObject->getEntry()), It.Key() ) );
204   
205   bool append = false;
206   setSelected( remainsOwners, append );
207
208   emit currentSelectionChanged();
209
210   TColStd_IndexedMapOfInteger anIndexes;
211   GetIndexes( IObject, anIndexes );
212   return !anIndexes.IsEmpty();
213
214 }
215
216 /*
217   select 'subobjects' with given indexes
218
219 */
220 void SalomeApp_SelectionMgr::selectObjects( const Handle(SALOME_InteractiveObject)& IObject, 
221                                             TColStd_IndexedMapOfInteger theIndex, bool append )
222 {
223   SUIT_DataOwnerPtrList aList;
224
225   if ( theIndex.IsEmpty() )
226     aList.append( new SalomeApp_DataOwner( QString(IObject->getEntry()) ) );
227   else
228     {
229       int i;
230       for ( i = 1; i <= theIndex.Extent(); i++ )
231         aList.append( new SalomeApp_DataSubOwner( QString(IObject->getEntry()), theIndex( i ) ) );
232     }
233
234   setSelected( aList, append );
235
236 }
237
238 /*
239   select 'subobjects' with given indexes
240
241 */
242 void SalomeApp_SelectionMgr::selectObjects( MapIOOfMapOfInteger theMapIO, bool append )
243 {
244   SUIT_DataOwnerPtrList aList;
245
246   MapIOOfMapOfInteger::Iterator it;
247   for ( it = theMapIO.begin(); it != theMapIO.end(); ++it ) 
248     {
249       if ( it.data().IsEmpty() )
250         aList.append( new SalomeApp_DataOwner( QString(it.key()->getEntry()) ) );
251       else
252         {
253           int i;
254           for ( i = 1; i <= it.data().Extent(); i++ )
255             aList.append( new SalomeApp_DataSubOwner( QString(it.key()->getEntry()), it.data()( i ) ) );
256         }
257     }
258   
259   setSelected( aList, append );
260
261 }
262
263 /*
264   get map of selected subowners : object's entry <-> map of indexes
265
266 */
267 void SalomeApp_SelectionMgr::selectedSubOwners( MapEntryOfMapOfInteger& theMap )
268 {
269   theMap.clear();
270
271   TColStd_IndexedMapOfInteger anIndexes;
272
273   SUIT_DataOwnerPtrList aList;
274   selected( aList );
275
276   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
277   {
278     const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( (*itr).operator->() );
279     if ( subOwner ) 
280     {
281       if ( !theMap.contains( subOwner->entry() ) )
282       {
283         anIndexes.Clear();
284         GetIndexes( subOwner->entry(), anIndexes );
285         theMap.insert( subOwner->entry(), anIndexes );
286       }
287     }
288   }
289 }