Salome HOME
847636f4be9f1c9cbc2d4ee271664907156ceef1
[modules/gui.git] / src / LightApp / LightApp_OCCSelector.cxx
1 // Copyright (C) 2007-2023  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_DataOwner.h"
24 #include "LightApp_OCCSelector.h"
25
26 #ifndef DISABLE_SALOMEOBJECT
27   #include <SALOME_InteractiveObject.hxx>
28 #endif
29 #include <AIS_ListOfInteractive.hxx>
30 #include <AIS_ListIteratorOfListOfInteractive.hxx>
31
32 /*!
33   Constructor
34 */
35 #ifndef DISABLE_OCCVIEWER
36 LightApp_OCCSelector::LightApp_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
37 : SUIT_Selector( mgr, viewer ),
38   myViewer( viewer )
39 {
40   if ( myViewer ) {
41     connect( myViewer, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
42     connect( myViewer, SIGNAL( deselection() ), this, SLOT( onDeselection() ) );
43   }
44 }
45 #else
46 LightApp_OCCSelector::LightApp_OCCSelector(  SUIT_SelectionMgr* mgr )
47 : SUIT_Selector( mgr )
48 {}
49 #endif
50
51 /*!
52   Destructor.
53 */
54 LightApp_OCCSelector::~LightApp_OCCSelector()
55 {
56 }
57
58 /*!
59   Gets viewer.
60 */
61 #ifndef DISABLE_OCCVIEWER
62 OCCViewer_Viewer* LightApp_OCCSelector::viewer() const
63 {
64   return myViewer;
65 }
66 #endif
67
68
69 /*!On selection changed.*/
70 void LightApp_OCCSelector::onSelectionChanged()
71 {
72   selectionChanged();
73 }
74
75 /*!On selection cleared.*/
76 void LightApp_OCCSelector::onDeselection()
77 {
78   mySelectedExternals.clear();
79 }
80
81 /*!Gets selection list.*/
82 void LightApp_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
83 {
84 #ifndef DISABLE_OCCVIEWER
85   if ( !myViewer )
86     return;
87
88   if ( !myViewer->isSelectionEnabled() )
89     return;
90
91   AIS_ListOfInteractive aSelList;
92   myViewer->getSelectedObjects( aSelList );
93   for ( AIS_ListIteratorOfListOfInteractive anIt( aSelList ); anIt.More(); anIt.Next() )
94     if ( !anIt.Value().IsNull() )
95     {
96 #ifndef DISABLE_SALOMEOBJECT
97       Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast(anIt.Value()->GetOwner());
98       if( !anObj.IsNull() )
99         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( anObj ) ) );
100 #else
101       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( entry( anIt.Value() ) ) ) );
102 #endif
103     }
104   // add externally selected objects
105   SUIT_DataOwnerPtrList::const_iterator anExtIter;
106   for(anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++) {
107     aList.append(*anExtIter);
108   }
109 #endif
110 }
111
112 /*!Sets selection list.*/
113 void LightApp_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
114 {
115 #ifndef DISABLE_OCCVIEWER
116   if ( !myViewer )
117     return;
118
119   QMap<QString, Handle(AIS_InteractiveObject)> aDisplayed;
120   Handle(AIS_InteractiveContext) aContext = myViewer->getAISContext();
121   if ( aContext.IsNull() )
122     return;
123     
124   AIS_ListOfInteractive aDispList, aSelList;
125   aContext->DisplayedObjects( aDispList );
126
127   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
128   {
129     QString entryStr = entry( it.Value() );
130     if ( !entryStr.isEmpty() )
131       aDisplayed.insert( entryStr, it.Value() );
132   }
133   
134   mySelectedExternals.clear();
135
136   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
137   {
138     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
139     if ( owner && aDisplayed.contains( owner->entry() ) )
140       aSelList.Append( aDisplayed[owner->entry()] );
141     else
142       mySelectedExternals.append(*itr);
143   }
144
145   myViewer->unHighlightAll( false );
146   myViewer->setObjectsSelected( aSelList );
147 #endif
148 }
149
150 #ifndef DISABLE_OCCVIEWER
151 /*!Gets entry ob object.*/
152 QString LightApp_OCCSelector::entry( const Handle(AIS_InteractiveObject)& anAIS ) const
153 {
154   if ( anAIS.IsNull() || !anAIS->HasOwner() )
155     return QString();
156
157   QString res;
158
159 #ifndef DISABLE_SALOMEOBJECT
160   Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast(anAIS->GetOwner());
161   if ( !anObj.IsNull() )
162     res = QString( anObj->getEntry() );
163 #endif
164
165   return res;
166 }
167 #endif