Salome HOME
Update from BR_V5_DEV 13Feb2009
[modules/gui.git] / src / SUIT / SUIT_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 "SUIT_SelectionMgr.h"
23
24 #include "SUIT_Selector.h"
25 #include "SUIT_SelectionFilter.h"
26
27 /*!\class SUIT_SelectionMgr
28  * Provide selection manager. Manipulate by selection filters, modes, data owners.
29  */
30
31 /*!constructor. initialize myIterations and myIsSelChangeEnabled.*/
32 SUIT_SelectionMgr::SUIT_SelectionMgr( const bool Feedback, QObject* p )
33 : QObject( p ),
34 myIterations( Feedback ? 1 : 0 ),
35 myAutoDelFilter( false ),
36 myIsSelChangeEnabled( true )
37 {
38 }
39
40 /*!destructor. mySelectors auto delete.*/
41 SUIT_SelectionMgr::~SUIT_SelectionMgr()
42 {
43   for ( SelectorList::iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
44     delete *it;
45 }
46
47 /*!Add selector \a sel to selectors list,if it's not exists in list.*/
48 void SUIT_SelectionMgr::installSelector( SUIT_Selector* sel )
49 {
50   if ( sel && !mySelectors.contains( sel ) )
51     mySelectors.append( sel );
52 }
53
54 /*!Remove selector \a sel from list.*/
55 void SUIT_SelectionMgr::removeSelector( SUIT_Selector* sel )
56 {
57   mySelectors.removeAll( sel );
58 }
59
60 /*!Gets selectors list to \a lst.*/
61 void SUIT_SelectionMgr::selectors( QList<SUIT_Selector*>& lst ) const
62 {
63   lst.clear();
64   for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
65     lst.append( *it );
66 }
67
68 /*!Gets selectors list to \a lst with type \a typ.*/
69 void SUIT_SelectionMgr::selectors( const QString& typ, QList<SUIT_Selector*>& lst ) const
70 {
71   lst.clear();
72   for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
73   {
74     if ( (*it)->type() == typ )
75       lst.append( *it );
76   }
77 }
78
79 /*! Sets ebabled to \a on for all data owners with type \a typ.
80 */
81 void SUIT_SelectionMgr::setEnabled( const bool on, const QString& typ )
82 {
83   for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
84   {
85     if ( typ.isEmpty() || (*it)->type() == typ )
86       (*it)->setEnabled( on );
87   }
88 }
89
90 /*! Gets selected data owners from list with type \a type to list \a lst.
91 */
92 void SUIT_SelectionMgr::selected( SUIT_DataOwnerPtrList& lst, const QString& type ) const
93 {
94   lst.clear();
95
96   for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
97   {
98     if ( !type.isEmpty() && (*it)->type() != type )
99       continue;
100
101     SUIT_DataOwnerPtrList curList;
102     (*it)->selected( curList );
103     for ( SUIT_DataOwnerPtrList::const_iterator itr = curList.begin(); itr != curList.end(); ++itr )
104       lst.append( *itr );
105   }
106 }
107
108 /*! Sets selected data owners from \a lst and append to list, if \a append - true.
109 */
110 void SUIT_SelectionMgr::setSelected( const SUIT_DataOwnerPtrList& lst, const bool append )
111 {
112   SUIT_DataOwnerPtrList owners;
113   filterOwners( lst, owners );
114
115   for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
116   {
117     if ( append )
118     {
119       SUIT_DataOwnerPtrList current;
120       (*it)->selected( current );
121       for ( SUIT_DataOwnerPtrList::const_iterator it = current.begin(); it != current.end(); ++it )
122         owners.append( *it );
123     }
124     (*it)->setSelected( owners );
125   }
126 }
127
128 /*! Clear selected data owners.
129 */
130 void SUIT_SelectionMgr::clearSelected()
131 {
132   setSelected( SUIT_DataOwnerPtrList() );
133 }
134
135 /*! On selection \a sel changed.
136 */
137 void SUIT_SelectionMgr::selectionChanged( SUIT_Selector* sel )
138 {
139   if ( !sel || !myIsSelChangeEnabled || !sel->isEnabled() )
140     return;
141
142   SUIT_DataOwnerPtrList owners;
143
144   myIsSelChangeEnabled = false;
145   sel->selected( owners );
146
147   SUIT_DataOwnerPtrList newOwners;
148   filterOwners( owners, newOwners );
149
150   for ( int i = 0; i < myIterations; i++ )
151   {
152     for ( SelectorList::iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
153     {
154       // Temporary action(to avoid selection of the objects which don't pass the filters):
155       //if ( *it != sel )
156         (*it)->setSelected( newOwners );
157     }
158   }
159   myIsSelChangeEnabled = true;
160
161   emit selectionChanged();
162 }
163
164 /*!
165   Returns true if selection manger is in synchronising mode
166   (during synchonisation of the selectors selection).
167 */
168 bool SUIT_SelectionMgr::isSynchronizing() const
169 {
170   return !myIsSelChangeEnabled;
171 }
172
173 /*! Checks: Is selection manager has selection mode \a mode?
174 */
175 bool SUIT_SelectionMgr::hasSelectionMode( const int mode ) const
176 {
177   return mySelModes.contains( mode );
178 }
179
180 /*! Gets selection modes to list \a vals.
181 */
182 void SUIT_SelectionMgr::selectionModes( QList<int>& vals ) const
183 {
184   vals = mySelModes;
185 }
186
187 /*! Set selection mode \a mode to list of selection modes.
188 */
189 void SUIT_SelectionMgr::setSelectionModes( const int mode )
190 {
191   QList<int> lst;
192   lst.append( mode );
193   setSelectionModes( lst );
194 }
195
196 /*! Sets selection modes list from \a lst.
197 */
198 void SUIT_SelectionMgr::setSelectionModes( const QList<int>& lst )
199 {
200   mySelModes = lst;
201 }
202
203 /*! Append selection mode \a mode to list of selection modes.
204 */
205 void SUIT_SelectionMgr::appendSelectionModes( const int mode )
206 {
207   QList<int> lst;
208   lst.append( mode );
209   appendSelectionModes( lst );
210 }
211
212 /*! Append selection modes \a lst list.
213 */
214 void SUIT_SelectionMgr::appendSelectionModes( const QList<int>& lst )
215 {
216   QMap<int, int> map;
217   for ( QList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
218     map.insert( *it, 0 );
219
220   for ( QList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
221   {
222     if ( !map.contains( *itr ) )
223       mySelModes.append( *itr );
224   }
225 }
226
227 /*! Remove selection mode \a mode from list.
228 */
229 void SUIT_SelectionMgr::removeSelectionModes( const int mode )
230 {
231   QList<int> lst;
232   lst.append( mode );
233   removeSelectionModes( lst );
234 }
235
236 /*! Remove selection modea \a lst from list.
237 */
238 void SUIT_SelectionMgr::removeSelectionModes( const QList<int>& lst )
239 {
240   QMap<int, int> map;
241   for ( QList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
242     map.insert( *it, 0 );
243
244   for ( QList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
245     map.remove( *itr );
246
247   mySelModes.clear();
248   for ( QMap<int, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
249     mySelModes.append( iter.key() );
250 }
251
252 /*! Checks data owner is ok?
253 */
254 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwner* owner ) const
255 {
256   if ( !owner )
257     return false;
258
259   bool ok = true;
260   for ( SelFilterList::const_iterator it = myFilters.begin(); it != myFilters.end() && ok; ++it )
261     ok = (*it)->isOk( owner );
262
263   return ok;
264 }
265
266 /*! Checks data owner pointer is ok?
267 */
268 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwnerPtr& ptr ) const
269 {
270   if ( ptr.isNull() )
271     return false;
272
273   return isOk( ptr.operator->() );
274 }
275
276 /*! Checks selection manager has filter \a f?
277 */
278 bool SUIT_SelectionMgr::hasFilter( SUIT_SelectionFilter* f ) const
279 {
280   return myFilters.contains( f );
281 }
282
283 /*! Install filter \a f and set selected, if \a update = true.
284 */
285 void SUIT_SelectionMgr::installFilter( SUIT_SelectionFilter* f, const bool updateSelection )
286 {
287   if ( !hasFilter( f ) )
288   {
289     SUIT_DataOwnerPtrList selOwners;
290     if( updateSelection )
291       selected( selOwners );
292       
293     myFilters.append( f );
294     
295     if( updateSelection )
296       setSelected( selOwners );
297   }
298 }
299
300 /*! Remove filter \a f from filters list.
301 */
302 void SUIT_SelectionMgr::removeFilter( SUIT_SelectionFilter* f )
303 {
304   if ( !myFilters.contains( f ) )
305     return;
306
307   myFilters.removeAll( f );
308
309   if ( autoDeleteFilter() )
310     delete f;
311 }
312
313 /*! Clear filters list.
314 */
315 void SUIT_SelectionMgr::clearFilters()
316 {
317   if ( autoDeleteFilter() )
318   {
319     for ( SelFilterList::const_iterator it = myFilters.begin(); it != myFilters.end(); ++it )
320       delete *it;
321   }
322
323   myFilters.clear();
324 }
325
326 /*! Sets auto delete filter.
327 */
328 bool SUIT_SelectionMgr::autoDeleteFilter() const
329 {
330   return myAutoDelFilter;
331 }
332
333 /*! Sets auto delete filter to \a on.
334 */
335 void SUIT_SelectionMgr::setAutoDeleteFilter( const bool on )
336 {
337   myAutoDelFilter = on;
338 }
339
340 /*! Gets good data owners list to \a out from \a in.
341 */
342 void SUIT_SelectionMgr::filterOwners( const SUIT_DataOwnerPtrList& in, SUIT_DataOwnerPtrList& out ) const
343 {
344   out.clear();
345   for ( SUIT_DataOwnerPtrList::const_iterator it = in.begin(); it != in.end(); ++it )
346   {
347     if ( isOk( *it ) )
348       out.append( *it );
349   }
350 }