1 // Copyright (C) 2005 OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either
6 // version 2.1 of the License.
8 // This library is distributed in the hope that it will be useful
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 // Lesser General Public License for more details.
13 // You should have received a copy of the GNU Lesser General Public
14 // License along with this library; if not, write to the Free Software
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 // See http://www.salome-platform.org/
19 #include "SUIT_SelectionMgr.h"
21 /*!\class SUIT_SelectionMgr
22 * Provide selection manager. Manipulate by selection filters, modes, data owners.
25 /*!constructor. initialize myIterations and myIsSelChangeEnabled.*/
26 SUIT_SelectionMgr::SUIT_SelectionMgr( const bool Feedback )
27 : myIterations( Feedback ? 1 : 0 ),
28 myIsSelChangeEnabled( true )
32 /*!destructor. mySelectors auto delete.*/
33 SUIT_SelectionMgr::~SUIT_SelectionMgr()
35 mySelectors.setAutoDelete( true );
38 /*!Add selector \a sel to selectors list,if it's not exists in list.*/
39 void SUIT_SelectionMgr::installSelector( SUIT_Selector* sel )
41 if ( sel && !mySelectors.contains( sel ) )
42 mySelectors.append( sel );
45 /*!Remove selector \a sel from list.*/
46 void SUIT_SelectionMgr::removeSelector( SUIT_Selector* sel )
48 mySelectors.remove( sel );
51 /*!Gets selectors list to \a lst.*/
52 void SUIT_SelectionMgr::selectors( QPtrList<SUIT_Selector>& lst ) const
55 for ( SelectorListIterator it( mySelectors ); it.current(); ++it )
56 lst.append( it.current() );
59 /*!Gets selectors list to \a lst with type \a typ.*/
60 void SUIT_SelectionMgr::selectors( const QString& typ, QPtrList<SUIT_Selector>& lst ) const
63 for ( SelectorListIterator it( mySelectors ); it.current(); ++it )
65 if ( it.current()->type() == typ )
66 lst.append( it.current() );
70 /*! Sets ebabled to \a on for all data owners with type \a typ.
72 void SUIT_SelectionMgr::setEnabled( const bool on, const QString& typ )
74 for ( SelectorListIterator it( mySelectors ); it.current(); ++it )
76 if ( typ.isEmpty() || it.current()->type() == typ )
77 it.current()->setEnabled( on );
81 /*! Gets selected data owners from list with type \a type to list \a lst.
83 void SUIT_SelectionMgr::selected( SUIT_DataOwnerPtrList& lst, const QString& type ) const
87 QMap<const SUIT_DataOwner*, int> map;
88 for ( SelectorListIterator it( mySelectors ); it.current(); ++it )
90 if ( !type.isEmpty() && it.current()->type() != type )
92 SUIT_DataOwnerPtrList curList;
93 it.current()->selected( curList );
94 for ( SUIT_DataOwnerPtrList::const_iterator itr = curList.begin(); itr != curList.end(); ++itr )
96 const SUIT_DataOwnerPtr& ptr = *itr;
97 if ( !map.contains( ptr.operator->() ) )
99 map.insert( ptr.operator->(), 0 );
104 /*! Sets selected data owners from \a lst and append to list, if \a append - true.
106 void SUIT_SelectionMgr::setSelected( const SUIT_DataOwnerPtrList& lst, const bool append )
108 SUIT_DataOwnerPtrList owners;
109 filterOwners( lst, owners );
111 for ( SelectorListIterator it( mySelectors ); it.current(); ++it )
115 SUIT_DataOwnerPtrList current;
116 it.current()->selected( current );
117 for ( SUIT_DataOwnerPtrList::const_iterator it = current.begin(); it != current.end(); ++it )
118 owners.append( *it );
120 it.current()->setSelected( owners );
124 /*! Clear selected data owners.
126 void SUIT_SelectionMgr::clearSelected()
128 setSelected( SUIT_DataOwnerPtrList() );
131 /*! On selection \a sel changed.
133 void SUIT_SelectionMgr::selectionChanged( SUIT_Selector* sel )
135 if ( !sel || !myIsSelChangeEnabled || !sel->isEnabled() )
138 SUIT_DataOwnerPtrList owners;
140 myIsSelChangeEnabled = false;
141 sel->selected( owners );
143 SUIT_DataOwnerPtrList newOwners;
144 filterOwners( owners, newOwners );
146 for ( int i = 0; i < myIterations; i++ )
148 for ( SUIT_Selector* aSel = mySelectors.first(); aSel; aSel = mySelectors.next() )
150 // Temporary action(to avoid selection of the objects which don't pass the filters):
152 aSel->setSelected( newOwners );
155 myIsSelChangeEnabled = true;
157 emit selectionChanged();
160 /*! Checks: Is selection manager has selection mode \a mode?
162 bool SUIT_SelectionMgr::hasSelectionMode( const int mode ) const
164 return mySelModes.contains( mode );
167 /*! Gets selection modes to list \a vals.
169 void SUIT_SelectionMgr::selectionModes( QValueList<int>& vals ) const
174 /*! Set selection mode \a mode to list of selection modes.
176 void SUIT_SelectionMgr::setSelectionModes( const int mode )
180 setSelectionModes( lst );
183 /*! Sets selection modes list from \a lst.
185 void SUIT_SelectionMgr::setSelectionModes( const QValueList<int>& lst )
190 /*! Append selection mode \a mode to list of selection modes.
192 void SUIT_SelectionMgr::appendSelectionModes( const int mode )
196 appendSelectionModes( lst );
199 /*! Append selection modes \a lst list.
201 void SUIT_SelectionMgr::appendSelectionModes( const QValueList<int>& lst )
204 for ( QValueList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
205 map.insert( *it, 0 );
207 for ( QValueList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
209 if ( !map.contains( *itr ) )
210 mySelModes.append( *itr );
214 /*! Remove selection mode \a mode from list.
216 void SUIT_SelectionMgr::removeSelectionModes( const int mode )
220 removeSelectionModes( lst );
223 /*! Remove selection modea \a lst from list.
225 void SUIT_SelectionMgr::removeSelectionModes( const QValueList<int>& lst )
228 for ( QValueList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
229 map.insert( *it, 0 );
231 for ( QValueList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
235 for ( QMap<int, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
236 mySelModes.append( iter.key() );
239 /*! Checks data owner is ok?
241 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwner* owner ) const
247 for ( SelFilterListIterator it( myFilters ); it.current() && ok; ++it )
248 ok = it.current()->isOk( owner );
253 /*! Checks data owner pointer is ok?
255 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwnerPtr& ptr ) const
260 return isOk( ptr.operator->() );
263 /*! Checks selection manager has filter \a f?
265 bool SUIT_SelectionMgr::hasFilter( SUIT_SelectionFilter* f ) const
267 return myFilters.contains( f );
270 /*! Install filter \a f and set selected, if \a update = true.
272 void SUIT_SelectionMgr::installFilter( SUIT_SelectionFilter* f, const bool updateSelection )
274 if ( !hasFilter( f ) )
276 SUIT_DataOwnerPtrList selOwners;
277 if( updateSelection )
278 selected( selOwners );
280 myFilters.append( f );
282 if( updateSelection )
283 setSelected( selOwners );
287 /*! Remove filter \a f from filters list.
289 void SUIT_SelectionMgr::removeFilter( SUIT_SelectionFilter* f )
291 myFilters.remove( f );
294 /*! Clear filters list.
296 void SUIT_SelectionMgr::clearFilters()
301 /*! Sets auto delete filter.
303 bool SUIT_SelectionMgr::autoDeleteFilter() const
305 return myFilters.autoDelete();
308 /*! Sets auto delete filter to \a on.
310 void SUIT_SelectionMgr::setAutoDeleteFilter( const bool on )
312 myFilters.setAutoDelete( on );
315 /*! Gets good data owners list to \a out from \a in.
317 void SUIT_SelectionMgr::filterOwners( const SUIT_DataOwnerPtrList& in, SUIT_DataOwnerPtrList& out ) const
320 for ( SUIT_DataOwnerPtrList::const_iterator it = in.begin(); it != in.end(); ++it )