1 // Copyright (C) 2007-2012 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
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.
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.
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
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "SUIT_SelectionMgr.h"
25 #include "SUIT_Selector.h"
26 #include "SUIT_SelectionFilter.h"
28 /*!\class SUIT_SelectionMgr
29 * Provide selection manager. Manipulate by selection filters, modes, data owners.
32 /*!constructor. initialize myIterations and myIsSelChangeEnabled.*/
33 SUIT_SelectionMgr::SUIT_SelectionMgr( const bool Feedback, QObject* p )
35 myIterations( Feedback ? 1 : 0 ),
36 myAutoDelFilter( false ),
37 myIsSelChangeEnabled( true )
41 /*!destructor. mySelectors auto delete.*/
42 SUIT_SelectionMgr::~SUIT_SelectionMgr()
44 for ( SelectorList::iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
48 /*!Add selector \a sel to selectors list,if it's not exists in list.*/
49 void SUIT_SelectionMgr::installSelector( SUIT_Selector* sel )
51 if ( sel && !mySelectors.contains( sel ) )
52 mySelectors.append( sel );
55 /*!Remove selector \a sel from list.*/
56 void SUIT_SelectionMgr::removeSelector( SUIT_Selector* sel )
58 mySelectors.removeAll( sel );
61 /*!Gets selectors list to \a lst.*/
62 void SUIT_SelectionMgr::selectors( QList<SUIT_Selector*>& lst ) const
65 for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
69 /*!Gets selectors list to \a lst with type \a typ.*/
70 void SUIT_SelectionMgr::selectors( const QString& typ, QList<SUIT_Selector*>& lst ) const
73 for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
75 if ( (*it)->type() == typ )
80 /*! Sets ebabled to \a on for all data owners with type \a typ.
82 void SUIT_SelectionMgr::setEnabled( const bool on, const QString& typ )
84 for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
86 if ( typ.isEmpty() || (*it)->type() == typ )
87 (*it)->setEnabled( on );
91 /*! Gets selected data owners from list with type \a type to list \a lst.
93 void SUIT_SelectionMgr::selected( SUIT_DataOwnerPtrList& lst, const QString& type ) const
97 for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
99 if ( !(*it)->isEnabled() )
101 if ( !type.isEmpty() && (*it)->type() != type )
104 SUIT_DataOwnerPtrList curList;
105 (*it)->selected( curList );
106 for ( SUIT_DataOwnerPtrList::const_iterator itr = curList.begin(); itr != curList.end(); ++itr )
111 /*! Sets selected data owners from \a lst and append to list, if \a append - true.
113 void SUIT_SelectionMgr::setSelected( const SUIT_DataOwnerPtrList& lst, const bool append )
115 SUIT_DataOwnerPtrList owners;
116 filterOwners( lst, owners );
118 for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
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 );
127 (*it)->setSelected( owners );
131 /*! Clear selected data owners.
133 void SUIT_SelectionMgr::clearSelected()
135 setSelected( SUIT_DataOwnerPtrList() );
138 /*! On selection \a sel changed.
140 void SUIT_SelectionMgr::selectionChanged( SUIT_Selector* sel )
142 if ( !sel || !myIsSelChangeEnabled || !sel->isEnabled() )
145 SUIT_DataOwnerPtrList owners;
147 myIsSelChangeEnabled = false;
148 sel->selected( owners );
150 SUIT_DataOwnerPtrList newOwners;
151 filterOwners( owners, newOwners );
153 for ( int i = 0; i < myIterations; i++ )
155 for ( SelectorList::iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
157 // Temporary action(to avoid selection of the objects which don't pass the filters):
159 (*it)->setSelected( newOwners );
162 myIsSelChangeEnabled = true;
164 emit selectionChanged();
168 Returns true if selection manger is in synchronising mode
169 (during synchonisation of the selectors selection).
171 bool SUIT_SelectionMgr::isSynchronizing() const
173 return !myIsSelChangeEnabled;
176 /*! Checks: Is selection manager has selection mode \a mode?
178 bool SUIT_SelectionMgr::hasSelectionMode( const int mode ) const
180 return mySelModes.contains( mode );
183 /*! Gets selection modes to list \a vals.
185 void SUIT_SelectionMgr::selectionModes( QList<int>& vals ) const
190 /*! Set selection mode \a mode to list of selection modes.
192 void SUIT_SelectionMgr::setSelectionModes( const int mode )
196 setSelectionModes( lst );
199 /*! Sets selection modes list from \a lst.
201 void SUIT_SelectionMgr::setSelectionModes( const QList<int>& lst )
206 /*! Append selection mode \a mode to list of selection modes.
208 void SUIT_SelectionMgr::appendSelectionModes( const int mode )
212 appendSelectionModes( lst );
215 /*! Append selection modes \a lst list.
217 void SUIT_SelectionMgr::appendSelectionModes( const QList<int>& lst )
220 for ( QList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
221 map.insert( *it, 0 );
223 for ( QList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
225 if ( !map.contains( *itr ) )
226 mySelModes.append( *itr );
230 /*! Remove selection mode \a mode from list.
232 void SUIT_SelectionMgr::removeSelectionModes( const int mode )
236 removeSelectionModes( lst );
239 /*! Remove selection modea \a lst from list.
241 void SUIT_SelectionMgr::removeSelectionModes( const QList<int>& lst )
244 for ( QList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
245 map.insert( *it, 0 );
247 for ( QList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
251 for ( QMap<int, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
252 mySelModes.append( iter.key() );
255 /*! Checks data owner is ok?
257 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwner* owner ) const
263 for ( SelFilterList::const_iterator it = myFilters.begin(); it != myFilters.end() && ok; ++it )
264 ok = (*it)->isOk( owner );
269 /*! Checks data owner pointer is ok?
271 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwnerPtr& ptr ) const
276 return isOk( ptr.operator->() );
279 /*! Checks selection manager has filter \a f?
281 bool SUIT_SelectionMgr::hasFilter( SUIT_SelectionFilter* f ) const
283 return myFilters.contains( f );
286 /*! Install filter \a f and set selected, if \a update = true.
288 void SUIT_SelectionMgr::installFilter( SUIT_SelectionFilter* f, const bool updateSelection )
290 if ( !hasFilter( f ) )
292 SUIT_DataOwnerPtrList selOwners;
293 if( updateSelection )
294 selected( selOwners );
296 myFilters.append( f );
298 if( updateSelection )
299 setSelected( selOwners );
303 /*! Remove filter \a f from filters list.
305 void SUIT_SelectionMgr::removeFilter( SUIT_SelectionFilter* f )
307 if ( !myFilters.contains( f ) )
310 myFilters.removeAll( f );
312 if ( autoDeleteFilter() )
316 /*! Clear filters list.
318 void SUIT_SelectionMgr::clearFilters()
320 if ( autoDeleteFilter() )
322 for ( SelFilterList::const_iterator it = myFilters.begin(); it != myFilters.end(); ++it )
329 /*! Sets auto delete filter.
331 bool SUIT_SelectionMgr::autoDeleteFilter() const
333 return myAutoDelFilter;
336 /*! Sets auto delete filter to \a on.
338 void SUIT_SelectionMgr::setAutoDeleteFilter( const bool on )
340 myAutoDelFilter = on;
343 /*! Gets good data owners list to \a out from \a in.
345 void SUIT_SelectionMgr::filterOwners( const SUIT_DataOwnerPtrList& in, SUIT_DataOwnerPtrList& out ) const
348 for ( SUIT_DataOwnerPtrList::const_iterator it = in.begin(); it != in.end(); ++it )