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