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