Salome HOME
Copyrights update
[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/
18 //
19 #include "LightApp_GLSelector.h"
20
21 #include "LightApp_DataOwner.h"
22
23 #include <SALOME_GLOwner.h>
24
25 #include <GLViewer_Context.h>
26
27 /*!Constructor. Initialize by GLViewer_Viewer2d and SUIT_SelectionMgr.*/
28 LightApp_GLSelector::LightApp_GLSelector( GLViewer_Viewer2d* viewer, SUIT_SelectionMgr* mgr )
29 : SUIT_Selector( mgr, viewer ),
30   myViewer( viewer )
31 {
32   if ( myViewer )
33     connect( myViewer, SIGNAL( selectionChanged( SelectionChangeStatus ) ), 
34              this, SLOT( onSelectionChanged() ) );
35 }
36
37 /*!Destructor. Do nothing.*/
38 LightApp_GLSelector::~LightApp_GLSelector()
39 {
40 }
41
42 /*!Gets viewer*/
43 GLViewer_Viewer2d* LightApp_GLSelector::viewer() const
44 {
45   return myViewer;
46 }
47
48 /*!On selection changed event.*/
49 void LightApp_GLSelector::onSelectionChanged()
50 {
51   selectionChanged();
52 }
53
54 /*!Gets list of selected Data Owner objects.*/
55 void LightApp_GLSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
56 {
57   if ( !myViewer )
58     return;
59
60   GLViewer_Context* cont = myViewer->getGLContext();
61   if ( !cont )
62     return;
63
64   for ( cont->InitSelected(); cont->MoreSelected(); cont->NextSelected() )
65   {
66     GLViewer_Object* obj = cont->SelectedObject();
67     if ( obj )
68     {
69       SALOME_GLOwner* owner = dynamic_cast< SALOME_GLOwner* >( obj->owner() );
70       if( owner )
71         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( owner->entry() ) ) );
72     }
73   }
74 }
75
76 /*!Sets to selected list of Data Owner objects.*/
77 void LightApp_GLSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
78 {
79   if ( !myViewer )
80     return;
81
82   GLViewer_Context* cont = myViewer->getGLContext();
83   if ( !cont )
84     return;
85
86   QMap<QString, GLViewer_Object*> aDisplayed;
87   const ObjList& displayed = cont->getObjects();
88   for ( ObjList::const_iterator it = displayed.begin(); it != displayed.end(); ++it )
89   {
90     GLViewer_Object* obj = *it;
91     if ( obj && obj->getVisible() )
92     {
93       SALOME_GLOwner* owner = dynamic_cast< SALOME_GLOwner* >( obj->owner() );
94       if ( owner )
95         aDisplayed.insert( owner->entry(), obj );
96     }
97   }
98
99   int Nb = 0;
100   cont->clearSelected( false );
101   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
102   {
103     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
104
105     if ( !owner )
106       continue;
107
108     if ( aDisplayed.contains( owner->entry() ) )
109     {
110       cont->setSelected( aDisplayed[owner->entry()], false );
111       Nb++;
112     }
113   }
114
115   if ( Nb > 0 )
116     myViewer->updateAll();
117 }