1 // SALOME SALOMEGUI : implementation of desktop and GUI kernel
3 // Copyright (C) 2003 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.opencascade.org/SALOME/ or email : webmaster.salome@opencascade.org
24 // File : SALOME_Selection.cxx
25 // Author : Nicolas REJNERI
31 \class SALOME_Selection SALOME_Selection.h
32 \brief Selection Mechanism of Interactive Object.
35 #include "SALOME_Selection.h"
36 #include "SALOME_Filter.hxx"
37 #include "SALOME_InteractiveObject.hxx"
38 #include "SALOME_ListIteratorOfListIO.hxx"
39 #include "SALOME_ListIteratorOfListOfFilter.hxx"
41 #include "QAD_Desktop.h"
42 #include "utilities.h"
44 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
46 static QList<SALOME_Selection>& SALOME_Sel_GetSelections()
48 static QList<SALOME_Selection> Selections;
52 //=======================================================================
53 // SELECTIONS MANAGEMENT
54 //=======================================================================
59 SALOME_Selection::SALOME_Selection(const QString& aName) :
64 mySelectionMode = ActorSelection; /*4*/
65 mySelActiveCompOnly = false;
67 QAD_Desktop* aDesktop = QAD_Application::getDesktop();
69 QAD_Application *anActiveApplication = aDesktop->getActiveApp();
70 if (anActiveApplication) QAD_ASSERT(connect(this, SIGNAL(currentSelectionChanged()),anActiveApplication, SLOT(updateActions())));
77 SALOME_Selection::~SALOME_Selection()
82 Create a Selection with name \a aName
83 \return TRUE if the Selection is created. Returns FALSE otherwise.
85 bool SALOME_Selection::CreateSelection(const QString& aName)
87 SALOME_Selection* S = NULL;
88 if ( SALOME_Selection::FindSelection( aName ) )
89 S = SALOME_Selection::Selection( aName );
91 S = new SALOME_Selection(aName);
92 SALOME_Sel_GetSelections().prepend(S);
99 Sets the current Selection with \a aName
101 SALOME_Selection* SALOME_Selection::Selection(const QString& aName)
103 SALOME_Selection* Sel = NULL;
104 if(SALOME_Sel_GetSelections().isEmpty()) return Sel;
106 for( Sel=SALOME_Sel_GetSelections().first(); Sel!=0; Sel=SALOME_Sel_GetSelections().next() ){
107 if( Sel->myName.compare(aName) == 0 )
115 Finds Selection with \a aName
116 \return TRUE if the Selection is found. Returns FALSE otherwise.
118 bool SALOME_Selection::FindSelection(const QString& aName)
120 SALOME_Selection* Sel;
121 for( Sel=SALOME_Sel_GetSelections().first(); Sel!=0; Sel=SALOME_Sel_GetSelections().next() ){
122 if( Sel->myName.compare(aName) == 0 )
130 Removes Selection with \a aName
131 \return TRUE if the Selection is removed. Returns FALSE otherwise.
133 bool SALOME_Selection::RemoveSelection(const QString& aName)
135 SALOME_Selection* Sel;
136 for( Sel=SALOME_Sel_GetSelections().first(); Sel!=0; Sel=SALOME_Sel_GetSelections().next() ){
137 if( Sel->myName.compare(aName) == 0 )
138 return SALOME_Sel_GetSelections().remove(Sel);
145 //=======================================================================
146 // FILTERS MANAGEMENT
147 //=======================================================================
152 void SALOME_Selection::AddFilter(const Handle(SALOME_Filter)& aFilter,
153 bool updateSelection)
155 if ( !FindFilter(aFilter) ) {
156 myFilters.Append( aFilter );
158 SALOME_ListIO removedIObjects;
160 if ( updateSelection ) {
161 SALOME_ListIteratorOfListIO It(myIObjects);
162 for(;It.More();It.Next()){
163 Handle(SALOME_InteractiveObject) Object = It.Value();
164 if( !IsOk(Object) ) {
165 removedIObjects.Append( Object );
166 // RemoveIObject( Object );
170 SALOME_ListIteratorOfListIO It1(removedIObjects);
171 for(;It1.More();It1.Next()){
172 Handle(SALOME_InteractiveObject) Object = It1.Value();
173 RemoveIObject( Object );
182 bool SALOME_Selection::RemoveFilter(const Handle(SALOME_Filter)& aFilter)
184 SALOME_ListIteratorOfListOfFilter It(myFilters);
185 for(;It.More();It.Next()){
186 if (aFilter==It.Value()){
187 myFilters.Remove(It);
194 //! Clears All Filters
195 void SALOME_Selection::ClearFilters()
201 bool SALOME_Selection::FindFilter(const Handle(SALOME_Filter)& aFilter)
203 SALOME_ListIteratorOfListOfFilter It(myFilters);
204 for(;It.More();It.Next())
205 if (aFilter==It.Value())
210 //! Returns the list of stored Filters
211 const SALOME_ListOfFilter& SALOME_Selection::StoredFilters()
218 //=======================================================================
219 // INTERACTIVE OBJECTS MANAGEMENT
220 //=======================================================================
222 void SALOME_Selection::Clear()
227 //! Clears all Interactive Objects
228 void SALOME_Selection::ClearIObjects()
232 QAD_Desktop* myDesktop = (QAD_Desktop*) QAD_Application::getDesktop();
233 QAD_Study* myActiveStudy = myDesktop->getActiveStudy();
234 myActiveStudy->unHighlightAll();
236 myMapIOSubIndex.Clear();
238 emit currentSelectionChanged();
241 //! Add an InteractiveObject
242 int SALOME_Selection::AddIObject(const Handle(SALOME_InteractiveObject)& anObject, bool update)
244 QAD_Desktop* myDesktop = (QAD_Desktop*) QAD_Application::getDesktop();
245 QAD_Study* myActiveStudy = myDesktop->getActiveStudy();
247 if ( !IsOk(anObject) ) {
248 MESSAGE ( "The Object not authorized by Filters" )
249 myActiveStudy->highlight(anObject,false, update);
254 SALOME_ListIteratorOfListIO It(myIObjects);
255 for(;It.More();It.Next()) {
256 if (anObject->isSame(It.Value())) {
262 // Il n'est pas dedans, on le rajoute....
264 myIObjects.Append( anObject );
265 myActiveStudy->highlight(anObject, true, update);
266 emit currentSelectionChanged();
272 //! Removes an InteractiveObject
273 int SALOME_Selection::RemoveIObject(const Handle(SALOME_InteractiveObject)& anObject, bool update)
275 SALOME_ListIteratorOfListIO It(myIObjects);
276 for(;It.More();It.Next()) {
277 if (anObject->isSame(It.Value())) {
278 QAD_Desktop* myDesktop = (QAD_Desktop*) QAD_Application::getDesktop();
279 QAD_Study* myActiveStudy = myDesktop->getActiveStudy();
280 myActiveStudy->highlight(anObject, false, update);
282 if ( myMapIOSubIndex.IsBound( anObject ) ) {
283 myMapIOSubIndex.UnBind( anObject );
286 myIObjects.Remove(It);
288 emit currentSelectionChanged();
295 //! Returns the list of InteractiveObjects
296 const SALOME_ListIO& SALOME_Selection::StoredIObjects()
301 //! Returns the number of InteractiveObjects in the selection.
302 int SALOME_Selection::IObjectCount()
304 return myIObjects.Extent();
307 //! Returns the first InteractiveObject in the selection.
308 Handle(SALOME_InteractiveObject) SALOME_Selection::firstIObject()
310 return myIObjects.First();
313 //! Returns the last InteractiveObject in the selection.
314 Handle(SALOME_InteractiveObject) SALOME_Selection::lastIObject()
316 return myIObjects.Last();
320 Returns TRUE if the InteractiveObject is authorized by Filters. Returns FALSE otherwise.
322 bool SALOME_Selection::IsOk(const Handle(SALOME_InteractiveObject)& anObj)
324 SALOME_ListIteratorOfListOfFilter It(myFilters);
325 for(;It.More();It.Next()){
326 Handle(SALOME_Filter) theFilter = It.Value();
327 if ( !theFilter->IsOk(anObj) )
333 void SALOME_Selection::SetSelectionMode(Selection_Mode mode, bool activeCompOnly)
335 mySelectionMode = mode;
336 mySelActiveCompOnly = activeCompOnly;
339 Selection_Mode SALOME_Selection::SelectionMode()
341 return mySelectionMode;
344 bool SALOME_Selection::IsSelectActiveCompOnly() const
346 return mySelActiveCompOnly;
349 bool SALOME_Selection::HasIndex( const Handle(SALOME_InteractiveObject)& IObject )
351 return myMapIOSubIndex.IsBound(IObject);
354 void SALOME_Selection::GetIndex( const Handle(SALOME_InteractiveObject)& IObject, TColStd_MapOfInteger& theIndex )
356 if ( myMapIOSubIndex.IsBound(IObject) ) {
357 theIndex = myMapIOSubIndex.Find(IObject);
366 bool SALOME_Selection::IsIndexSelected(const Handle(SALOME_InteractiveObject)& IObject, int index)
368 if ( !myMapIOSubIndex.IsBound( IObject ) ) {
371 TColStd_MapOfInteger& MapIndex = myMapIOSubIndex.ChangeFind( IObject );
372 return MapIndex.Contains( index );
378 bool SALOME_Selection::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject,
383 MESSAGE ( " SALOME_Selection::AddOrRemoveIndex " << index << " - " << modeShift );
384 QAD_Desktop* myDesktop = (QAD_Desktop*) QAD_Application::getDesktop();
385 QAD_Study* myActiveStudy = myDesktop->getActiveStudy();
387 if ( !myMapIOSubIndex.IsBound( IObject ) ) {
388 TColStd_MapOfInteger Empty;
389 myMapIOSubIndex.Bind( IObject, Empty );
391 TColStd_MapOfInteger& MapIndex = myMapIOSubIndex.ChangeFind( IObject );
393 if ( MapIndex.Contains( index )) {
395 MapIndex.Remove( index );
396 myActiveStudy->highlight( IObject, true, update );
402 MapIndex.Add( index );
403 myActiveStudy->highlight( IObject, true, update );
404 emit currentSelectionChanged();
408 if ( MapIndex.IsEmpty() ) {
409 myMapIOSubIndex.UnBind( IObject );
410 RemoveIObject( IObject, update );
413 emit currentSelectionChanged();
417 bool SALOME_Selection::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject,
418 const TColStd_MapOfInteger& theIndices,
422 QAD_Desktop* myDesktop = (QAD_Desktop*) QAD_Application::getDesktop();
423 QAD_Study* myActiveStudy = myDesktop->getActiveStudy();
425 if ( !myMapIOSubIndex.IsBound( IObject ) ) {
426 TColStd_MapOfInteger Empty;
427 myMapIOSubIndex.Bind( IObject, Empty );
429 TColStd_MapOfInteger& MapIndex = myMapIOSubIndex.ChangeFind( IObject );
430 TColStd_MapIteratorOfMapOfInteger It;
431 It.Initialize(theIndices);
435 if (MapIndex.Contains(It.Key()))
437 if (!modeShift) return add;
444 for(;It.More();It.Next())
445 MapIndex.Add(It.Key());
447 for(;It.More();It.Next())
448 MapIndex.Remove(It.Key());
451 myActiveStudy->highlight( IObject, true, update );
452 if ( MapIndex.IsEmpty() ) myMapIOSubIndex.UnBind( IObject );
453 emit currentSelectionChanged();
458 bool SALOME_Selection::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject,
459 const std::vector<int>& theIndices,
463 QAD_Desktop* myDesktop = (QAD_Desktop*) QAD_Application::getDesktop();
464 QAD_Study* myActiveStudy = myDesktop->getActiveStudy();
466 if ( !myMapIOSubIndex.IsBound( IObject ) ) {
467 TColStd_MapOfInteger Empty;
468 myMapIOSubIndex.Bind( IObject, Empty );
470 TColStd_MapOfInteger& MapIndex = myMapIOSubIndex.ChangeFind( IObject );
474 if (MapIndex.Contains(theIndices[0]))
476 if (!modeShift) return add;
483 for (int i=0; i<theIndices.size();i++)
484 MapIndex.Add(theIndices[i]);
486 for (int i=0; i<theIndices.size();i++)
487 MapIndex.Remove(theIndices[i]);
489 myActiveStudy->highlight( IObject, true, update );
490 if ( MapIndex.IsEmpty() )
491 myMapIOSubIndex.UnBind( IObject );
492 emit currentSelectionChanged();
497 void SALOME_Selection::RemoveIndex( const Handle(SALOME_InteractiveObject)& IObject, int index )
499 if ( myMapIOSubIndex.IsBound( IObject ) ) {
500 TColStd_MapOfInteger& MapIndex = myMapIOSubIndex.ChangeFind( IObject );
501 if ( MapIndex.Contains( index ) )
502 MapIndex.Remove( index );