Salome HOME
Updated copyright comment
[modules/gui.git] / src / SUIT / SUIT_SelectionMgr.cxx
1 // Copyright (C) 2007-2024  CEA, EDF, 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, or (at your option) any later version.
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 bool onlyOne ) 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( /*skipAllEqual=*/false );
107     (*it)->selected( curList );
108     if ( onlyOne )
109     {
110       for ( SUIT_DataOwnerPtrList::const_iterator itr = curList.begin(); itr != curList.end(); ++itr )
111       {
112         lst.append( *itr );
113         if ( lst.count() > 1 )
114         {
115           lst.clear();
116           return;
117         }
118       }
119     }
120     else
121     {
122       for ( SUIT_DataOwnerPtrList::const_iterator itr = curList.begin(); itr != curList.end(); ++itr )
123         lst.append( *itr );
124     }
125   }
126 }
127
128 /*! Sets selected data owners from \a lst and append to list, if \a append - true.
129 */
130 void SUIT_SelectionMgr::setSelected( const SUIT_DataOwnerPtrList& lst, const bool append )
131 {
132   SUIT_DataOwnerPtrList owners;
133   filterOwners( lst, owners );
134
135   for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
136   {
137     if ( append )
138     {
139       SUIT_DataOwnerPtrList current;
140       (*it)->selected( current );
141       for ( SUIT_DataOwnerPtrList::const_iterator it = current.begin(); it != current.end(); ++it )
142         owners.append( *it );
143     }
144     (*it)->setSelected( owners );
145   }
146 }
147
148 /*! Clear selected data owners.
149 */
150 void SUIT_SelectionMgr::clearSelected()
151 {
152   setSelected( SUIT_DataOwnerPtrList() );
153 }
154
155 /*! On selection \a sel changed.
156 */
157 void SUIT_SelectionMgr::selectionChanged( SUIT_Selector* sel )
158 {
159   if ( !sel || !myIsSelChangeEnabled || !sel->isEnabled() )
160     return;
161
162   SUIT_DataOwnerPtrList owners;
163
164   myIsSelChangeEnabled = false;
165   sel->selected( owners );
166
167   SUIT_DataOwnerPtrList newOwners;
168   filterOwners( owners, newOwners );
169
170   for ( int i = 0; i < myIterations; i++ )
171   {
172     for ( SelectorList::iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
173     {
174       // Temporary action(to avoid selection of the objects which don't pass the filters):
175       //if ( *it != sel )
176         (*it)->setSelected( newOwners );
177     }
178   }
179   myIsSelChangeEnabled = true;
180
181   emit selectionChanged();
182 }
183
184 /*!
185   Returns true if selection manger is in synchronising mode
186   (during synchonisation of the selectors selection).
187 */
188 bool SUIT_SelectionMgr::isSynchronizing() const
189 {
190   return !myIsSelChangeEnabled;
191 }
192
193 /*! Checks: Is selection manager has selection mode \a mode?
194 */
195 bool SUIT_SelectionMgr::hasSelectionMode( const int mode ) const
196 {
197   return mySelModes.contains( mode );
198 }
199
200 /*! Gets selection modes to list \a vals.
201 */
202 void SUIT_SelectionMgr::selectionModes( QList<int>& vals ) const
203 {
204   vals = mySelModes;
205 }
206
207 /*! Set selection mode \a mode to list of selection modes.
208 */
209 void SUIT_SelectionMgr::setSelectionModes( const int mode )
210 {
211   QList<int> lst;
212   lst.append( mode );
213   setSelectionModes( lst );
214 }
215
216 /*! Sets selection modes list from \a lst.
217 */
218 void SUIT_SelectionMgr::setSelectionModes( const QList<int>& lst )
219 {
220   mySelModes = lst;
221 }
222
223 /*! Append selection mode \a mode to list of selection modes.
224 */
225 void SUIT_SelectionMgr::appendSelectionModes( const int mode )
226 {
227   QList<int> lst;
228   lst.append( mode );
229   appendSelectionModes( lst );
230 }
231
232 /*! Append selection modes \a lst list.
233 */
234 void SUIT_SelectionMgr::appendSelectionModes( const QList<int>& lst )
235 {
236   QMap<int, int> map;
237   for ( QList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
238     map.insert( *it, 0 );
239
240   for ( QList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
241   {
242     if ( !map.contains( *itr ) )
243       mySelModes.append( *itr );
244   }
245 }
246
247 /*! Remove selection mode \a mode from list.
248 */
249 void SUIT_SelectionMgr::removeSelectionModes( const int mode )
250 {
251   QList<int> lst;
252   lst.append( mode );
253   removeSelectionModes( lst );
254 }
255
256 /*! Remove selection modea \a lst from list.
257 */
258 void SUIT_SelectionMgr::removeSelectionModes( const QList<int>& lst )
259 {
260   QMap<int, int> map;
261   for ( QList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
262     map.insert( *it, 0 );
263
264   for ( QList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
265     map.remove( *itr );
266
267   mySelModes.clear();
268   for ( QMap<int, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
269     mySelModes.append( iter.key() );
270 }
271
272 /*! Checks data owner is ok?
273 */
274 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwner* owner ) const
275 {
276   if ( !owner )
277     return false;
278
279   bool ok = true;
280   for ( SelFilterList::const_iterator it = myFilters.begin(); it != myFilters.end() && ok; ++it )
281     ok = (*it)->isOk( owner );
282
283   return ok;
284 }
285
286 /*! Checks data owner pointer is ok?
287 */
288 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwnerPtr& ptr ) const
289 {
290   if ( ptr.isNull() )
291     return false;
292
293   return isOk( ptr.operator->() );
294 }
295
296 /*! Checks selection manager has filter \a f?
297 */
298 bool SUIT_SelectionMgr::hasFilter( SUIT_SelectionFilter* f ) const
299 {
300   return myFilters.contains( f );
301 }
302
303 /*! Install filter \a f and set selected, if \a update = true.
304 */
305 void SUIT_SelectionMgr::installFilter( SUIT_SelectionFilter* f, const bool updateSelection )
306 {
307   if ( !hasFilter( f ) )
308   {
309     SUIT_DataOwnerPtrList selOwners;
310     if( updateSelection )
311       selected( selOwners );
312       
313     myFilters.append( f );
314     
315     if( updateSelection )
316       setSelected( selOwners );
317   }
318 }
319
320 /*! Remove filter \a f from filters list.
321 */
322 void SUIT_SelectionMgr::removeFilter( SUIT_SelectionFilter* f )
323 {
324   if ( !myFilters.contains( f ) )
325     return;
326
327   myFilters.removeAll( f );
328
329   if ( autoDeleteFilter() )
330     delete f;
331 }
332
333 /*! Clear filters list.
334 */
335 void SUIT_SelectionMgr::clearFilters()
336 {
337   if ( autoDeleteFilter() )
338   {
339     for ( SelFilterList::const_iterator it = myFilters.begin(); it != myFilters.end(); ++it )
340       delete *it;
341   }
342
343   myFilters.clear();
344 }
345
346 /*! Sets auto delete filter.
347 */
348 bool SUIT_SelectionMgr::autoDeleteFilter() const
349 {
350   return myAutoDelFilter;
351 }
352
353 /*! Sets auto delete filter to \a on.
354 */
355 void SUIT_SelectionMgr::setAutoDeleteFilter( const bool on )
356 {
357   myAutoDelFilter = on;
358 }
359
360 /*! Gets good data owners list to \a out from \a in.
361 */
362 void SUIT_SelectionMgr::filterOwners( const SUIT_DataOwnerPtrList& in, SUIT_DataOwnerPtrList& out ) const
363 {
364   out.clear();
365   for ( SUIT_DataOwnerPtrList::const_iterator it = in.begin(); it != in.end(); ++it )
366   {
367     if ( isOk( *it ) )
368       out.append( *it );
369   }
370 }