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 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
14 IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Object)
15 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Object)
17 HYDROData_Image::HYDROData_Image()
21 HYDROData_Image::~HYDROData_Image()
25 void HYDROData_Image::SetImage(const QImage& theImage)
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());
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);
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());
47 QImage HYDROData_Image::Image()
49 Handle(TDataStd_IntegerArray) aParams;
50 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
51 return QImage(); // return empty image if there is no array
53 uchar* anArray = (uchar*)ByteArray(0, 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)));
61 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
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);
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());
80 QTransform HYDROData_Image::Trsf()
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
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));
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)
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);
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());
118 void HYDROData_Image::TrsfPoints(QPoint& thePointAIn,
121 QPointF& thePointAOut,
122 QPointF& thePointBOut,
123 QPointF& thePointCOut)
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) );
136 void HYDROData_Image::AppendReference(Handle(HYDROData_Image) theReferenced)
138 Handle(TDataStd_ReferenceList) aRefs;
139 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
140 aRefs = TDataStd_ReferenceList::Set(myLab);
141 aRefs->Append(theReferenced->Label());
144 int HYDROData_Image::NbReferences()
146 Handle(TDataStd_ReferenceList) aRefs;
147 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
149 return aRefs->Extent();
152 Handle(HYDROData_Image) HYDROData_Image::Reference(const int theIndex) const
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)();
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));
166 void HYDROData_Image::ChangeReference(
167 const int theIndex, Handle(HYDROData_Image) theReferenced)
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());
177 for(; anIndex != theIndex; anIter.Next(), anIndex++);
178 const TDF_Label& aRemovedLab = anIter.Value();
180 aRefs->Remove(aRemovedLab);
182 aRefs->InsertBefore(theReferenced->Label(), anIter.Value());
184 aRefs->Append(theReferenced->Label());
188 void HYDROData_Image::RemoveReference(const int theIndex)
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
197 TDF_ListIteratorOfLabelList anIter(aRefs->List());
199 for(; anIndex != theIndex && anIter.More(); anIter.Next(), anIndex++);
201 aRefs->Remove(anIter.Value());
204 void HYDROData_Image::ClearReferences()
206 myLab.ForgetAttribute(TDataStd_ReferenceList::GetID());
209 void HYDROData_Image::SetOperatorName(const QString theOpName)
211 TDataStd_Name::Set(myLab.FindChild(DataTag_Operator),
212 TCollection_ExtendedString(theOpName.toLatin1().constData()));
215 QString HYDROData_Image::OperatorName()
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());
226 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
228 SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
231 QByteArray HYDROData_Image::Args()
234 const char* aData = ByteArray(DataTag_Operator, aLen);
237 return QByteArray(aData, aLen);
240 void HYDROData_Image::MustBeUpdated(bool theFlag)
243 TDataStd_UAttribute::Set(myLab, GUID_MUST_BE_UPDATED);
245 myLab.ForgetAttribute(GUID_MUST_BE_UPDATED);
249 bool HYDROData_Image::MustBeUpdated()
251 return myLab.IsAttribute(GUID_MUST_BE_UPDATED);