2 #include "HYDROData_Image.h"
4 #include "HYDROData_Document.h"
5 #include "HYDROData_Tool.h"
6 #include "HYDROOperations_Factory.h"
8 #include <TDataStd_RealArray.hxx>
9 #include <TDataStd_ByteArray.hxx>
10 #include <TDataStd_IntegerArray.hxx>
11 #include <TDataStd_ReferenceList.hxx>
12 #include <TDataStd_Name.hxx>
13 #include <TDataStd_UAttribute.hxx>
14 #include <TDataStd_AsciiString.hxx>
16 #include <ImageComposer_Operator.h>
17 #include <ImageComposer_MetaTypes.h>
19 #include <QStringList>
21 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
22 static const Standard_GUID GUID_SELF_SPLITTED("997995aa-5c19-40bf-9a60-ab4b70ad04d8");
24 #define PYTHON_IMAGE_ID "KIND_IMAGE"
26 IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Object)
27 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Object)
29 HYDROData_Image::HYDROData_Image()
33 HYDROData_Image::~HYDROData_Image()
37 QStringList HYDROData_Image::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
41 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this );
42 if ( aDocument.IsNull() )
45 QString aDocName = aDocument->GetDocPyName();
46 QString anImageName = GetName();
48 aResList << QString( "%1 = %2.CreateObject( %3 );" )
49 .arg( anImageName ).arg( aDocName ).arg( PYTHON_IMAGE_ID );
50 aResList << QString( "%1.SetName( \"%2\" );" )
51 .arg( anImageName ).arg( anImageName );
53 QString aFilePath = GetFilePath();
54 if ( !aFilePath.isEmpty() )
56 aResList << QString( "" );
57 aResList << QString( "%1.LoadImage( \"%2\" );" )
58 .arg( anImageName ).arg( aFilePath );
60 // Dump transformation matrix for image
61 aResList << QString( "" );
63 QTransform aTrsf = Trsf();
65 aResList << QString( "trsf = QTransform( %1, %2, %3," )
66 .arg( aTrsf.m11() ).arg( aTrsf.m12() ).arg( aTrsf.m13() );
67 aResList << QString( " %1, %2, %3," )
68 .arg( aTrsf.m21() ).arg( aTrsf.m22() ).arg( aTrsf.m23() );
69 aResList << QString( " %1, %2, %3 );" )
70 .arg( aTrsf.m31() ).arg( aTrsf.m32() ).arg( aTrsf.m33() );
72 aResList << QString( "%1.SetTrsf( trsf );" ).arg( anImageName );
74 // Dump transformation points for image
75 aResList << QString( "" );
77 QPoint aPointAIn, aPointBIn, aPointCIn;
78 QPointF aPointAOut, aPointBOut, aPointCOut;
79 TrsfPoints( aPointAIn, aPointBIn, aPointCIn,
80 aPointAOut, aPointBOut, aPointCOut );
82 aResList << QString( "a_in = QPoint( %1, %2 );" )
83 .arg( aPointAIn.x() ).arg( aPointAIn.y() );
85 aResList << QString( "b_in = QPoint( %1, %2 );" )
86 .arg( aPointBIn.x() ).arg( aPointBIn.y() );
88 aResList << QString( "c_in = QPoint( %1, %2 );" )
89 .arg( aPointCIn.x() ).arg( aPointCIn.y() );
91 aResList << QString( "a_out = QPointF( %1, %2 );" )
92 .arg( aPointAOut.x() ).arg( aPointAOut.y() );
94 aResList << QString( "b_out = QPointF( %1, %2 );" )
95 .arg( aPointBOut.x() ).arg( aPointBOut.y() );
97 aResList << QString( "c_out = QPointF( %1, %2 );" )
98 .arg( aPointCOut.x() ).arg( aPointCOut.y() );
100 QString aGap = QString().fill( ' ', anImageName.size() + 16 );
101 aResList << QString( "%1.SetTrsfPoints( a_in, b_in, c_in," ).arg( anImageName );
102 aResList << QString( aGap + "a_out, b_out, c_out );" );
106 // Image is composed from other image(s)
108 QString anOperatorName = OperatorName();
109 if ( !anOperatorName.isEmpty() )
111 aResList << QString( "" );
113 aResList << QString( "%1.SetOperatorName( \"%2\" );" )
114 .arg( anImageName ).arg( anOperatorName );
116 ImageComposer_Operator* anImageOp = HYDROOperations_Factory::Factory()->Operator( this );
119 // Dump operation arguments
120 QString anOpArgsArrayName;
121 QStringList anOpArgs = anImageOp->dumpArgsToPython( anOpArgsArrayName );
122 if ( !anOpArgs.isEmpty() )
124 aResList << QString( "" );
125 aResList << anOpArgs;
127 aResList << QString( "" );
128 aResList << QString( "%1.SetArgs( %2 );" )
129 .arg( anImageName ).arg( anOpArgsArrayName );
134 int aNbReferences = NbReferences();
135 if ( aNbReferences > 0 )
137 aResList << QString( "" );
139 for ( int i = 0; i < aNbReferences; ++i )
141 Handle(HYDROData_Image) aRefImg = Handle(HYDROData_Image)::DownCast( Reference( i ) );
142 if ( aRefImg.IsNull() )
145 QString aRefImgName = aRefImg->GetName();
147 // The definition of reference image must be dumped before this
148 if ( !theTreatedObjects.contains( aRefImgName ) )
150 // Write definition of reference image
151 QStringList aRefImgDump = aRefImg->DumpToPython( theTreatedObjects );
152 if ( aRefImgDump.isEmpty() )
155 QStringList aTmpList = aResList;
156 aResList = aRefImgDump;
158 aResList.append( "" );
159 aResList.append( aTmpList );
161 theTreatedObjects.insert( aRefImgName, aRefImg );
164 aResList << QString( "%1.AppendReference( %2 );" )
165 .arg( anImageName ).arg( aRefImgName );
169 // Necessary to update image in case of composed operator
170 aResList << QString( "" );
171 aResList << QString( "%1.Update( False );" ).arg( anImageName );
177 void HYDROData_Image::Update( const bool theIsForce )
179 HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory();
181 // Update image only if there is an operation
182 ImageComposer_Operator* anOp = aFactory->Operator( OperatorName() );
185 // Fill by arguments and process the operation
186 QVariant anObj1, anObj2;
187 int aNbReferences = NbReferences();
189 if ( aNbReferences > 0 )
191 // First referenced object
192 Handle(HYDROData_Object) aRefObj = Reference( 0 );
193 if ( !aRefObj.IsNull() )
195 anObj1 = aRefObj->GetDataVariant();
196 if ( !anObj1.isNull() && anObj1.canConvert<ImageComposer_Image>() )
198 ImageComposer_Image anImage = anObj1.value<ImageComposer_Image>();
199 QTransform aTransform = anImage.transform();
200 SetTrsf( aTransform );
205 if ( aNbReferences > 1 )
207 // Second referenced object
208 Handle(HYDROData_Object) aRefObj = Reference( 1 );
209 if ( !aRefObj.IsNull() )
210 anObj2 = aRefObj->GetDataVariant();
213 ImageComposer_Image aResImg = anOp->process( anObj1, anObj2 );
219 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this );
220 if ( !aDocument.IsNull() )
222 // Change the states of this and all depended images
223 MustBeUpdated( true );
224 HYDROData_Tool::SetMustBeUpdatedImages( aDocument );
225 MustBeUpdated( false );
230 QVariant HYDROData_Image::GetDataVariant()
232 QTransform aTransform = Trsf();
234 ImageComposer_Image anImage = Image();
235 anImage.setTransform( aTransform );
238 aVarData.setValue<ImageComposer_Image>( anImage );
243 void HYDROData_Image::SetImage(const QImage& theImage)
245 if (theImage.isNull()) {
246 // for empty image remove all previously stored attributes
247 myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
248 myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
251 // store width, height, bytes per line and format in integer array
252 Handle(TDataStd_IntegerArray) aParams;
253 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams)) {
254 aParams = TDataStd_IntegerArray::Set(myLab, 1, 4);
256 aParams->SetValue(1, theImage.width());
257 aParams->SetValue(2, theImage.height());
258 aParams->SetValue(3, theImage.bytesPerLine());
259 aParams->SetValue(4, (int)(theImage.format()));
260 // store data of image in byte array
261 const char* aData = (const char*)(theImage.bits());
262 SaveByteArray(0, aData, theImage.byteCount());
265 bool HYDROData_Image::LoadImage(const QString& theFilePath)
267 QImage anImage( theFilePath );
269 return !anImage.isNull();
272 QImage HYDROData_Image::Image()
274 Handle(TDataStd_IntegerArray) aParams;
275 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
276 return QImage(); // return empty image if there is no array
278 uchar* anArray = (uchar*)ByteArray(0, aLen);
280 return QImage(); // return empty image if there is no array
281 QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
282 aParams->Value(3), QImage::Format(aParams->Value(4)));
286 void HYDROData_Image::SetFilePath(const QString& theFilePath)
288 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
289 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
292 QString HYDROData_Image::GetFilePath() const
296 Handle(TDataStd_AsciiString) anAsciiStr;
297 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
298 aRes = QString( anAsciiStr->Get().ToCString() );
303 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
305 // locate 9 coeffs of matrix into the real array
306 Handle(TDataStd_RealArray) anArray;
307 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
308 if (theTrsf.isIdentity()) return; // no need to store identity transformation
309 anArray = TDataStd_RealArray::Set(myLab, 1, 9);
311 anArray->SetValue(1, theTrsf.m11());
312 anArray->SetValue(2, theTrsf.m12());
313 anArray->SetValue(3, theTrsf.m13());
314 anArray->SetValue(4, theTrsf.m21());
315 anArray->SetValue(5, theTrsf.m22());
316 anArray->SetValue(6, theTrsf.m23());
317 anArray->SetValue(7, theTrsf.m31());
318 anArray->SetValue(8, theTrsf.m32());
319 anArray->SetValue(9, theTrsf.m33());
322 QTransform HYDROData_Image::Trsf() const
324 // get 9 coeffs of matrix from the real array
325 Handle(TDataStd_RealArray) anArray;
326 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
327 return QTransform(); // return identity if there is no array
329 anArray->Value(1), anArray->Value(2), anArray->Value(3),
330 anArray->Value(4), anArray->Value(5), anArray->Value(6),
331 anArray->Value(7), anArray->Value(8), anArray->Value(9));
335 void HYDROData_Image::SetTrsfPoints(const QPoint& thePointAIn,
336 const QPoint& thePointBIn,
337 const QPoint& thePointCIn,
338 const QPointF& thePointAOut,
339 const QPointF& thePointBOut,
340 const QPointF& thePointCOut)
342 Handle(TDataStd_RealArray) anArray;
343 if (!myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
344 anArray = TDataStd_RealArray::Set(myLab.FindChild(DataTag_TrsfPoints), 1, 12);
346 anArray->SetValue(1, thePointAIn.x());
347 anArray->SetValue(2, thePointAIn.y());
348 anArray->SetValue(3, thePointBIn.x());
349 anArray->SetValue(4, thePointBIn.y());
350 anArray->SetValue(5, thePointCIn.x());
351 anArray->SetValue(6, thePointCIn.y());
352 anArray->SetValue(7, thePointAOut.x());
353 anArray->SetValue(8, thePointAOut.y());
354 anArray->SetValue(9, thePointBOut.x());
355 anArray->SetValue(10, thePointBOut.y());
356 anArray->SetValue(11, thePointCOut.x());
357 anArray->SetValue(12, thePointCOut.y());
360 void HYDROData_Image::TrsfPoints(QPoint& thePointAIn,
363 QPointF& thePointAOut,
364 QPointF& thePointBOut,
365 QPointF& thePointCOut) const
367 Handle(TDataStd_RealArray) anArray;
368 if (myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
369 thePointAIn = QPointF( anArray->Value(1), anArray->Value(2) ).toPoint();
370 thePointBIn = QPointF( anArray->Value(3), anArray->Value(4) ).toPoint();
371 thePointCIn = QPointF( anArray->Value(5), anArray->Value(6) ).toPoint();
372 thePointAOut = QPointF( anArray->Value(7), anArray->Value(8) );
373 thePointBOut = QPointF( anArray->Value(9), anArray->Value(10) );
374 thePointCOut = QPointF( anArray->Value(11), anArray->Value(12) );
378 bool HYDROData_Image::HasTrsfPoints() const
380 Handle(TDataStd_RealArray) anArray;
381 return myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray);
384 void HYDROData_Image::AppendReference( const Handle(HYDROData_Object)& theReferenced )
386 AddReferenceObject( theReferenced, 0 );
389 int HYDROData_Image::NbReferences() const
391 return NbReferenceObjects( 0 );
394 Handle(HYDROData_Object) HYDROData_Image::Reference( const int theIndex ) const
396 return GetReferenceObject( 0, theIndex );
399 void HYDROData_Image::ChangeReference(
400 const int theIndex, Handle(HYDROData_Object) theReferenced)
402 SetReferenceObject( theReferenced, 0, theIndex );
405 void HYDROData_Image::RemoveReference(const int theIndex)
407 RemoveReferenceObject( 0, theIndex );
410 void HYDROData_Image::ClearReferences()
412 ClearReferenceObjects( 0 );
415 void HYDROData_Image::SetOperatorName(const QString theOpName)
417 TDataStd_Name::Set(myLab.FindChild(DataTag_Operator),
418 TCollection_ExtendedString(theOpName.toLatin1().constData()));
421 QString HYDROData_Image::OperatorName() const
423 Handle(TDataStd_Name) aName;
424 if (myLab.FindChild(DataTag_Operator).
425 FindAttribute(TDataStd_Name::GetID(), aName)) {
426 TCollection_AsciiString aStr(aName->Get());
427 return QString(aStr.ToCString());
432 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
434 SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
437 QByteArray HYDROData_Image::Args() const
440 const char* aData = ByteArray(DataTag_Operator, aLen);
443 return QByteArray(aData, aLen);
446 void HYDROData_Image::MustBeUpdated(bool theFlag)
449 TDataStd_UAttribute::Set(myLab, GUID_MUST_BE_UPDATED);
451 myLab.ForgetAttribute(GUID_MUST_BE_UPDATED);
455 bool HYDROData_Image::MustBeUpdated() const
457 return myLab.IsAttribute(GUID_MUST_BE_UPDATED);
460 void HYDROData_Image::SetIsSelfSplitted(bool theFlag)
463 TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLITTED);
465 myLab.ForgetAttribute(GUID_SELF_SPLITTED);
469 bool HYDROData_Image::IsSelfSplitted() const
471 return myLab.IsAttribute(GUID_SELF_SPLITTED);