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