Salome HOME
updated copyright message
[modules/gui.git] / src / LightApp / LightApp_SelectionMgr.cxx
index dcdae22e0bd7b751601a3313cb833835428d85ec..2faa58531da8c3ee9c1a1a4b18d2e93837909f84 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2023  CEA, EDF, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 
 #ifndef DISABLE_SALOMEOBJECT
   #include <SALOME_ListIO.hxx>
-  #include <SALOME_ListIteratorOfListIO.hxx>
 
   // Open CASCADE Include
-  #include <TColStd_MapOfInteger.hxx>
   #include <TColStd_MapIteratorOfMapOfInteger.hxx>
-  #include <TColStd_IndexedMapOfInteger.hxx>
   #include <TCollection_AsciiString.hxx>
 #endif
 
@@ -78,10 +75,25 @@ void LightApp_SelectionMgr::setSelected( const SUIT_DataOwnerPtrList& lst, const
 
 #ifndef DISABLE_SALOMEOBJECT
 /*!
-  Get all selected objects from selection manager
+  Get a sole selected objects from selection manager. If more than one object selected, return NULL.
+  Useful to optimize performance in case of large number of objects (IPAL0054049)
 */
-void LightApp_SelectionMgr::selectedObjects( SALOME_ListIO& theList, const QString& theType,
+Handle(SALOME_InteractiveObject)
+  LightApp_SelectionMgr::soleSelectedObject( const QString& theType,
                                              const bool convertReferences ) const
+{
+  SALOME_ListIO list;
+  selectedObjects( list, theType, convertReferences, true );
+  return  list.Extent() == 1 ? list.First() : Handle(SALOME_InteractiveObject)();
+}
+
+/*!
+  Get all selected objects from selection manager
+*/
+void LightApp_SelectionMgr::selectedObjects( SALOME_ListIO& theList,
+                                             const QString& theType,
+                                             const bool     convertReferences,
+                                             const bool     sole) const
 {
   LightApp_Study* study = dynamic_cast<LightApp_Study*>( application()->activeStudy() );
   if ( !study )
@@ -91,7 +103,7 @@ void LightApp_SelectionMgr::selectedObjects( SALOME_ListIO& theList, const QStri
 
   QList<Handle(SALOME_InteractiveObject)> selList;
 
-  if ( isActualSelectionCache( theType ) ) {
+  if ( !sole && isActualSelectionCache( theType )) {
     selList = selectionCache( theType );
   }
   else {
@@ -104,27 +116,27 @@ void LightApp_SelectionMgr::selectedObjects( SALOME_ListIO& theList, const QStri
     QSet<QString> aSet;
     for ( QStringList::iterator it = types.begin(); it != types.end(); ++it ) {
       SUIT_DataOwnerPtrList aList;
-      selected( aList, *it );
+      selected( aList, *it, sole );
 
       QList<Handle(SALOME_InteractiveObject)> typeSelList;
 
       for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr ) {
-       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
-       if ( !owner )
-         continue;
+        const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
+        if ( !owner )
+          continue;
 
-       if ( !aSet.contains( owner->entry() ) ) {
-         selList.append( owner->IO() );
-         aSet.insert( owner->entry() );
-       }
+        if ( !aSet.contains( owner->entry() ) && !owner->IO().IsNull() ) {
+          selList.append( owner->IO() );
+          aSet.insert( owner->entry() );
+        }
 
-       typeSelList.append( owner->IO() );
+        typeSelList.append( owner->IO() );
       }
 
-      if ( isSelectionCacheEnabled() ) {
-       LightApp_SelectionMgr* that = (LightApp_SelectionMgr*)this;
-       that->myCacheSelection.insert( *it, typeSelList );
-       that->myCacheTimes.insert( *it, QTime::currentTime() );
+      if ( !sole && isSelectionCacheEnabled() ) {
+        LightApp_SelectionMgr* that = (LightApp_SelectionMgr*)this;
+        that->myCacheSelection.insert( *it, typeSelList );
+        that->myCacheTimes.insert( *it, QTime::currentTime() );
       }
     }
   }
@@ -141,11 +153,11 @@ void LightApp_SelectionMgr::selectedObjects( SALOME_ListIO& theList, const QStri
       QString refEntry = study->referencedToEntry( entry );
       if ( !entrySet.contains( refEntry ) ) {
         if ( refEntry != entry ) {
-         entry = refEntry;
+          entry = refEntry;
           QString component = study->componentDataType( entry );
-          theList.Append( new SALOME_InteractiveObject( (const char*)entry.toLatin1(),
-                                                       (const char*)component.toLatin1(),
-                                                       ""/*refobj->Name().c_str()*/ ) );
+          theList.Append( new SALOME_InteractiveObject( (const char*)entry.toUtf8(),
+                                                        (const char*)component.toLatin1(),
+                                                        ""/*refobj->Name().c_str()*/ ) );
         }
         else if ( !io.IsNull() )
           theList.Append( io );
@@ -156,6 +168,9 @@ void LightApp_SelectionMgr::selectedObjects( SALOME_ListIO& theList, const QStri
 
     entrySet.insert( entry );
   }
+
+  if ( sole && theList.Extent() > 1 )
+    theList.Clear();
 }
 
 /*!
@@ -205,22 +220,22 @@ void LightApp_SelectionMgr::selectedObjects( QStringList& theList, const QString
       QStringList typeSelList;
 
       for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr ) {
-       const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
-       if ( !owner )
-         continue;
+        const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
+        if ( !owner )
+          continue;
 
-        if ( !aSet.contains( owner->entry() ) && !owner->IO().IsNull() ) {
-         selList.append( owner->entry() );
-         aSet.insert( owner->entry() );
-       }
+        if ( !aSet.contains( owner->entry() ) ) {
+          selList.append( owner->entry() );
+          aSet.insert( owner->entry() );
+        }
 
-       typeSelList.append( owner->entry() );
+        typeSelList.append( owner->entry() );
       }
 
       if ( isSelectionCacheEnabled() ) {
-       LightApp_SelectionMgr* that = (LightApp_SelectionMgr*)this;
-       that->myCacheSelection.insert( *it, typeSelList );
-       that->myCacheTimes.insert( *it, QTime::currentTime() );
+        LightApp_SelectionMgr* that = (LightApp_SelectionMgr*)this;
+        that->myCacheSelection.insert( *it, typeSelList );
+        that->myCacheTimes.insert( *it, QTime::currentTime() );
       }
     }
   }
@@ -228,6 +243,20 @@ void LightApp_SelectionMgr::selectedObjects( QStringList& theList, const QString
   theList = selList;
 }
 
+/*!
+  Append selected objects.
+*/
+void LightApp_SelectionMgr::setSelectedObjects( const QStringList& lst, const bool append )
+{
+  SUIT_DataOwnerPtrList owners;
+  foreach( const QString& aValue, lst ) {
+    if ( !aValue.isNull() )
+      owners.append( new LightApp_DataOwner( aValue ) );
+  }
+
+  setSelected( owners, append );
+}
+
 #endif
 
 /*!
@@ -396,7 +425,7 @@ void LightApp_SelectionMgr::selectedSubOwners( MapEntryOfMapOfInteger& theMap )
     if ( subOwner )
     {
 //#ifndef WIN32
-      if ( !theMap.IsBound( TCollection_AsciiString(subOwner->entry().toLatin1().data()) ) )
+      if ( !theMap.IsBound( TCollection_AsciiString(subOwner->entry().toUtf8().data()) ) )
 //#else
 //      if ( !theMap.IsBound( subOwner->entry().toLatin1().data() ) )
 //#endif
@@ -413,7 +442,7 @@ void LightApp_SelectionMgr::selectedSubOwners( MapEntryOfMapOfInteger& theMap )
               anIndexes.Add( subOwner2->index() );
         }
         //
-        theMap.Bind( subOwner->entry().toLatin1().data(), anIndexes );
+        theMap.Bind( subOwner->entry().toUtf8().data(), anIndexes );
       }
     }
   }
@@ -445,9 +474,9 @@ void LightApp_SelectionMgr::setSelectionCacheEnabled( bool on )
 
 #ifndef DISABLE_SALOMEOBJECT
 
-QList<Handle_SALOME_InteractiveObject> LightApp_SelectionMgr::selectionCache( const QString& type ) const
+QList<Handle(SALOME_InteractiveObject)> LightApp_SelectionMgr::selectionCache( const QString& type ) const
 {
-  QList<Handle_SALOME_InteractiveObject> res;
+  QList<Handle(SALOME_InteractiveObject)> res;
 
   QStringList types;
   if ( !type.isEmpty() )
@@ -460,10 +489,10 @@ QList<Handle_SALOME_InteractiveObject> LightApp_SelectionMgr::selectionCache( co
     if ( myCacheSelection.contains( *it ) ) {
       const SelList& lst = myCacheSelection[*it];
       for ( SelList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr ) {
-       if ( !(*itr).IsNull() && !set.contains( (*itr)->getEntry() ) ) {
-         res.append( *itr );
-         set.insert( (*itr)->getEntry() );
-       }
+        if ( !(*itr).IsNull() && !set.contains( (*itr)->getEntry() ) ) {
+          res.append( *itr );
+          set.insert( (*itr)->getEntry() );
+        }
       }
     }
   }
@@ -487,10 +516,10 @@ QStringList LightApp_SelectionMgr::selectionCache( const QString& type ) const
     if ( myCacheSelection.contains( *it ) ) {
       const SelList& lst = myCacheSelection[*it];
       for ( SelList::const_iterator itr = lst.begin(); itr != lst.end(); ++itr ) {
-       if ( !set.contains( *itr ) ) {
-         res.append( *itr );
-         set.insert( *itr );
-       }
+        if ( !set.contains( *itr ) ) {
+          res.append( *itr );
+          set.insert( *itr );
+        }
       }
     }
   }