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 // tage of the child of my label that contains information about the operator
13 static const int TAG_OPERATOR = 1;
14 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
16 IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Object)
17 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Object)
19 HYDROData_Image::HYDROData_Image()
23 HYDROData_Image::~HYDROData_Image()
27 void HYDROData_Image::SetImage(const QImage& theImage)
29 if (theImage.isNull()) {
30 // for empty image remove all previously stored attributes
31 myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
32 myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
35 // store width, height, bytes per line and format in integer array
36 Handle(TDataStd_IntegerArray) aParams;
37 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams)) {
38 aParams = TDataStd_IntegerArray::Set(myLab, 1, 4);
40 aParams->SetValue(1, theImage.width());
41 aParams->SetValue(2, theImage.height());
42 aParams->SetValue(3, theImage.bytesPerLine());
43 aParams->SetValue(4, (int)(theImage.format()));
44 // store data of image in byte array
45 const char* aData = (const char*)(theImage.bits());
46 SaveByteArray(0, aData, theImage.byteCount());
49 QImage HYDROData_Image::Image()
51 Handle(TDataStd_IntegerArray) aParams;
52 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
53 return QImage(); // return empty image if there is no array
55 uchar* anArray = (uchar*)ByteArray(0, aLen);
57 return QImage(); // return empty image if there is no array
58 QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
59 aParams->Value(3), QImage::Format(aParams->Value(4)));
63 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
65 // locate 9 coeffs of matrix into the real array
66 Handle(TDataStd_RealArray) anArray;
67 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
68 if (theTrsf.isIdentity()) return; // no need to store identity transformation
69 anArray = TDataStd_RealArray::Set(myLab, 1, 9);
71 anArray->SetValue(1, theTrsf.m11());
72 anArray->SetValue(2, theTrsf.m12());
73 anArray->SetValue(3, theTrsf.m13());
74 anArray->SetValue(4, theTrsf.m21());
75 anArray->SetValue(5, theTrsf.m22());
76 anArray->SetValue(6, theTrsf.m23());
77 anArray->SetValue(7, theTrsf.m31());
78 anArray->SetValue(8, theTrsf.m32());
79 anArray->SetValue(9, theTrsf.m33());
82 QTransform HYDROData_Image::Trsf()
84 // get 9 coeffs of matrix from the real array
85 Handle(TDataStd_RealArray) anArray;
86 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
87 return QTransform(); // return identity if there is no array
89 anArray->Value(1), anArray->Value(2), anArray->Value(3),
90 anArray->Value(4), anArray->Value(5), anArray->Value(6),
91 anArray->Value(7), anArray->Value(8), anArray->Value(9));
95 void HYDROData_Image::AppendReference(Handle(HYDROData_Image) theReferenced)
97 Handle(TDataStd_ReferenceList) aRefs;
98 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
99 aRefs = TDataStd_ReferenceList::Set(myLab);
100 aRefs->Append(theReferenced->Label());
103 int HYDROData_Image::NbReferences()
105 Handle(TDataStd_ReferenceList) aRefs;
106 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
108 return aRefs->Extent();
111 Handle(HYDROData_Image) HYDROData_Image::Reference(const int theIndex) const
113 Handle(TDataStd_ReferenceList) aRefs;
114 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
115 return Handle(HYDROData_Image)();
116 if (theIndex < 0 || theIndex >= aRefs->Extent())
117 return Handle(HYDROData_Image)();
119 TDF_ListIteratorOfLabelList anIter(aRefs->List());
120 for(int anIndex = 0; anIndex != theIndex; anIter.Next(), anIndex++);
121 const TDF_Label& aRefLab = anIter.Value();
122 return Handle(HYDROData_Image)::DownCast(HYDROData_Iterator::Object(aRefLab));
125 void HYDROData_Image::ChangeReference(
126 const int theIndex, Handle(HYDROData_Image) theReferenced)
128 Handle(TDataStd_ReferenceList) aRefs;
129 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
130 aRefs = TDataStd_ReferenceList::Set(myLab);
131 if (theIndex >= aRefs->Extent()) { // for too big index append it just to the end
132 AppendReference(theReferenced);
133 } else { // remove and insert new
134 TDF_ListIteratorOfLabelList anIter(aRefs->List());
136 for(; anIndex != theIndex; anIter.Next(), anIndex++);
137 const TDF_Label& aRemovedLab = anIter.Value();
139 aRefs->Remove(aRemovedLab);
141 aRefs->InsertBefore(theReferenced->Label(), anIter.Value());
143 aRefs->Append(theReferenced->Label());
147 void HYDROData_Image::RemoveReference(const int theIndex)
149 Handle(TDataStd_ReferenceList) aRefs;
150 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
151 return; // no references, nothing to remove
152 if (aRefs->Extent() == 1 && theIndex == 0) { // remove all if only one
156 TDF_ListIteratorOfLabelList anIter(aRefs->List());
158 for(; anIndex != theIndex && anIter.More(); anIter.Next(), anIndex++);
160 aRefs->Remove(anIter.Value());
163 void HYDROData_Image::ClearReferences()
165 myLab.ForgetAttribute(TDataStd_ReferenceList::GetID());
168 void HYDROData_Image::SetOperatorName(const QString theOpName)
170 TDataStd_Name::Set(myLab.FindChild(TAG_OPERATOR),
171 TCollection_ExtendedString(theOpName.toLatin1().constData()));
174 QString HYDROData_Image::OperatorName()
176 Handle(TDataStd_Name) aName;
177 if (myLab.FindChild(TAG_OPERATOR).
178 FindAttribute(TDataStd_Name::GetID(), aName)) {
179 TCollection_AsciiString aStr(aName->Get());
180 return QString(aStr.ToCString());
185 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
187 SaveByteArray(TAG_OPERATOR, theArgs.constData(), theArgs.length());
190 QByteArray HYDROData_Image::Args()
193 const char* aData = ByteArray(TAG_OPERATOR, aLen);
196 return QByteArray(aData, aLen);
199 void HYDROData_Image::MustBeUpdated(bool theFlag)
202 TDataStd_UAttribute::Set(myLab, GUID_MUST_BE_UPDATED);
204 myLab.ForgetAttribute(GUID_MUST_BE_UPDATED);
208 bool HYDROData_Image::MustBeUpdated()
210 return myLab.IsAttribute(GUID_MUST_BE_UPDATED);