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