1 // Copyright (C) 2007-2016 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_MapIteratorOfMapOfInteger.hxx>
38 #include <TCollection_AsciiString.hxx>
41 #include <QtCore/QSet>
46 LightApp_SelectionMgr::LightApp_SelectionMgr( LightApp_Application* app, const bool fb )
47 : SUIT_SelectionMgr( fb ),
49 myTimeStamp( QTime::currentTime() ),
57 LightApp_SelectionMgr::~LightApp_SelectionMgr()
64 LightApp_Application* LightApp_SelectionMgr::application() const
69 void LightApp_SelectionMgr::setSelected( const SUIT_DataOwnerPtrList& lst, const bool append )
71 SUIT_SelectionMgr::setSelected( lst, append );
73 myTimeStamp = QTime::currentTime();
76 #ifndef DISABLE_SALOMEOBJECT
78 Get all selected objects from selection manager
80 void LightApp_SelectionMgr::selectedObjects( SALOME_ListIO& theList, const QString& theType,
81 const bool convertReferences ) const
83 LightApp_Study* study = dynamic_cast<LightApp_Study*>( application()->activeStudy() );
89 QList<Handle(SALOME_InteractiveObject)> selList;
91 if ( isActualSelectionCache( theType ) ) {
92 selList = selectionCache( theType );
96 if ( !theType.isEmpty() )
97 types.append( theType );
99 types = selectorTypes();
102 for ( QStringList::iterator it = types.begin(); it != types.end(); ++it ) {
103 SUIT_DataOwnerPtrList aList;
104 selected( aList, *it );
106 QList<Handle(SALOME_InteractiveObject)> typeSelList;
108 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr ) {
109 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
113 if ( !aSet.contains( owner->entry() ) && !owner->IO().IsNull() ) {
114 selList.append( owner->IO() );
115 aSet.insert( owner->entry() );
118 typeSelList.append( owner->IO() );
121 if ( isSelectionCacheEnabled() ) {
122 LightApp_SelectionMgr* that = (LightApp_SelectionMgr*)this;
123 that->myCacheSelection.insert( *it, typeSelList );
124 that->myCacheTimes.insert( *it, QTime::currentTime() );
129 QSet<QString> entrySet;
130 for ( QList<Handle(SALOME_InteractiveObject)>::const_iterator itr = selList.begin(); itr != selList.end(); ++itr )
132 Handle(SALOME_InteractiveObject) io = *itr;
133 QString entry( io->getEntry() );
134 // Entry to check object uniqueness.
135 // It is selected owner entry in the case, when we do not convert references,
136 // and entry of a real object, when we convert references.
137 if ( convertReferences ) {
138 QString refEntry = study->referencedToEntry( entry );
139 if ( !entrySet.contains( refEntry ) ) {
140 if ( refEntry != entry ) {
142 QString component = study->componentDataType( entry );
143 theList.Append( new SALOME_InteractiveObject( (const char*)entry.toLatin1(),
144 (const char*)component.toLatin1(),
145 ""/*refobj->Name().c_str()*/ ) );
147 else if ( !io.IsNull() )
148 theList.Append( io );
151 else if ( !entrySet.contains( entry ) && !io.IsNull() )
152 theList.Append( io );
154 entrySet.insert( entry );
159 Append selected objects.
161 void LightApp_SelectionMgr::setSelectedObjects( const SALOME_ListIO& lst, const bool append )
163 SUIT_DataOwnerPtrList owners;
164 for ( SALOME_ListIteratorOfListIO it( lst ); it.More(); it.Next() )
166 if ( it.Value()->hasEntry() )
167 owners.append( new LightApp_DataOwner( it.Value() ) );
170 setSelected( owners, append );
175 Get all selected objects from selection manager
177 void LightApp_SelectionMgr::selectedObjects( QStringList& theList, const QString& theType,
178 const bool convertReferences ) const
180 LightApp_Study* study = dynamic_cast<LightApp_Study*>( application()->activeStudy() );
188 if ( isActualSelectionCache( theType ) )
189 selList = selectionCache( theType );
192 if ( !theType.isEmpty() )
193 types.append( theType );
195 types = selectorTypes();
198 for ( QStringList::iterator it = types.begin(); it != types.end(); ++it ) {
199 SUIT_DataOwnerPtrList aList;
200 selected( aList, *it );
202 QStringList typeSelList;
204 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr ) {
205 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
209 if ( !aSet.contains( owner->entry() ) ) {
210 selList.append( owner->entry() );
211 aSet.insert( owner->entry() );
214 typeSelList.append( owner->entry() );
217 if ( isSelectionCacheEnabled() ) {
218 LightApp_SelectionMgr* that = (LightApp_SelectionMgr*)this;
219 that->myCacheSelection.insert( *it, typeSelList );
220 that->myCacheTimes.insert( *it, QTime::currentTime() );
229 Append selected objects.
231 void LightApp_SelectionMgr::setSelectedObjects( const QStringList& lst, const bool append )
233 SUIT_DataOwnerPtrList owners;
234 foreach( const QString& aValue, lst ) {
235 if ( !aValue.isNull() )
236 owners.append( new LightApp_DataOwner( aValue ) );
239 setSelected( owners, append );
245 Emit current selection changed.
247 void LightApp_SelectionMgr::selectionChanged( SUIT_Selector* theSel )
249 SUIT_SelectionMgr::selectionChanged( theSel );
251 myTimeStamp = QTime::currentTime();
253 emit currentSelectionChanged();
256 #ifndef DISABLE_SALOMEOBJECT
259 get map of indexes for the given SALOME_InteractiveObject
261 void LightApp_SelectionMgr::GetIndexes( const Handle(SALOME_InteractiveObject)& IObject,
262 TColStd_IndexedMapOfInteger& theIndex)
266 SUIT_DataOwnerPtrList aList;
269 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
271 LightApp_DataSubOwner* subOwner = dynamic_cast<LightApp_DataSubOwner*>( (*itr).operator->() );
272 if ( subOwner && subOwner->entry() == QString(IObject->getEntry()) )
273 theIndex.Add( subOwner->index() );
278 get map of indexes for the given entry of SALOME_InteractiveObject
280 void LightApp_SelectionMgr::GetIndexes( const QString& theEntry, TColStd_IndexedMapOfInteger& theIndex )
284 SUIT_DataOwnerPtrList aList;
287 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
289 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
291 if ( subOwner->entry() == theEntry )
292 theIndex.Add( subOwner->index() );
298 Add or remove interactive objects from selection manager.
300 //bool LightApp_SelectionMgr::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject,
301 void LightApp_SelectionMgr::AddOrRemoveIndex( const Handle(SALOME_InteractiveObject)& IObject,
302 const TColStd_MapOfInteger& theIndexes,
305 SUIT_DataOwnerPtrList remainsOwners;
307 SUIT_DataOwnerPtrList aList;
310 QString ioEntry (IObject->getEntry());
313 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
315 const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
318 if ( owner->entry() != ioEntry )
320 const LightApp_DataSubOwner* subOwner = dynamic_cast<const LightApp_DataSubOwner*>( owner );
322 remainsOwners.append( new LightApp_DataSubOwner( subOwner->entry(), subOwner->index() ) );
324 remainsOwners.append( new LightApp_DataOwner( owner->entry() ) );
330 remainsOwners = aList;
332 TColStd_MapIteratorOfMapOfInteger It;
333 It.Initialize(theIndexes);
334 for(;It.More();It.Next())
335 remainsOwners.append( new LightApp_DataSubOwner( ioEntry, It.Key() ) );
338 setSelected( remainsOwners, append );
340 emit currentSelectionChanged();
342 // Bug 17269: To avoid calling of selected(aList)
343 //TColStd_IndexedMapOfInteger anIndexes;
344 //GetIndexes( IObject, anIndexes );
345 //return !anIndexes.IsEmpty();
349 select 'subobjects' with given indexes
351 void LightApp_SelectionMgr::selectObjects( const Handle(SALOME_InteractiveObject)& IObject,
352 TColStd_IndexedMapOfInteger theIndex, bool append )
354 SUIT_DataOwnerPtrList aList;
356 if ( theIndex.IsEmpty() )
357 aList.append( new LightApp_DataOwner( QString(IObject->getEntry()) ) );
361 for ( i = 1; i <= theIndex.Extent(); i++ )
362 aList.append( new LightApp_DataSubOwner( QString(IObject->getEntry()), theIndex( i ) ) );
365 setSelected( aList, append );
369 select 'subobjects' with given indexes
371 void LightApp_SelectionMgr::selectObjects( MapIOOfMapOfInteger theMapIO, bool append )
373 SUIT_DataOwnerPtrList aList;
375 MapIOOfMapOfInteger::Iterator it(theMapIO);
376 for ( ; it.More(); it.Next() )
378 if ( it.Value().IsEmpty() )
379 aList.append( new LightApp_DataOwner( QString(it.Key()->getEntry()) ) );
383 for ( i = 1; i <= it.Value().Extent(); i++ )
384 aList.append( new LightApp_DataSubOwner( QString(it.Key()->getEntry()), it.Value()( i ) ) );
388 setSelected( aList, append );
392 get map of selected subowners : object's entry <-> map of indexes
394 void LightApp_SelectionMgr::selectedSubOwners( MapEntryOfMapOfInteger& theMap )
398 TColStd_IndexedMapOfInteger anIndexes;
400 SUIT_DataOwnerPtrList aList;
403 for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
405 const LightApp_DataSubOwner* subOwner =
406 dynamic_cast<const LightApp_DataSubOwner*>( (*itr).operator->() );
410 if ( !theMap.IsBound( TCollection_AsciiString(subOwner->entry().toLatin1().data()) ) )
412 // if ( !theMap.IsBound( subOwner->entry().toLatin1().data() ) )
416 //Bug 17269: GetIndexes( subOwner->entry(), anIndexes );
417 //Bug 17269: To avoid multiple calling of selected(aList)
418 for ( SUIT_DataOwnerPtrList::const_iterator itr2 = itr; itr2 != aList.end(); ++itr2 )
420 const LightApp_DataSubOwner* subOwner2 =
421 dynamic_cast<const LightApp_DataSubOwner*>( (*itr2).operator->() );
423 if ( subOwner2->entry() == subOwner->entry() )
424 anIndexes.Add( subOwner2->index() );
427 theMap.Bind( subOwner->entry().toLatin1().data(), anIndexes );
435 void LightApp_SelectionMgr::clearSelectionCache()
437 myCacheTimes.clear();
438 myCacheSelection.clear();
441 bool LightApp_SelectionMgr::isSelectionCacheEnabled() const
446 void LightApp_SelectionMgr::setSelectionCacheEnabled( bool on )
448 if ( myCacheState == on )
454 clearSelectionCache();
457 #ifndef DISABLE_SALOMEOBJECT
459 QList<Handle(SALOME_InteractiveObject)> LightApp_SelectionMgr::selectionCache( const QString& type ) const
461 QList<Handle(SALOME_InteractiveObject)> res;
464 if ( !type.isEmpty() )
465 types.append( type );
467 types = selectorTypes();
470 for ( QStringList::iterator it = types.begin(); it != types.end(); ++it ) {
471 if ( myCacheSelection.contains( *it ) ) {
472 const SelList& lst = myCacheSelection[*it];
473 for ( SelList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr ) {
474 if ( !(*itr).IsNull() && !set.contains( (*itr)->getEntry() ) ) {
476 set.insert( (*itr)->getEntry() );
486 QStringList LightApp_SelectionMgr::selectionCache( const QString& type ) const
491 if ( !type.isEmpty() )
492 types.append( type );
494 types = selectorTypes();
497 for ( QStringList::iterator it = types.begin(); it != types.end(); ++it ) {
498 if ( myCacheSelection.contains( *it ) ) {
499 const SelList& lst = myCacheSelection[*it];
500 for ( SelList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr ) {
501 if ( !set.contains( *itr ) ) {
513 bool LightApp_SelectionMgr::isActualSelectionCache( const QString& type ) const
518 if ( !type.isEmpty() )
519 types.append( type );
521 types = selectorTypes();
523 for ( QStringList::iterator it = types.begin(); it != types.end() && ok; ++it )
524 ok = myCacheTimes.contains( *it ) && myCacheTimes[*it].isValid() && myCacheTimes[*it] >= myTimeStamp;
529 QStringList LightApp_SelectionMgr::selectorTypes() const
532 QList<SUIT_Selector*> selectorList;
533 selectors( selectorList );
534 for ( QList<SUIT_Selector*>::const_iterator it = selectorList.begin(); it != selectorList.end(); ++it ) {
535 if ( (*it)->isEnabled() )
536 types.append( (*it)->type() );