Back to index

salome-gui  6.5.0
LightApp_GLSelector.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 "LightApp_GLSelector.h"
00024 
00025 #include "LightApp_DataOwner.h"
00026 
00027 #include <GLViewer_Context.h>
00028 #include <GLViewer_Object.h>
00029 
00031 LightApp_GLSelector::LightApp_GLSelector( GLViewer_Viewer2d* viewer, SUIT_SelectionMgr* mgr )
00032 : SUIT_Selector( mgr, viewer ),
00033   myViewer( viewer )
00034 {
00035   if ( myViewer )
00036     connect( myViewer, SIGNAL( selectionChanged( SelectionChangeStatus ) ), 
00037              this, SLOT( onSelectionChanged() ) );
00038 }
00039 
00041 LightApp_GLSelector::~LightApp_GLSelector()
00042 {
00043 }
00044 
00046 GLViewer_Viewer2d* LightApp_GLSelector::viewer() const
00047 {
00048   return myViewer;
00049 }
00050 
00052 void LightApp_GLSelector::onSelectionChanged()
00053 {
00054   selectionChanged();
00055 }
00056 
00058 void LightApp_GLSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
00059 {
00060   if ( !myViewer )
00061     return;
00062 
00063   GLViewer_Context* cont = myViewer->getGLContext();
00064   if ( !cont )
00065     return;
00066 
00067   for ( cont->InitSelected(); cont->MoreSelected(); cont->NextSelected() )
00068   {
00069     GLViewer_Object* obj = cont->SelectedObject();
00070     if ( obj )
00071     {
00072       LightApp_DataOwner* owner = dynamic_cast<LightApp_DataOwner*>( obj->owner() );
00073       if ( owner )
00074         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( owner->entry() ) ) );
00075     }
00076   }
00077 }
00078 
00080 void LightApp_GLSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
00081 {
00082   if ( !myViewer )
00083     return;
00084 
00085   GLViewer_Context* cont = myViewer->getGLContext();
00086   if ( !cont )
00087     return;
00088 
00089   QMap<QString, int> aSelected;
00090   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
00091   {
00092     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
00093 
00094     if ( owner )
00095       aSelected.insert( owner->entry(), 0 );
00096   }
00097 
00098   bool changed = false;
00099   const ObjList& displayed = cont->getObjects();
00100   for ( ObjList::const_iterator it = displayed.begin(); it != displayed.end(); ++it )
00101   {
00102     GLViewer_Object* obj = *it;
00103     if ( obj && obj->getVisible() )
00104     {
00105       LightApp_DataOwner* owner = dynamic_cast<LightApp_DataOwner*>( obj->owner() );
00106       bool sel = owner && aSelected.contains( owner->entry() );
00107       changed = changed || sel != (bool)obj->isSelected();
00108       if ( sel && !obj->isSelected() )
00109         cont->setSelected( obj, false );
00110       else if ( !sel && obj->isSelected() )
00111         cont->remSelected( obj, false );
00112     }
00113   }
00114 
00115   if ( changed )
00116     myViewer->updateAll();
00117 }