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