]> SALOME platform Git repositories - modules/gui.git/blob - src/SUIT/SUIT_SelectionMgr.cxx
Salome HOME
Fix for Bug IPAL9053( 3.0.0: "Check Geometry" and "Load script" functionalities from...
[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       // Temporary action(to avoid selection of the objects which don't pass the filters):
112       //if ( aSel != sel )
113         aSel->setSelected( newOwners );
114     }
115   }
116   myIsSelChangeEnabled = true;
117
118   emit selectionChanged();
119 }
120
121 bool SUIT_SelectionMgr::hasSelectionMode( const int mode ) const
122 {
123   return mySelModes.contains( mode );
124 }
125
126 void SUIT_SelectionMgr::selectionModes( QValueList<int>& vals ) const
127 {
128   vals = mySelModes;
129 }
130
131 void SUIT_SelectionMgr::setSelectionModes( const int mode )
132 {
133   QValueList<int> lst;
134   lst.append( mode );
135   setSelectionModes( lst );
136 }
137
138 void SUIT_SelectionMgr::setSelectionModes( const QValueList<int>& lst )
139 {
140   mySelModes = lst;
141 }
142
143 void SUIT_SelectionMgr::appendSelectionModes( const int mode )
144 {
145   QValueList<int> lst;
146   lst.append( mode );
147   appendSelectionModes( lst );
148 }
149
150 void SUIT_SelectionMgr::appendSelectionModes( const QValueList<int>& lst )
151 {
152   QMap<int, int> map;
153   for ( QValueList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
154     map.insert( *it, 0 );
155
156   for ( QValueList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
157   {
158     if ( !map.contains( *itr ) )
159       mySelModes.append( *itr );
160   }
161 }
162
163 void SUIT_SelectionMgr::removeSelectionModes( const int mode )
164 {
165   QValueList<int> lst;
166   lst.append( mode );
167   removeSelectionModes( lst );
168 }
169
170 void SUIT_SelectionMgr::removeSelectionModes( const QValueList<int>& lst )
171 {
172   QMap<int, int> map;
173   for ( QValueList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
174     map.insert( *it, 0 );
175
176   for ( QValueList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
177     map.remove( *itr );
178
179   mySelModes.clear();
180   for ( QMap<int, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
181     mySelModes.append( iter.key() );
182 }
183
184 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwner* owner ) const
185 {
186   if ( !owner )
187     return false;
188
189   bool ok = true;
190   for ( SelFilterListIterator it( myFilters ); it.current() && ok; ++it )
191     ok = it.current()->isOk( owner );
192
193   return ok;
194 }
195
196 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwnerPtr& ptr ) const
197 {
198   if ( ptr.isNull() )
199     return false;
200
201   return isOk( ptr.operator->() );
202 }
203
204 bool SUIT_SelectionMgr::hasFilter( SUIT_SelectionFilter* f ) const
205 {
206   return myFilters.contains( f );
207 }
208
209 void SUIT_SelectionMgr::installFilter( SUIT_SelectionFilter* f, const bool updateSelection )
210 {
211   if ( !hasFilter( f ) )
212   {
213     SUIT_DataOwnerPtrList selOwners;
214     if( updateSelection )
215       selected( selOwners );
216       
217     myFilters.append( f );
218     
219     if( updateSelection )
220       setSelected( selOwners );
221   }
222 }
223
224 void SUIT_SelectionMgr::removeFilter( SUIT_SelectionFilter* f )
225 {
226   myFilters.remove( f );
227 }
228
229 void SUIT_SelectionMgr::clearFilters()
230 {
231   myFilters.clear();
232 }
233
234 bool SUIT_SelectionMgr::autoDeleteFilter() const
235 {
236   return myFilters.autoDelete();
237 }
238
239 void SUIT_SelectionMgr::setAutoDeleteFilter( const bool on )
240 {
241   myFilters.setAutoDelete( on );
242 }
243
244 void SUIT_SelectionMgr::filterOwners( const SUIT_DataOwnerPtrList& in, SUIT_DataOwnerPtrList& out ) const
245 {
246   out.clear();
247   for ( SUIT_DataOwnerPtrList::const_iterator it = in.begin(); it != in.end(); ++it )
248   {
249     if ( isOk( *it ) )
250       out.append( *it );
251   }
252 }