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