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_Integer.hxx>
11 #include <TDataStd_IntegerArray.hxx>
12 #include <TDataStd_ReferenceList.hxx>
13 #include <TDataStd_Name.hxx>
14 #include <TDataStd_UAttribute.hxx>
15 #include <TDataStd_AsciiString.hxx>
17 #include <ImageComposer_Operator.h>
18 #include <ImageComposer_MetaTypes.h>
20 #include <QStringList>
22 static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
23 static const Standard_GUID GUID_SELF_SPLITTED("997995aa-5c19-40bf-9a60-ab4b70ad04d8");
25 #define PYTHON_IMAGE_ID "KIND_IMAGE"
27 IMPLEMENT_STANDARD_HANDLE(HYDROData_Image, HYDROData_Object)
28 IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Image, HYDROData_Object)
30 HYDROData_Image::HYDROData_Image()
34 HYDROData_Image::~HYDROData_Image()
38 QStringList HYDROData_Image::DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const
42 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this );
43 if ( aDocument.IsNull() )
46 QString aDocName = aDocument->GetDocPyName();
47 QString anImageName = GetName();
49 aResList << QString( "%1 = %2.CreateObject( %3 );" )
50 .arg( anImageName ).arg( aDocName ).arg( PYTHON_IMAGE_ID );
51 aResList << QString( "%1.SetName( \"%2\" );" )
52 .arg( anImageName ).arg( anImageName );
54 QString aFilePath = GetFilePath();
55 if ( !aFilePath.isEmpty() )
57 aResList << QString( "" );
58 aResList << QString( "%1.LoadImage( \"%2\" );" )
59 .arg( anImageName ).arg( aFilePath );
61 // Dump transformation matrix for image
62 aResList << QString( "" );
64 QTransform aTrsf = Trsf();
66 aResList << QString( "trsf = QTransform( %1, %2, %3," )
67 .arg( aTrsf.m11() ).arg( aTrsf.m12() ).arg( aTrsf.m13() );
68 aResList << QString( " %1, %2, %3," )
69 .arg( aTrsf.m21() ).arg( aTrsf.m22() ).arg( aTrsf.m23() );
70 aResList << QString( " %1, %2, %3 );" )
71 .arg( aTrsf.m31() ).arg( aTrsf.m32() ).arg( aTrsf.m33() );
73 aResList << QString( "%1.SetTrsf( trsf );" ).arg( anImageName );
75 // Dump transformation points for image
76 aResList << QString( "" );
78 QPoint aPointA, aPointB, aPointC;
79 QPointF aLambertPointA, aLambertPointB, aLambertPointC;
80 QPointF aCartesianPointA, aCartesianPointB, aCartesianPointC;
81 TrsfPoints( aPointA, aPointB, aPointC,
82 aLambertPointA, aLambertPointB, aLambertPointC,
83 aCartesianPointA, aCartesianPointB, aCartesianPointC );
85 aResList << QString( "pa = QPoint( %1, %2 );" )
86 .arg( aPointA.x() ).arg( aPointA.y() );
88 aResList << QString( "pb = QPoint( %1, %2 );" )
89 .arg( aPointB.x() ).arg( aPointB.y() );
91 aResList << QString( "pc = QPoint( %1, %2 );" )
92 .arg( aPointC.x() ).arg( aPointC.y() );
94 aResList << QString( "lpa = QPointF( %1, %2 );" )
95 .arg( aLambertPointA.x() ).arg( aLambertPointA.y() );
97 aResList << QString( "lpb = QPointF( %1, %2 );" )
98 .arg( aLambertPointB.x() ).arg( aLambertPointB.y() );
100 aResList << QString( "lpc = QPointF( %1, %2 );" )
101 .arg( aLambertPointC.x() ).arg( aLambertPointC.y() );
103 aResList << QString( "cpa = QPointF( %1, %2 );" )
104 .arg( aCartesianPointA.x() ).arg( aCartesianPointA.y() );
106 aResList << QString( "cpb = QPointF( %1, %2 );" )
107 .arg( aCartesianPointB.x() ).arg( aCartesianPointB.y() );
109 aResList << QString( "cpc = QPointF( %1, %2 );" )
110 .arg( aCartesianPointC.x() ).arg( aCartesianPointC.y() );
112 QString aGap = QString().fill( ' ', anImageName.size() + 16 );
113 aResList << QString( "%1.SetTrsfPoints( pa, pb, pc," ).arg( anImageName );
114 aResList << QString( aGap + "lpa, lpb, lpc," );
115 aResList << QString( aGap + "cpa, cpb, cpc );" );
119 // Image is composed from other image(s)
121 QString anOperatorName = OperatorName();
122 if ( !anOperatorName.isEmpty() )
124 aResList << QString( "" );
126 aResList << QString( "%1.SetOperatorName( \"%2\" );" )
127 .arg( anImageName ).arg( anOperatorName );
129 ImageComposer_Operator* anImageOp = HYDROOperations_Factory::Factory()->Operator( this );
132 // Dump operation arguments
133 QString anOpArgsArrayName;
134 QStringList anOpArgs = anImageOp->dumpArgsToPython( anOpArgsArrayName );
135 if ( !anOpArgs.isEmpty() )
137 aResList << QString( "" );
138 aResList << anOpArgs;
140 aResList << QString( "" );
141 aResList << QString( "%1.SetArgs( %2 );" )
142 .arg( anImageName ).arg( anOpArgsArrayName );
147 int aNbReferences = NbReferences();
148 if ( aNbReferences > 0 )
150 aResList << QString( "" );
152 for ( int i = 0; i < aNbReferences; ++i )
154 Handle(HYDROData_Image) aRefImg = Handle(HYDROData_Image)::DownCast( Reference( i ) );
155 setPythonReferenceObject( theTreatedObjects, aResList, aRefImg, "AppendReference" );
159 // Necessary to update image in case of composed operator
160 aResList << QString( "" );
161 aResList << QString( "%1.Update( False );" ).arg( anImageName );
167 void HYDROData_Image::Update( const bool theIsForce )
169 HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory();
171 // Update image only if there is an operation
172 ImageComposer_Operator* anOp = aFactory->Operator( OperatorName() );
175 // Fill by arguments and process the operation
176 QVariant anObj1, anObj2;
177 int aNbReferences = NbReferences();
179 if ( aNbReferences > 0 )
181 // First referenced object
182 Handle(HYDROData_Object) aRefObj = Reference( 0 );
183 if ( !aRefObj.IsNull() )
185 anObj1 = aRefObj->GetDataVariant();
186 if ( !anObj1.isNull() && anObj1.canConvert<ImageComposer_Image>() )
188 ImageComposer_Image anImage = anObj1.value<ImageComposer_Image>();
189 QTransform aTransform = anImage.transform();
190 SetTrsf( aTransform );
195 if ( aNbReferences > 1 )
197 // Second referenced object
198 Handle(HYDROData_Object) aRefObj = Reference( 1 );
199 if ( !aRefObj.IsNull() )
200 anObj2 = aRefObj->GetDataVariant();
203 ImageComposer_Image aResImg = anOp->process( anObj1, anObj2 );
209 Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this );
210 if ( !aDocument.IsNull() )
212 // Change the states of this and all depended images
213 MustBeUpdated( true );
214 HYDROData_Tool::SetMustBeUpdatedImages( aDocument );
215 MustBeUpdated( false );
220 QVariant HYDROData_Image::GetDataVariant()
222 QTransform aTransform = Trsf();
224 ImageComposer_Image anImage = Image();
225 anImage.setTransform( aTransform );
228 aVarData.setValue<ImageComposer_Image>( anImage );
233 void HYDROData_Image::SetImage(const QImage& theImage)
235 if (theImage.isNull()) {
236 // for empty image remove all previously stored attributes
237 myLab.ForgetAttribute(TDataStd_IntegerArray::GetID());
238 myLab.ForgetAttribute(TDataStd_ByteArray::GetID());
241 // store width, height, bytes per line and format in integer array
242 Handle(TDataStd_IntegerArray) aParams;
243 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams)) {
244 aParams = TDataStd_IntegerArray::Set(myLab, 1, 4);
246 aParams->SetValue(1, theImage.width());
247 aParams->SetValue(2, theImage.height());
248 aParams->SetValue(3, theImage.bytesPerLine());
249 aParams->SetValue(4, (int)(theImage.format()));
250 // store data of image in byte array
251 const char* aData = (const char*)(theImage.bits());
252 SaveByteArray(0, aData, theImage.byteCount());
255 bool HYDROData_Image::LoadImage(const QString& theFilePath)
257 QImage anImage( theFilePath );
259 return !anImage.isNull();
262 QImage HYDROData_Image::Image()
264 Handle(TDataStd_IntegerArray) aParams;
265 if (!myLab.FindAttribute(TDataStd_IntegerArray::GetID(), aParams))
266 return QImage(); // return empty image if there is no array
268 uchar* anArray = (uchar*)ByteArray(0, aLen);
270 return QImage(); // return empty image if there is no array
271 QImage aResult(anArray, aParams->Value(1), aParams->Value(2),
272 aParams->Value(3), QImage::Format(aParams->Value(4)));
276 void HYDROData_Image::SetFilePath(const QString& theFilePath)
278 TCollection_AsciiString anAsciiStr( theFilePath.toStdString().c_str() );
279 TDataStd_AsciiString::Set( myLab.FindChild( DataTag_FilePath ), anAsciiStr );
282 QString HYDROData_Image::GetFilePath() const
286 Handle(TDataStd_AsciiString) anAsciiStr;
287 if ( myLab.FindChild( DataTag_FilePath ).FindAttribute( TDataStd_AsciiString::GetID(), anAsciiStr ) )
288 aRes = QString( anAsciiStr->Get().ToCString() );
293 void HYDROData_Image::SetTrsf(const QTransform& theTrsf)
295 // locate 9 coeffs of matrix into the real array
296 Handle(TDataStd_RealArray) anArray;
297 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
298 if (theTrsf.isIdentity()) return; // no need to store identity transformation
299 anArray = TDataStd_RealArray::Set(myLab, 1, 9);
301 anArray->SetValue(1, theTrsf.m11());
302 anArray->SetValue(2, theTrsf.m12());
303 anArray->SetValue(3, theTrsf.m13());
304 anArray->SetValue(4, theTrsf.m21());
305 anArray->SetValue(5, theTrsf.m22());
306 anArray->SetValue(6, theTrsf.m23());
307 anArray->SetValue(7, theTrsf.m31());
308 anArray->SetValue(8, theTrsf.m32());
309 anArray->SetValue(9, theTrsf.m33());
312 QTransform HYDROData_Image::Trsf() const
314 // get 9 coeffs of matrix from the real array
315 Handle(TDataStd_RealArray) anArray;
316 if (!myLab.FindAttribute(TDataStd_RealArray::GetID(), anArray))
317 return QTransform(); // return identity if there is no array
319 anArray->Value(1), anArray->Value(2), anArray->Value(3),
320 anArray->Value(4), anArray->Value(5), anArray->Value(6),
321 anArray->Value(7), anArray->Value(8), anArray->Value(9));
325 void HYDROData_Image::SetTrsfPoints(const QPoint& thePointA,
326 const QPoint& thePointB,
327 const QPoint& thePointC,
328 const QPointF& theLambertPointA,
329 const QPointF& theLambertPointB,
330 const QPointF& theLambertPointC,
331 const QPointF& theCartesianPointA,
332 const QPointF& theCartesianPointB,
333 const QPointF& theCartesianPointC)
335 Handle(TDataStd_RealArray) anArray;
336 if (!myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
337 anArray = TDataStd_RealArray::Set(myLab.FindChild(DataTag_TrsfPoints), 1, 18);
339 anArray->SetValue(1, thePointA.x());
340 anArray->SetValue(2, thePointA.y());
341 anArray->SetValue(3, thePointB.x());
342 anArray->SetValue(4, thePointB.y());
343 anArray->SetValue(5, thePointC.x());
344 anArray->SetValue(6, thePointC.y());
345 anArray->SetValue(7, theLambertPointA.x());
346 anArray->SetValue(8, theLambertPointA.y());
347 anArray->SetValue(9, theLambertPointB.x());
348 anArray->SetValue(10, theLambertPointB.y());
349 anArray->SetValue(11, theLambertPointC.x());
350 anArray->SetValue(12, theLambertPointC.y());
351 anArray->SetValue(13, theCartesianPointA.x());
352 anArray->SetValue(14, theCartesianPointA.y());
353 anArray->SetValue(15, theCartesianPointB.x());
354 anArray->SetValue(16, theCartesianPointB.y());
355 anArray->SetValue(17, theCartesianPointC.x());
356 anArray->SetValue(18, theCartesianPointC.y());
359 void HYDROData_Image::TrsfPoints(QPoint& thePointA,
362 QPointF& theLambertPointA,
363 QPointF& theLambertPointB,
364 QPointF& theLambertPointC,
365 QPointF& theCartesianPointA,
366 QPointF& theCartesianPointB,
367 QPointF& theCartesianPointC) const
369 Handle(TDataStd_RealArray) anArray;
370 if (myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray)) {
371 thePointA = QPointF( anArray->Value(1), anArray->Value(2) ).toPoint();
372 thePointB = QPointF( anArray->Value(3), anArray->Value(4) ).toPoint();
373 thePointC = QPointF( anArray->Value(5), anArray->Value(6) ).toPoint();
374 theLambertPointA = QPointF( anArray->Value(7), anArray->Value(8) );
375 theLambertPointB = QPointF( anArray->Value(9), anArray->Value(10) );
376 theLambertPointC = QPointF( anArray->Value(11), anArray->Value(12) );
377 theCartesianPointA = QPointF( anArray->Value(13), anArray->Value(14) );
378 theCartesianPointB = QPointF( anArray->Value(15), anArray->Value(16) );
379 theCartesianPointC = QPointF( anArray->Value(17), anArray->Value(18) );
383 bool HYDROData_Image::HasTrsfPoints() const
385 Handle(TDataStd_RealArray) anArray;
386 return myLab.FindChild(DataTag_TrsfPoints).FindAttribute(TDataStd_RealArray::GetID(), anArray);
389 void HYDROData_Image::SetTrsfMode(const int theMode)
391 TDataStd_Integer::Set(myLab.FindChild(DataTag_TrsfMode), theMode);
394 int HYDROData_Image::TrsfMode() const
396 Handle(TDataStd_Integer) aMode;
397 if(myLab.FindChild(DataTag_TrsfMode).FindAttribute(TDataStd_Integer::GetID(), aMode))
402 void HYDROData_Image::AppendReference( const Handle(HYDROData_Object)& theReferenced )
404 AddReferenceObject( theReferenced, 0 );
407 int HYDROData_Image::NbReferences() const
409 return NbReferenceObjects( 0 );
412 Handle(HYDROData_Object) HYDROData_Image::Reference( const int theIndex ) const
414 return GetReferenceObject( 0, theIndex );
417 void HYDROData_Image::ChangeReference(
418 const int theIndex, Handle(HYDROData_Object) theReferenced)
420 SetReferenceObject( theReferenced, 0, theIndex );
423 void HYDROData_Image::RemoveReference(const int theIndex)
425 RemoveReferenceObject( 0, theIndex );
428 void HYDROData_Image::ClearReferences()
430 ClearReferenceObjects( 0 );
433 void HYDROData_Image::SetOperatorName(const QString theOpName)
435 TDataStd_Name::Set(myLab.FindChild(DataTag_Operator),
436 TCollection_ExtendedString(theOpName.toLatin1().constData()));
439 QString HYDROData_Image::OperatorName() const
441 Handle(TDataStd_Name) aName;
442 if (myLab.FindChild(DataTag_Operator).
443 FindAttribute(TDataStd_Name::GetID(), aName)) {
444 TCollection_AsciiString aStr(aName->Get());
445 return QString(aStr.ToCString());
450 void HYDROData_Image::SetArgs(const QByteArray& theArgs)
452 SaveByteArray(DataTag_Operator, theArgs.constData(), theArgs.length());
455 QByteArray HYDROData_Image::Args() const
458 const char* aData = ByteArray(DataTag_Operator, aLen);
461 return QByteArray(aData, aLen);
464 void HYDROData_Image::MustBeUpdated(bool theFlag)
467 TDataStd_UAttribute::Set(myLab, GUID_MUST_BE_UPDATED);
469 myLab.ForgetAttribute(GUID_MUST_BE_UPDATED);
473 bool HYDROData_Image::MustBeUpdated() const
475 return myLab.IsAttribute(GUID_MUST_BE_UPDATED);
478 void HYDROData_Image::SetIsSelfSplitted(bool theFlag)
481 TDataStd_UAttribute::Set(myLab, GUID_SELF_SPLITTED);
483 myLab.ForgetAttribute(GUID_SELF_SPLITTED);
487 bool HYDROData_Image::IsSelfSplitted() const
489 return myLab.IsAttribute(GUID_SELF_SPLITTED);