Salome HOME
Merge remote-tracking branch 'origin/BR_LAND_COVER' into BR_v14_rc
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_OCCSelector.cxx
1 // Copyright (C) 2014-2015  EDF-R&D
2 // This library is free software; you can redistribute it and/or
3 // modify it under the terms of the GNU Lesser General Public
4 // License as published by the Free Software Foundation; either
5 // version 2.1 of the License, or (at your option) any later version.
6 //
7 // This library is distributed in the hope that it will be useful,
8 // but WITHOUT ANY WARRANTY; without even the implied warranty of
9 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
10 // Lesser General Public License for more details.
11 //
12 // You should have received a copy of the GNU Lesser General Public
13 // License along with this library; if not, write to the Free Software
14 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
15 //
16 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
17 //
18
19 #include "HYDROGUI_OCCSelector.h"
20
21 #include "HYDROGUI_DataModel.h"
22 #include "HYDROGUI_DataObject.h"
23 #include "HYDROGUI_DataOwner.h"
24 #include "HYDROGUI_Module.h"
25
26 #include <AIS_ListOfInteractive.hxx>
27 #include <AIS_ListIteratorOfListOfInteractive.hxx>
28
29 #include <LightApp_DataOwner.h>
30
31 HYDROGUI_OCCSelector::HYDROGUI_OCCSelector( HYDROGUI_Module*   theModule,
32                                             OCCViewer_Viewer*  theViewer,
33                                             SUIT_SelectionMgr* theSelMgr )
34 : LightApp_OCCSelector( theViewer, theSelMgr ),
35   myModule( theModule )
36 {
37 }
38
39 HYDROGUI_OCCSelector::~HYDROGUI_OCCSelector()
40 {
41 }
42
43 void HYDROGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
44 {
45   OCCViewer_Viewer* aViewer = viewer();
46   if ( !aViewer )
47     return;
48
49   Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
50   bool isLocalContext = aContext->HasOpenedContext();
51
52   AIS_ListOfInteractive aSelList;
53   aViewer->getSelectedObjects( aSelList );
54   for ( AIS_ListIteratorOfListOfInteractive anIt( aSelList ); anIt.More(); anIt.Next() )
55     if ( !anIt.Value().IsNull() )
56     {
57       if ( !isLocalContext ) {
58         QString anEntry = entry( anIt.Value() );
59         if ( !anEntry.isEmpty() ) {
60           aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( entry( anIt.Value() ) ) ) );
61         } else {
62           aList.append( SUIT_DataOwnerPtr( new HYDROGUI_DataOwner( anIt.Value() ) ) );
63         }
64       }
65     }
66   // add externally selected objects
67   SUIT_DataOwnerPtrList::const_iterator anExtIter;
68   for(anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++) {
69     aList.append(*anExtIter);
70   }
71 }
72
73 void HYDROGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
74 {
75   OCCViewer_Viewer* aViewer = viewer();
76   if ( !aViewer )
77     return;
78
79   Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
80   if ( aContext.IsNull() || aContext->HasOpenedContext() ) {
81     return;
82   }
83
84   QMap<QString, Handle(AIS_InteractiveObject)> aDisplayed;
85   AIS_ListOfInteractive aDispList, aSelList;
86   aContext->DisplayedObjects( aDispList );
87
88   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
89   {
90     QString entryStr = entry( it.Value() );
91     if ( !entryStr.isEmpty() )
92       aDisplayed.insert( entryStr, it.Value() );
93   }
94   
95   mySelectedExternals.clear();
96
97   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
98   {
99     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
100     if ( owner && aDisplayed.contains( owner->entry() ) )
101       aSelList.Append( aDisplayed[owner->entry()] );
102     else {
103       const HYDROGUI_DataOwner* hydroOwner = dynamic_cast<const HYDROGUI_DataOwner*>( (*itr).operator->() );
104       if ( hydroOwner && !hydroOwner->IO().IsNull() ) {
105         aSelList.Append( hydroOwner->IO() );
106       } else
107         mySelectedExternals.append(*itr);
108     }
109   }
110  
111   aViewer->unHighlightAll( false );
112   aViewer->setObjectsSelected( aSelList );
113 }
114
115 QString HYDROGUI_OCCSelector::entry( const Handle(AIS_InteractiveObject)& anAIS ) const
116 {
117   QString aRes;
118
119   if ( anAIS.IsNull() || !anAIS->HasOwner() )
120     return aRes;
121
122   Handle(HYDROData_Entity) anObj = 
123     Handle(HYDROData_Entity)::DownCast( anAIS->GetOwner() );
124   if ( !anObj.IsNull() )
125     aRes = HYDROGUI_DataObject::dataObjectEntry( anObj );
126
127   return aRes;
128 }