Salome HOME
lots 3,8 - corrections
[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 #include "HYDROGUI_Shape.h"
26
27 #include <AIS_ListOfInteractive.hxx>
28 #include <AIS_ListIteratorOfListOfInteractive.hxx>
29
30 #include <LightApp_DataOwner.h>
31
32 HYDROGUI_OCCSelector::HYDROGUI_OCCSelector( HYDROGUI_Module*   theModule,
33                                             OCCViewer_Viewer*  theViewer,
34                                             SUIT_SelectionMgr* theSelMgr )
35 : LightApp_OCCSelector( theViewer, theSelMgr ),
36   myModule( theModule )
37 {
38 }
39
40 HYDROGUI_OCCSelector::~HYDROGUI_OCCSelector()
41 {
42 }
43
44 void HYDROGUI_OCCSelector::getSelection( SUIT_DataOwnerPtrList& aList ) const
45 {
46   OCCViewer_Viewer* aViewer = viewer();
47   if ( !aViewer )
48     return;
49
50   Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
51   bool isLocalContext = aContext->HasOpenedContext();
52
53   AIS_ListOfInteractive aSelList;
54   aViewer->getSelectedObjects( aSelList );
55   for ( AIS_ListIteratorOfListOfInteractive anIt( aSelList ); anIt.More(); anIt.Next() )
56     if ( !anIt.Value().IsNull() )
57     {
58       if ( !isLocalContext ) {
59         QString anEntry = entry( anIt.Value() );
60         if ( !anEntry.isEmpty() ) {
61           aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( entry( anIt.Value() ) ) ) );
62         } else {
63           aList.append( SUIT_DataOwnerPtr( new HYDROGUI_DataOwner( anIt.Value() ) ) );
64         }
65       }
66     }
67   // add externally selected objects
68   SUIT_DataOwnerPtrList::const_iterator anExtIter;
69   for(anExtIter = mySelectedExternals.begin(); anExtIter != mySelectedExternals.end(); anExtIter++) {
70     aList.append(*anExtIter);
71   }
72 }
73
74 void HYDROGUI_OCCSelector::setSelection( const SUIT_DataOwnerPtrList& aList )
75 {
76   OCCViewer_Viewer* aViewer = viewer();
77   if ( !aViewer )
78     return;
79
80   int aViewerId = (size_t)aViewer;
81
82   Handle(AIS_InteractiveContext) aContext = aViewer->getAISContext();
83   if ( aContext.IsNull() || aContext->HasOpenedContext() ) {
84     return;
85   }
86
87   AIS_ListOfInteractive aSelList;
88
89   /*QMap<QString, Handle(AIS_InteractiveObject)> aDisplayed;
90   AIS_ListOfInteractive aDispList;
91   aContext->DisplayedObjects( aDispList );
92
93   for ( AIS_ListIteratorOfListOfInteractive it( aDispList ); it.More(); it.Next() )
94   {
95     QString entryStr = entry( it.Value() );
96     if ( !entryStr.isEmpty() )
97       aDisplayed.insert( entryStr, it.Value() );
98   }*/
99   
100   mySelectedExternals.clear();
101
102   for ( SUIT_DataOwnerPtrList::const_iterator itr = aList.begin(); itr != aList.end(); ++itr )
103   {
104     const LightApp_DataOwner* owner = dynamic_cast<const LightApp_DataOwner*>( (*itr).operator->() );
105     if ( owner )   //&& aDisplayed.contains( owner->entry() ) )
106     {
107       QString anEntry = owner->entry();
108       Handle(HYDROData_Entity) anEntity = myModule->getDataModel()->objectByEntry( anEntry );
109       if( !anEntity.IsNull() )
110       {
111         HYDROGUI_Shape* aShape = myModule->getObjectShape( aViewerId, anEntity );
112         if( aShape && aShape->isVisible() )
113         {
114           foreach( Handle(AIS_InteractiveObject) obj, aShape->getAISObjects() )
115             aSelList.Append( obj );
116         }
117       }
118     }
119     else {
120       const HYDROGUI_DataOwner* hydroOwner = dynamic_cast<const HYDROGUI_DataOwner*>( (*itr).operator->() );
121       if ( hydroOwner && !hydroOwner->IO().IsNull() ) {
122         aSelList.Append( hydroOwner->IO() );
123       } else
124         mySelectedExternals.append(*itr);
125     }
126   }
127  
128   aViewer->unHighlightAll( false );
129   aViewer->setObjectsSelected( aSelList );
130 }
131
132 QString HYDROGUI_OCCSelector::entry( const Handle(AIS_InteractiveObject)& anAIS ) const
133 {
134   QString aRes;
135
136   if ( anAIS.IsNull() || !anAIS->HasOwner() )
137     return aRes;
138
139   Handle(HYDROData_Entity) anObj = 
140     Handle(HYDROData_Entity)::DownCast( anAIS->GetOwner() );
141   if ( !anObj.IsNull() )
142     aRes = HYDROGUI_DataObject::dataObjectEntry( anObj );
143
144   return aRes;
145 }