Salome HOME
compilation on Linux
[modules/hydro.git] / src / HYDROData / HYDROData_Object.cxx
1 // Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
2 //
3 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
5 //
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
10 //
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 // Lesser General Public License for more details.
15 //
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
19 //
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
21 //
22
23 #include "HYDROData_Object.h"
24
25 #include "HYDROData_DummyObject3D.h"
26 #include "HYDROData_ShapesGroup.h"
27 #include "HYDROData_Tool.h"
28 #include "HYDROData_Iterator.h"
29 #include "HYDROData_IAltitudeObject.h"
30
31 #include <TNaming_Builder.hxx>
32 #include <TNaming_NamedShape.hxx>
33
34 #include <TopoDS_Shape.hxx>
35
36 #include <QColor>
37
38 IMPLEMENT_STANDARD_HANDLE(HYDROData_Object,HYDROData_Entity)
39 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Object,HYDROData_Entity)
40
41 HYDROData_Object::HYDROData_Object()
42 : HYDROData_Entity()
43 {
44 }
45
46 HYDROData_Object::~HYDROData_Object()
47 {
48 }
49
50 void HYDROData_Object::SetName( const QString& theName )
51 {
52   QString anOldObjName = GetName();
53   if ( anOldObjName != theName )
54   {
55     HYDROData_SequenceOfObjects aGroups = GetGroups();
56     HYDROData_SequenceOfObjects::Iterator anIter( aGroups );
57     for ( ; anIter.More(); anIter.Next() )
58     {
59       Handle(HYDROData_ShapesGroup) aGroup =
60         Handle(HYDROData_ShapesGroup)::DownCast( anIter.Value() );
61       if ( aGroup.IsNull() )
62         continue;
63
64       HYDROData_Tool::UpdateChildObjectName( anOldObjName, theName, aGroup );
65     }
66
67     Handle(HYDROData_DummyObject3D) anObject3D = GetObject3D();
68     if ( !anObject3D.IsNull() )
69       HYDROData_Tool::UpdateChildObjectName( anOldObjName, theName, anObject3D );
70
71     Handle(HYDROData_IAltitudeObject) anAltitudeObj = getChildAltitudeObject();
72     if ( !anAltitudeObj.IsNull() )
73       HYDROData_Tool::UpdateChildObjectName( anOldObjName, theName, anAltitudeObj );
74   }
75
76   HYDROData_Entity::SetName( theName );
77 }
78
79 void HYDROData_Object::Update()
80 {
81   removeTopShape();
82   removeShape3D();
83   removeGroupObjects();
84   checkAndSetAltitudeObject();
85   HYDROData_Entity::Update();
86 }
87
88 HYDROData_SequenceOfObjects HYDROData_Object::GetAllReferenceObjects() const
89 {
90   HYDROData_SequenceOfObjects aResSeq = HYDROData_Entity::GetAllReferenceObjects();
91
92   Handle(HYDROData_IAltitudeObject) aRefAltitude = GetAltitudeObject();
93   if ( !aRefAltitude.IsNull() )
94     aResSeq.Append( aRefAltitude );
95
96   return aResSeq;
97 }
98
99 void HYDROData_Object::SetToUpdate( bool theFlag )
100 {
101   HYDROData_Entity::SetToUpdate( theFlag );
102
103   Handle(HYDROData_DummyObject3D) anObject3D = GetObject3D();
104   if ( !anObject3D.IsNull() )
105     anObject3D->SetToUpdate( theFlag );
106 }
107
108 void HYDROData_Object::SetTopShape( const TopoDS_Shape& theShape )
109 {
110   TNaming_Builder aBuilder( myLab.FindChild( DataTag_TopShape ) );
111   aBuilder.Generated( theShape );
112 }
113
114 void HYDROData_Object::SetShape3D( const TopoDS_Shape& theShape )
115 {
116   TNaming_Builder aBuilder( myLab.FindChild( DataTag_Shape3D ) );
117   aBuilder.Generated( theShape );
118   
119   // Check the object 3D existance
120   checkAndSetObject3D();
121 }
122
123 Handle(HYDROData_DummyObject3D) HYDROData_Object::GetObject3D() const
124 {
125   Handle(HYDROData_DummyObject3D) anObject;
126   
127   TDF_Label aLabel = myLab.FindChild( DataTag_Object3D, false );
128   if ( !aLabel.IsNull() )
129   {
130     TDF_Label aChildLabel = aLabel.FindChild( 0, false );
131     if ( !aChildLabel.IsNull() )
132     {
133       anObject = Handle(HYDROData_DummyObject3D)::DownCast(
134         HYDROData_Iterator::Object( aChildLabel ) );
135     }
136   }
137
138   return anObject;
139 }
140
141 void HYDROData_Object::checkAndSetObject3D()
142 {
143   TDF_Label aLabel = myLab.FindChild( DataTag_Object3D, false );
144   if ( !aLabel.IsNull() )
145     return;
146
147   TDF_Label aChildLabel = myLab.FindChild( DataTag_Object3D ).FindChild( 0 );
148   HYDROData_Iterator::CreateObject( aChildLabel, KIND_DUMMY_3D );
149 }
150
151 HYDROData_SequenceOfObjects HYDROData_Object::GetGroups() const
152 {
153   return GetReferenceObjects( DataTag_EdgesGroup );
154 }
155
156 Handle(HYDROData_ShapesGroup) HYDROData_Object::GetGroup( const int theGroupId ) const
157 {
158   Handle(HYDROData_ShapesGroup) aResGroup;
159
160   HYDROData_SequenceOfObjects aGroups = GetGroups();
161   if ( theGroupId < 0 || theGroupId >= aGroups.Length() )
162     return aResGroup;
163
164   aResGroup = Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( theGroupId + 1 ) );
165
166   return aResGroup;
167 }
168
169 int HYDROData_Object::GetGroupId( const Handle(HYDROData_ShapesGroup)& theGroup ) const
170 {
171   int aRes = -1;
172
173   HYDROData_SequenceOfObjects aGroups = GetGroups();
174   for ( int i = 1, n = aGroups.Length(); i <= n; ++i )
175   {
176     Handle(HYDROData_ShapesGroup) aGroup =
177       Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( i ) );
178     if ( IsEqual( theGroup, aGroup ) )
179     {
180       aRes = i - 1;
181       break;
182     }
183   }
184
185   return aRes;
186 }
187
188 bool HYDROData_Object::SetAltitudeObject( 
189   const Handle(HYDROData_IAltitudeObject)& theAltitude )
190 {
191   if ( theAltitude.IsNull() )
192     return false;
193   
194   Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
195   if ( IsEqual( aPrevAltitude, theAltitude ) )
196     return true;
197
198   SetReferenceObject( theAltitude, DataTag_AltitudeObject );
199
200   // Indicate model of the need to update object
201   SetToUpdate( true );
202
203   return true;
204 }
205
206 Handle(HYDROData_IAltitudeObject) HYDROData_Object::GetAltitudeObject() const
207 {
208   return Handle(HYDROData_IAltitudeObject)::DownCast( 
209            GetReferenceObject( DataTag_AltitudeObject ) );
210 }
211
212 void HYDROData_Object::RemoveAltitudeObject()
213 {
214   Handle(HYDROData_IAltitudeObject) aPrevAltitude = GetAltitudeObject();
215   if ( aPrevAltitude.IsNull() )
216     return;
217
218   ClearReferenceObjects( DataTag_AltitudeObject );
219
220   // Indicate model of the need to update object
221   SetToUpdate( true );
222 }
223
224 void HYDROData_Object::SetFillingColor( const QColor& theColor )
225 {
226   SetColor( theColor, DataTag_FillingColor );
227 }
228
229 QColor HYDROData_Object::GetFillingColor() const
230 {
231   return GetColor( getDefaultFillingColor(), DataTag_FillingColor );
232 }
233
234 void HYDROData_Object::SetBorderColor( const QColor& theColor )
235 {
236   SetColor( theColor, DataTag_BorderColor );
237 }
238
239 QColor HYDROData_Object::GetBorderColor() const
240 {
241   return GetColor( getDefaultBorderColor(), DataTag_BorderColor );
242 }
243
244 QColor HYDROData_Object::getDefaultFillingColor() const
245 {
246   return QColor( Qt::yellow );
247 }
248
249 QColor HYDROData_Object::getDefaultBorderColor() const
250 {
251   return QColor( Qt::transparent );
252 }
253
254 QStringList HYDROData_Object::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
255 {
256   QStringList aResList = HYDROData_Entity::dumpObjectCreation( theTreatedObjects );
257   if ( aResList.isEmpty() )
258     return aResList; //Object was not created
259
260   QStringList aColorsDef;
261
262   QColor aFillingColor = GetFillingColor();
263   setPythonObjectColor( aColorsDef, aFillingColor, getDefaultFillingColor(), "SetFillingColor" );
264
265   QColor aBorderColor = GetBorderColor();
266   setPythonObjectColor( aColorsDef, aBorderColor, getDefaultBorderColor(), "SetBorderColor" );
267
268   if ( !aColorsDef.isEmpty() )
269   {
270     aResList << aColorsDef;
271     aResList << QString( "" );
272   }
273
274   return aResList;
275 }
276
277 ObjectKind HYDROData_Object::getAltitudeObjectType() const
278 {
279   return KIND_UNKNOWN;
280 }
281
282 Handle(HYDROData_IAltitudeObject) HYDROData_Object::getChildAltitudeObject() const
283 {
284   Handle(HYDROData_IAltitudeObject) anObject;
285   
286   TDF_Label aLabel = myLab.FindChild( DataTag_ChildAltitudeObject, false );
287   if ( !aLabel.IsNull() )
288   {
289     TDF_Label aChildLabel = aLabel.FindChild( 0, false );
290     if ( !aChildLabel.IsNull() )
291     {
292       anObject = Handle(HYDROData_IAltitudeObject)::DownCast(
293         HYDROData_Iterator::Object( aChildLabel ) );
294     }
295   }
296
297   return anObject;
298 }
299
300 void HYDROData_Object::checkAndSetAltitudeObject()
301 {
302   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
303
304   ObjectKind anAltitudeObjectType = getAltitudeObjectType();
305   if ( anAltitudeObjectType == KIND_UNKNOWN )
306     return; // No need to create altitude object
307
308   TDF_Label aLabel = myLab.FindChild( DataTag_ChildAltitudeObject, false );
309   if ( !aLabel.IsNull() )
310     return;
311
312   TDF_Label aChildLabel = myLab.FindChild( DataTag_ChildAltitudeObject ).FindChild( 0 );
313
314   Handle(HYDROData_IAltitudeObject) anAltitudeObject =
315     Handle(HYDROData_IAltitudeObject)::DownCast(
316       HYDROData_Iterator::CreateObject( aChildLabel, anAltitudeObjectType ) );
317
318   QString anAltitudePref = GetName() + "_Altitude";
319   QString anAltitudeName = HYDROData_Tool::GenerateObjectName( aDocument, anAltitudePref );
320   anAltitudeObject->SetName( anAltitudeName );
321
322   SetAltitudeObject( anAltitudeObject );
323 }
324
325 Handle(HYDROData_ShapesGroup) HYDROData_Object::createGroupObject()
326 {
327   TDF_Label aNewLab = myLab.FindChild( DataTag_EdgesGroup ).NewChild();
328
329   Handle(HYDROData_ShapesGroup) aNewGroup =
330     Handle(HYDROData_ShapesGroup)::DownCast( HYDROData_Iterator::CreateObject( aNewLab, KIND_SHAPES_GROUP ) );
331   AddReferenceObject( aNewGroup, DataTag_EdgesGroup );
332
333   return aNewGroup;
334 }
335
336 void HYDROData_Object::removeGroupObjects()
337 {
338   TDF_Label aLabel = myLab.FindChild( DataTag_EdgesGroup, false );
339   if ( !aLabel.IsNull() )
340     aLabel.ForgetAllAttributes();
341 }
342
343 TopoDS_Shape HYDROData_Object::getTopShape() const
344 {
345   TDF_Label aLabel = myLab.FindChild( DataTag_TopShape, false );
346   if ( !aLabel.IsNull() )
347   {
348     Handle(TNaming_NamedShape) aNamedShape;
349     if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
350       return aNamedShape->Get();
351   }
352
353   return TopoDS_Shape();
354 }
355
356 void HYDROData_Object::removeTopShape()
357 {
358   TDF_Label aLabel = myLab.FindChild( DataTag_TopShape, false );
359   if ( !aLabel.IsNull() )
360     aLabel.ForgetAllAttributes();
361 }
362
363 TopoDS_Shape HYDROData_Object::getShape3D() const
364 {
365   TDF_Label aLabel = myLab.FindChild( DataTag_Shape3D, false );
366   if ( !aLabel.IsNull() )
367   {
368     Handle(TNaming_NamedShape) aNamedShape;
369     if( aLabel.FindAttribute( TNaming_NamedShape::GetID(), aNamedShape ) )
370       return aNamedShape->Get();
371   }
372
373   return TopoDS_Shape();
374 }
375
376 void HYDROData_Object::removeShape3D()
377 {
378   TDF_Label aLabel = myLab.FindChild( DataTag_Shape3D, false );
379   if ( !aLabel.IsNull() )
380     aLabel.ForgetAllAttributes();
381 }
382