Salome HOME
Join modifications from BR_Dev_For_4_0 tag V4_1_1.
[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     connect( myViewer, SIGNAL( deselection() ), this, SLOT( onDeselection() ) );
39   }
40 }
41
42 /*!
43   Destructor.
44 */
45 LightApp_OCCSelector::~LightApp_OCCSelector()
46 {
47 }
48
49 /*!
50   Gets viewer.
51 */
52 OCCViewer_Viewer* LightApp_OCCSelector::viewer() const
53 {
54   return myViewer;
55 }
56
57
58 /*!On selection changed.*/
59 void LightApp_OCCSelector::onSelectionChanged()
60 {
61   selectionChanged();
62 }
63
64 /*!On selection cleared.*/
65 void LightApp_OCCSelector::onDeselection()
66 {
67   mySelectedExternals.clear();
68 }
69
70 /*!Gets selection list.*/
71 void LightApp_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
72 {
73   if ( !myViewer )
74     return;
75
76   AIS_ListOfInteractive aSelList;
77   myViewer->getSelectedObjects( aSelList );
78   for ( AIS_ListIteratorOfListOfInteractive anIt( aSelList ); anIt.More(); anIt.Next() )
79     if ( !anIt.Value().IsNull() )
80     {
81 #ifndef DISABLE_SALOMEOBJECT
82       Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast(anIt.Value()->GetOwner());
83       if( !anObj.IsNull() )
84         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( anObj ) ) );
85 #else
86       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( entry( anIt.Value() ) ) ) );
87 #endif
88     }
89   // add externally selected objects
90   SUIT_DataOwnerPtrList::const_iterator anExtIter;
91   for(anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++) {
92     aList.append(*anExtIter);
93   }
94 }
95
96 /*!Sets selection list.*/
97 void LightApp_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
98 {
99   if ( !myViewer )
100     return;
101
102   QMap<QString, Handle(AIS_InteractiveObject)> aDisplayed;
103   Handle(AIS_InteractiveContext) aContext = myViewer->getAISContext();
104   if ( aContext.IsNull() )
105     return;
106     
107   AIS_ListOfInteractive aDispList, aSelList;
108   aContext->DisplayedObjects( aDispList );
109
110   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
111   {
112     QString entryStr = entry( it.Value() );
113     if ( !entryStr.isEmpty() )
114       aDisplayed.insert( entryStr, it.Value() );
115   }
116   
117   mySelectedExternals.clear();
118
119   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
120   {
121     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
122     if ( owner && aDisplayed.contains( owner->entry() ) )
123       aSelList.Append( aDisplayed[owner->entry()] );
124     else
125       mySelectedExternals.append(*itr);
126   }
127
128   myViewer->unHighlightAll( false );
129   myViewer->setObjectsSelected( aSelList );
130 }
131
132 /*!Gets entry ob object.*/
133 QString LightApp_OCCSelector::entry( const Handle(AIS_InteractiveObject)& anAIS ) const
134 {
135   if ( anAIS.IsNull() || !anAIS->HasOwner() )
136     return QString::null;
137
138   QString res;
139
140 #ifndef DISABLE_SALOMEOBJECT
141   Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast(anAIS->GetOwner());
142   if ( !anObj.IsNull() )
143     res = QString( anObj->getEntry() );
144 #endif
145
146   return res;
147 }