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