Back to index

salome-gui  6.5.0
LightApp_OBSelector.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 // File   : LightApp_OBSelector.cxx
00024 // Author :
00025 //
00026 #include "LightApp_OBSelector.h"
00027 
00028 #include "LightApp_DataOwner.h"
00029 #include "LightApp_DataObject.h"
00030 #include "LightApp_Application.h"
00031 #include <SUIT_DataBrowser.h>
00032 #include <SUIT_Session.h>
00033 #include <SUIT_DataObjectIterator.h>
00034 #include <QTime>
00035 #include <time.h>
00036 
00047 LightApp_OBSelector::LightApp_OBSelector( SUIT_DataBrowser* ob, SUIT_SelectionMgr* mgr )
00048 : SUIT_Selector( mgr, ob ),
00049   myBrowser( ob )
00050 {
00051   if ( myBrowser ) {
00052     connect( myBrowser, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
00053   }
00054   setModified();
00055 }
00056 
00060 LightApp_OBSelector::~LightApp_OBSelector()
00061 {
00062 }
00063 
00068 SUIT_DataBrowser* LightApp_OBSelector::browser() const
00069 {
00070   return myBrowser;
00071 }
00072 
00077 QString LightApp_OBSelector::type() const
00078 { 
00079   return "ObjectBrowser"; 
00080 }
00081 
00086 unsigned long LightApp_OBSelector::getModifiedTime() const
00087 {
00088   return myModifiedTime;
00089 }
00090 
00094 void LightApp_OBSelector::setModified()
00095 {
00096   myModifiedTime = clock();
00097 }
00098 
00102 void LightApp_OBSelector::onSelectionChanged()
00103 {
00104   QTime t1 = QTime::currentTime();
00105   mySelectedList.clear();
00106   selectionChanged();
00107   QTime t2 = QTime::currentTime();
00108   qDebug( QString( "selection time = %1 msecs" ).arg( t1.msecsTo( t2 ) ).toLatin1().constData() );
00109 }
00110 
00115 void LightApp_OBSelector::getSelection( SUIT_DataOwnerPtrList& theList ) const
00116 {
00117   if ( mySelectedList.count() == 0 ) {
00118     SUIT_Session* session = SUIT_Session::session();
00119     SUIT_Application* sapp = session ? session->activeApplication() : 0;
00120     LightApp_Application* app = dynamic_cast<LightApp_Application*>( sapp );
00121     if( !app || !myBrowser )
00122       return;
00123 
00124     DataObjectList objlist;
00125     myBrowser->getSelected( objlist );
00126     LightApp_OBSelector* that = (LightApp_OBSelector*)this;
00127     QListIterator<SUIT_DataObject*> it( objlist );
00128     while ( it.hasNext() ) {
00129       LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.next() );
00130       if ( obj && app->checkDataObject( obj) ) {
00131 #ifndef DISABLE_SALOMEOBJECT
00132         Handle(SALOME_InteractiveObject) aSObj = new SALOME_InteractiveObject
00133           ( obj->entry().toLatin1().constData(),
00134             obj->componentDataType().toLatin1().constData(),
00135             obj->name().toLatin1().constData() );
00136         LightApp_DataOwner* owner = new LightApp_DataOwner( aSObj  );
00137 #else
00138         LightApp_DataOwner* owner = new LightApp_DataOwner( obj->entry() );
00139 #endif
00140         that->mySelectedList.append( SUIT_DataOwnerPtr( owner ) );
00141       }
00142     }
00143   }
00144   theList = mySelectedList;
00145 }
00146 
00151 void LightApp_OBSelector::setSelection( const SUIT_DataOwnerPtrList& theList )
00152 {
00153   if ( !myBrowser )
00154     return;
00155 
00156   if( myEntries.count() == 0 || myModifiedTime < myBrowser->getModifiedTime() )
00157     fillEntries( myEntries );
00158 
00159   DataObjectList objList;
00160   for ( SUIT_DataOwnerPtrList::const_iterator it = theList.begin(); 
00161         it != theList.end(); ++it ) {
00162     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*it).operator->() );
00163     if ( owner && myEntries.contains( owner->entry() ) )
00164       objList.append( myEntries[owner->entry()] );
00165   }
00166 
00167   myBrowser->setSelected( objList );
00168   mySelectedList.clear();
00169 }
00170 
00175 void LightApp_OBSelector::fillEntries( QMap<QString, LightApp_DataObject*>& entries )
00176 {
00177   entries.clear();
00178 
00179   if ( !myBrowser )
00180     return;
00181 
00182   for ( SUIT_DataObjectIterator it( myBrowser->root(),
00183                                     SUIT_DataObjectIterator::DepthLeft ); it.current(); ++it ) {
00184     LightApp_DataObject* obj = dynamic_cast<LightApp_DataObject*>( it.current() );
00185     if ( obj )
00186       entries.insert( obj->entry(), obj );
00187   }
00188 
00189   setModified();
00190 }
00191