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