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