]> SALOME platform Git repositories - modules/gui.git/blob - src/SalomeApp/SalomeApp_GLSelector.cxx
Salome HOME
New Help for KERNEL & GUI.
[modules/gui.git] / src / SalomeApp / SalomeApp_GLSelector.cxx
1 #include "SalomeApp_GLSelector.h"
2
3 #include "SalomeApp_DataOwner.h"
4
5 #include <SALOME_GLOwner.h>
6
7 #include <GLViewer_Context.h>
8
9 /*!Constructor. Initialize by GLViewer_Viewer2d and SUIT_SelectionMgr.*/
10 SalomeApp_GLSelector::SalomeApp_GLSelector( GLViewer_Viewer2d* viewer, SUIT_SelectionMgr* mgr )
11 : SUIT_Selector( mgr, viewer ),
12   myViewer( viewer )
13 {
14   if ( myViewer )
15     connect( myViewer, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
16 }
17
18 /*!Destructor. Do nothing.*/
19 SalomeApp_GLSelector::~SalomeApp_GLSelector()
20 {
21 }
22
23 /*!Gets viewer*/
24 GLViewer_Viewer2d* SalomeApp_GLSelector::viewer() const
25 {
26   return myViewer;
27 }
28
29 /*!On selection changed event.*/
30 void SalomeApp_GLSelector::onSelectionChanged()
31 {
32   selectionChanged();
33 }
34
35 /*!Gets list of selected Data Owner objects.*/
36 void SalomeApp_GLSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
37 {
38   if ( !myViewer )
39     return;
40
41   GLViewer_Context* cont = myViewer->getGLContext();
42   if ( !cont )
43     return;
44
45   for ( cont->InitSelected(); cont->MoreSelected(); cont->NextSelected() )
46   {
47     GLViewer_Object* obj = cont->SelectedObject();
48     if ( obj )
49     {
50       SALOME_GLOwner* owner = dynamic_cast< SALOME_GLOwner* >( obj->owner() );
51       if( owner )
52         aList.append( SUIT_DataOwnerPtr( new SalomeApp_DataOwner( owner->entry() ) ) );
53     }
54   }
55 }
56
57 /*!Sets to selected list of Data Owner objects.*/
58 void SalomeApp_GLSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
59 {
60   if ( !myViewer )
61     return;
62
63   GLViewer_Context* cont = myViewer->getGLContext();
64   if ( !cont )
65     return;
66
67   QMap<QString, GLViewer_Object*> aDisplayed;
68   const ObjList& displayed = cont->getObjects();
69   for ( ObjList::const_iterator it = displayed.begin(); it != displayed.end(); ++it )
70   {
71     GLViewer_Object* obj = *it;
72     if ( obj && obj->getVisible() )
73     {
74       SALOME_GLOwner* owner = dynamic_cast< SALOME_GLOwner* >( obj->owner() );
75       if ( owner )
76         aDisplayed.insert( owner->entry(), obj );
77     }
78   }
79
80   int Nb = 0;
81   cont->clearSelected( false );
82   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
83   {
84     const SalomeApp_DataOwner* owner = dynamic_cast<const SalomeApp_DataOwner*>( (*itr).operator->() );
85
86     if ( !owner )
87       continue;
88
89     if ( aDisplayed.contains( owner->entry() ) )
90     {
91       cont->setSelected( aDisplayed[owner->entry()], false );
92       Nb++;
93     }
94   }
95
96   if ( Nb > 0 )
97     myViewer->updateAll();
98 }