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