1 #include <HYDROData_Image.h>
2 #include <HYDROData_Iterator.h>
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>
12 // tag of the child of my label that contains information about the operator
13 static const int TAG_OPERATOR = 1;
15 // tag of the child of my label that contains information transformation points
16 static const int TAG_TRSF_POINTS = 2;
18 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
20 IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Object)
21 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Object)
23 HYDROData_Image::HYDROData_Image()
27 HYDROData_Image::~HYDROData_Image()
31 void HYDROData_Image::SetImage(const QImage& theImage)
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());
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);
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());
53 QImage HYDROData_Image::Image()
55 Handle(TDataStd_IntegerArray) aParams;
56 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
57 return QImage(); // return empty image if there is no array
59 uchar* anArray = (uchar*)ByteArray(0, 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)));
67 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
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);
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());
86 QTransform HYDROData_Image::Trsf()
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
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));
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)
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);
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());
124 void HYDROData_Image::TrsfPoints(QPoint& thePointAIn,
127 QPointF& thePointAOut,
128 QPointF& thePointBOut,
129 QPointF& thePointCOut)
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) );
142 void HYDROData_Image::AppendReference(Handle(HYDROData_Image) theReferenced)
144 Handle(TDataStd_ReferenceList) aRefs;
145 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
146 aRefs = TDataStd_ReferenceList::Set(myLab);
147 aRefs->Append(theReferenced->Label());
150 int HYDROData_Image::NbReferences()
152 Handle(TDataStd_ReferenceList) aRefs;
153 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
155 return aRefs->Extent();
158 Handle(HYDROData_Image) HYDROData_Image::Reference(const int theIndex) const
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)();
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));
172 void HYDROData_Image::ChangeReference(
173 const int theIndex, Handle(HYDROData_Image) theReferenced)
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());
183 for(; anIndex != theIndex; anIter.Next(), anIndex++);
184 const TDF_Label& aRemovedLab = anIter.Value();
186 aRefs->Remove(aRemovedLab);
188 aRefs->InsertBefore(theReferenced->Label(), anIter.Value());
190 aRefs->Append(theReferenced->Label());
194 void HYDROData_Image::RemoveReference(const int theIndex)
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
203 TDF_ListIteratorOfLabelList anIter(aRefs->List());
205 for(; anIndex != theIndex && anIter.More(); anIter.Next(), anIndex++);
207 aRefs->Remove(anIter.Value());
210 void HYDROData_Image::ClearReferences()
212 myLab.ForgetAttribute(TDataStd_ReferenceList::GetID());
215 void HYDROData_Image::SetOperatorName(const QString theOpName)
217 TDataStd_Name::Set(myLab.FindChild(TAG_OPERATOR),
218 TCollection_ExtendedString(theOpName.toLatin1().constData()));
221 QString HYDROData_Image::OperatorName()
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());
232 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
234 SaveByteArray(TAG_OPERATOR, theArgs.constData(), theArgs.length());
237 QByteArray HYDROData_Image::Args()
240 const char* aData = ByteArray(TAG_OPERATOR, aLen);
243 return QByteArray(aData, aLen);
246 void HYDROData_Image::MustBeUpdated(bool theFlag)
249 TDataStd_UAttribute::Set(myLab, GUID_MUST_BE_UPDATED);
251 myLab.ForgetAttribute(GUID_MUST_BE_UPDATED);
255 bool HYDROData_Image::MustBeUpdated()
257 return myLab.IsAttribute(GUID_MUST_BE_UPDATED);