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