0032969: Coding - get rid of unused headers [IMeshData to PLib]
[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 <SelectMgr_SelectableObject.hxx>
22 #include <Standard_Type.hxx>
23 #include <TColStd_HPackedMapOfInteger.hxx>
24 #include <TColStd_MapIteratorOfPackedMapOfInteger.hxx>
25
26 IMPLEMENT_STANDARD_RTTIEXT(MeshVS_MeshOwner,SelectMgr_EntityOwner)
27
28 #ifndef MeshVS_PRSBUILDERHXX
29 #endif
30
31
32 //================================================================
33 // Function : Constructor MeshVS_MeshOwner
34 // Purpose  :
35 //================================================================
36 MeshVS_MeshOwner::MeshVS_MeshOwner (const SelectMgr_SelectableObject* theSelObj,
37                                     const Handle(MeshVS_DataSource)& theDS,
38                                     const Standard_Integer           thePriority)
39 : SelectMgr_EntityOwner ( theSelObj, thePriority )
40 {
41   myLastID = -1;
42   if( !theDS.IsNull() )
43     myDataSource = theDS;
44 }
45
46 //================================================================
47 // Function : GetDataSource
48 // Purpose  :
49 //================================================================
50 const Handle(MeshVS_DataSource)& MeshVS_MeshOwner::GetDataSource () const
51 {
52   return myDataSource;
53 }
54
55 //================================================================
56 // Function : GetSelectedNodes
57 // Purpose  :
58 //================================================================
59 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_MeshOwner::GetSelectedNodes () const
60 {
61   return mySelectedNodes;
62 }
63
64 //================================================================
65 // Function : GetSelectedElements
66 // Purpose  :
67 //================================================================
68 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_MeshOwner::GetSelectedElements () const
69 {
70   return mySelectedElems;
71 }
72
73 //================================================================
74 // Function : AddSelectedEntities
75 // Purpose  :
76 //================================================================
77 void MeshVS_MeshOwner::AddSelectedEntities (const Handle(TColStd_HPackedMapOfInteger)& Nodes,
78                                             const Handle(TColStd_HPackedMapOfInteger)& Elems)
79 {
80   if( mySelectedNodes.IsNull() )
81     mySelectedNodes = Nodes;
82   else if( !Nodes.IsNull() )
83     mySelectedNodes->ChangeMap().Unite( Nodes->Map() );
84   if( mySelectedElems.IsNull() )
85     mySelectedElems = Elems;
86   else if( !Elems.IsNull() )
87     mySelectedElems->ChangeMap().Unite( Elems->Map() );
88 }
89
90 //================================================================
91 // Function : ClearSelectedEntities
92 // Purpose  :
93 //================================================================
94 void MeshVS_MeshOwner::ClearSelectedEntities ()
95 {
96   mySelectedNodes.Nullify();
97   mySelectedElems.Nullify();
98 }
99
100 //================================================================
101 // Function : GetDetectedNodes
102 // Purpose  :
103 //================================================================
104 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_MeshOwner::GetDetectedNodes () const
105 {
106   return myDetectedNodes;
107 }
108
109 //================================================================
110 // Function : GetDetectedElements
111 // Purpose  :
112 //================================================================
113 const Handle(TColStd_HPackedMapOfInteger)& MeshVS_MeshOwner::GetDetectedElements () const
114 {
115   return myDetectedElems;
116 }
117
118 //================================================================
119 // Function : SetDetectedEntities
120 // Purpose  :
121 //================================================================
122 void MeshVS_MeshOwner::SetDetectedEntities (const Handle(TColStd_HPackedMapOfInteger)& Nodes,
123                                             const Handle(TColStd_HPackedMapOfInteger)& Elems)
124 {
125   myDetectedNodes = Nodes;
126   myDetectedElems = Elems;
127   if (IsSelected()) SetSelected (Standard_False);
128 }
129
130 //================================================================
131 // Function : HilightWithColor
132 // Purpose  :
133 //================================================================
134 void MeshVS_MeshOwner::HilightWithColor (const Handle(PrsMgr_PresentationManager)& thePM,
135                                          const Handle(Prs3d_Drawer)& theStyle,
136                                          const Standard_Integer /*theMode*/)
137 {
138   Handle( SelectMgr_SelectableObject ) aSelObj;
139   if ( HasSelectable() )
140     aSelObj = Selectable();
141
142   if ( thePM->IsImmediateModeOn() && aSelObj->IsKind( STANDARD_TYPE( MeshVS_Mesh ) ) )
143   {
144     // Update last detected entity ID
145     Handle(TColStd_HPackedMapOfInteger) aNodes = GetDetectedNodes();
146     Handle(TColStd_HPackedMapOfInteger) aElems = GetDetectedElements(); 
147     if( !aNodes.IsNull() && aNodes->Map().Extent() == 1 )
148     {
149       TColStd_MapIteratorOfPackedMapOfInteger anIt( aNodes->Map() );
150       if( myLastID != anIt.Key() )
151       {
152         myLastID = anIt.Key();
153       }
154     }
155     else if( !aElems.IsNull() && aElems->Map().Extent() == 1 )
156     {
157       TColStd_MapIteratorOfPackedMapOfInteger anIt( aElems->Map() );
158       if( myLastID != anIt.Key() )
159       {
160         myLastID = anIt.Key();
161       }
162     }
163
164     // hilight detected entities
165     Handle( MeshVS_Mesh ) aMesh = Handle( MeshVS_Mesh )::DownCast ( aSelObj );
166     aMesh->HilightOwnerWithColor ( thePM, theStyle, this );
167   }
168 }
169
170 void MeshVS_MeshOwner::Unhilight (const Handle(PrsMgr_PresentationManager)& thePM, const Standard_Integer )
171 {
172   SelectMgr_EntityOwner::Unhilight (thePM);
173
174   Handle(TColStd_HPackedMapOfInteger) aNodes = GetDetectedNodes();
175   Handle(TColStd_HPackedMapOfInteger) aElems = GetDetectedElements();  
176   if( ( !aNodes.IsNull() && !aNodes->Map().Contains( myLastID ) ) ||
177       ( !aElems.IsNull() && !aElems->Map().Contains( myLastID ) ) )
178     return;
179   // Reset last detected ID
180   myLastID = -1;
181 }
182
183 Standard_Boolean MeshVS_MeshOwner::IsForcedHilight () const
184 {
185   Standard_Boolean aHilight = Standard_True;
186   Standard_Integer aKey = -1;
187   if( myLastID > 0 )
188   {
189     // Check the detected entity and 
190     // allow to hilight it if it differs from the last detected entity <myLastID>
191     Handle(TColStd_HPackedMapOfInteger) aNodes = GetDetectedNodes();
192     if( !aNodes.IsNull() && aNodes->Map().Extent() == 1 )
193     {
194       TColStd_MapIteratorOfPackedMapOfInteger anIt( aNodes->Map() );
195       aKey = anIt.Key();
196       if( myLastID == aKey )
197       {  
198          aHilight = Standard_False;
199       }
200     }  
201     Handle(TColStd_HPackedMapOfInteger) aElems = GetDetectedElements();
202     if( !aElems.IsNull() && aElems->Map().Extent() == 1 )
203     {
204       TColStd_MapIteratorOfPackedMapOfInteger anIt( aElems->Map() );
205       aKey = anIt.Key();
206       if( myLastID == aKey )
207       {
208           aHilight = Standard_False;
209       }
210     }
211   } 
212   return aHilight;
213 }