Salome HOME
Columns in object browser
[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& theList, const QString& theType ) const
40 {
41   theList.Clear();
42
43   SUIT_DataOwnerPtrList aList;
44   selected( aList, theType );
45
46   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
47   {
48     const SalomeApp_DataOwner* owner = dynamic_cast<const SalomeApp_DataOwner*>( (*itr).operator->() );
49     if( owner )
50       theList.Append( owner->IO() );
51   }
52 }
53
54 void SalomeApp_SelectionMgr::setSelectedObjects( const SALOME_ListIO& lst, const bool append )
55 {
56   SUIT_DataOwnerPtrList owners;
57   for ( SALOME_ListIteratorOfListIO it( lst ); it.More(); it.Next() )
58   {
59     if ( it.Value()->hasEntry() )
60       owners.append( new SalomeApp_DataOwner( it.Value() ) );
61   }
62
63   setSelected( owners, append );
64 }
65
66 void SalomeApp_SelectionMgr::selectionChanged( SUIT_Selector* theSel )
67 {
68   SUIT_SelectionMgr::selectionChanged( theSel );
69
70   emit currentSelectionChanged();
71 }
72
73 /*
74   get map of indexes for the given SALOME_InteractiveObject
75
76 */
77 void SalomeApp_SelectionMgr::GetIndexes( const Handle(SALOME_InteractiveObject)& IObject, 
78                                          TColStd_IndexedMapOfInteger& theIndex)
79 {
80   theIndex.Clear();
81
82   SUIT_DataOwnerPtrList aList;
83   selected( aList );
84
85   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
86   {
87     const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( (*itr).operator->() );
88     if ( subOwner )
89       if ( subOwner->entry() == QString(IObject->getEntry()) )
90         theIndex.Add( subOwner->index() );
91   }
92   
93 }
94
95 /*
96   get map of indexes for the given entry of SALOME_InteractiveObject
97
98 */
99 void SalomeApp_SelectionMgr::GetIndexes( const QString& theEntry, TColStd_IndexedMapOfInteger& theIndex )
100 {
101   theIndex.Clear();
102
103   SUIT_DataOwnerPtrList aList;
104   selected( aList );
105
106   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
107   {
108     const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( (*itr).operator->() );
109     if ( subOwner )
110       if ( subOwner->entry() == theEntry )
111         theIndex.Add( subOwner->index() );
112   }
113
114 }
115
116 bool SalomeApp_SelectionMgr::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject, 
117                                                const TColStd_MapOfInteger& theIndexes, 
118                                                bool modeShift)
119 {
120   SUIT_DataOwnerPtrList remainsOwners;
121   
122   SUIT_DataOwnerPtrList aList;
123   selected( aList );
124
125   if ( !modeShift ) {
126     for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
127     {
128       const SalomeApp_DataOwner* owner = dynamic_cast<const SalomeApp_DataOwner*>( (*itr).operator->() );
129       if ( owner ) 
130       {
131         if ( owner->entry() != QString(IObject->getEntry()) ) 
132         {         
133           const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( owner );
134           if ( subOwner )
135             remainsOwners.append( new SalomeApp_DataSubOwner( subOwner->entry(), subOwner->index() ) );
136           else
137             remainsOwners.append( new SalomeApp_DataOwner( owner->entry() ) );
138         }
139       }
140     }
141   }
142   else
143     remainsOwners = aList;
144
145   TColStd_MapIteratorOfMapOfInteger It;
146   It.Initialize(theIndexes);
147   for(;It.More();It.Next())
148     remainsOwners.append( new SalomeApp_DataSubOwner( QString(IObject->getEntry()), It.Key() ) );
149   
150   bool append = false;
151   setSelected( remainsOwners, append );
152
153   emit currentSelectionChanged();
154
155   TColStd_IndexedMapOfInteger anIndexes;
156   GetIndexes( IObject, anIndexes );
157   return !anIndexes.IsEmpty();
158
159 }
160
161 /*
162   select 'subobjects' with given indexes
163
164 */
165 void SalomeApp_SelectionMgr::selectObjects( const Handle(SALOME_InteractiveObject)& IObject, 
166                                             TColStd_IndexedMapOfInteger theIndex, bool append )
167 {
168   SUIT_DataOwnerPtrList aList;
169
170   if ( theIndex.IsEmpty() )
171     aList.append( new SalomeApp_DataOwner( QString(IObject->getEntry()) ) );
172   else
173     {
174       int i;
175       for ( i = 1; i <= theIndex.Extent(); i++ )
176         aList.append( new SalomeApp_DataSubOwner( QString(IObject->getEntry()), theIndex( i ) ) );
177     }
178
179   setSelected( aList, append );
180
181 }
182
183 /*
184   select 'subobjects' with given indexes
185
186 */
187 void SalomeApp_SelectionMgr::selectObjects( MapIOOfMapOfInteger theMapIO, bool append )
188 {
189   SUIT_DataOwnerPtrList aList;
190
191   MapIOOfMapOfInteger::Iterator it;
192   for ( it = theMapIO.begin(); it != theMapIO.end(); ++it ) 
193     {
194       if ( it.data().IsEmpty() )
195         aList.append( new SalomeApp_DataOwner( QString(it.key()->getEntry()) ) );
196       else
197         {
198           int i;
199           for ( i = 1; i <= it.data().Extent(); i++ )
200             aList.append( new SalomeApp_DataSubOwner( QString(it.key()->getEntry()), it.data()( i ) ) );
201         }
202     }
203   
204   setSelected( aList, append );
205
206 }
207
208 /*
209   get map of selected subowners : object's entry <-> map of indexes
210
211 */
212 void SalomeApp_SelectionMgr::selectedSubOwners( MapEntryOfMapOfInteger& theMap )
213 {
214   theMap.clear();
215
216   TColStd_IndexedMapOfInteger anIndexes;
217
218   SUIT_DataOwnerPtrList aList;
219   selected( aList );
220
221   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
222   {
223     const SalomeApp_DataSubOwner* subOwner = dynamic_cast<const SalomeApp_DataSubOwner*>( (*itr).operator->() );
224     if ( subOwner ) 
225     {
226       if ( !theMap.contains( subOwner->entry() ) )
227       {
228         anIndexes.Clear();
229         GetIndexes( subOwner->entry(), anIndexes );
230         theMap.insert( subOwner->entry(), anIndexes );
231       }
232     }
233   }
234 }