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