Salome HOME
Drawing of zones in OCC view improved.
[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 Handle(HYDROData_Object) HYDROData_Object::GetReferenceObject( const int theTag,
179                                                                const int theIndex ) const
180 {
181   Handle(HYDROData_Object) aRes;
182
183   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
184   if ( aRefs.IsNull() || theIndex < 0 || theIndex >= aRefs->Extent() )
185     return aRes;
186
187   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
188   for ( int anIndex = 0; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
189
190   const TDF_Label& aRefLabel = anIter.Value();
191   aRes = HYDROData_Iterator::Object( aRefLabel );
192
193   return aRes;
194 }
195
196 HYDROData_SequenceOfObjects HYDROData_Object::GetReferenceObjects( const int theTag ) const
197 {
198   HYDROData_SequenceOfObjects aRes;
199
200   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
201   if ( aRefs.IsNull() )
202     return aRes;
203
204   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
205   for ( ; anIter.More(); anIter.Next() )
206   {
207     const TDF_Label& aRefLabel = anIter.Value();
208
209     Handle(HYDROData_Object) aRefObject = HYDROData_Iterator::Object( aRefLabel );
210     if ( aRefObject.IsNull() )
211       continue;
212
213     aRes.Append( aRefObject );
214   }
215
216   return aRes;
217 }
218
219 void HYDROData_Object::RemoveReferenceObject( const int theTag,
220                                               const int theIndex )
221 {
222   Handle(TDataStd_ReferenceList) aRefs = getReferenceList( theTag, false );
223   if ( aRefs.IsNull() )
224     return;
225
226   if ( aRefs->Extent() == 1 && theIndex == 0 )
227   { 
228     // remove all if only one
229     ClearReferenceObjects( theTag );
230     return;
231   }
232
233   int anIndex = 0;
234   TDF_ListIteratorOfLabelList anIter( aRefs->List() );
235   for ( ; anIndex != theIndex && anIter.More(); anIter.Next(), ++anIndex );
236
237   if ( anIndex != theIndex )
238     return;
239
240   const TDF_Label& aRefLabel = anIter.Value();
241   aRefs->Remove( aRefLabel );
242 }
243
244 void HYDROData_Object::ClearReferenceObjects( const int theTag )
245 {
246   TDF_Label aSetLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
247   aSetLabel.ForgetAttribute( TDataStd_ReferenceList::GetID() );
248 }
249
250 Handle(TDataStd_ReferenceList) HYDROData_Object::getReferenceList( const int theTag,
251                                                                    const bool theIsCreate ) const
252 {
253   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
254
255   Handle(TDataStd_ReferenceList) aRefs;
256   if ( !aLabel.FindAttribute( TDataStd_ReferenceList::GetID(), aRefs ) && theIsCreate )
257     aRefs = TDataStd_ReferenceList::Set( aLabel );
258
259   return aRefs;
260 }
261
262 void HYDROData_Object::SetColor( const QColor& theColor,
263                                  const int     theTag )
264 {
265   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
266
267   Handle(TDataStd_IntegerArray) aColorArray;
268   if ( !aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
269     aColorArray = TDataStd_IntegerArray::Set( aLabel, 1, 4 );
270
271   aColorArray->SetValue( 1, theColor.red()   );
272   aColorArray->SetValue( 2, theColor.green() );
273   aColorArray->SetValue( 3, theColor.blue()  );
274   aColorArray->SetValue( 4, theColor.alpha() );
275 }
276
277 QColor HYDROData_Object::GetColor( const QColor& theDefColor,
278                                    const int     theTag ) const
279 {
280   QColor aResColor = theDefColor;
281
282   TDF_Label aLabel = theTag == 0 ? myLab : myLab.FindChild( theTag );
283
284   Handle(TDataStd_IntegerArray) aColorArray;
285   if ( aLabel.FindAttribute( TDataStd_IntegerArray::GetID(), aColorArray ) )
286   {
287     aResColor.setRed(   aColorArray->Value( 1 ) );
288     aResColor.setGreen( aColorArray->Value( 2 ) );
289     aResColor.setBlue(  aColorArray->Value( 3 ) );
290     aResColor.setAlpha( aColorArray->Value( 4 ) );
291   }
292
293   return aResColor;
294 }