Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[modules/gui.git] / src / LightApp / LightApp_GLSelector.cxx
1 // Copyright (C) 2005  OPEN CASCADE, CEA/DEN, EDF R&D, PRINCIPIA R&D
2 // 
3 // This library is free software; you can redistribute it and/or
4 // modify it under the terms of the GNU Lesser General Public
5 // License as published by the Free Software Foundation; either 
6 // version 2.1 of the License.
7 // 
8 // This library is distributed in the hope that it will be useful 
9 // but WITHOUT ANY WARRANTY; without even the implied warranty of 
10 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
11 // Lesser General Public License for more details.
12 //
13 // You should have received a copy of the GNU Lesser General Public  
14 // License along with this library; if not, write to the Free Software 
15 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 //
17 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
18 //
19 #include "LightApp_GLSelector.h"
20
21 #include "LightApp_DataOwner.h"
22 #include "LightApp_DataObject.h"
23
24 #include <GLViewer_Context.h>
25
26 /*!Constructor. Initialize by GLViewer_Viewer2d and SUIT_SelectionMgr.*/
27 LightApp_GLSelector::LightApp_GLSelector( GLViewer_Viewer2d* viewer, SUIT_SelectionMgr* mgr )
28 : SUIT_Selector( mgr, viewer ),
29   myViewer( viewer )
30 {
31   if ( myViewer )
32     connect( myViewer, SIGNAL( selectionChanged( SelectionChangeStatus ) ), 
33              this, SLOT( onSelectionChanged() ) );
34 }
35
36 /*!Destructor. Do nothing.*/
37 LightApp_GLSelector::~LightApp_GLSelector()
38 {
39 }
40
41 /*!Gets viewer*/
42 GLViewer_Viewer2d* LightApp_GLSelector::viewer() const
43 {
44   return myViewer;
45 }
46
47 /*!On selection changed event.*/
48 void LightApp_GLSelector::onSelectionChanged()
49 {
50   selectionChanged();
51 }
52
53 /*!Gets list of selected Data Owner objects.*/
54 void LightApp_GLSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
55 {
56   if ( !myViewer )
57     return;
58
59   GLViewer_Context* cont = myViewer->getGLContext();
60   if ( !cont )
61     return;
62
63   for ( cont->InitSelected(); cont->MoreSelected(); cont->NextSelected() )
64   {
65     GLViewer_Object* obj = cont->SelectedObject();
66     if ( obj )
67     {
68       LightApp_DataOwner* owner = dynamic_cast<LightApp_DataOwner*>( obj->owner() );
69       if ( owner )
70         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( owner->entry() ) ) );
71     }
72   }
73 }
74
75 /*!Sets to selected list of Data Owner objects.*/
76 void LightApp_GLSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
77 {
78   if ( !myViewer )
79     return;
80
81   GLViewer_Context* cont = myViewer->getGLContext();
82   if ( !cont )
83     return;
84
85   QMap<QString, int> aSelected;
86   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
87   {
88     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
89
90     if ( owner )
91       aSelected.insert( owner->entry(), 0 );
92   }
93
94   bool changed = false;
95   const ObjList& displayed = cont->getObjects();
96   for ( ObjList::const_iterator it = displayed.begin(); it != displayed.end(); ++it )
97   {
98     GLViewer_Object* obj = *it;
99     if ( obj && obj->getVisible() )
100     {
101       LightApp_DataOwner* owner = dynamic_cast<LightApp_DataOwner*>( obj->owner() );
102       bool sel = owner && aSelected.contains( owner->entry() );
103       changed = changed || sel != (bool)obj->isSelected();
104       if ( sel && !obj->isSelected() )
105         cont->setSelected( obj, false );
106       else if ( !sel && obj->isSelected() )
107         cont->remSelected( obj, false );
108     }
109   }
110
111   if ( changed )
112     myViewer->updateAll();
113 }