]> SALOME platform Git repositories - modules/gui.git/blob - src/SalomeApp/SalomeApp_SelectionMgr.cxx
Salome HOME
*** empty log message ***
[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   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
58   {
59     const SalomeApp_DataOwner* owner = dynamic_cast<const SalomeApp_DataOwner*>( (*itr).operator->() );
60     if( owner )
61       lst.Append( owner->IO() );
62 /*
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->GetName().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->GetName().c_str() );
92           if ( anIO ) lst.Append( anIO );
93         }
94       }
95     }
96 */
97   }
98 }
99
100 void SalomeApp_SelectionMgr::setSelectedObjects( const SALOME_ListIO& lst, const bool append )
101 {
102   if ( !application() )
103     return;
104
105   SalomeApp_Study* appStudy = dynamic_cast<SalomeApp_Study*>( application()->activeStudy() );
106   if ( !appStudy )
107     return;
108
109   _PTR(Study) aStudy ( appStudy->studyDS() );
110   if ( !aStudy )
111     return;
112
113   SUIT_DataOwnerPtrList owners;
114   for ( SALOME_ListIteratorOfListIO it( lst ); it.More(); it.Next() )
115   {
116     if ( it.Value()->hasEntry() )
117       owners.append( new SalomeApp_DataOwner( it.Value()->getEntry() ) );
118   }
119   setSelected( owners, append );
120 }
121
122 void SalomeApp_SelectionMgr::selectionChanged( SUIT_Selector* theSel )
123 {
124   SUIT_SelectionMgr::selectionChanged( theSel );
125
126   emit currentSelectionChanged();
127 }
128
129 /*
130   get map of indexes for the given SALOME_InteractiveObject
131
132 */
133 void SalomeApp_SelectionMgr::GetIndexes( const Handle(SALOME_InteractiveObject)& IObject, 
134                                          TColStd_IndexedMapOfInteger& theIndex)
135 {
136   theIndex.Clear();
137
138   SUIT_DataOwnerPtrList aList;
139   selected( aList );
140
141   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
142   {
143     const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( (*itr).operator->() );
144     if ( subOwner )
145       if ( subOwner->entry() == QString(IObject->getEntry()) )
146         theIndex.Add( subOwner->index() );
147   }
148   
149 }
150
151 /*
152   get map of indexes for the given entry of SALOME_InteractiveObject
153
154 */
155 void SalomeApp_SelectionMgr::GetIndexes( const QString& theEntry, TColStd_IndexedMapOfInteger& theIndex )
156 {
157   theIndex.Clear();
158
159   SUIT_DataOwnerPtrList aList;
160   selected( aList );
161
162   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
163   {
164     const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( (*itr).operator->() );
165     if ( subOwner )
166       if ( subOwner->entry() == theEntry )
167         theIndex.Add( subOwner->index() );
168   }
169
170 }
171
172 bool SalomeApp_SelectionMgr::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject, 
173                                                const TColStd_MapOfInteger& theIndexes, 
174                                                bool modeShift)
175 {
176   SUIT_DataOwnerPtrList remainsOwners;
177   
178   SUIT_DataOwnerPtrList aList;
179   selected( aList );
180
181   if ( !modeShift ) {
182     for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
183     {
184       const SalomeApp_DataOwner* owner = dynamic_cast<const SalomeApp_DataOwner*>( (*itr).operator->() );
185       if ( owner ) 
186       {
187         if ( owner->entry() != QString(IObject->getEntry()) ) 
188         {         
189           const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( owner );
190           if ( subOwner )
191             remainsOwners.append( new SalomeApp_DataSubOwner( subOwner->entry(), subOwner->index() ) );
192           else
193             remainsOwners.append( new SalomeApp_DataOwner( owner->entry() ) );
194         }
195       }
196     }
197   }
198   else
199     remainsOwners = aList;
200
201   TColStd_MapIteratorOfMapOfInteger It;
202   It.Initialize(theIndexes);
203   for(;It.More();It.Next())
204     remainsOwners.append( new SalomeApp_DataSubOwner( QString(IObject->getEntry()), It.Key() ) );
205   
206   bool append = false;
207   setSelected( remainsOwners, append );
208
209   emit currentSelectionChanged();
210
211   TColStd_IndexedMapOfInteger anIndexes;
212   GetIndexes( IObject, anIndexes );
213   return !anIndexes.IsEmpty();
214
215 }
216
217 /*
218   select 'subobjects' with given indexes
219
220 */
221 void SalomeApp_SelectionMgr::selectObjects( const Handle(SALOME_InteractiveObject)& IObject, 
222                                             TColStd_IndexedMapOfInteger theIndex, bool append )
223 {
224   SUIT_DataOwnerPtrList aList;
225
226   if ( theIndex.IsEmpty() )
227     aList.append( new SalomeApp_DataOwner( QString(IObject->getEntry()) ) );
228   else
229     {
230       int i;
231       for ( i = 1; i <= theIndex.Extent(); i++ )
232         aList.append( new SalomeApp_DataSubOwner( QString(IObject->getEntry()), theIndex( i ) ) );
233     }
234
235   setSelected( aList, append );
236
237 }
238
239 /*
240   select 'subobjects' with given indexes
241
242 */
243 void SalomeApp_SelectionMgr::selectObjects( MapIOOfMapOfInteger theMapIO, bool append )
244 {
245   SUIT_DataOwnerPtrList aList;
246
247   MapIOOfMapOfInteger::Iterator it;
248   for ( it = theMapIO.begin(); it != theMapIO.end(); ++it ) 
249     {
250       if ( it.data().IsEmpty() )
251         aList.append( new SalomeApp_DataOwner( QString(it.key()->getEntry()) ) );
252       else
253         {
254           int i;
255           for ( i = 1; i <= it.data().Extent(); i++ )
256             aList.append( new SalomeApp_DataSubOwner( QString(it.key()->getEntry()), it.data()( i ) ) );
257         }
258     }
259   
260   setSelected( aList, append );
261
262 }
263
264 /*
265   get map of selected subowners : object's entry <-> map of indexes
266
267 */
268 void SalomeApp_SelectionMgr::selectedSubOwners( MapEntryOfMapOfInteger& theMap )
269 {
270   theMap.clear();
271
272   TColStd_IndexedMapOfInteger anIndexes;
273
274   SUIT_DataOwnerPtrList aList;
275   selected( aList );
276
277   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
278   {
279     const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( (*itr).operator->() );
280     if ( subOwner ) 
281     {
282       if ( !theMap.contains( subOwner->entry() ) )
283       {
284         anIndexes.Clear();
285         GetIndexes( subOwner->entry(), anIndexes );
286         theMap.insert( subOwner->entry(), anIndexes );
287       }
288     }
289   }
290 }