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