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 <TDataStd_AsciiString.hxx>
11 #include <TDF_ListIteratorOfLabelList.hxx>
13 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
15 IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Object)
16 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Object)
18 HYDROData_Image::HYDROData_Image()
22 HYDROData_Image::~HYDROData_Image()
26 void HYDROData_Image::SetImage(const QImage& theImage)
28 if (theImage.isNull()) {
29 // for empty image remove all previously stored attributes
30 myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
31 myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
34 // store width, height, bytes per line and format in integer array
35 Handle(TDataStd_IntegerArray) aParams;
36 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams)) {
37 aParams = TDataStd_IntegerArray::Set(myLab, 1, 4);
39 aParams->SetValue(1, theImage.width());
40 aParams->SetValue(2, theImage.height());
41 aParams->SetValue(3, theImage.bytesPerLine());
42 aParams->SetValue(4, (int)(theImage.format()));
43 // store data of image in byte array
44 const char* aData = (const char*)(theImage.bits());
45 SaveByteArray(0, aData, theImage.byteCount());
48 QImage HYDROData_Image::Image()
50 Handle(TDataStd_IntegerArray) aParams;
51 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
52 return QImage(); // return empty image if there is no array
54 uchar* anArray = (uchar*)ByteArray(0, aLen);
56 return QImage(); // return empty image if there is no array
57 QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
58 aParams->Value(3), QImage::Format(aParams->Value(4)));
62 void HYDROData_Image::SetFilePath(const QString& theFilePath)
64 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
65 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
68 QString HYDROData_Image::GetFilePath() const
72 Handle(TDataStd_AsciiString) anAsciiStr;
73 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
74 aRes = QString( anAsciiStr->Get().ToCString() );
79 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
81 // locate 9 coeffs of matrix into the real array
82 Handle(TDataStd_RealArray) anArray;
83 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
84 if (theTrsf.isIdentity()) return; // no need to store identity transformation
85 anArray = TDataStd_RealArray::Set(myLab, 1, 9);
87 anArray->SetValue(1, theTrsf.m11());
88 anArray->SetValue(2, theTrsf.m12());
89 anArray->SetValue(3, theTrsf.m13());
90 anArray->SetValue(4, theTrsf.m21());
91 anArray->SetValue(5, theTrsf.m22());
92 anArray->SetValue(6, theTrsf.m23());
93 anArray->SetValue(7, theTrsf.m31());
94 anArray->SetValue(8, theTrsf.m32());
95 anArray->SetValue(9, theTrsf.m33());
98 QTransform HYDROData_Image::Trsf()
100 // get 9 coeffs of matrix from the real array
101 Handle(TDataStd_RealArray) anArray;
102 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
103 return QTransform(); // return identity if there is no array
105 anArray->Value(1), anArray->Value(2), anArray->Value(3),
106 anArray->Value(4), anArray->Value(5), anArray->Value(6),
107 anArray->Value(7), anArray->Value(8), anArray->Value(9));
111 void HYDROData_Image::SetTrsfPoints(const QPoint& thePointAIn,
112 const QPoint& thePointBIn,
113 const QPoint& thePointCIn,
114 const QPointF& thePointAOut,
115 const QPointF& thePointBOut,
116 const QPointF& thePointCOut)
118 Handle(TDataStd_RealArray) anArray;
119 if (!myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
120 anArray = TDataStd_RealArray::Set(myLab.FindChild(DataTag_TrsfPoints), 1, 12);
122 anArray->SetValue(1, thePointAIn.x());
123 anArray->SetValue(2, thePointAIn.y());
124 anArray->SetValue(3, thePointBIn.x());
125 anArray->SetValue(4, thePointBIn.y());
126 anArray->SetValue(5, thePointCIn.x());
127 anArray->SetValue(6, thePointCIn.y());
128 anArray->SetValue(7, thePointAOut.x());
129 anArray->SetValue(8, thePointAOut.y());
130 anArray->SetValue(9, thePointBOut.x());
131 anArray->SetValue(10, thePointBOut.y());
132 anArray->SetValue(11, thePointCOut.x());
133 anArray->SetValue(12, thePointCOut.y());
136 void HYDROData_Image::TrsfPoints(QPoint& thePointAIn,
139 QPointF& thePointAOut,
140 QPointF& thePointBOut,
141 QPointF& thePointCOut)
143 Handle(TDataStd_RealArray) anArray;
144 if (myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
145 thePointAIn = QPointF( anArray->Value(1), anArray->Value(2) ).toPoint();
146 thePointBIn = QPointF( anArray->Value(3), anArray->Value(4) ).toPoint();
147 thePointCIn = QPointF( anArray->Value(5), anArray->Value(6) ).toPoint();
148 thePointAOut = QPointF( anArray->Value(7), anArray->Value(8) );
149 thePointBOut = QPointF( anArray->Value(9), anArray->Value(10) );
150 thePointCOut = QPointF( anArray->Value(11), anArray->Value(12) );
154 void HYDROData_Image::AppendReference(Handle(HYDROData_Image) theReferenced)
156 Handle(TDataStd_ReferenceList) aRefs;
157 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
158 aRefs = TDataStd_ReferenceList::Set(myLab);
159 aRefs->Append(theReferenced->Label());
162 int HYDROData_Image::NbReferences()
164 Handle(TDataStd_ReferenceList) aRefs;
165 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
167 return aRefs->Extent();
170 Handle(HYDROData_Image) HYDROData_Image::Reference(const int theIndex) const
172 Handle(TDataStd_ReferenceList) aRefs;
173 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
174 return Handle(HYDROData_Image)();
175 if (theIndex < 0 || theIndex >= aRefs->Extent())
176 return Handle(HYDROData_Image)();
178 TDF_ListIteratorOfLabelList anIter(aRefs->List());
179 for(int anIndex = 0; anIndex != theIndex; anIter.Next(), anIndex++);
180 const TDF_Label& aRefLab = anIter.Value();
181 return Handle(HYDROData_Image)::DownCast(HYDROData_Iterator::Object(aRefLab));
184 void HYDROData_Image::ChangeReference(
185 const int theIndex, Handle(HYDROData_Image) theReferenced)
187 Handle(TDataStd_ReferenceList) aRefs;
188 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
189 aRefs = TDataStd_ReferenceList::Set(myLab);
190 if (theIndex >= aRefs->Extent()) { // for too big index append it just to the end
191 AppendReference(theReferenced);
192 } else { // remove and insert new
193 TDF_ListIteratorOfLabelList anIter(aRefs->List());
195 for(; anIndex != theIndex; anIter.Next(), anIndex++);
196 const TDF_Label& aRemovedLab = anIter.Value();
198 aRefs->Remove(aRemovedLab);
200 aRefs->InsertBefore(theReferenced->Label(), anIter.Value());
202 aRefs->Append(theReferenced->Label());
206 void HYDROData_Image::RemoveReference(const int theIndex)
208 Handle(TDataStd_ReferenceList) aRefs;
209 if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
210 return; // no references, nothing to remove
211 if (aRefs->Extent() == 1 && theIndex == 0) { // remove all if only one
215 TDF_ListIteratorOfLabelList anIter(aRefs->List());
217 for(; anIndex != theIndex && anIter.More(); anIter.Next(), anIndex++);
219 aRefs->Remove(anIter.Value());
222 void HYDROData_Image::ClearReferences()
224 myLab.ForgetAttribute(TDataStd_ReferenceList::GetID());
227 void HYDROData_Image::SetOperatorName(const QString theOpName)
229 TDataStd_Name::Set(myLab.FindChild(DataTag_Operator),
230 TCollection_ExtendedString(theOpName.toLatin1().constData()));
233 QString HYDROData_Image::OperatorName()
235 Handle(TDataStd_Name) aName;
236 if (myLab.FindChild(DataTag_Operator).
237 FindAttribute(TDataStd_Name::GetID(), aName)) {
238 TCollection_AsciiString aStr(aName->Get());
239 return QString(aStr.ToCString());
244 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
246 SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
249 QByteArray HYDROData_Image::Args()
252 const char* aData = ByteArray(DataTag_Operator, aLen);
255 return QByteArray(aData, aLen);
258 void HYDROData_Image::MustBeUpdated(bool theFlag)
261 TDataStd_UAttribute::Set(myLab, GUID_MUST_BE_UPDATED);
263 myLab.ForgetAttribute(GUID_MUST_BE_UPDATED);
267 bool HYDROData_Image::MustBeUpdated()
269 return myLab.IsAttribute(GUID_MUST_BE_UPDATED);