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