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