1 // Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE
3 // Copyright (C) 2003-2007 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, or (at your option) any later version.
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.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 #include "LightApp_SelectionMgr.h"
25 #include "LightApp_Study.h"
26 #include "LightApp_DataOwner.h"
27 #include "LightApp_DataSubOwner.h"
28 #include "LightApp_Application.h"
30 #include <SUIT_Session.h>
31 #include <SUIT_Selector.h>
33 #ifndef DISABLE_SALOMEOBJECT
34 #include <SALOME_ListIO.hxx>
36 // Open CASCADE Include
37 #include <TColStd_MapOfInteger.hxx>
38 #include <TColStd_MapIteratorOfMapOfInteger.hxx>
39 #include <TColStd_IndexedMapOfInteger.hxx>
40 #include <TCollection_AsciiString.hxx>
43 #include <QtCore/QSet>
48 LightApp_SelectionMgr::LightApp_SelectionMgr( LightApp_Application* app, const bool fb )
49 : SUIT_SelectionMgr( fb ),
51 myTimeStamp( QTime::currentTime() ),
59 LightApp_SelectionMgr::~LightApp_SelectionMgr()
66 LightApp_Application* LightApp_SelectionMgr::application() const
71 void LightApp_SelectionMgr::setSelected( const SUIT_DataOwnerPtrList& lst, const bool append )
73 SUIT_SelectionMgr::setSelected( lst, append );
75 myTimeStamp = QTime::currentTime();
78 #ifndef DISABLE_SALOMEOBJECT
80 Get all selected objects from selection manager
82 void LightApp_SelectionMgr::selectedObjects( SALOME_ListIO& theList, const QString& theType,
83 const bool convertReferences ) const
85 LightApp_Study* study = dynamic_cast<LightApp_Study*>( application()->activeStudy() );
91 QList<Handle(SALOME_InteractiveObject)> selList;
93 if ( isActualSelectionCache( theType ) ) {
94 selList = selectionCache( theType );
98 if ( !theType.isEmpty() )
99 types.append( theType );
101 types = selectorTypes();
104 for ( QStringList::iterator it = types.begin(); it != types.end(); ++it ) {
105 SUIT_DataOwnerPtrList aList;
106 selected( aList, *it );
108 QList<Handle(SALOME_InteractiveObject)> typeSelList;
110 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr ) {
111 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
115 if ( !aSet.contains( owner->entry() ) && !owner->IO().IsNull() ) {
116 selList.append( owner->IO() );
117 aSet.insert( owner->entry() );
120 typeSelList.append( owner->IO() );
123 if ( isSelectionCacheEnabled() ) {
124 LightApp_SelectionMgr* that = (LightApp_SelectionMgr*)this;
125 that->myCacheSelection.insert( *it, typeSelList );
126 that->myCacheTimes.insert( *it, QTime::currentTime() );
131 QSet<QString> entrySet;
132 for ( QList<Handle(SALOME_InteractiveObject)>::const_iterator itr = selList.begin(); itr != selList.end(); ++itr )
134 Handle(SALOME_InteractiveObject) io = *itr;
135 QString entry( io->getEntry() );
136 // Entry to check object uniqueness.
137 // It is selected owner entry in the case, when we do not convert references,
138 // and entry of a real object, when we convert references.
139 if ( convertReferences ) {
140 QString refEntry = study->referencedToEntry( entry );
141 if ( !entrySet.contains( refEntry ) ) {
142 if ( refEntry != entry ) {
144 QString component = study->componentDataType( entry );
145 theList.Append( new SALOME_InteractiveObject( (const char*)entry.toLatin1(),
146 (const char*)component.toLatin1(),
147 ""/*refobj->Name().c_str()*/ ) );
149 else if ( !io.IsNull() )
150 theList.Append( io );
153 else if ( !entrySet.contains( entry ) && !io.IsNull() )
154 theList.Append( io );
156 entrySet.insert( entry );
161 Append selected objects.
163 void LightApp_SelectionMgr::setSelectedObjects( const SALOME_ListIO& lst, const bool append )
165 SUIT_DataOwnerPtrList owners;
166 for ( SALOME_ListIteratorOfListIO it( lst ); it.More(); it.Next() )
168 if ( it.Value()->hasEntry() )
169 owners.append( new LightApp_DataOwner( it.Value() ) );
172 setSelected( owners, append );
177 Get all selected objects from selection manager
179 void LightApp_SelectionMgr::selectedObjects( QStringList& theList, const QString& theType,
180 const bool convertReferences ) const
182 LightApp_Study* study = dynamic_cast<LightApp_Study*>( application()->activeStudy() );
190 if ( isActualSelectionCache( theType ) )
191 selList = selectionCache( theType );
194 if ( !theType.isEmpty() )
195 types.append( theType );
197 types = selectorTypes();
200 for ( QStringList::iterator it = types.begin(); it != types.end(); ++it ) {
201 SUIT_DataOwnerPtrList aList;
202 selected( aList, *it );
204 QStringList typeSelList;
206 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr ) {
207 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
211 if ( !aSet.contains( owner->entry() ) ) {
212 selList.append( owner->entry() );
213 aSet.insert( owner->entry() );
216 typeSelList.append( owner->entry() );
219 if ( isSelectionCacheEnabled() ) {
220 LightApp_SelectionMgr* that = (LightApp_SelectionMgr*)this;
221 that->myCacheSelection.insert( *it, typeSelList );
222 that->myCacheTimes.insert( *it, QTime::currentTime() );
231 Append selected objects.
233 void LightApp_SelectionMgr::setSelectedObjects( const QStringList& lst, const bool append )
235 SUIT_DataOwnerPtrList owners;
236 foreach( const QString& aValue, lst ) {
237 if ( !aValue.isNull() )
238 owners.append( new LightApp_DataOwner( aValue ) );
241 setSelected( owners, append );
247 Emit current selection changed.
249 void LightApp_SelectionMgr::selectionChanged( SUIT_Selector* theSel )
251 SUIT_SelectionMgr::selectionChanged( theSel );
253 myTimeStamp = QTime::currentTime();
255 emit currentSelectionChanged();
258 #ifndef DISABLE_SALOMEOBJECT
261 get map of indexes for the given SALOME_InteractiveObject
263 void LightApp_SelectionMgr::GetIndexes( const Handle(SALOME_InteractiveObject)& IObject,
264 TColStd_IndexedMapOfInteger& theIndex)
268 SUIT_DataOwnerPtrList aList;
271 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
273 LightApp_DataSubOwner* subOwner = dynamic_cast<LightApp_DataSubOwner*>( (*itr).operator->() );
274 if ( subOwner && subOwner->entry() == QString(IObject->getEntry()) )
275 theIndex.Add( subOwner->index() );
280 get map of indexes for the given entry of SALOME_InteractiveObject
282 void LightApp_SelectionMgr::GetIndexes( const QString& theEntry, TColStd_IndexedMapOfInteger& theIndex )
286 SUIT_DataOwnerPtrList aList;
289 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
291 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
293 if ( subOwner->entry() == theEntry )
294 theIndex.Add( subOwner->index() );
300 Add or remove interactive objects from selection manager.
302 //bool LightApp_SelectionMgr::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject,
303 void LightApp_SelectionMgr::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject,
304 const TColStd_MapOfInteger& theIndexes,
307 SUIT_DataOwnerPtrList remainsOwners;
309 SUIT_DataOwnerPtrList aList;
312 QString ioEntry (IObject->getEntry());
315 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
317 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
320 if ( owner->entry() != ioEntry )
322 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( owner );
324 remainsOwners.append( new LightApp_DataSubOwner( subOwner->entry(), subOwner->index() ) );
326 remainsOwners.append( new LightApp_DataOwner( owner->entry() ) );
332 remainsOwners = aList;
334 TColStd_MapIteratorOfMapOfInteger It;
335 It.Initialize(theIndexes);
336 for(;It.More();It.Next())
337 remainsOwners.append( new LightApp_DataSubOwner( ioEntry, It.Key() ) );
340 setSelected( remainsOwners, append );
342 emit currentSelectionChanged();
344 // Bug 17269: To avoid calling of selected(aList)
345 //TColStd_IndexedMapOfInteger anIndexes;
346 //GetIndexes( IObject, anIndexes );
347 //return !anIndexes.IsEmpty();
351 select 'subobjects' with given indexes
353 void LightApp_SelectionMgr::selectObjects( const Handle(SALOME_InteractiveObject)& IObject,
354 TColStd_IndexedMapOfInteger theIndex, bool append )
356 SUIT_DataOwnerPtrList aList;
358 if ( theIndex.IsEmpty() )
359 aList.append( new LightApp_DataOwner( QString(IObject->getEntry()) ) );
363 for ( i = 1; i <= theIndex.Extent(); i++ )
364 aList.append( new LightApp_DataSubOwner( QString(IObject->getEntry()), theIndex( i ) ) );
367 setSelected( aList, append );
371 select 'subobjects' with given indexes
373 void LightApp_SelectionMgr::selectObjects( MapIOOfMapOfInteger theMapIO, bool append )
375 SUIT_DataOwnerPtrList aList;
377 MapIOOfMapOfInteger::Iterator it(theMapIO);
378 for ( ; it.More(); it.Next() )
380 if ( it.Value().IsEmpty() )
381 aList.append( new LightApp_DataOwner( QString(it.Key()->getEntry()) ) );
385 for ( i = 1; i <= it.Value().Extent(); i++ )
386 aList.append( new LightApp_DataSubOwner( QString(it.Key()->getEntry()), it.Value()( i ) ) );
390 setSelected( aList, append );
394 get map of selected subowners : object's entry <-> map of indexes
396 void LightApp_SelectionMgr::selectedSubOwners( MapEntryOfMapOfInteger& theMap )
400 TColStd_IndexedMapOfInteger anIndexes;
402 SUIT_DataOwnerPtrList aList;
405 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
407 const LightApp_DataSubOwner* subOwner =
408 dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
412 if ( !theMap.IsBound( TCollection_AsciiString(subOwner->entry().toLatin1().data()) ) )
414 // if ( !theMap.IsBound( subOwner->entry().toLatin1().data() ) )
418 //Bug 17269: GetIndexes( subOwner->entry(), anIndexes );
419 //Bug 17269: To avoid multiple calling of selected(aList)
420 for ( SUIT_DataOwnerPtrList::const_iterator itr2 = itr; itr2 != aList.end(); ++itr2 )
422 const LightApp_DataSubOwner* subOwner2 =
423 dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
425 if ( subOwner2->entry() == subOwner->entry() )
426 anIndexes.Add( subOwner2->index() );
429 theMap.Bind( subOwner->entry().toLatin1().data(), anIndexes );
437 void LightApp_SelectionMgr::clearSelectionCache()
439 myCacheTimes.clear();
440 myCacheSelection.clear();
443 bool LightApp_SelectionMgr::isSelectionCacheEnabled() const
448 void LightApp_SelectionMgr::setSelectionCacheEnabled( bool on )
450 if ( myCacheState == on )
456 clearSelectionCache();
459 #ifndef DISABLE_SALOMEOBJECT
461 QList<Handle_SALOME_InteractiveObject> LightApp_SelectionMgr::selectionCache( const QString& type ) const
463 QList<Handle_SALOME_InteractiveObject> res;
466 if ( !type.isEmpty() )
467 types.append( type );
469 types = selectorTypes();
472 for ( QStringList::iterator it = types.begin(); it != types.end(); ++it ) {
473 if ( myCacheSelection.contains( *it ) ) {
474 const SelList& lst = myCacheSelection[*it];
475 for ( SelList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr ) {
476 if ( !(*itr).IsNull() && !set.contains( (*itr)->getEntry() ) ) {
478 set.insert( (*itr)->getEntry() );
488 QStringList LightApp_SelectionMgr::selectionCache( const QString& type ) const
493 if ( !type.isEmpty() )
494 types.append( type );
496 types = selectorTypes();
499 for ( QStringList::iterator it = types.begin(); it != types.end(); ++it ) {
500 if ( myCacheSelection.contains( *it ) ) {
501 const SelList& lst = myCacheSelection[*it];
502 for ( SelList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr ) {
503 if ( !set.contains( *itr ) ) {
515 bool LightApp_SelectionMgr::isActualSelectionCache( const QString& type ) const
520 if ( !type.isEmpty() )
521 types.append( type );
523 types = selectorTypes();
525 for ( QStringList::iterator it = types.begin(); it != types.end() && ok; ++it )
526 ok = myCacheTimes.contains( *it ) && myCacheTimes[*it].isValid() && myCacheTimes[*it] >= myTimeStamp;
531 QStringList LightApp_SelectionMgr::selectorTypes() const
534 QList<SUIT_Selector*> selectorList;
535 selectors( selectorList );
536 for ( QList<SUIT_Selector*>::const_iterator it = selectorList.begin(); it != selectorList.end(); ++it ) {
537 if ( (*it)->isEnabled() )
538 types.append( (*it)->type() );