Salome HOME
930d2a0e0733b9c04f8aa5c4ee5761e3cc7fc69a
[modules/hydro.git] / src / HYDROData / HYDROData_Image.cxx
1 #include <HYDROData_Image.h>
2 #include <HYDROData_Iterator.h>
3
4 #include <TDataStd_RealArray.hxx>
5 #include <TDataStd_ByteArray.hxx>
6 #include <TDataStd_IntegerArray.hxx>
7 #include <TDataStd_ReferenceList.hxx>
8 #include <TDataStd_Name.hxx>
9 #include <TDataStd_UAttribute.hxx>
10 #include <TDataStd_AsciiString.hxx>
11 #include <TDF_ListIteratorOfLabelList.hxx>
12
13 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
14
15 IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Object)
16 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Object)
17
18 HYDROData_Image::HYDROData_Image()
19 {
20 }
21
22 HYDROData_Image::~HYDROData_Image()
23 {
24 }
25
26 void HYDROData_Image::SetImage(const QImage& theImage)
27 {
28   if (theImage.isNull()) {
29     // for empty image remove all previously stored attributes
30     myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
31     myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
32     return;
33   }
34   // store width, height, bytes per line and format in integer array 
35   Handle(TDataStd_IntegerArray) aParams;
36   if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams)) {
37     aParams = TDataStd_IntegerArray::Set(myLab, 1, 4);
38   }
39   aParams->SetValue(1, theImage.width());
40   aParams->SetValue(2, theImage.height());
41   aParams->SetValue(3, theImage.bytesPerLine());
42   aParams->SetValue(4, (int)(theImage.format()));
43   // store data of image in byte array
44   const char* aData = (const char*)(theImage.bits());
45   SaveByteArray(0, aData, theImage.byteCount());
46 }
47
48 QImage HYDROData_Image::Image()
49 {
50   Handle(TDataStd_IntegerArray) aParams;
51   if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
52     return QImage(); // return empty image if there is no array
53   int aLen = 0;
54   uchar* anArray = (uchar*)ByteArray(0, aLen);
55   if (!aLen)
56     return QImage(); // return empty image if there is no array
57   QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
58                  aParams->Value(3), QImage::Format(aParams->Value(4)));
59   return aResult;
60 }
61
62 void HYDROData_Image::SetFilePath(const QString& theFilePath)
63 {
64   TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
65   TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
66 }
67
68 QString HYDROData_Image::GetFilePath() const
69 {
70   QString aRes;
71
72   Handle(TDataStd_AsciiString) anAsciiStr;
73   if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
74     aRes = QString( anAsciiStr->Get().ToCString() );
75
76   return aRes;
77 }
78
79 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
80 {
81   // locate 9 coeffs of matrix into the real array
82   Handle(TDataStd_RealArray) anArray;
83   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
84     if (theTrsf.isIdentity()) return; // no need to store identity transformation
85     anArray = TDataStd_RealArray::Set(myLab, 1, 9);
86   }
87   anArray->SetValue(1, theTrsf.m11());
88   anArray->SetValue(2, theTrsf.m12());
89   anArray->SetValue(3, theTrsf.m13());
90   anArray->SetValue(4, theTrsf.m21());
91   anArray->SetValue(5, theTrsf.m22());
92   anArray->SetValue(6, theTrsf.m23());
93   anArray->SetValue(7, theTrsf.m31());
94   anArray->SetValue(8, theTrsf.m32());
95   anArray->SetValue(9, theTrsf.m33());
96 }
97
98 QTransform HYDROData_Image::Trsf()
99 {
100   // get 9 coeffs of matrix from the real array
101   Handle(TDataStd_RealArray) anArray;
102   if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
103     return QTransform(); // return identity if there is no array
104   QTransform aTrsf(
105     anArray->Value(1), anArray->Value(2), anArray->Value(3), 
106     anArray->Value(4), anArray->Value(5), anArray->Value(6), 
107     anArray->Value(7), anArray->Value(8), anArray->Value(9));
108   return aTrsf;
109 }
110
111 void HYDROData_Image::SetTrsfPoints(const QPoint& thePointAIn,
112                                     const QPoint& thePointBIn,
113                                     const QPoint& thePointCIn,
114                                     const QPointF& thePointAOut,
115                                     const QPointF& thePointBOut,
116                                     const QPointF& thePointCOut)
117 {
118   Handle(TDataStd_RealArray) anArray;
119   if (!myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
120     anArray = TDataStd_RealArray::Set(myLab.FindChild(DataTag_TrsfPoints), 1, 12);
121   }
122   anArray->SetValue(1, thePointAIn.x());
123   anArray->SetValue(2, thePointAIn.y());
124   anArray->SetValue(3, thePointBIn.x());
125   anArray->SetValue(4, thePointBIn.y());
126   anArray->SetValue(5, thePointCIn.x());
127   anArray->SetValue(6, thePointCIn.y());
128   anArray->SetValue(7, thePointAOut.x());
129   anArray->SetValue(8, thePointAOut.y());
130   anArray->SetValue(9, thePointBOut.x());
131   anArray->SetValue(10, thePointBOut.y());
132   anArray->SetValue(11, thePointCOut.x());
133   anArray->SetValue(12, thePointCOut.y());
134 }
135
136 void HYDROData_Image::TrsfPoints(QPoint& thePointAIn,
137                                  QPoint& thePointBIn,
138                                  QPoint& thePointCIn,
139                                  QPointF& thePointAOut,
140                                  QPointF& thePointBOut,
141                                  QPointF& thePointCOut)
142 {
143   Handle(TDataStd_RealArray) anArray;
144   if (myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
145     thePointAIn = QPointF( anArray->Value(1), anArray->Value(2) ).toPoint();
146     thePointBIn = QPointF( anArray->Value(3), anArray->Value(4) ).toPoint();
147     thePointCIn = QPointF( anArray->Value(5), anArray->Value(6) ).toPoint();
148     thePointAOut = QPointF( anArray->Value(7), anArray->Value(8) );
149     thePointBOut = QPointF( anArray->Value(9), anArray->Value(10) );
150     thePointCOut = QPointF( anArray->Value(11), anArray->Value(12) );
151   }
152 }
153
154 void HYDROData_Image::AppendReference(Handle(HYDROData_Image) theReferenced)
155 {
156   Handle(TDataStd_ReferenceList) aRefs;
157   if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
158     aRefs = TDataStd_ReferenceList::Set(myLab);
159   aRefs->Append(theReferenced->Label());
160 }
161
162 int HYDROData_Image::NbReferences()
163 {
164   Handle(TDataStd_ReferenceList) aRefs;
165   if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
166     return 0;
167   return aRefs->Extent();
168 }
169
170 Handle(HYDROData_Image) HYDROData_Image::Reference(const int theIndex) const
171 {
172   Handle(TDataStd_ReferenceList) aRefs;
173   if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
174     return Handle(HYDROData_Image)();
175   if (theIndex < 0 || theIndex >= aRefs->Extent())
176     return Handle(HYDROData_Image)();
177
178   TDF_ListIteratorOfLabelList anIter(aRefs->List());
179   for(int anIndex = 0; anIndex != theIndex; anIter.Next(), anIndex++);
180   const TDF_Label& aRefLab = anIter.Value();
181   return Handle(HYDROData_Image)::DownCast(HYDROData_Iterator::Object(aRefLab));
182 }
183
184 void HYDROData_Image::ChangeReference(
185     const int theIndex, Handle(HYDROData_Image) theReferenced)
186 {
187   Handle(TDataStd_ReferenceList) aRefs;
188   if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
189     aRefs = TDataStd_ReferenceList::Set(myLab);
190   if (theIndex >= aRefs->Extent()) { // for too big index append it just to the end
191     AppendReference(theReferenced);
192   } else { // remove and insert new
193     TDF_ListIteratorOfLabelList anIter(aRefs->List());
194     int anIndex = 0;
195     for(; anIndex != theIndex; anIter.Next(), anIndex++);
196     const TDF_Label& aRemovedLab = anIter.Value();
197     anIter.Next();
198     aRefs->Remove(aRemovedLab);
199     if (anIter.More()) 
200       aRefs->InsertBefore(theReferenced->Label(), anIter.Value());
201     else 
202       aRefs->Append(theReferenced->Label());
203   }
204 }
205
206 void HYDROData_Image::RemoveReference(const int theIndex)
207 {
208   Handle(TDataStd_ReferenceList) aRefs;
209   if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
210     return; // no references, nothing to remove
211   if (aRefs->Extent() == 1 && theIndex == 0) { // remove all if only one
212     ClearReferences();
213     return;
214   }
215   TDF_ListIteratorOfLabelList anIter(aRefs->List());
216   int anIndex = 0;
217   for(; anIndex != theIndex && anIter.More(); anIter.Next(), anIndex++);
218   if (anIter.More())
219     aRefs->Remove(anIter.Value());
220 }
221
222 void HYDROData_Image::ClearReferences()
223 {
224   myLab.ForgetAttribute(TDataStd_ReferenceList::GetID());
225 }
226
227 void HYDROData_Image::SetOperatorName(const QString theOpName)
228 {
229   TDataStd_Name::Set(myLab.FindChild(DataTag_Operator),
230     TCollection_ExtendedString(theOpName.toLatin1().constData()));
231 }
232
233 QString HYDROData_Image::OperatorName()
234 {
235   Handle(TDataStd_Name) aName;
236   if (myLab.FindChild(DataTag_Operator).
237         FindAttribute(TDataStd_Name::GetID(), aName)) {
238     TCollection_AsciiString aStr(aName->Get());
239     return QString(aStr.ToCString());
240   }
241   return QString();
242 }
243
244 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
245 {
246   SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
247 }
248
249 QByteArray HYDROData_Image::Args()
250 {
251   int aLen = 0;
252   const char* aData = ByteArray(DataTag_Operator, aLen);
253   if (!aLen)
254     return QByteArray();
255   return QByteArray(aData, aLen);
256 }
257
258 void HYDROData_Image::MustBeUpdated(bool theFlag)
259 {
260   if (theFlag) {
261     TDataStd_UAttribute::Set(myLab, GUID_MUST_BE_UPDATED);
262   } else {
263     myLab.ForgetAttribute(GUID_MUST_BE_UPDATED);
264   }
265 }
266
267 bool HYDROData_Image::MustBeUpdated()
268 {
269   return myLab.IsAttribute(GUID_MUST_BE_UPDATED);
270 }