Back to index

salome-gui  6.5.0
SUIT_SelectionMgr.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 
00023 #include "SUIT_SelectionMgr.h"
00024 
00025 #include "SUIT_Selector.h"
00026 #include "SUIT_SelectionFilter.h"
00027 
00033 SUIT_SelectionMgr::SUIT_SelectionMgr( const bool Feedback, QObject* p )
00034 : QObject( p ),
00035 myIterations( Feedback ? 1 : 0 ),
00036 myAutoDelFilter( false ),
00037 myIsSelChangeEnabled( true )
00038 {
00039 }
00040 
00042 SUIT_SelectionMgr::~SUIT_SelectionMgr()
00043 {
00044   for ( SelectorList::iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
00045     delete *it;
00046 }
00047 
00049 void SUIT_SelectionMgr::installSelector( SUIT_Selector* sel )
00050 {
00051   if ( sel && !mySelectors.contains( sel ) )
00052     mySelectors.append( sel );
00053 }
00054 
00056 void SUIT_SelectionMgr::removeSelector( SUIT_Selector* sel )
00057 {
00058   mySelectors.removeAll( sel );
00059 }
00060 
00062 void SUIT_SelectionMgr::selectors( QList<SUIT_Selector*>& lst ) const
00063 {
00064   lst.clear();
00065   for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
00066     lst.append( *it );
00067 }
00068 
00070 void SUIT_SelectionMgr::selectors( const QString& typ, QList<SUIT_Selector*>& lst ) const
00071 {
00072   lst.clear();
00073   for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
00074   {
00075     if ( (*it)->type() == typ )
00076       lst.append( *it );
00077   }
00078 }
00079 
00082 void SUIT_SelectionMgr::setEnabled( const bool on, const QString& typ )
00083 {
00084   for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
00085   {
00086     if ( typ.isEmpty() || (*it)->type() == typ )
00087       (*it)->setEnabled( on );
00088   }
00089 }
00090 
00093 void SUIT_SelectionMgr::selected( SUIT_DataOwnerPtrList& lst, const QString& type ) const
00094 {
00095   lst.clear();
00096 
00097   for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
00098   {
00099     if ( !(*it)->isEnabled() )
00100       continue;
00101     if ( !type.isEmpty() && (*it)->type() != type )
00102       continue;
00103 
00104     SUIT_DataOwnerPtrList curList;
00105     (*it)->selected( curList );
00106     for ( SUIT_DataOwnerPtrList::const_iterator itr = curList.begin(); itr != curList.end(); ++itr )
00107       lst.append( *itr );
00108   }
00109 }
00110 
00113 void SUIT_SelectionMgr::setSelected( const SUIT_DataOwnerPtrList& lst, const bool append )
00114 {
00115   SUIT_DataOwnerPtrList owners;
00116   filterOwners( lst, owners );
00117 
00118   for ( SelectorList::const_iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
00119   {
00120     if ( append )
00121     {
00122       SUIT_DataOwnerPtrList current;
00123       (*it)->selected( current );
00124       for ( SUIT_DataOwnerPtrList::const_iterator it = current.begin(); it != current.end(); ++it )
00125         owners.append( *it );
00126     }
00127     (*it)->setSelected( owners );
00128   }
00129 }
00130 
00133 void SUIT_SelectionMgr::clearSelected()
00134 {
00135   setSelected( SUIT_DataOwnerPtrList() );
00136 }
00137 
00140 void SUIT_SelectionMgr::selectionChanged( SUIT_Selector* sel )
00141 {
00142   if ( !sel || !myIsSelChangeEnabled || !sel->isEnabled() )
00143     return;
00144 
00145   SUIT_DataOwnerPtrList owners;
00146 
00147   myIsSelChangeEnabled = false;
00148   sel->selected( owners );
00149 
00150   SUIT_DataOwnerPtrList newOwners;
00151   filterOwners( owners, newOwners );
00152 
00153   for ( int i = 0; i < myIterations; i++ )
00154   {
00155     for ( SelectorList::iterator it = mySelectors.begin(); it != mySelectors.end(); ++it )
00156     {
00157       // Temporary action(to avoid selection of the objects which don't pass the filters):
00158       //if ( *it != sel )
00159         (*it)->setSelected( newOwners );
00160     }
00161   }
00162   myIsSelChangeEnabled = true;
00163 
00164   emit selectionChanged();
00165 }
00166 
00171 bool SUIT_SelectionMgr::isSynchronizing() const
00172 {
00173   return !myIsSelChangeEnabled;
00174 }
00175 
00178 bool SUIT_SelectionMgr::hasSelectionMode( const int mode ) const
00179 {
00180   return mySelModes.contains( mode );
00181 }
00182 
00185 void SUIT_SelectionMgr::selectionModes( QList<int>& vals ) const
00186 {
00187   vals = mySelModes;
00188 }
00189 
00192 void SUIT_SelectionMgr::setSelectionModes( const int mode )
00193 {
00194   QList<int> lst;
00195   lst.append( mode );
00196   setSelectionModes( lst );
00197 }
00198 
00201 void SUIT_SelectionMgr::setSelectionModes( const QList<int>& lst )
00202 {
00203   mySelModes = lst;
00204 }
00205 
00208 void SUIT_SelectionMgr::appendSelectionModes( const int mode )
00209 {
00210   QList<int> lst;
00211   lst.append( mode );
00212   appendSelectionModes( lst );
00213 }
00214 
00217 void SUIT_SelectionMgr::appendSelectionModes( const QList<int>& lst )
00218 {
00219   QMap<int, int> map;
00220   for ( QList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
00221     map.insert( *it, 0 );
00222 
00223   for ( QList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
00224   {
00225     if ( !map.contains( *itr ) )
00226       mySelModes.append( *itr );
00227   }
00228 }
00229 
00232 void SUIT_SelectionMgr::removeSelectionModes( const int mode )
00233 {
00234   QList<int> lst;
00235   lst.append( mode );
00236   removeSelectionModes( lst );
00237 }
00238 
00241 void SUIT_SelectionMgr::removeSelectionModes( const QList<int>& lst )
00242 {
00243   QMap<int, int> map;
00244   for ( QList<int>::const_iterator it = mySelModes.begin(); it != mySelModes.end(); ++it )
00245     map.insert( *it, 0 );
00246 
00247   for ( QList<int>::const_iterator itr = lst.begin(); itr != lst.end(); ++itr )
00248     map.remove( *itr );
00249 
00250   mySelModes.clear();
00251   for ( QMap<int, int>::ConstIterator iter = map.begin(); iter != map.end(); ++iter )
00252     mySelModes.append( iter.key() );
00253 }
00254 
00257 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwner* owner ) const
00258 {
00259   if ( !owner )
00260     return false;
00261 
00262   bool ok = true;
00263   for ( SelFilterList::const_iterator it = myFilters.begin(); it != myFilters.end() && ok; ++it )
00264     ok = (*it)->isOk( owner );
00265 
00266   return ok;
00267 }
00268 
00271 bool SUIT_SelectionMgr::isOk( const SUIT_DataOwnerPtr& ptr ) const
00272 {
00273   if ( ptr.isNull() )
00274     return false;
00275 
00276   return isOk( ptr.operator->() );
00277 }
00278 
00281 bool SUIT_SelectionMgr::hasFilter( SUIT_SelectionFilter* f ) const
00282 {
00283   return myFilters.contains( f );
00284 }
00285 
00288 void SUIT_SelectionMgr::installFilter( SUIT_SelectionFilter* f, const bool updateSelection )
00289 {
00290   if ( !hasFilter( f ) )
00291   {
00292     SUIT_DataOwnerPtrList selOwners;
00293     if( updateSelection )
00294       selected( selOwners );
00295       
00296     myFilters.append( f );
00297     
00298     if( updateSelection )
00299       setSelected( selOwners );
00300   }
00301 }
00302 
00305 void SUIT_SelectionMgr::removeFilter( SUIT_SelectionFilter* f )
00306 {
00307   if ( !myFilters.contains( f ) )
00308     return;
00309 
00310   myFilters.removeAll( f );
00311 
00312   if ( autoDeleteFilter() )
00313     delete f;
00314 }
00315 
00318 void SUIT_SelectionMgr::clearFilters()
00319 {
00320   if ( autoDeleteFilter() )
00321   {
00322     for ( SelFilterList::const_iterator it = myFilters.begin(); it != myFilters.end(); ++it )
00323       delete *it;
00324   }
00325 
00326   myFilters.clear();
00327 }
00328 
00331 bool SUIT_SelectionMgr::autoDeleteFilter() const
00332 {
00333   return myAutoDelFilter;
00334 }
00335 
00338 void SUIT_SelectionMgr::setAutoDeleteFilter( const bool on )
00339 {
00340   myAutoDelFilter = on;
00341 }
00342 
00345 void SUIT_SelectionMgr::filterOwners( const SUIT_DataOwnerPtrList& in, SUIT_DataOwnerPtrList& out ) const
00346 {
00347   out.clear();
00348   for ( SUIT_DataOwnerPtrList::const_iterator it = in.begin(); it != in.end(); ++it )
00349   {
00350     if ( isOk( *it ) )
00351       out.append( *it );
00352   }
00353 }