]> SALOME platform Git repositories - modules/hydro.git/blob - src/HYDROData/HYDROData_Object.cxx
Salome HOME
Base implementation of Zone data object (Feature #31).
[modules/hydro.git] / src / HYDROData / HYDROData_Object.cxx
1
2 #include "HYDROData_Object.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 <QString>
18 #include <QStringList>
19 #include <QVariant>
20
21 IMPLEMENT_STANDARD_HANDLE(HYDROData_Object,MMgt_TShared)
22 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Object,MMgt_TShared)
23
24 // is equal function for unique object mapping
25 bool IsEqual(const Handle_HYDROData_Object& theObj1, const Handle_HYDROData_Object& theObj2)
26 {
27   return (theObj1->ID() == theObj2->ID());
28 }
29
30 QString HYDROData_Object::GetName() const
31 {
32   Handle(TDataStd_Name) aName;
33   if (myLab.FindAttribute(TDataStd_Name::GetID(), aName)) {
34     TCollection_AsciiString aStr(aName->Get());
35     return QString(aStr.ToCString());
36   }
37   return QString();
38 }
39
40 void HYDROData_Object::SetName(const QString& theName)
41 {
42   TDataStd_Name::Set(myLab, TCollection_ExtendedString(theName.toLatin1().constData()));
43 }
44
45 QStringList HYDROData_Object::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
46 {
47   QStringList anEmptyList;
48   return anEmptyList;
49 }
50
51 void HYDROData_Object::Update( const bool theIsForce )
52 {
53 }
54
55 QVariant HYDROData_Object::GetDataVariant()
56 {
57   return QVariant();
58 }
59
60 bool HYDROData_Object::IsRemoved() const
61 {
62   return !myLab.HasAttribute();
63 }
64
65 void HYDROData_Object::Remove()
66 {
67   return myLab.ForgetAllAttributes(Standard_True);
68 }
69
70 HYDROData_Object::HYDROData_Object()
71 {
72 }
73
74 HYDROData_Object::~HYDROData_Object()
75 {
76 }
77
78 void HYDROData_Object::CopyTo(Handle_HYDROData_Object theDestination) const
79 {
80   TDF_CopyLabel aCopy(myLab, theDestination->Label());
81   aCopy.Perform();
82 }
83
84 void HYDROData_Object::SetLabel(TDF_Label theLabel)
85 {
86   myLab = theLabel;
87 }
88
89 void HYDROData_Object::SaveByteArray(const int theTag, 
90   const char* theData, const int theLen)
91 {
92   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
93   // array is empty, remove the attribute
94   if (theLen <= 0) {
95     aLab.ForgetAttribute(TDataStd_ByteArray::GetID());
96     return;
97   }
98   // store data of image in byte array
99   Handle(TDataStd_ByteArray) aData;
100   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData)) {
101     aData = TDataStd_ByteArray::Set(aLab, 1, theLen);
102   }
103   // copy bytes one by one
104   if (aData->Length() != theLen) {
105     Handle(TColStd_HArray1OfByte) aNewData = new TColStd_HArray1OfByte(1, theLen);
106     for(int a = 0; a < theLen; a++)
107       aNewData->SetValue(a + 1, theData[a]);
108     aData->ChangeArray(aNewData);
109   } else {
110     for(int a = 0; a < theLen; a++)
111       aData->SetValue(a + 1, theData[a]);
112   }
113 }
114
115 const char* HYDROData_Object::ByteArray(const int theTag, int& theLen) const
116 {
117   TDF_Label aLab = theTag == 0 ? myLab : myLab.FindChild(theTag);
118   Handle(TDataStd_ByteArray) aData;
119   if (!aLab.FindAttribute(TDataStd_ByteArray::GetID(), aData))
120     return NULL; // return empty image if there is no array
121   theLen = aData->Length();
122   if (theLen)
123     return (const char*)(&(aData->InternalArray()->ChangeArray1().ChangeValue(1)));
124   return NULL;
125 }
126
127 int HYDROData_Object::NbReferenceObjects( const int theTag ) const
128 {
129   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
130   return aRefs.IsNull() ? 0 : aRefs->Extent();
131 }
132
133 void HYDROData_Object::AddReferenceObject( const Handle_HYDROData_Object& theObj,
134                                            const int                      theTag )
135 {
136   if ( theObj.IsNull() )
137     return;
138
139   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
140   aRefs->Append( theObj->Label() );
141 }
142
143 void HYDROData_Object::SetReferenceObject( const Handle_HYDROData_Object& theObj,
144                                            const int                      theTag,
145                                            const int                      theIndex )
146 {
147   if ( theObj.IsNull() )
148     return;
149
150   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, true );
151
152   if ( theIndex >= aRefs->Extent() )
153   {
154     aRefs->Append( theObj->Label() );
155   }
156   else if ( theIndex < 0 )
157   {
158     aRefs->Prepend( theObj->Label() );
159   }
160   else
161   {
162     RemoveReferenceObject( theTag, theIndex );
163
164     Handle(HYDROData_Object) aBeforeObj = GetReferenceObject( theTag, theIndex );
165
166     aRefs = getReferenceList( theTag, true ); // because reference list can be removed
167     if ( !aBeforeObj.IsNull() )
168       aRefs->InsertBefore( theObj->Label(), aBeforeObj->Label() );
169     else 
170       aRefs->Append( theObj->Label() );
171   }
172 }
173
174 Handle(HYDROData_Object) HYDROData_Object::GetReferenceObject( const int theTag,
175                                                                const int theIndex ) const
176 {
177   Handle(HYDROData_Object) aRes;
178
179   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
180   if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
181     return aRes;
182
183   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
184   for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
185
186   const TDF_Label& aRefLabel = anIter.Value();
187   aRes = HYDROData_Iterator::Object( aRefLabel );
188
189   return aRes;
190 }
191
192 HYDROData_SequenceOfObjects HYDROData_Object::GetReferenceObjects( const int theTag ) const
193 {
194   HYDROData_SequenceOfObjects aRes;
195
196   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
197   if ( aRefs.IsNull() )
198     return aRes;
199
200   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
201   for ( ; anIter.More(); anIter.Next() )
202   {
203     const TDF_Label& aRefLabel = anIter.Value();
204
205     Handle(HYDROData_Object) aRefObject = HYDROData_Iterator::Object( aRefLabel );
206     if ( aRefObject.IsNull() )
207       continue;
208
209     aRes.Append( aRefObject );
210   }
211
212   return aRes;
213 }
214
215 void HYDROData_Object::RemoveReferenceObject( const int theTag,
216                                               const int theIndex )
217 {
218   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
219   if ( aRefs.IsNull() )
220     return;
221
222   if ( aRefs->Extent() == 1 && theIndex == 0 )
223   { 
224     // remove all if only one
225     ClearReferenceObjects( theTag );
226     return;
227   }
228
229   Handle(HYDROData_Object) aRemovedObj = GetReferenceObject( theTag, theIndex );
230   if ( aRemovedObj.IsNull() )
231     return;
232
233   aRefs->Remove( aRemovedObj->Label() );
234 }
235
236 void HYDROData_Object::ClearReferenceObjects( const int theTag )
237 {
238   TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
239   aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
240 }
241
242 Handle(TDataStd_ReferenceList) HYDROData_Object::getReferenceList( const int theTag,
243                                                                    const bool theIsCreate ) const
244 {
245   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
246
247   Handle(TDataStd_ReferenceList) aRefs;
248   if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
249     aRefs = TDataStd_ReferenceList::Set( aLabel );
250
251   return aRefs;
252 }