Salome HOME
New method "isSynchronizing()"
[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   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       lst.append( *itr );
96   }
97 }
98
99 /*! Sets selected data owners from \a lst and append to list, if \a append - true.
100 */
101 void SUIT_SelectionMgr::setSelected( const SUIT_DataOwnerPtrList& lst, const bool append )
102 {
103   SUIT_DataOwnerPtrList owners;
104   filterOwners( lst, owners );
105
106   for ( SelectorListIterator it( mySelectors ); it.current(); ++it )
107   {
108     if ( append )
109     {
110       SUIT_DataOwnerPtrList current;
111       it.current()->selected( current );
112       for ( SUIT_DataOwnerPtrList::const_iterator it = current.begin(); it != current.end(); ++it )
113         owners.append( *it );
114     }
115     it.current()->setSelected( owners );
116   }
117 }
118
119 /*! Clear selected data owners.
120 */
121 void SUIT_SelectionMgr::clearSelected()
122 {
123   setSelected( SUIT_DataOwnerPtrList() );
124 }
125
126 /*! On selection \a sel changed.
127 */
128 void SUIT_SelectionMgr::selectionChanged( SUIT_Selector* sel )
129 {
130   if ( !sel || !myIsSelChangeEnabled || !sel->isEnabled() )
131     return;
132
133   SUIT_DataOwnerPtrList owners;
134
135   myIsSelChangeEnabled = false;
136   sel->selected( owners );
137
138   SUIT_DataOwnerPtrList newOwners;
139   filterOwners( owners, newOwners );
140
141   for ( int i = 0; i < myIterations; i++ )
142   {
143     for ( SUIT_Selector* aSel = mySelectors.first(); aSel; aSel = mySelectors.next() )
144     {
145       // Temporary action(to avoid selection of the objects which don't pass the filters):
146       //if ( aSel != sel )
147             aSel->setSelected( newOwners );
148     }
149   }
150   myIsSelChangeEnabled = true;
151
152   emit selectionChanged();
153 }
154
155 /*!
156   Returns true if selection manger is in synchronising mode
157   (during synchonisation of the selectors selection).
158 */
159 bool SUIT_SelectionMgr::isSynchronizing() const
160 {
161   return !myIsSelChangeEnabled;
162 }
163
164 /*! Checks: Is selection manager has selection mode \a mode?
165 */
166 bool SUIT_SelectionMgr::hasSelectionMode( const int mode ) const
167 {
168   return mySelModes.contains( mode );
169 }
170
171 /*! Gets selection modes to list \a vals.
172 */
173 void SUIT_SelectionMgr::selectionModes( QValueList<int>& vals ) const
174 {
175   vals = mySelModes;
176 }
177
178 /*! Set selection mode \a mode to list of selection modes.
179 */
180 void SUIT_SelectionMgr::setSelectionModes( const int mode )
181 {
182   QValueList<int> lst;
183   lst.append( mode );
184   setSelectionModes( lst );
185 }
186
187 /*! Sets selection modes list from \a lst.
188 */
189 void SUIT_SelectionMgr::setSelectionModes( const QValueList<int>& lst )
190 {
191   mySelModes = lst;
192 }
193
194 /*! Append selection mode \a mode to list of selection modes.
195 */
196 void SUIT_SelectionMgr::appendSelectionModes( const int mode )
197 {
198   QValueList<int> lst;
199   lst.append( mode );
200   appendSelectionModes( lst );
201 }
202
203 /*! Append selection modes \a lst list.
204 */
205 void SUIT_SelectionMgr::appendSelectionModes( const QValueList<int>& lst )
206 {
207   QMap<int, int> map;
208   for ( QValueList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
209     map.insert( *it, 0 );
210
211   for ( QValueList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
212   {
213     if ( !map.contains( *itr ) )
214       mySelModes.append( *itr );
215   }
216 }
217
218 /*! Remove selection mode \a mode from list.
219 */
220 void SUIT_SelectionMgr::removeSelectionModes( const int mode )
221 {
222   QValueList<int> lst;
223   lst.append( mode );
224   removeSelectionModes( lst );
225 }
226
227 /*! Remove selection modea \a lst from list.
228 */
229 void SUIT_SelectionMgr::removeSelectionModes( const QValueList<int>& lst )
230 {
231   QMap<int, int> map;
232   for ( QValueList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
233     map.insert( *it, 0 );
234
235   for ( QValueList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
236     map.remove( *itr );
237
238   mySelModes.clear();
239   for ( QMap<int, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
240     mySelModes.append( iter.key() );
241 }
242
243 /*! Checks data owner is ok?
244 */
245 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwner* owner ) const
246 {
247   if ( !owner )
248     return false;
249
250   bool ok = true;
251   for ( SelFilterListIterator it( myFilters ); it.current() && ok; ++it )
252     ok = it.current()->isOk( owner );
253
254   return ok;
255 }
256
257 /*! Checks data owner pointer is ok?
258 */
259 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwnerPtr& ptr ) const
260 {
261   if ( ptr.isNull() )
262     return false;
263
264   return isOk( ptr.operator->() );
265 }
266
267 /*! Checks selection manager has filter \a f?
268 */
269 bool SUIT_SelectionMgr::hasFilter( SUIT_SelectionFilter* f ) const
270 {
271   return myFilters.contains( f );
272 }
273
274 /*! Install filter \a f and set selected, if \a update = true.
275 */
276 void SUIT_SelectionMgr::installFilter( SUIT_SelectionFilter* f, const bool updateSelection )
277 {
278   if ( !hasFilter( f ) )
279   {
280     SUIT_DataOwnerPtrList selOwners;
281     if( updateSelection )
282       selected( selOwners );
283       
284     myFilters.append( f );
285     
286     if( updateSelection )
287       setSelected( selOwners );
288   }
289 }
290
291 /*! Remove filter \a f from filters list.
292 */
293 void SUIT_SelectionMgr::removeFilter( SUIT_SelectionFilter* f )
294 {
295   myFilters.remove( f );
296 }
297
298 /*! Clear filters list.
299 */
300 void SUIT_SelectionMgr::clearFilters()
301 {
302   myFilters.clear();
303 }
304
305 /*! Sets auto delete filter.
306 */
307 bool SUIT_SelectionMgr::autoDeleteFilter() const
308 {
309   return myFilters.autoDelete();
310 }
311
312 /*! Sets auto delete filter to \a on.
313 */
314 void SUIT_SelectionMgr::setAutoDeleteFilter( const bool on )
315 {
316   myFilters.setAutoDelete( on );
317 }
318
319 /*! Gets good data owners list to \a out from \a in.
320 */
321 void SUIT_SelectionMgr::filterOwners( const SUIT_DataOwnerPtrList& in, SUIT_DataOwnerPtrList& out ) const
322 {
323   out.clear();
324   for ( SUIT_DataOwnerPtrList::const_iterator it = in.begin(); it != in.end(); ++it )
325   {
326     if ( isOk( *it ) )
327       out.append( *it );
328   }
329 }