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