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