Salome HOME
Merge from V6_main 01/04/2013
[modules/gui.git] / src / LightApp / LightApp_OCCSelector.cxx
1 // Copyright (C) 2007-2013  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
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   AIS_ListOfInteractive aSelList;
89   myViewer->getSelectedObjects( aSelList );
90   for ( AIS_ListIteratorOfListOfInteractive anIt( aSelList ); anIt.More(); anIt.Next() )
91     if ( !anIt.Value().IsNull() )
92     {
93 #ifndef DISABLE_SALOMEOBJECT
94       Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast(anIt.Value()->GetOwner());
95       if( !anObj.IsNull() )
96         aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( anObj ) ) );
97 #else
98       aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( entry( anIt.Value() ) ) ) );
99 #endif
100     }
101   // add externally selected objects
102   SUIT_DataOwnerPtrList::const_iterator anExtIter;
103   for(anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++) {
104     aList.append(*anExtIter);
105   }
106 #endif
107 }
108
109 /*!Sets selection list.*/
110 void LightApp_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
111 {
112 #ifndef DISABLE_OCCVIEWER
113   if ( !myViewer )
114     return;
115
116   QMap<QString, Handle(AIS_InteractiveObject)> aDisplayed;
117   Handle(AIS_InteractiveContext) aContext = myViewer->getAISContext();
118   if ( aContext.IsNull() )
119     return;
120     
121   AIS_ListOfInteractive aDispList, aSelList;
122   aContext->DisplayedObjects( aDispList );
123
124   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
125   {
126     QString entryStr = entry( it.Value() );
127     if ( !entryStr.isEmpty() )
128       aDisplayed.insert( entryStr, it.Value() );
129   }
130   
131   mySelectedExternals.clear();
132
133   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
134   {
135     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
136     if ( owner && aDisplayed.contains( owner->entry() ) )
137       aSelList.Append( aDisplayed[owner->entry()] );
138     else
139       mySelectedExternals.append(*itr);
140   }
141
142   myViewer->unHighlightAll( false );
143   myViewer->setObjectsSelected( aSelList );
144 #endif
145 }
146
147 #ifndef DISABLE_OCCVIEWER
148 /*!Gets entry ob object.*/
149 QString LightApp_OCCSelector::entry( const Handle(AIS_InteractiveObject)& anAIS ) const
150 {
151   if ( anAIS.IsNull() || !anAIS->HasOwner() )
152     return QString();
153
154   QString res;
155
156 #ifndef DISABLE_SALOMEOBJECT
157   Handle(SALOME_InteractiveObject) anObj = Handle(SALOME_InteractiveObject)::DownCast(anAIS->GetOwner());
158   if ( !anObj.IsNull() )
159     res = QString( anObj->getEntry() );
160 #endif
161
162   return res;
163 }
164 #endif