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