Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / LightApp / LightApp_SelectionMgr.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "LightApp_SelectionMgr.h"
20
21 #include "LightApp_Study.h"
22 #include "LightApp_DataOwner.h"
23 #include "LightApp_DataSubOwner.h"
24 #include "LightApp_Application.h"
25
26 #include <SUIT_Session.h>
27
28 #ifndef DISABLE_SALOMEOBJECT
29   #include <SALOME_ListIO.hxx>
30   #include <SALOME_ListIteratorOfListIO.hxx>
31
32   // Open CASCADE Include
33   #include <TColStd_MapOfInteger.hxx>
34   #include <TColStd_MapIteratorOfMapOfInteger.hxx>
35   #include <TColStd_IndexedMapOfInteger.hxx>
36 #endif
37
38 /*!
39   Constructor.
40 */
41 LightApp_SelectionMgr::LightApp_SelectionMgr( LightApp_Application* app, const bool fb )
42 : SUIT_SelectionMgr( fb ),
43 myApp( app )
44 {
45 }
46
47 /*!
48   Destructor.
49 */
50 LightApp_SelectionMgr::~LightApp_SelectionMgr()
51 {
52 }
53
54 /*!
55   Gets application.
56 */
57 LightApp_Application* LightApp_SelectionMgr::application() const
58 {
59   return myApp;
60 }
61
62 #ifndef DISABLE_SALOMEOBJECT
63 /*!
64   Get all selected objects from selection manager
65 */
66 void LightApp_SelectionMgr::selectedObjects( SALOME_ListIO& theList, const QString& theType,
67                                              const bool convertReferences ) const
68 {
69   theList.Clear();
70
71   SUIT_DataOwnerPtrList aList;
72   selected( aList, theType );
73
74   QMap<QString,int> entryMap;
75
76   QString entry, checkEntry;
77   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
78   {
79     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
80     if( !owner )
81       continue;
82
83     LightApp_Study* study = dynamic_cast<LightApp_Study*>( application()->activeStudy() );
84     if ( !study )
85       return;
86
87     entry = owner->entry();
88     // Entry to check object uniqueness.
89     // It is selected owner entry in the case, when we do not convert references,
90     // and entry of a real object, when we convert references.
91     checkEntry = entry;
92     if ( convertReferences ) {
93       QString refEntry = study->referencedToEntry( entry );
94       checkEntry = refEntry;
95       if ( !entryMap.contains( checkEntry ) ) {
96         if ( refEntry != entry ) {
97           QString component = study->componentDataType( refEntry );
98           theList.Append( new SALOME_InteractiveObject( refEntry, component, ""/*refobj->Name().c_str()*/ ) );
99         }
100         else if( !owner->IO().IsNull() )
101           theList.Append( owner->IO() );
102       }
103     }
104     else {
105       if( !entryMap.contains( entry ) && !owner->IO().IsNull() )
106         theList.Append( owner->IO() );
107     }
108
109     entryMap.insert(checkEntry, 1);
110   }
111 }
112
113 /*!
114   Append selected objects.
115 */
116 void LightApp_SelectionMgr::setSelectedObjects( const SALOME_ListIO& lst, const bool append )
117 {
118   SUIT_DataOwnerPtrList owners;
119   for ( SALOME_ListIteratorOfListIO it( lst ); it.More(); it.Next() )
120   {
121     if ( it.Value()->hasEntry() )
122       owners.append( new LightApp_DataOwner( it.Value() ) );
123   }
124
125   setSelected( owners, append );
126 }
127
128 #else
129 /*!
130   Get all selected objects from selection manager
131 */
132 void LightApp_SelectionMgr::selectedObjects( QStringList& theList, const QString& theType,
133                                              const bool convertReferences ) const
134 {
135   theList.clear();
136
137   SUIT_DataOwnerPtrList aList;
138   selected( aList, theType );
139
140   QString entry;
141   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
142   {
143     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
144     if( !owner )
145       continue;
146
147     LightApp_Study* study = dynamic_cast<LightApp_Study*>( application()->activeStudy() );
148     if ( !study )
149       return;
150
151     entry = owner->entry();
152     if( !theList.contains( entry ) )
153       theList.append( entry );
154   }
155 }
156
157 #endif
158
159 /*!
160   Emit current selection changed.
161 */
162 void LightApp_SelectionMgr::selectionChanged( SUIT_Selector* theSel )
163 {
164   SUIT_SelectionMgr::selectionChanged( theSel );
165
166   emit currentSelectionChanged();
167 }
168
169 #ifndef DISABLE_SALOMEOBJECT
170
171 /*!
172   get map of indexes for the given SALOME_InteractiveObject
173 */
174 void LightApp_SelectionMgr::GetIndexes( const Handle(SALOME_InteractiveObject)& IObject, 
175                                         TColStd_IndexedMapOfInteger& theIndex)
176 {
177   theIndex.Clear();
178
179   SUIT_DataOwnerPtrList aList;
180   selected( aList );
181
182   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
183   {
184     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
185     if ( subOwner )
186       if ( subOwner->entry() == QString(IObject->getEntry()) )
187         theIndex.Add( subOwner->index() );
188   }
189   
190 }
191
192 /*!
193   get map of indexes for the given entry of SALOME_InteractiveObject
194 */
195 void LightApp_SelectionMgr::GetIndexes( const QString& theEntry, TColStd_IndexedMapOfInteger& theIndex )
196 {
197   theIndex.Clear();
198
199   SUIT_DataOwnerPtrList aList;
200   selected( aList );
201
202   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
203   {
204     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
205     if ( subOwner )
206       if ( subOwner->entry() == theEntry )
207         theIndex.Add( subOwner->index() );
208   }
209
210 }
211
212 /*!
213   Add or remove interactive objects from selection manager.
214 */
215 //bool LightApp_SelectionMgr::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject,
216 void LightApp_SelectionMgr::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject,
217                                               const TColStd_MapOfInteger& theIndexes,
218                                               bool modeShift)
219 {
220   SUIT_DataOwnerPtrList remainsOwners;
221
222   SUIT_DataOwnerPtrList aList;
223   selected( aList );
224
225   QString ioEntry (IObject->getEntry());
226
227   if ( !modeShift ) {
228     for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
229     {
230       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
231       if ( owner )
232       {
233         if ( owner->entry() != ioEntry )
234         {
235           const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( owner );
236           if ( subOwner )
237             remainsOwners.append( new LightApp_DataSubOwner( subOwner->entry(), subOwner->index() ) );
238           else
239             remainsOwners.append( new LightApp_DataOwner( owner->entry() ) );
240         }
241       }
242     }
243   }
244   else
245     remainsOwners = aList;
246
247   TColStd_MapIteratorOfMapOfInteger It;
248   It.Initialize(theIndexes);
249   for(;It.More();It.Next())
250     remainsOwners.append( new LightApp_DataSubOwner( ioEntry, It.Key() ) );
251
252   bool append = false;
253   setSelected( remainsOwners, append );
254
255   emit currentSelectionChanged();
256
257   // Bug 17269: To avoid calling of selected(aList)
258   //TColStd_IndexedMapOfInteger anIndexes;
259   //GetIndexes( IObject, anIndexes );
260   //return !anIndexes.IsEmpty();
261 }
262
263 /*!
264   select 'subobjects' with given indexes
265 */
266 void LightApp_SelectionMgr::selectObjects( const Handle(SALOME_InteractiveObject)& IObject, 
267                                             TColStd_IndexedMapOfInteger theIndex, bool append )
268 {
269   SUIT_DataOwnerPtrList aList;
270
271   if ( theIndex.IsEmpty() )
272     aList.append( new LightApp_DataOwner( QString(IObject->getEntry()) ) );
273   else
274     {
275       int i;
276       for ( i = 1; i <= theIndex.Extent(); i++ )
277         aList.append( new LightApp_DataSubOwner( QString(IObject->getEntry()), theIndex( i ) ) );
278     }
279
280   setSelected( aList, append );
281
282 }
283
284 /*!
285   select 'subobjects' with given indexes
286 */
287 void LightApp_SelectionMgr::selectObjects( MapIOOfMapOfInteger theMapIO, bool append )
288 {
289   SUIT_DataOwnerPtrList aList;
290
291   MapIOOfMapOfInteger::Iterator it;
292   for ( it = theMapIO.begin(); it != theMapIO.end(); ++it ) 
293     {
294       if ( it.data().IsEmpty() )
295         aList.append( new LightApp_DataOwner( QString(it.key()->getEntry()) ) );
296       else
297         {
298           int i;
299           for ( i = 1; i <= it.data().Extent(); i++ )
300             aList.append( new LightApp_DataSubOwner( QString(it.key()->getEntry()), it.data()( i ) ) );
301         }
302     }
303   
304   setSelected( aList, append );
305
306 }
307
308 /*!
309   get map of selected subowners : object's entry <-> map of indexes
310 */
311 void LightApp_SelectionMgr::selectedSubOwners( MapEntryOfMapOfInteger& theMap )
312 {
313   theMap.clear();
314
315   TColStd_IndexedMapOfInteger anIndexes;
316
317   SUIT_DataOwnerPtrList aList;
318   selected( aList );
319
320   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
321   {
322     const LightApp_DataSubOwner* subOwner =
323       dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
324     if ( subOwner ) 
325     {
326       if ( !theMap.contains( subOwner->entry() ) )
327       {
328         anIndexes.Clear();
329         //Bug 17269: GetIndexes( subOwner->entry(), anIndexes );
330         //Bug 17269: To avoid multiple calling of selected(aList)
331         for ( SUIT_DataOwnerPtrList::const_iterator itr2 = itr; itr2 != aList.end(); ++itr2 )
332         {
333           const LightApp_DataSubOwner* subOwner2 =
334             dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
335           if ( subOwner2 )
336             if ( subOwner2->entry() == subOwner->entry() )
337               anIndexes.Add( subOwner2->index() );
338         }
339         //
340         theMap.insert( subOwner->entry(), anIndexes );
341       }
342     }
343   }
344 }
345
346 #endif