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