Salome HOME
NRI : Add KERNEL_ROOT_DIR in addition to SALOME_[ROOT, SITE]_DIR.
[modules/kernel.git] / src / SALOMEGUI / SALOME_Selection.cxx
1 using namespace std;
2 //  File      : SALOME_Selection.cxx
3 //  Created   : Wed Feb 20 17:24:59 2002
4 //  Author    : Nicolas REJNERI
5 //  Project   : SALOME
6 //  Module    : SALOMEGUI
7 //  Copyright : Open CASCADE 2002
8 //  $Header$
9
10 /*!
11   \class SALOME_Selection SALOME_Selection.h
12   \brief Selection Mechanism of Interactive Object.
13 */
14
15 #include "SALOME_Selection.h"
16 #include "SALOME_Filter.hxx"
17 #include "SALOME_InteractiveObject.hxx"
18 #include "SALOME_ListIteratorOfListIO.hxx"
19 #include "SALOME_ListIteratorOfListOfFilter.hxx"
20
21 #include "QAD_Desktop.h"
22 #include "utilities.h"
23
24 static QList<SALOME_Selection>&  SALOME_Sel_GetSelections()
25 {
26   static QList<SALOME_Selection> Selections;
27   return Selections;
28 }
29
30 //=======================================================================
31 //                       SELECTIONS MANAGEMENT
32 //=======================================================================
33
34 /*!
35   Constructor
36 */
37 SALOME_Selection::SALOME_Selection(const QString& aName) :
38   myName(aName)
39 {
40   myFilters.Clear();
41   myIObjects.Clear();
42   mySelectionMode = 4; /*Actor*/
43   mySelActiveCompOnly = false;
44   
45   QAD_Desktop* aDesktop = QAD_Application::getDesktop();
46   if (aDesktop) {
47     QAD_Application *anActiveApplication = aDesktop->getActiveApp();
48     if (anActiveApplication) QAD_ASSERT(connect(this, SIGNAL(currentSelectionChanged()),anActiveApplication, SLOT(updateActions())));
49   }
50 }
51
52 /*!
53   Destructor
54 */
55 SALOME_Selection::~SALOME_Selection()
56 {
57 }
58
59 /*!
60   Create a Selection with name \a aName
61   \return TRUE if the Selection is created. Returns FALSE otherwise. 
62 */
63 bool SALOME_Selection::CreateSelection(const QString& aName)
64
65   SALOME_Selection* S = NULL;
66   if ( SALOME_Selection::FindSelection( aName ) )
67     S = SALOME_Selection::Selection( aName );
68   else {
69     S = new SALOME_Selection(aName);
70     SALOME_Sel_GetSelections().prepend(S);
71   }
72   return true;
73 }
74
75
76 /*!
77   Sets the current Selection with \a aName
78 */
79 SALOME_Selection* SALOME_Selection::Selection(const QString& aName) 
80 {
81   SALOME_Selection* Sel = NULL;
82   if(SALOME_Sel_GetSelections().isEmpty()) return Sel;
83
84   for( Sel=SALOME_Sel_GetSelections().first(); Sel!=0; Sel=SALOME_Sel_GetSelections().next() ){
85     if( Sel->myName.compare(aName) == 0 ) 
86       return Sel;
87   }
88   
89   return Sel;
90 }
91
92 /*!
93   Finds Selection with \a aName
94   \return TRUE if the Selection is found. Returns FALSE otherwise. 
95 */
96 bool SALOME_Selection::FindSelection(const QString& aName) 
97 {
98   SALOME_Selection* Sel;
99   for( Sel=SALOME_Sel_GetSelections().first(); Sel!=0; Sel=SALOME_Sel_GetSelections().next() ){
100     if( Sel->myName.compare(aName) == 0 )
101       return true;
102   }
103
104   return false;
105 }
106
107 /*!
108   Removes Selection with \a aName
109   \return TRUE if the Selection is removed. Returns FALSE otherwise. 
110 */
111 bool SALOME_Selection::RemoveSelection(const QString& aName) 
112 {
113   SALOME_Selection* Sel;
114   for( Sel=SALOME_Sel_GetSelections().first(); Sel!=0; Sel=SALOME_Sel_GetSelections().next() ){
115     if( Sel->myName.compare(aName) == 0 )
116        return SALOME_Sel_GetSelections().remove(Sel);
117   }
118   return false;
119 }
120
121
122
123 //=======================================================================
124 //                                FILTERS MANAGEMENT
125 //=======================================================================
126
127 /*!
128   Adds Filter
129 */
130 void SALOME_Selection::AddFilter(const Handle(SALOME_Filter)& aFilter,
131                                  bool updateSelection) 
132 {
133   if ( !FindFilter(aFilter) ) {
134     myFilters.Append( aFilter );
135
136     SALOME_ListIO removedIObjects;
137
138     if ( updateSelection ) {
139       SALOME_ListIteratorOfListIO It(myIObjects);
140       for(;It.More();It.Next()){
141         Handle(SALOME_InteractiveObject) Object = It.Value();
142         if( !IsOk(Object) ) {
143           removedIObjects.Append( Object );
144           //      RemoveIObject( Object );
145         }
146       }
147       
148       SALOME_ListIteratorOfListIO It1(removedIObjects);
149       for(;It1.More();It1.Next()){
150         Handle(SALOME_InteractiveObject) Object = It1.Value();
151         RemoveIObject( Object );
152       }
153     }
154   }
155 }
156
157 /*!
158   Removes Filter
159 */
160 bool SALOME_Selection::RemoveFilter(const Handle(SALOME_Filter)& aFilter) 
161 {
162   SALOME_ListIteratorOfListOfFilter It(myFilters);
163   for(;It.More();It.Next()){
164     if (aFilter==It.Value()){ 
165       myFilters.Remove(It);
166       return true;
167     }
168   }
169   return false;
170 }
171
172 //! Clears All Filters
173 void SALOME_Selection::ClearFilters()
174 {
175   myFilters.Clear();
176 }
177
178 //! Finds aFilter 
179 bool SALOME_Selection::FindFilter(const Handle(SALOME_Filter)& aFilter) 
180 {
181   SALOME_ListIteratorOfListOfFilter It(myFilters);
182   for(;It.More();It.Next())
183     if (aFilter==It.Value()) 
184       return true;
185   return false;
186 }
187
188 //! Returns the list of stored Filters
189 const SALOME_ListOfFilter& SALOME_Selection::StoredFilters()
190 {
191   return myFilters;
192 }
193
194
195
196 //=======================================================================
197 //                     INTERACTIVE OBJECTS MANAGEMENT
198 //=======================================================================
199
200 void SALOME_Selection::Clear() 
201 {
202   myIObjects.Clear();
203 }
204
205 //! Clears all Interactive Objects
206 void SALOME_Selection::ClearIObjects() 
207 {
208   myIObjects.Clear();
209
210   QAD_Desktop*   myDesktop = (QAD_Desktop*) QAD_Application::getDesktop();
211   QAD_Study* myActiveStudy = myDesktop->getActiveStudy();
212   myActiveStudy->unHighlightAll();
213
214   myMapIOSubIndex.Clear();
215
216   emit currentSelectionChanged();
217 }
218
219 //! Add an InteractiveObject
220 int SALOME_Selection::AddIObject(const Handle(SALOME_InteractiveObject)& anObject, bool update) 
221 {
222   QAD_Desktop*   myDesktop = (QAD_Desktop*) QAD_Application::getDesktop();
223   QAD_Study* myActiveStudy = myDesktop->getActiveStudy();
224
225   if ( !IsOk(anObject) ) {
226     MESSAGE ( "The Object not authorized by Filters" )
227     myActiveStudy->highlight(anObject,false, update);
228     return -1;
229   }
230
231   bool Found = false;
232   SALOME_ListIteratorOfListIO It(myIObjects);
233   for(;It.More();It.Next()) {
234     if (anObject->isSame(It.Value())) {
235       Found = true;
236       break;
237     }
238   }
239
240   // Il n'est pas dedans, on le rajoute....
241   if(Found==false) {
242     myIObjects.Append( anObject );
243     myActiveStudy->highlight(anObject, true, update);
244     emit currentSelectionChanged();
245     return 1;
246   }
247   return 0;
248 }
249
250 //! Removes an InteractiveObject
251 int SALOME_Selection::RemoveIObject(const Handle(SALOME_InteractiveObject)& anObject, bool update) 
252 {
253   SALOME_ListIteratorOfListIO It(myIObjects);
254   for(;It.More();It.Next()) {
255     if (anObject->isSame(It.Value())) {
256       QAD_Desktop*   myDesktop = (QAD_Desktop*) QAD_Application::getDesktop();
257       QAD_Study* myActiveStudy = myDesktop->getActiveStudy();
258       myActiveStudy->highlight(anObject, false, update);
259
260       if ( myMapIOSubIndex.IsBound( anObject ) ) {
261         myMapIOSubIndex.UnBind( anObject );
262       }
263       
264       myIObjects.Remove(It);
265
266       emit currentSelectionChanged();
267       return 1; 
268     }
269   }
270   return 0;
271 }
272
273 //! Returns the list of InteractiveObjects
274 const SALOME_ListIO& SALOME_Selection::StoredIObjects()
275 {
276   return myIObjects;
277 }
278
279 //! Returns the number of InteractiveObjects in the selection. 
280 int SALOME_Selection::IObjectCount()
281 {
282   return myIObjects.Extent();
283 }
284
285 //!  Returns the first InteractiveObject in the selection.
286 Handle(SALOME_InteractiveObject) SALOME_Selection::firstIObject()
287 {
288   return myIObjects.First();
289 }
290
291 //! Returns the last InteractiveObject in the selection.
292 Handle(SALOME_InteractiveObject) SALOME_Selection::lastIObject()
293 {
294   return myIObjects.Last();
295 }
296
297 /*!
298   Returns TRUE if the InteractiveObject is authorized by Filters. Returns FALSE otherwise.
299 */
300 bool SALOME_Selection::IsOk(const Handle(SALOME_InteractiveObject)& anObj)
301 {
302   SALOME_ListIteratorOfListOfFilter It(myFilters);
303   for(;It.More();It.Next()){
304     Handle(SALOME_Filter) theFilter = It.Value();
305     if ( !theFilter->IsOk(anObj) ) 
306       return false;
307   }
308   return true;
309 }
310
311 void SALOME_Selection::SetSelectionMode(int mode, bool activeCompOnly)
312 {
313   mySelectionMode = mode;
314   mySelActiveCompOnly = activeCompOnly;
315 }
316
317 int SALOME_Selection::SelectionMode()
318 {
319   return mySelectionMode;
320 }
321
322 bool SALOME_Selection::IsSelectActiveCompOnly() const
323 {
324   return mySelActiveCompOnly;
325 }
326
327 bool SALOME_Selection::HasIndex( const Handle(SALOME_InteractiveObject)& IObject )
328 {
329   return myMapIOSubIndex.IsBound(IObject);
330 }
331
332 void SALOME_Selection::GetIndex( const Handle(SALOME_InteractiveObject)& IObject, TColStd_MapOfInteger& theIndex )
333 {
334   if ( myMapIOSubIndex.IsBound(IObject) ) {
335     theIndex = myMapIOSubIndex.Find(IObject);
336   }
337   else {
338     theIndex.Clear();
339   }
340 }
341
342
343
344 bool SALOME_Selection::IsIndexSelected(const Handle(SALOME_InteractiveObject)& IObject, int index)
345 {
346   if ( !myMapIOSubIndex.IsBound( IObject ) ) {
347     return false;
348   }
349   TColStd_MapOfInteger& MapIndex = myMapIOSubIndex.ChangeFind( IObject );
350   return MapIndex.Contains( index );
351 }
352
353
354
355
356 bool SALOME_Selection::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject, 
357                                          int index, 
358                                          bool modeShift,
359                                          bool update)
360 {
361   MESSAGE ( " SALOME_Selection::AddOrRemoveIndex " << index << " - " << modeShift )
362   QAD_Desktop*   myDesktop = (QAD_Desktop*) QAD_Application::getDesktop();
363   QAD_Study* myActiveStudy = myDesktop->getActiveStudy();
364
365   if ( !myMapIOSubIndex.IsBound( IObject ) ) {
366     TColStd_MapOfInteger Empty;
367     myMapIOSubIndex.Bind( IObject, Empty );
368   }
369   TColStd_MapOfInteger& MapIndex = myMapIOSubIndex.ChangeFind( IObject );
370
371   if ( MapIndex.Contains( index )) {
372     if ( modeShift ) {
373       MapIndex.Remove( index );
374       myActiveStudy->highlight( IObject, true, update );
375     }
376   } else {
377     if ( !modeShift )
378       MapIndex.Clear();
379
380     MapIndex.Add( index );
381     myActiveStudy->highlight( IObject, true, update );
382     emit currentSelectionChanged();
383     return true;
384   }
385
386   if ( MapIndex.IsEmpty() ) {
387     myMapIOSubIndex.UnBind( IObject );
388     RemoveIObject( IObject, update );
389   }
390
391   emit currentSelectionChanged();
392   return false;
393 }
394
395 void SALOME_Selection::RemoveIndex( const Handle(SALOME_InteractiveObject)& IObject, int index )
396 {
397   if ( myMapIOSubIndex.IsBound( IObject ) ) {
398     TColStd_MapOfInteger& MapIndex = myMapIOSubIndex.ChangeFind( IObject );
399     if ( MapIndex.Contains( index ) )
400       MapIndex.Remove( index );
401   }
402 }
403