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