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