Salome HOME
SMH: Fix for compilation with correct CASCADE
[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
51 {
52   lst.clear();
53
54   QMap<const SUIT_DataOwner*, int> map;
55   for ( SelectorListIterator it( mySelectors ); it.current(); ++it )
56   {
57     SUIT_DataOwnerPtrList curList;
58     it.current()->selected( curList );
59     for ( SUIT_DataOwnerPtrList::const_iterator itr = curList.begin(); itr != curList.end(); ++itr )
60     {
61       const SUIT_DataOwnerPtr& ptr = *itr;
62       if ( !map.contains( ptr.operator->() ) )
63         lst.append( ptr );
64       map.insert( ptr.operator->(), 0 );
65     }
66   }
67 }
68
69 void SUIT_SelectionMgr::setSelected( const SUIT_DataOwnerPtrList& lst, const bool append )
70 {
71   SUIT_DataOwnerPtrList owners;
72   filterOwners( lst, owners );
73
74   for ( SelectorListIterator it( mySelectors ); it.current(); ++it )
75   {
76     if ( append )
77     {
78       SUIT_DataOwnerPtrList current;
79       it.current()->selected( current );
80       for ( SUIT_DataOwnerPtrList::const_iterator it = current.begin(); it != current.end(); ++it )
81         owners.append( *it );
82     }
83     it.current()->setSelected( owners );
84   }
85 }
86
87 void SUIT_SelectionMgr::clearSelected()
88 {
89   setSelected( SUIT_DataOwnerPtrList() );
90 }
91
92 void SUIT_SelectionMgr::selectionChanged( SUIT_Selector* sel )
93 {
94   if ( !sel || !myIsSelChangeEnabled || !sel->isEnabled() )
95     return;
96
97   SUIT_DataOwnerPtrList owners;
98
99   myIsSelChangeEnabled = false;
100   sel->selected( owners );
101
102   SUIT_DataOwnerPtrList newOwners;
103   filterOwners( owners, newOwners );
104
105   for ( int i = 0; i < myIterations; i++ )
106   {
107     for ( SUIT_Selector* aSel = mySelectors.first(); aSel; aSel = mySelectors.next() )
108     {
109       if ( aSel != sel )
110             aSel->setSelected( newOwners );
111     }
112   }
113   myIsSelChangeEnabled = true;
114
115   emit selectionChanged();
116 }
117
118 bool SUIT_SelectionMgr::hasSelectionMode( const int mode ) const
119 {
120   return mySelModes.contains( mode );
121 }
122
123 void SUIT_SelectionMgr::selectionModes( QValueList<int>& vals ) const
124 {
125   vals = mySelModes;
126 }
127
128 void SUIT_SelectionMgr::setSelectionModes( const int mode )
129 {
130   QValueList<int> lst;
131   lst.append( mode );
132   setSelectionModes( lst );
133 }
134
135 void SUIT_SelectionMgr::setSelectionModes( const QValueList<int>& lst )
136 {
137   mySelModes = lst;
138 }
139
140 void SUIT_SelectionMgr::appendSelectionModes( const int mode )
141 {
142   QValueList<int> lst;
143   lst.append( mode );
144   appendSelectionModes( lst );
145 }
146
147 void SUIT_SelectionMgr::appendSelectionModes( const QValueList<int>& lst )
148 {
149   QMap<int, int> map;
150   for ( QValueList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
151     map.insert( *it, 0 );
152
153   for ( QValueList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
154   {
155     if ( !map.contains( *itr ) )
156       mySelModes.append( *itr );
157   }
158 }
159
160 void SUIT_SelectionMgr::removeSelectionModes( const int mode )
161 {
162   QValueList<int> lst;
163   lst.append( mode );
164   removeSelectionModes( lst );
165 }
166
167 void SUIT_SelectionMgr::removeSelectionModes( const QValueList<int>& lst )
168 {
169   QMap<int, int> map;
170   for ( QValueList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
171     map.insert( *it, 0 );
172
173   for ( QValueList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
174     map.remove( *itr );
175
176   mySelModes.clear();
177   for ( QMap<int, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
178     mySelModes.append( iter.key() );
179 }
180
181 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwner* owner ) const
182 {
183   if ( !owner )
184     return false;
185
186   bool ok = true;
187   for ( SelFilterListIterator it( myFilters ); it.current() && ok; ++it )
188     ok = it.current()->isOk( owner );
189
190   return ok;
191 }
192
193 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwnerPtr& ptr ) const
194 {
195   if ( ptr.isNull() )
196     return false;
197
198   return isOk( ptr.operator->() );
199 }
200
201 bool SUIT_SelectionMgr::hasFilter( SUIT_SelectionFilter* f ) const
202 {
203   return myFilters.contains( f );
204 }
205
206 void SUIT_SelectionMgr::installFilter( SUIT_SelectionFilter* f )
207 {
208   if ( !hasFilter( f ) )
209     myFilters.append( f );
210 }
211
212 void SUIT_SelectionMgr::removeFilter( SUIT_SelectionFilter* f )
213 {
214   myFilters.remove( f );
215 }
216
217 void SUIT_SelectionMgr::clearFilters()
218 {
219   myFilters.clear();
220 }
221
222 bool SUIT_SelectionMgr::autoDeleteFilter() const
223 {
224   return myFilters.autoDelete();
225 }
226
227 void SUIT_SelectionMgr::setAutoDeleteFilter( const bool on )
228 {
229   myFilters.setAutoDelete( on );
230 }
231
232 void SUIT_SelectionMgr::filterOwners( const SUIT_DataOwnerPtrList& in, SUIT_DataOwnerPtrList& out ) const
233 {
234   out.clear();
235   for ( SUIT_DataOwnerPtrList::const_iterator it = in.begin(); it != in.end(); ++it )
236   {
237     if ( isOk( *it ) )
238       out.append( *it );
239   }
240 }