0030687: Visualization - remove redundant interfaces SelectBasics_EntityOwner and...
[occt.git] / src / MeshVS / MeshVS_MeshOwner.cxx
1 // Created on: 2007-01-25
2 // Created by: Sergey KOCHETKOV
3 // Copyright (c) 2007-2014 OPEN CASCADE SAS
4 //
5 // This file is part of Open CASCADE Technology software library.
6 //
7 // This library is free software; you can redistribute it and/or modify it under
8 // the terms of the GNU Lesser General Public License version 2.1 as published
9 // by the Free Software Foundation, with special exception defined in the file
10 // OCCT_LGPL_EXCEPTION.txt. Consult the file LICENSE_LGPL_21.txt included in OCCT
11 // distribution for complete text of the license and disclaimer of any warranty.
12 //
13 // Alternatively, this file may be used under the terms of Open CASCADE
14 // commercial license or contractual agreement.
15
16
17 #include <MeshVS_DataSource.hxx>
18 #include <MeshVS_Mesh.hxx>
19 #include <MeshVS_MeshOwner.hxx>
20 #include <PrsMgr_PresentationManager.hxx>
21 #include <PrsMgr_PresentationManager3d.hxx>
22 #include <SelectMgr_SelectableObject.hxx>
23 #include <Standard_Type.hxx>
24 #include <TColStd_HPackedMapOfInteger.hxx>
25 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
26
27 IMPLEMENT_STANDARD_RTTIEXT(MeshVS_MeshOwner,SelectMgr_EntityOwner)
28
29 #ifndef MeshVS_PRSBUILDERHXX
30 #include <MeshVS_PrsBuilder.hxx>
31 #endif
32
33
34 //================================================================
35 // Function : Constructor MeshVS_MeshOwner
36 // Purpose  :
37 //================================================================
38 MeshVS_MeshOwner::MeshVS_MeshOwner (const SelectMgr_SOPtr&           theSelObj,
39                                     const Handle(MeshVS_DataSource)& theDS,
40                                     const Standard_Integer           thePriority)
41 : SelectMgr_EntityOwner ( theSelObj, thePriority )
42 {
43   myLastID = -1;
44   if( !theDS.IsNull() )
45     myDataSource = theDS;
46 }
47
48 //================================================================
49 // Function : GetDataSource
50 // Purpose  :
51 //================================================================
52 const Handle(MeshVS_DataSource)& MeshVS_MeshOwner::GetDataSource () const
53 {
54   return myDataSource;
55 }
56
57 //================================================================
58 // Function : GetSelectedNodes
59 // Purpose  :
60 //================================================================
61 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_MeshOwner::GetSelectedNodes () const
62 {
63   return mySelectedNodes;
64 }
65
66 //================================================================
67 // Function : GetSelectedElements
68 // Purpose  :
69 //================================================================
70 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_MeshOwner::GetSelectedElements () const
71 {
72   return mySelectedElems;
73 }
74
75 //================================================================
76 // Function : AddSelectedEntities
77 // Purpose  :
78 //================================================================
79 void MeshVS_MeshOwner::AddSelectedEntities (const Handle(TColStd_HPackedMapOfInteger)& Nodes,
80                                             const Handle(TColStd_HPackedMapOfInteger)& Elems)
81 {
82   if( mySelectedNodes.IsNull() )
83     mySelectedNodes = Nodes;
84   else if( !Nodes.IsNull() )
85     mySelectedNodes->ChangeMap().Unite( Nodes->Map() );
86   if( mySelectedElems.IsNull() )
87     mySelectedElems = Elems;
88   else if( !Elems.IsNull() )
89     mySelectedElems->ChangeMap().Unite( Elems->Map() );
90 }
91
92 //================================================================
93 // Function : ClearSelectedEntities
94 // Purpose  :
95 //================================================================
96 void MeshVS_MeshOwner::ClearSelectedEntities ()
97 {
98   mySelectedNodes.Nullify();
99   mySelectedElems.Nullify();
100 }
101
102 //================================================================
103 // Function : GetDetectedNodes
104 // Purpose  :
105 //================================================================
106 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_MeshOwner::GetDetectedNodes () const
107 {
108   return myDetectedNodes;
109 }
110
111 //================================================================
112 // Function : GetDetectedElements
113 // Purpose  :
114 //================================================================
115 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_MeshOwner::GetDetectedElements () const
116 {
117   return myDetectedElems;
118 }
119
120 //================================================================
121 // Function : SetDetectedEntities
122 // Purpose  :
123 //================================================================
124 void MeshVS_MeshOwner::SetDetectedEntities (const Handle(TColStd_HPackedMapOfInteger)& Nodes,
125                                             const Handle(TColStd_HPackedMapOfInteger)& Elems)
126 {
127   myDetectedNodes = Nodes;
128   myDetectedElems = Elems;
129   if (IsSelected()) SetSelected (Standard_False);
130 }
131
132 //================================================================
133 // Function : HilightWithColor
134 // Purpose  :
135 //================================================================
136 void MeshVS_MeshOwner::HilightWithColor (const Handle(PrsMgr_PresentationManager3d)& thePM,
137                                          const Handle(Prs3d_Drawer)& theStyle,
138                                          const Standard_Integer /*theMode*/)
139 {
140   Handle( SelectMgr_SelectableObject ) aSelObj;
141   if ( HasSelectable() )
142     aSelObj = Selectable();
143
144   if ( thePM->IsImmediateModeOn() && aSelObj->IsKind( STANDARD_TYPE( MeshVS_Mesh ) ) )
145   {
146     // Update last detected entity ID
147     Handle(TColStd_HPackedMapOfInteger) aNodes = GetDetectedNodes();
148     Handle(TColStd_HPackedMapOfInteger) aElems = GetDetectedElements(); 
149     if( !aNodes.IsNull() && aNodes->Map().Extent() == 1 )
150     {
151       TColStd_MapIteratorOfPackedMapOfInteger anIt( aNodes->Map() );
152       if( myLastID != anIt.Key() )
153       {
154         myLastID = anIt.Key();
155       }
156     }
157     else if( !aElems.IsNull() && aElems->Map().Extent() == 1 )
158     {
159       TColStd_MapIteratorOfPackedMapOfInteger anIt( aElems->Map() );
160       if( myLastID != anIt.Key() )
161       {
162         myLastID = anIt.Key();
163       }
164     }
165
166     // hilight detected entities
167     Handle( MeshVS_Mesh ) aMesh = Handle( MeshVS_Mesh )::DownCast ( aSelObj );
168     aMesh->HilightOwnerWithColor ( thePM, theStyle, this );
169   }
170 }
171
172 void MeshVS_MeshOwner::Unhilight (const Handle(PrsMgr_PresentationManager)& thePM, const Standard_Integer )
173 {
174   SelectMgr_EntityOwner::Unhilight (thePM);
175
176   Handle(TColStd_HPackedMapOfInteger) aNodes = GetDetectedNodes();
177   Handle(TColStd_HPackedMapOfInteger) aElems = GetDetectedElements();  
178   if( ( !aNodes.IsNull() && !aNodes->Map().Contains( myLastID ) ) ||
179       ( !aElems.IsNull() && !aElems->Map().Contains( myLastID ) ) )
180     return;
181   // Reset last detected ID
182   myLastID = -1;
183 }
184
185 Standard_Boolean MeshVS_MeshOwner::IsForcedHilight () const
186 {
187   Standard_Boolean aHilight = Standard_True;
188   Standard_Integer aKey = -1;
189   if( myLastID > 0 )
190   {
191     // Check the detected entity and 
192     // allow to hilight it if it differs from the last detected entity <myLastID>
193     Handle(TColStd_HPackedMapOfInteger) aNodes = GetDetectedNodes();
194     if( !aNodes.IsNull() && aNodes->Map().Extent() == 1 )
195     {
196       TColStd_MapIteratorOfPackedMapOfInteger anIt( aNodes->Map() );
197       aKey = anIt.Key();
198       if( myLastID == aKey )
199       {  
200          aHilight = Standard_False;
201       }
202     }  
203     Handle(TColStd_HPackedMapOfInteger) aElems = GetDetectedElements();
204     if( !aElems.IsNull() && aElems->Map().Extent() == 1 )
205     {
206       TColStd_MapIteratorOfPackedMapOfInteger anIt( aElems->Map() );
207       aKey = anIt.Key();
208       if( myLastID == aKey )
209       {
210           aHilight = Standard_False;
211       }
212     }
213   } 
214   return aHilight;
215 }