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