Salome HOME
Copyrights update
[modules/gui.git] / src / LightApp / LightApp_OCCSelector.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
20 #include "LightApp_DataOwner.h"
21 #include "LightApp_OCCSelector.h"
22
23 #include <SALOME_InteractiveObject.hxx>
24
25 #include <AIS_ListOfInteractive.hxx>
26 #include <AIS_ListIteratorOfListOfInteractive.hxx>
27
28 /*!
29   Constructor
30 */
31 LightApp_OCCSelector::LightApp_OCCSelector( OCCViewer_Viewer* viewer, SUIT_SelectionMgr* mgr )
32 : SUIT_Selector( mgr, viewer ),
33   myViewer( viewer )
34 {
35   if ( myViewer )
36     connect( myViewer, SIGNAL( selectionChanged() ), this, SLOT( onSelectionChanged() ) );
37 }
38
39 /*!
40   Destructor.
41 */
42 LightApp_OCCSelector::~LightApp_OCCSelector()
43 {
44 }
45
46 /*!
47   Gets viewer.
48 */
49 OCCViewer_Viewer* LightApp_OCCSelector::viewer() const
50 {
51   return myViewer;
52 }
53
54 /*!On selection changed.*/
55 void LightApp_OCCSelector::onSelectionChanged()
56 {
57   selectionChanged();
58 }
59
60 /*!Gets selection list.*/
61 void LightApp_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
62 {
63   if ( !myViewer )
64     return;
65
66   AIS_ListOfInteractive aSelList;
67   myViewer->getSelectedObjects( aSelList );
68   for ( AIS_ListIteratorOfListOfInteractive anIt( aSelList ); anIt.More(); anIt.Next() )
69     if ( !anIt.Value().IsNull() )
70     {
71       Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast(anIt.Value()->GetOwner());
72       if( !anObj.IsNull() )
73         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( anObj ) ) );
74     }
75 }
76
77 /*!Sets selection list.*/
78 void LightApp_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
79 {
80   if ( !myViewer )
81     return;
82
83   QMap<QString, Handle(AIS_InteractiveObject)> aDisplayed;
84   Handle(AIS_InteractiveContext) aContext = myViewer->getAISContext();
85   if ( aContext.IsNull() )
86     return;
87     
88   AIS_ListOfInteractive aDispList, aSelList;
89   aContext->DisplayedObjects( aDispList );
90
91   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
92   {
93     QString entryStr = entry( it.Value() );
94     if ( !entryStr.isEmpty() )
95       aDisplayed.insert( entryStr, it.Value() );
96   }
97
98   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
99   {
100     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
101     if ( owner && aDisplayed.contains( owner->entry() ) )
102       aSelList.Append( aDisplayed[owner->entry()] );
103   }
104
105   myViewer->unHighlightAll( false );
106   myViewer->setObjectsSelected( aSelList );
107 }
108
109 /*!Gets entry ob object.*/
110 QString LightApp_OCCSelector::entry( const Handle(AIS_InteractiveObject)& anAIS ) const
111 {
112   if ( anAIS.IsNull() || !anAIS->HasOwner() )
113     return QString::null;
114
115   Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast(anAIS->GetOwner());
116
117   QString res;
118   if ( !anObj.IsNull() )
119     res = QString( anObj->getEntry() );
120   return res;
121 }