Salome HOME
Join modifications from branch OCC_development_for_3_2_0a2
[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/
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;
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     if ( convertReferences ) {
89       QString refEntry = study->referencedToEntry( entry );
90       if( !entryMap.contains( entry ) ) {
91         if ( refEntry != entry ) {
92           QString component = study->componentDataType( refEntry );
93           theList.Append( new SALOME_InteractiveObject( refEntry, component, ""/*refobj->Name().c_str()*/ ) );
94         }
95         else
96           theList.Append( owner->IO() );
97       }
98     }
99     else {
100       if( !entryMap.contains( entry ) )
101         theList.Append( owner->IO() );
102     }
103
104     entryMap.insert(owner->entry(), 1);
105   }
106 }
107
108 /*!
109   Append selected objects.
110 */
111 void LightApp_SelectionMgr::setSelectedObjects( const SALOME_ListIO& lst, const bool append )
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 LightApp_DataOwner( it.Value() ) );
118   }
119
120   setSelected( owners, append );
121 }
122
123 #else
124 /*!
125   Get all selected objects from selection manager
126 */
127 void LightApp_SelectionMgr::selectedObjects( QStringList& theList, const QString& theType,
128                                              const bool convertReferences ) const
129 {
130   theList.clear();
131
132   SUIT_DataOwnerPtrList aList;
133   selected( aList, theType );
134
135   QString entry;
136   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
137   {
138     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
139     if( !owner )
140       continue;
141
142     LightApp_Study* study = dynamic_cast<LightApp_Study*>( application()->activeStudy() );
143     if ( !study )
144       return;
145
146     entry = owner->entry();
147     if( !theList.contains( entry ) )
148       theList.append( entry );
149   }
150 }
151
152 #endif
153
154 /*!
155   Emit current selection changed.
156 */
157 void LightApp_SelectionMgr::selectionChanged( SUIT_Selector* theSel )
158 {
159   SUIT_SelectionMgr::selectionChanged( theSel );
160
161   emit currentSelectionChanged();
162 }
163
164 #ifndef DISABLE_SALOMEOBJECT
165
166 /*!
167   get map of indexes for the given SALOME_InteractiveObject
168 */
169 void LightApp_SelectionMgr::GetIndexes( const Handle(SALOME_InteractiveObject)& IObject, 
170                                         TColStd_IndexedMapOfInteger& theIndex)
171 {
172   theIndex.Clear();
173
174   SUIT_DataOwnerPtrList aList;
175   selected( aList );
176
177   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
178   {
179     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
180     if ( subOwner )
181       if ( subOwner->entry() == QString(IObject->getEntry()) )
182         theIndex.Add( subOwner->index() );
183   }
184   
185 }
186
187 /*!
188   get map of indexes for the given entry of SALOME_InteractiveObject
189 */
190 void LightApp_SelectionMgr::GetIndexes( const QString& theEntry, TColStd_IndexedMapOfInteger& theIndex )
191 {
192   theIndex.Clear();
193
194   SUIT_DataOwnerPtrList aList;
195   selected( aList );
196
197   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
198   {
199     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
200     if ( subOwner )
201       if ( subOwner->entry() == theEntry )
202         theIndex.Add( subOwner->index() );
203   }
204
205 }
206
207 /*!
208   Add or remove interactive objects from selection manager.
209 */
210 bool LightApp_SelectionMgr::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject, 
211                                                const TColStd_MapOfInteger& theIndexes, 
212                                                bool modeShift)
213 {
214   SUIT_DataOwnerPtrList remainsOwners;
215   
216   SUIT_DataOwnerPtrList aList;
217   selected( aList );
218
219   if ( !modeShift ) {
220     for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
221     {
222       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
223       if ( owner ) 
224       {
225         if ( owner->entry() != QString(IObject->getEntry()) ) 
226         {         
227           const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( owner );
228           if ( subOwner )
229             remainsOwners.append( new LightApp_DataSubOwner( subOwner->entry(), subOwner->index() ) );
230           else
231             remainsOwners.append( new LightApp_DataOwner( owner->entry() ) );
232         }
233       }
234     }
235   }
236   else
237     remainsOwners = aList;
238
239   TColStd_MapIteratorOfMapOfInteger It;
240   It.Initialize(theIndexes);
241   for(;It.More();It.Next())
242     remainsOwners.append( new LightApp_DataSubOwner( QString(IObject->getEntry()), It.Key() ) );
243   
244   bool append = false;
245   setSelected( remainsOwners, append );
246
247   emit currentSelectionChanged();
248
249   TColStd_IndexedMapOfInteger anIndexes;
250   GetIndexes( IObject, anIndexes );
251   return !anIndexes.IsEmpty();
252
253 }
254
255 /*!
256   select 'subobjects' with given indexes
257 */
258 void LightApp_SelectionMgr::selectObjects( const Handle(SALOME_InteractiveObject)& IObject, 
259                                             TColStd_IndexedMapOfInteger theIndex, bool append )
260 {
261   SUIT_DataOwnerPtrList aList;
262
263   if ( theIndex.IsEmpty() )
264     aList.append( new LightApp_DataOwner( QString(IObject->getEntry()) ) );
265   else
266     {
267       int i;
268       for ( i = 1; i <= theIndex.Extent(); i++ )
269         aList.append( new LightApp_DataSubOwner( QString(IObject->getEntry()), theIndex( i ) ) );
270     }
271
272   setSelected( aList, append );
273
274 }
275
276 /*!
277   select 'subobjects' with given indexes
278 */
279 void LightApp_SelectionMgr::selectObjects( MapIOOfMapOfInteger theMapIO, bool append )
280 {
281   SUIT_DataOwnerPtrList aList;
282
283   MapIOOfMapOfInteger::Iterator it;
284   for ( it = theMapIO.begin(); it != theMapIO.end(); ++it ) 
285     {
286       if ( it.data().IsEmpty() )
287         aList.append( new LightApp_DataOwner( QString(it.key()->getEntry()) ) );
288       else
289         {
290           int i;
291           for ( i = 1; i <= it.data().Extent(); i++ )
292             aList.append( new LightApp_DataSubOwner( QString(it.key()->getEntry()), it.data()( i ) ) );
293         }
294     }
295   
296   setSelected( aList, append );
297
298 }
299
300 /*!
301   get map of selected subowners : object's entry <-> map of indexes
302 */
303 void LightApp_SelectionMgr::selectedSubOwners( MapEntryOfMapOfInteger& theMap )
304 {
305   theMap.clear();
306
307   TColStd_IndexedMapOfInteger anIndexes;
308
309   SUIT_DataOwnerPtrList aList;
310   selected( aList );
311
312   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
313   {
314     const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
315     if ( subOwner ) 
316     {
317       if ( !theMap.contains( subOwner->entry() ) )
318       {
319         anIndexes.Clear();
320         GetIndexes( subOwner->entry(), anIndexes );
321         theMap.insert( subOwner->entry(), anIndexes );
322       }
323     }
324   }
325 }
326
327 #endif