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