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