Salome HOME
Minor changes.
[modules/hydro.git] / src / HYDROData / HYDROData_Object.cxx
1
2 #include "HYDROData_Object.h"
3
4 #include "HYDROData_Bathymetry.h"
5 #include "HYDROData_DummyObject3D.h"
6 #include "HYDROData_ShapesGroup.h"
7 #include "HYDROData_Iterator.h"
8
9 #include <TNaming_Builder.hxx>
10 #include <TNaming_NamedShape.hxx>
11
12 #include <TopoDS_Shape.hxx>
13
14 #include <QColor>
15
16 IMPLEMENT_STANDARD_HANDLE(HYDROData_Object,HYDROData_Entity)
17 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Object,HYDROData_Entity)
18
19 HYDROData_Object::HYDROData_Object()
20 : HYDROData_Entity()
21 {
22 }
23
24 HYDROData_Object::~HYDROData_Object()
25 {
26 }
27
28 void HYDROData_Object::SetName( const QString& theName )
29 {
30   QString anOldObjName = GetName();
31   if ( anOldObjName != theName )
32   {
33     HYDROData_SequenceOfObjects aGroups = GetGroups();
34     HYDROData_SequenceOfObjects::Iterator anIter( aGroups );
35     for ( ; anIter.More(); anIter.Next() )
36     {
37       Handle(HYDROData_ShapesGroup) aGroup =
38         Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
39       if ( aGroup.IsNull() )
40         continue;
41
42       QString aGroupName = aGroup->GetName();
43       if ( anOldObjName.isEmpty() )
44         aGroupName.prepend( theName + "_" );
45       else if ( aGroupName.startsWith( anOldObjName ) )
46         aGroupName.replace( anOldObjName, theName );
47       else
48         continue;
49
50       aGroup->SetName( aGroupName );
51     }
52   }
53
54   HYDROData_Entity::SetName( theName );
55 }
56
57 void HYDROData_Object::Update()
58 {
59   HYDROData_Entity::Update();
60   removeTopShape();
61   removeShape3D();
62   removeGroupObjects();
63 }
64
65 HYDROData_SequenceOfObjects HYDROData_Object::GetAllReferenceObjects() const
66 {
67   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
68
69   Handle(HYDROData_Bathymetry) aRefBathymetry = GetBathymetry();
70   if ( !aRefBathymetry.IsNull() )
71     aResSeq.Append( aRefBathymetry );
72
73   return aResSeq;
74 }
75
76 void HYDROData_Object::SetToUpdate( bool theFlag )
77 {
78   HYDROData_Entity::SetToUpdate( theFlag );
79
80   Handle(HYDROData_DummyObject3D) anObject3D = GetObject3D();
81   if ( !anObject3D.IsNull() )
82     anObject3D->SetToUpdate( theFlag );
83 }
84
85 void HYDROData_Object::SetTopShape( const TopoDS_Shape& theShape )
86 {
87   TNaming_Builder aBuilder( myLab.FindChild( DataTag_TopShape ) );
88   aBuilder.Generated( theShape );
89 }
90
91 void HYDROData_Object::SetShape3D( const TopoDS_Shape& theShape )
92 {
93   TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape3D ) );
94   aBuilder.Generated( theShape );
95   
96   // Check the object 3D existance
97   checkAndSetObject3D();
98 }
99
100 Handle(HYDROData_DummyObject3D) HYDROData_Object::GetObject3D() const
101 {
102   Handle(HYDROData_DummyObject3D) anObject;
103   
104   TDF_Label aLabel = myLab.FindChild( DataTag_Object3D, false );
105   if ( !aLabel.IsNull() )
106   {
107     TDF_Label aChildLabel = aLabel.FindChild( 0, false );
108     if ( !aChildLabel.IsNull() )
109     {
110       anObject = Handle(HYDROData_DummyObject3D)::DownCast(
111         HYDROData_Iterator::Object( aChildLabel ) );
112     }
113   }
114
115   return anObject;
116 }
117
118 void HYDROData_Object::checkAndSetObject3D()
119 {
120   TDF_Label aLabel = myLab.FindChild( DataTag_Object3D, false );
121   if ( !aLabel.IsNull() )
122     return;
123
124   TDF_Label aChildLabel = myLab.FindChild( DataTag_Object3D ).FindChild( 0 );
125   HYDROData_Iterator::CreateObject( aChildLabel, KIND_DUMMY_3D );
126 }
127
128 HYDROData_SequenceOfObjects HYDROData_Object::GetGroups() const
129 {
130   return GetReferenceObjects( DataTag_EdgesGroup );
131 }
132
133 bool HYDROData_Object::SetBathymetry( const Handle(HYDROData_Bathymetry)& theBathymetry )
134 {
135   if ( theBathymetry.IsNull() )
136     return false;
137   
138   Handle(HYDROData_Bathymetry) aPrevBathymetry = GetBathymetry();
139   if ( IsEqual( aPrevBathymetry, theBathymetry ) )
140     return true;
141
142   SetReferenceObject( theBathymetry, DataTag_Bathymetry );
143
144   // Indicate model of the need to update object
145   SetToUpdate( true );
146
147   return true;
148 }
149
150
151 Handle(HYDROData_Bathymetry) HYDROData_Object::GetBathymetry() const
152 {
153   return Handle(HYDROData_Bathymetry)::DownCast( 
154            GetReferenceObject( DataTag_Bathymetry ) );
155 }
156
157 void HYDROData_Object::RemoveBathymetry()
158 {
159   Handle(HYDROData_Bathymetry) aPrevBathymetry = GetBathymetry();
160   if ( aPrevBathymetry.IsNull() )
161     return;
162
163   ClearReferenceObjects( DataTag_Bathymetry );
164
165   // Indicate model of the need to update object
166   SetToUpdate( true );
167 }
168
169 void HYDROData_Object::SetFillingColor( const QColor& theColor )
170 {
171   SetColor( theColor, DataTag_FillingColor );
172 }
173
174 QColor HYDROData_Object::GetFillingColor() const
175 {
176   return GetColor( getDefaultFillingColor(), DataTag_FillingColor );
177 }
178
179 void HYDROData_Object::SetBorderColor( const QColor& theColor )
180 {
181   SetColor( theColor, DataTag_BorderColor );
182 }
183
184 QColor HYDROData_Object::GetBorderColor() const
185 {
186   return GetColor( getDefaultBorderColor(), DataTag_BorderColor );
187 }
188
189 QColor HYDROData_Object::DefaultFillingColor()
190 {
191   return QColor( Qt::yellow );
192 }
193
194 QColor HYDROData_Object::DefaultBorderColor()
195 {
196   return QColor( Qt::transparent );
197 }
198
199 QColor HYDROData_Object::getDefaultFillingColor() const
200 {
201   return DefaultFillingColor();
202 }
203
204 QColor HYDROData_Object::getDefaultBorderColor() const
205 {
206   return DefaultBorderColor();
207 }
208
209 Handle(HYDROData_ShapesGroup) HYDROData_Object::createGroupObject()
210 {
211   TDF_Label aNewLab = myLab.FindChild( DataTag_EdgesGroup ).NewChild();
212
213   Handle(HYDROData_ShapesGroup) aNewGroup =
214     Handle(HYDROData_ShapesGroup)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_SHAPES_GROUP ) );
215   AddReferenceObject( aNewGroup, DataTag_EdgesGroup );
216
217   return aNewGroup;
218 }
219
220 void HYDROData_Object::createGroupObjects()
221 {
222 }
223
224 void HYDROData_Object::removeGroupObjects()
225 {
226   TDF_Label aLabel = myLab.FindChild( DataTag_EdgesGroup, false );
227   if ( !aLabel.IsNull() )
228     aLabel.ForgetAllAttributes();
229 }
230
231 TopoDS_Shape HYDROData_Object::getTopShape() const
232 {
233   TDF_Label aLabel = myLab.FindChild( DataTag_TopShape, false );
234   if ( !aLabel.IsNull() )
235   {
236     Handle(TNaming_NamedShape) aNamedShape;
237     if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
238       return aNamedShape->Get();
239   }
240
241   return TopoDS_Shape();
242 }
243
244 void HYDROData_Object::removeTopShape()
245 {
246   TDF_Label aLabel = myLab.FindChild( DataTag_TopShape, false );
247   if ( !aLabel.IsNull() )
248     aLabel.ForgetAllAttributes();
249 }
250
251 TopoDS_Shape HYDROData_Object::getShape3D() const
252 {
253   TDF_Label aLabel = myLab.FindChild( DataTag_Shape3D, false );
254   if ( !aLabel.IsNull() )
255   {
256     Handle(TNaming_NamedShape) aNamedShape;
257     if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
258       return aNamedShape->Get();
259   }
260
261   return TopoDS_Shape();
262 }
263
264 void HYDROData_Object::removeShape3D()
265 {
266   TDF_Label aLabel = myLab.FindChild( DataTag_Shape3D, false );
267   if ( !aLabel.IsNull() )
268     aLabel.ForgetAllAttributes();
269 }
270