Salome HOME
Undo/Redo 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 // 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");
15
16 IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Object)
17 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Object)
18
19 HYDROData_Image::HYDROData_Image()
20 {
21 }
22
23 HYDROData_Image::~HYDROData_Image()
24 {
25 }
26
27 void HYDROData_Image::SetImage(const QImage& theImage)
28 {
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());
33     return;
34   }
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);
39   }
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());
47 }
48
49 QImage HYDROData_Image::Image()
50 {
51   Handle(TDataStd_IntegerArray) aParams;
52   if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
53     return QImage(); // return empty image if there is no array
54   int aLen = 0;
55   uchar* anArray = (uchar*)ByteArray(0, aLen);
56   if (!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)));
60   return aResult;
61 }
62
63 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
64 {
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);
70   }
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());
80 }
81
82 QTransform HYDROData_Image::Trsf()
83 {
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
88   QTransform aTrsf(
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));
92   return aTrsf;
93 }
94
95 void HYDROData_Image::AppendReference(Handle(HYDROData_Image) theReferenced)
96 {
97   Handle(TDataStd_ReferenceList) aRefs;
98   if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
99     aRefs = TDataStd_ReferenceList::Set(myLab);
100   aRefs->Append(theReferenced->Label());
101 }
102
103 int HYDROData_Image::NbReferences()
104 {
105   Handle(TDataStd_ReferenceList) aRefs;
106   if (!myLab.FindAttribute(TDataStd_ReferenceList::GetID(), aRefs))
107     return 0;
108   return aRefs->Extent();
109 }
110
111 Handle(HYDROData_Image) HYDROData_Image::Reference(const int theIndex) const
112 {
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)();
118
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));
123 }
124
125 void HYDROData_Image::ChangeReference(
126     const int theIndex, Handle(HYDROData_Image) theReferenced)
127 {
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());
135     int anIndex = 0;
136     for(; anIndex != theIndex; anIter.Next(), anIndex++);
137     const TDF_Label& aRemovedLab = anIter.Value();
138     anIter.Next();
139     aRefs->Remove(aRemovedLab);
140     if (anIter.More()) 
141       aRefs->InsertBefore(theReferenced->Label(), anIter.Value());
142     else 
143       aRefs->Append(theReferenced->Label());
144   }
145 }
146
147 void HYDROData_Image::RemoveReference(const int theIndex)
148 {
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
153     ClearReferences();
154     return;
155   }
156   TDF_ListIteratorOfLabelList anIter(aRefs->List());
157   int anIndex = 0;
158   for(; anIndex != theIndex && anIter.More(); anIter.Next(), anIndex++);
159   if (anIter.More())
160     aRefs->Remove(anIter.Value());
161 }
162
163 void HYDROData_Image::ClearReferences()
164 {
165   myLab.ForgetAttribute(TDataStd_ReferenceList::GetID());
166 }
167
168 void HYDROData_Image::SetOperatorName(const QString theOpName)
169 {
170   TDataStd_Name::Set(myLab.FindChild(TAG_OPERATOR),
171     TCollection_ExtendedString(theOpName.toLatin1().constData()));
172 }
173
174 QString HYDROData_Image::OperatorName()
175 {
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());
181   }
182   return QString();
183 }
184
185 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
186 {
187   SaveByteArray(TAG_OPERATOR, theArgs.constData(), theArgs.length());
188 }
189
190 QByteArray HYDROData_Image::Args()
191 {
192   int aLen = 0;
193   const char* aData = ByteArray(TAG_OPERATOR, aLen);
194   if (!aLen)
195     return QByteArray();
196   return QByteArray(aData, aLen);
197 }
198
199 void HYDROData_Image::MustBeUpdated(bool theFlag)
200 {
201   if (theFlag) {
202     TDataStd_UAttribute::Set(myLab, GUID_MUST_BE_UPDATED);
203   } else {
204     myLab.ForgetAttribute(GUID_MUST_BE_UPDATED);
205   }
206 }
207
208 bool HYDROData_Image::MustBeUpdated()
209 {
210   return myLab.IsAttribute(GUID_MUST_BE_UPDATED);
211 }