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