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