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