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