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