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