Salome HOME
Python dump correction (Bug #352).
[modules/hydro.git] / src / HYDROData / HYDROData_Entity.cxx
1
2 #include "HYDROData_Entity.h"
3
4 #include "HYDROData_Iterator.h"
5 #include "HYDROData_Tool.h"
6
7 #include <TDataStd_Name.hxx>
8 #include <TDataStd_ByteArray.hxx>
9 #include <TDataStd_UAttribute.hxx>
10 #include <TDataStd_IntegerArray.hxx>
11 #include <TDataStd_BooleanArray.hxx>
12 #include <TDataStd_RealArray.hxx>
13 #include <TDataStd_ReferenceList.hxx>
14
15 #include <TDF_CopyLabel.hxx>
16 #include <TDF_ListIteratorOfLabelList.hxx>
17
18 #include <QColor>
19 #include <QString>
20 #include <QStringList>
21 #include <QVariant>
22
23 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
24
25 IMPLEMENT_STANDARD_HANDLE(HYDROData_Entity,MMgt_TShared)
26 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Entity,MMgt_TShared)
27
28 // is equal function for unique object mapping
29 bool IsEqual(const Handle_HYDROData_Entity& theObj1, const Handle_HYDROData_Entity& theObj2)
30 {
31   if ( !theObj1.IsNull() && !theObj2.IsNull() )
32     return theObj1->Label() == theObj2->Label();
33   return false;
34 }
35
36 QString HYDROData_Entity::GetName() const
37 {
38   Handle(TDataStd_Name) aName;
39   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
40     TCollection_AsciiString aStr(aName->Get());
41     return QString(aStr.ToCString());
42   }
43   return QString();
44 }
45
46 QString HYDROData_Entity::GetObjPyName() const
47 {
48   return GetName().replace(" ", "_");
49 }
50
51 void HYDROData_Entity::SetName(const QString& theName)
52 {
53   TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
54 }
55
56 QStringList HYDROData_Entity::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
57 {
58   QStringList anEmptyList;
59   return anEmptyList;
60 }
61
62 QStringList HYDROData_Entity::dumpObjectCreation( MapOfTreatedObjects& theTreatedObjects ) const
63 {
64   QStringList aResList;
65
66   Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
67   if ( aDocument.IsNull() )
68     return aResList;
69
70   QString aDocName = aDocument->GetDocPyName();
71   QString aName = GetObjPyName();
72
73   aResList << QString( "%1 = %2.CreateObject( %3 );" )
74               .arg( aName ).arg( aDocName ).arg( getPyTypeID() );
75   aResList << QString( "%1.SetName( \"%2\" );" )
76               .arg( aName ).arg( GetName() );
77   aResList << QString( "" );
78
79   return aResList;
80 }
81
82 void HYDROData_Entity::Update()
83 {
84   SetToUpdate( false );
85 }
86
87 QString HYDROData_Entity::getPyTypeID() const
88 {
89   switch( GetKind() )
90   {
91     case KIND_IMAGE:             return "KIND_IMAGE";
92     case KIND_POLYLINE:          return "KIND_POLYLINE";
93     case KIND_BATHYMETRY:        return "KIND_BATHYMETRY";
94     case KIND_ALTITUDE:          return "KIND_ALTITUDE";
95     case KIND_IMMERSIBLE_ZONE:   return "KIND_IMMERSIBLE_ZONE";
96     case KIND_RIVER:             return "KIND_RIVER";
97     case KIND_STREAM:            return "KIND_STREAM";
98     case KIND_CONFLUENCE:        return "KIND_CONFLUENCE";
99     case KIND_CHANNEL:           return "KIND_CHANNEL";
100     case KIND_OBSTACLE:          return "KIND_OBSTACLE";
101     case KIND_DIGUE:             return "KIND_DIGUE";
102     case KIND_PROFILE:           return "KIND_PROFILE";
103     case KIND_PROFILEUZ:         return "KIND_PROFILEUZ";
104     case KIND_POLYLINEXY:        return "KIND_POLYLINEXY";
105     case KIND_CALCULATION:       return "KIND_CALCULATION";
106     case KIND_ZONE:              return "KIND_ZONE";
107     case KIND_REGION:            return "KIND_REGION";
108     case KIND_VISUAL_STATE:      return "KIND_VISUAL_STATE";
109     case KIND_ARTIFICIAL_OBJECT: return "KIND_ARTIFICIAL_OBJECT";
110     case KIND_NATURAL_OBJECT:    return "KIND_NATURAL_OBJECT";
111     case KIND_DUMMY_3D:          return "KIND_DUMMY_3D";
112     case KIND_SHAPES_GROUP:      return "KIND_SHAPES_GROUP";
113     case KIND_SPLITTED_GROUP:    return "KIND_SPLITTED_GROUP";
114     case KIND_STREAM_ALTITUDE:   return "KIND_STREAM_ALTITUDE";
115     case KIND_OBSTACLE_ALTITUDE: return "KIND_OBSTACLE_ALTITUDE";
116     default:                     return "KIND_UNKNOWN"; ///! Unrecognized object
117   }
118 }
119
120 QVariant HYDROData_Entity::GetDataVariant()
121 {
122   return QVariant();
123 }
124
125 void HYDROData_Entity::SetToUpdate( bool theFlag )
126 {
127   if ( IsMustBeUpdated() == theFlag )
128     return;
129
130   if ( theFlag )
131   {
132     TDataStd_UAttribute::Set( myLab, GUID_MUST_BE_UPDATED );
133
134     Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
135     if ( !aDocument.IsNull() )
136     {
137       // Change the states of this and all depended objects
138       HYDROData_Tool::SetMustBeUpdatedObjects( aDocument );
139     }
140   }
141   else
142   {
143     myLab.ForgetAttribute( GUID_MUST_BE_UPDATED );
144   }
145 }
146
147 bool HYDROData_Entity::IsMustBeUpdated() const
148 {
149   return myLab.IsAttribute( GUID_MUST_BE_UPDATED );
150 }
151
152 bool HYDROData_Entity::CanBeUpdated() const
153 {
154   return true;
155 }
156
157 bool HYDROData_Entity::IsRemoved() const
158 {
159   return !myLab.HasAttribute();
160 }
161
162 void HYDROData_Entity::Remove()
163 {
164   return myLab.ForgetAllAttributes( true );
165 }
166
167 bool HYDROData_Entity::CanRemove()
168 {
169   return true;
170 }
171
172 HYDROData_Entity::HYDROData_Entity()
173 {
174 }
175
176 HYDROData_Entity::~HYDROData_Entity()
177 {
178 }
179
180 void HYDROData_Entity::CopyTo( const Handle(HYDROData_Entity)& theDestination ) const
181 {
182   TDF_CopyLabel aCopy(myLab, theDestination->Label());
183   aCopy.Perform();
184 }
185
186 Handle(HYDROData_Entity) HYDROData_Entity::GetFatherObject() const
187 {
188   Handle(HYDROData_Entity) aFather;
189
190   if ( !myLab.IsNull() )
191   {
192     TDF_Label aFatherLabel = myLab.Father();
193
194     while ( aFather.IsNull() && !aFatherLabel.IsNull() && !aFatherLabel.IsRoot() )
195     {
196       aFather = HYDROData_Iterator::Object( aFatherLabel );
197       aFatherLabel = aFatherLabel.Father();
198     }
199   }
200
201   return aFather;
202 }
203
204 HYDROData_SequenceOfObjects HYDROData_Entity::GetAllReferenceObjects() const
205 {
206   return HYDROData_SequenceOfObjects();
207 }
208
209 void HYDROData_Entity::SetLabel(TDF_Label theLabel)
210 {
211   myLab = theLabel;
212 }
213
214 void HYDROData_Entity::SaveByteArray(const int theTag, 
215   const char* theData, const int theLen)
216 {
217   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
218   // array is empty, remove the attribute
219   if (theLen <= 0) {
220     aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
221     return;
222   }
223   // store data of image in byte array
224   Handle(TDataStd_ByteArray) aData;
225   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
226     aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
227   }
228   // copy bytes one by one
229   if (aData->Length() != theLen) {
230     Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
231     for(int a = 0; a < theLen; a++)
232       aNewData->SetValue(a + 1, theData[a]);
233     aData->ChangeArray(aNewData);
234   } else {
235     for(int a = 0; a < theLen; a++)
236       aData->SetValue(a + 1, theData[a]);
237   }
238 }
239
240 const char* HYDROData_Entity::ByteArray(const int theTag, int& theLen) const
241 {
242   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
243   Handle(TDataStd_ByteArray) aData;
244   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
245     return NULL; // return empty image if there is no array
246   theLen = aData->Length();
247   if (theLen)
248     return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
249   return NULL;
250 }
251
252 int HYDROData_Entity::NbReferenceObjects( const int theTag ) const
253 {
254   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
255   return aRefs.IsNull() ? 0 : aRefs->Extent();
256 }
257
258 bool HYDROData_Entity::HasReference( const Handle_HYDROData_Entity& theObj,
259                                      const int                      theTag ) const
260 {
261   if ( theObj.IsNull() )
262     return false;
263
264   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
265   if ( aRefs.IsNull() || aRefs->IsEmpty() )
266     return false;
267
268   TDF_ListIteratorOfLabelList aListIt( aRefs->List() );
269   for ( ; aListIt.More(); aListIt.Next() )
270   {
271     const TDF_Label& aRefLabel = aListIt.Value();
272     if  ( theObj->Label() == aRefLabel )
273       return true;
274   }
275
276   return false;
277 }
278
279 void HYDROData_Entity::AddReferenceObject( const Handle_HYDROData_Entity& theObj,
280                                            const int                      theTag )
281 {
282   if ( theObj.IsNull() )
283     return;
284
285   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
286   aRefs->Append( theObj->Label() );
287 }
288
289 void HYDROData_Entity::SetReferenceObject( const Handle_HYDROData_Entity& theObj,
290                                            const int                      theTag,
291                                            const int                      theIndex )
292 {
293   if ( theObj.IsNull() )
294   {
295     RemoveReferenceObject( theTag, theIndex );
296     return;
297   }
298
299   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
300
301   if ( theIndex >= aRefs->Extent() )
302   {
303     aRefs->Append( theObj->Label() );
304   }
305   else if ( theIndex < 0 )
306   {
307     aRefs->Prepend( theObj->Label() );
308   }
309   else
310   {
311     RemoveReferenceObject( theTag, theIndex );
312
313     Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theIndex );
314
315     aRefs = getReferenceList( theTag, true ); // because reference list can be removed
316     if ( !aBeforeObj.IsNull() )
317       aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
318     else 
319       aRefs->Append( theObj->Label() );
320   }
321 }
322
323 void HYDROData_Entity::InsertReferenceObject( const Handle_HYDROData_Entity& theObj,
324                                               const int                      theTag,
325                                               const int                      theBeforeIndex )
326 {
327   if ( theObj.IsNull() )
328     return;
329
330   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
331
332   if ( theBeforeIndex >= aRefs->Extent() )
333   {
334     aRefs->Append( theObj->Label() );
335   }
336   else if ( theBeforeIndex < 0 )
337   {
338     aRefs->Prepend( theObj->Label() );
339   }
340   else
341   {
342     Handle(HYDROData_Entity) aBeforeObj = GetReferenceObject( theTag, theBeforeIndex );
343     if ( !aBeforeObj.IsNull() )
344       aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
345     else 
346       aRefs->Append( theObj->Label() );
347   }
348 }
349
350 void HYDROData_Entity::SetReferenceObjects( const HYDROData_SequenceOfObjects& theObjects,
351                                             const int                          theTag )
352 {
353   ClearReferenceObjects( theTag );
354   if ( theObjects.IsEmpty() )
355     return;
356
357   HYDROData_SequenceOfObjects::Iterator anIter( theObjects );
358   for ( ; anIter.More(); anIter.Next() )
359     AddReferenceObject( anIter.Value(), theTag );
360 }
361
362 Handle(HYDROData_Entity) HYDROData_Entity::GetReferenceObject( const int theTag,
363                                                                const int theIndex ) const
364 {
365   Handle(HYDROData_Entity) aRes;
366
367   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
368   if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
369     return aRes;
370
371   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
372   for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
373
374   const TDF_Label& aRefLabel = anIter.Value();
375   aRes = HYDROData_Iterator::Object( aRefLabel );
376
377   return aRes;
378 }
379
380 HYDROData_SequenceOfObjects HYDROData_Entity::GetReferenceObjects( const int theTag ) const
381 {
382   HYDROData_SequenceOfObjects aRes;
383
384   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
385   if ( aRefs.IsNull() )
386     return aRes;
387
388   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
389   for ( ; anIter.More(); anIter.Next() )
390   {
391     const TDF_Label& aRefLabel = anIter.Value();
392
393     Handle(HYDROData_Entity) aRefObject = HYDROData_Iterator::Object( aRefLabel );
394     if ( aRefObject.IsNull() )
395       continue;
396
397     aRes.Append( aRefObject );
398   }
399
400   return aRes;
401 }
402
403 void HYDROData_Entity::RemoveReferenceObject( const TDF_Label& theRefLabel,
404                                               const int        theTag )
405 {
406   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
407   if ( aRefs.IsNull() )
408     return;
409
410   if ( aRefs->Extent() == 1 )
411   { 
412     // remove all if only one
413     ClearReferenceObjects( theTag );
414     return;
415   }
416
417   aRefs->Remove( theRefLabel );
418 }
419
420 void HYDROData_Entity::RemoveReferenceObject( const int theTag,
421                                               const int theIndex )
422 {
423   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
424   if ( aRefs.IsNull() )
425     return;
426
427   if ( aRefs->Extent() == 1 && theIndex == 0 )
428   { 
429     // remove all if only one
430     ClearReferenceObjects( theTag );
431     return;
432   }
433
434   int anIndex = 0;
435   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
436   for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
437
438   if ( anIndex != theIndex || !anIter.More() )
439     return;
440
441   const TDF_Label& aRefLabel = anIter.Value();
442   aRefs->Remove( aRefLabel );
443 }
444
445 void HYDROData_Entity::ClearReferenceObjects( const int theTag )
446 {
447   TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
448   aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
449 }
450
451 Handle(TDataStd_ReferenceList) HYDROData_Entity::getReferenceList( const int theTag,
452                                                                    const bool theIsCreate ) const
453 {
454   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
455
456   Handle(TDataStd_ReferenceList) aRefs;
457   if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
458     aRefs = TDataStd_ReferenceList::Set( aLabel );
459
460   return aRefs;
461 }
462
463 void HYDROData_Entity::SetColor( const QColor& theColor,
464                                  const int     theTag )
465 {
466   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
467
468   Handle(TDataStd_IntegerArray) aColorArray;
469   if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
470     aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
471
472   aColorArray->SetValue( 1, theColor.red()   );
473   aColorArray->SetValue( 2, theColor.green() );
474   aColorArray->SetValue( 3, theColor.blue()  );
475   aColorArray->SetValue( 4, theColor.alpha() );
476 }
477
478 QColor HYDROData_Entity::GetColor( const QColor& theDefColor,
479                                    const int     theTag ) const
480 {
481   QColor aResColor = theDefColor;
482
483   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
484
485   Handle(TDataStd_IntegerArray) aColorArray;
486   if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
487   {
488     aResColor.setRed(   aColorArray->Value( 1 ) );
489     aResColor.setGreen( aColorArray->Value( 2 ) );
490     aResColor.setBlue(  aColorArray->Value( 3 ) );
491     aResColor.setAlpha( aColorArray->Value( 4 ) );
492   }
493
494   return aResColor;
495 }
496
497 void HYDROData_Entity::setPythonReferenceObject( MapOfTreatedObjects&            theTreatedObjects,
498                                                  QStringList&                    theScript,
499                                                  const Handle(HYDROData_Entity)& theRefObject,
500                                                  const QString&                  theMethod ) const
501 {
502   if ( !checkObjectPythonDefinition( theTreatedObjects, theScript, theRefObject ) )
503     return;
504
505   QString aRefObjName = theRefObject->GetObjPyName();
506
507   QString anObjName = GetObjPyName();
508   theScript << QString( "%1.%2( %3 );" )
509                .arg( anObjName ).arg( theMethod ).arg( aRefObjName );
510 }
511
512 bool HYDROData_Entity::checkObjectPythonDefinition( MapOfTreatedObjects&            theTreatedObjects,
513                                                     QStringList&                    theScript,
514                                                     const Handle(HYDROData_Entity)& theRefObject ) const
515 {
516   if ( theRefObject.IsNull() )
517     return false;
518
519   QString aRefObjName = theRefObject->GetName();
520   if ( aRefObjName.isEmpty() )
521     return false;
522
523   if ( theTreatedObjects.contains( aRefObjName ) )
524     return true;
525
526   // The definition of reference object must be dumped before this
527   QStringList aRefObjDump = theRefObject->DumpToPython( theTreatedObjects );
528   if ( aRefObjDump.isEmpty() )
529     return false;
530
531   QStringList aTmpList = theScript;
532   theScript = aRefObjDump;
533
534   theScript << QString( "" );
535   theScript << aTmpList;
536
537   theTreatedObjects.insert( aRefObjName, theRefObject );
538
539   return true;
540 }
541
542 void HYDROData_Entity::setPythonObjectColor( QStringList&         theScript,
543                                              const QColor&        theColor,
544                                              const QColor&        theDefaultColor,
545                                              const QString&       theMethod ) const
546 {
547   if ( theColor == theDefaultColor )
548     return; //Do not set the color for object if it like default
549
550   QString anObjName = GetObjPyName();
551   theScript << QString( "%1.%2( QColor( %3, %4, %5, %6 ) );" )
552               .arg( anObjName ).arg( theMethod )
553               .arg( theColor.red()  ).arg( theColor.green() )
554               .arg( theColor.blue() ).arg( theColor.alpha() );
555 }
556
557