Salome HOME
PAL10670 - it is necessary to increase speed of selection and popup construction...
[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 /*! Checks: Is selection manager has selection mode \a mode?
156 */
157 bool SUIT_SelectionMgr::hasSelectionMode( const int mode ) const
158 {
159   return mySelModes.contains( mode );
160 }
161
162 /*! Gets selection modes to list \a vals.
163 */
164 void SUIT_SelectionMgr::selectionModes( QValueList<int>& vals ) const
165 {
166   vals = mySelModes;
167 }
168
169 /*! Set selection mode \a mode to list of selection modes.
170 */
171 void SUIT_SelectionMgr::setSelectionModes( const int mode )
172 {
173   QValueList<int> lst;
174   lst.append( mode );
175   setSelectionModes( lst );
176 }
177
178 /*! Sets selection modes list from \a lst.
179 */
180 void SUIT_SelectionMgr::setSelectionModes( const QValueList<int>& lst )
181 {
182   mySelModes = lst;
183 }
184
185 /*! Append selection mode \a mode to list of selection modes.
186 */
187 void SUIT_SelectionMgr::appendSelectionModes( const int mode )
188 {
189   QValueList<int> lst;
190   lst.append( mode );
191   appendSelectionModes( lst );
192 }
193
194 /*! Append selection modes \a lst list.
195 */
196 void SUIT_SelectionMgr::appendSelectionModes( const QValueList<int>& lst )
197 {
198   QMap<int, int> map;
199   for ( QValueList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
200     map.insert( *it, 0 );
201
202   for ( QValueList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
203   {
204     if ( !map.contains( *itr ) )
205       mySelModes.append( *itr );
206   }
207 }
208
209 /*! Remove selection mode \a mode from list.
210 */
211 void SUIT_SelectionMgr::removeSelectionModes( const int mode )
212 {
213   QValueList<int> lst;
214   lst.append( mode );
215   removeSelectionModes( lst );
216 }
217
218 /*! Remove selection modea \a lst from list.
219 */
220 void SUIT_SelectionMgr::removeSelectionModes( const QValueList<int>& lst )
221 {
222   QMap<int, int> map;
223   for ( QValueList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
224     map.insert( *it, 0 );
225
226   for ( QValueList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
227     map.remove( *itr );
228
229   mySelModes.clear();
230   for ( QMap<int, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
231     mySelModes.append( iter.key() );
232 }
233
234 /*! Checks data owner is ok?
235 */
236 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwner* owner ) const
237 {
238   if ( !owner )
239     return false;
240
241   bool ok = true;
242   for ( SelFilterListIterator it( myFilters ); it.current() && ok; ++it )
243     ok = it.current()->isOk( owner );
244
245   return ok;
246 }
247
248 /*! Checks data owner pointer is ok?
249 */
250 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwnerPtr& ptr ) const
251 {
252   if ( ptr.isNull() )
253     return false;
254
255   return isOk( ptr.operator->() );
256 }
257
258 /*! Checks selection manager has filter \a f?
259 */
260 bool SUIT_SelectionMgr::hasFilter( SUIT_SelectionFilter* f ) const
261 {
262   return myFilters.contains( f );
263 }
264
265 /*! Install filter \a f and set selected, if \a update = true.
266 */
267 void SUIT_SelectionMgr::installFilter( SUIT_SelectionFilter* f, const bool updateSelection )
268 {
269   if ( !hasFilter( f ) )
270   {
271     SUIT_DataOwnerPtrList selOwners;
272     if( updateSelection )
273       selected( selOwners );
274       
275     myFilters.append( f );
276     
277     if( updateSelection )
278       setSelected( selOwners );
279   }
280 }
281
282 /*! Remove filter \a f from filters list.
283 */
284 void SUIT_SelectionMgr::removeFilter( SUIT_SelectionFilter* f )
285 {
286   myFilters.remove( f );
287 }
288
289 /*! Clear filters list.
290 */
291 void SUIT_SelectionMgr::clearFilters()
292 {
293   myFilters.clear();
294 }
295
296 /*! Sets auto delete filter.
297 */
298 bool SUIT_SelectionMgr::autoDeleteFilter() const
299 {
300   return myFilters.autoDelete();
301 }
302
303 /*! Sets auto delete filter to \a on.
304 */
305 void SUIT_SelectionMgr::setAutoDeleteFilter( const bool on )
306 {
307   myFilters.setAutoDelete( on );
308 }
309
310 /*! Gets good data owners list to \a out from \a in.
311 */
312 void SUIT_SelectionMgr::filterOwners( const SUIT_DataOwnerPtrList& in, SUIT_DataOwnerPtrList& out ) const
313 {
314   out.clear();
315   for ( SUIT_DataOwnerPtrList::const_iterator it = in.begin(); it != in.end(); ++it )
316   {
317     if ( isOk( *it ) )
318       out.append( *it );
319   }
320 }