From d63a2e4caa93023bac64aef525cc4f597bc72512 Mon Sep 17 00:00:00 2001 From: adv Date: Wed, 11 Sep 2013 12:43:23 +0000 Subject: [PATCH] The code refactoring since the ImageComposer API has been changed (Feature #6). --- src/HYDROData/HYDROData_Image.cxx | 72 +++++++++++++++++- src/HYDROData/HYDROData_Image.h | 15 ++++ src/HYDROData/HYDROData_Object.cxx | 10 +++ src/HYDROData/HYDROData_Object.h | 13 ++++ src/HYDROData/HYDROData_Polyline.cxx | 13 +++- src/HYDROData/HYDROData_Polyline.h | 6 ++ src/HYDROData/HYDROData_Tool.cxx | 36 +++++++++ src/HYDROData/HYDROData_Tool.h | 7 ++ src/HYDROData/HYDROOperations_Factory.cxx | 90 +++-------------------- src/HYDROData/HYDROOperations_Factory.h | 27 ++----- src/HYDROGUI/HYDROGUI_ImportImageOp.cxx | 3 +- src/HYDROGUI/HYDROGUI_TwoImagesOp.cxx | 41 ++++------- src/HYDROGUI/HYDROGUI_UpdateImageOp.cxx | 7 +- 13 files changed, 202 insertions(+), 138 deletions(-) diff --git a/src/HYDROData/HYDROData_Image.cxx b/src/HYDROData/HYDROData_Image.cxx index 88650062..cd63a215 100644 --- a/src/HYDROData/HYDROData_Image.cxx +++ b/src/HYDROData/HYDROData_Image.cxx @@ -1,7 +1,9 @@ -#include -#include -#include +#include "HYDROData_Image.h" + +#include "HYDROData_Iterator.h" +#include "HYDROOperations_Factory.h" +#include "HYDROData_Tool.h" #include #include @@ -13,6 +15,7 @@ #include #include +#include #include @@ -168,6 +171,69 @@ QStringList HYDROData_Image::DumpToPython( MapOfTreatedObjects& theTreatedObject return aResList; } +void HYDROData_Image::Update() +{ + HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory(); + + // Update image only if there is an operation + ImageComposer_Operator* anOp = aFactory->Operator( this ); + if ( anOp ) + { + // Fill by arguments and process the operation + QVariant anObj1, anObj2; + int aNbReferences = NbReferences(); + + if ( aNbReferences > 0 ) + { + // First referenced object + Handle(HYDROData_Object) aRefObj = Reference( 0 ); + if ( !aRefObj.IsNull() ) + { + anObj1 = aRefObj->GetDataVariant(); + if ( !anObj1.isNull() && anObj1.canConvert() ) + { + ImageComposer_Image anImage = anObj1.value(); + QTransform aTransform = anImage.transform(); + SetTrsf( aTransform ); + } + } + } + + if ( aNbReferences > 1 ) + { + // Second referenced object + Handle(HYDROData_Object) aRefObj = Reference( 1 ); + if ( !aRefObj.IsNull() ) + anObj2 = aRefObj->GetDataVariant(); + } + + ImageComposer_Image aResImg = anOp->process( anObj1, anObj2 ); + SetImage( aResImg ); + } + + Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this ); + if ( !aDocument.IsNull() ) + { + // Change the states of this and all depended images + MustBeUpdated( true ); + HYDROData_Tool::SetMustBeUpdatedImages( aDocument ); + MustBeUpdated( false ); + } +} + +QVariant HYDROData_Image::GetDataVariant() +{ + QTransform aTransform = Trsf(); + + ImageComposer_Image anImage = Image(); + anImage.setTransform( aTransform ); + + QVariant aVarData; + aVarData.setValue( anImage ); + + return aVarData; +} + void HYDROData_Image::SetImage(const QImage& theImage) { if (theImage.isNull()) { diff --git a/src/HYDROData/HYDROData_Image.h b/src/HYDROData/HYDROData_Image.h index 4966616e..0b92af77 100644 --- a/src/HYDROData/HYDROData_Image.h +++ b/src/HYDROData/HYDROData_Image.h @@ -41,6 +41,21 @@ public: */ HYDRODATA_EXPORT virtual QStringList DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const; + /** + * Updates object state. + * Reimplemented to update an Image object in the data structure. + * Call this method whenever you made changes for operator or reference objects. + * If it is changed, sets "MustBeUpdated" flag to other depended images. + */ + HYDRODATA_EXPORT virtual void Update(); + + /** + * Returns data of object wrapped to QVariant. + * Reimplemented to wrap and return saved image. + * Transformation are applied to result image. + */ + HYDRODATA_EXPORT virtual QVariant GetDataVariant(); + /** * Stores the image * \param theImage new image diff --git a/src/HYDROData/HYDROData_Object.cxx b/src/HYDROData/HYDROData_Object.cxx index 1b6337a0..9b84cef2 100644 --- a/src/HYDROData/HYDROData_Object.cxx +++ b/src/HYDROData/HYDROData_Object.cxx @@ -10,6 +10,7 @@ #include #include +#include IMPLEMENT_STANDARD_HANDLE(HYDROData_Object,MMgt_TShared) IMPLEMENT_STANDARD_RTTIEXT(HYDROData_Object,MMgt_TShared) @@ -41,6 +42,15 @@ QStringList HYDROData_Object::DumpToPython( MapOfTreatedObjects& theTreatedObjec return anEmptyList; } +void HYDROData_Object::Update() +{ +} + +QVariant HYDROData_Object::GetDataVariant() +{ + return QVariant(); +} + bool HYDROData_Object::IsRemoved() const { return !myLab.HasAttribute(); diff --git a/src/HYDROData/HYDROData_Object.h b/src/HYDROData/HYDROData_Object.h index 0c733954..7ea49e6b 100644 --- a/src/HYDROData/HYDROData_Object.h +++ b/src/HYDROData/HYDROData_Object.h @@ -8,6 +8,7 @@ #include class QString; +class QVariant; class QStringList; ///! Kind of an object in a document @@ -68,6 +69,18 @@ public: */ HYDRODATA_EXPORT virtual QStringList DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const; + /** + * Updates object state. + * Base implementation dose nothing. + */ + HYDRODATA_EXPORT virtual void Update(); + + /** + * Returns data of object wrapped to QVariant. + * Base implementation returns null value. + */ + HYDRODATA_EXPORT virtual QVariant GetDataVariant(); + /** * Checks is object exists in the data structure. * \returns true is object is not exists in the data model diff --git a/src/HYDROData/HYDROData_Polyline.cxx b/src/HYDROData/HYDROData_Polyline.cxx index 7db4062a..d80af2eb 100755 --- a/src/HYDROData/HYDROData_Polyline.cxx +++ b/src/HYDROData/HYDROData_Polyline.cxx @@ -1,6 +1,8 @@ #include #include +#include + #include #include #include @@ -11,7 +13,6 @@ #include #include -#include #include // tage of the child of my label that contains information about the operator @@ -104,6 +105,16 @@ QStringList HYDROData_Polyline::DumpToPython( MapOfTreatedObjects& theTreatedObj return aResList; } +QVariant HYDROData_Polyline::GetDataVariant() +{ + QPainterPath aPath = painterPath(); + + QVariant aVarData; + aVarData.setValue( aPath ); + + return aVarData; +} + /** * Return polyline dimension * \return polyline dimension. 2 or 3 is valid. 0 is invalid. diff --git a/src/HYDROData/HYDROData_Polyline.h b/src/HYDROData/HYDROData_Polyline.h index 8dc6dced..cd7928c8 100755 --- a/src/HYDROData/HYDROData_Polyline.h +++ b/src/HYDROData/HYDROData_Polyline.h @@ -61,6 +61,12 @@ public: */ HYDRODATA_EXPORT virtual QStringList DumpToPython( MapOfTreatedObjects& theTreatedObjects ) const; + /** + * Returns data of object wrapped to QVariant. + * Reimplemented to wrap and return saved path. + */ + HYDRODATA_EXPORT virtual QVariant GetDataVariant(); + /** * Replace current polyline data by new sections list * \param theSections the sections list diff --git a/src/HYDROData/HYDROData_Tool.cxx b/src/HYDROData/HYDROData_Tool.cxx index 9cb932d3..58511d16 100644 --- a/src/HYDROData/HYDROData_Tool.cxx +++ b/src/HYDROData/HYDROData_Tool.cxx @@ -1,6 +1,9 @@ #include "HYDROData_Tool.h" +#include "HYDROData_Image.h" +#include "HYDROData_Iterator.h" + #include #include #include @@ -19,3 +22,36 @@ void HYDROData_Tool::WriteStringsToFile( QFile& theFile, QTextStream anOutStream( &theFile ); anOutStream << aWriteStr << theSep << theSep; } + +void HYDROData_Tool::SetMustBeUpdatedImages( + Handle_HYDROData_Document theDoc) +{ + bool aChanged = true; + + // iterate until there is no changes because images on all level of dependency must be updated + while ( aChanged ) + { + aChanged = false; + + HYDROData_Iterator anIter( theDoc, KIND_IMAGE ); + for ( ; anIter.More(); anIter.Next() ) + { + Handle(HYDROData_Image) anImage = + Handle(HYDROData_Image)::DownCast( anIter.Current() ); + if ( anImage.IsNull() || anImage->MustBeUpdated() ) + continue; + + for ( int i = 0, aNBRefs = anImage->NbReferences(); i < aNBRefs; ++i ) + { + Handle(HYDROData_Image) aRefImage = + Handle(HYDROData_Image)::DownCast( anImage->Reference( i ) ); + if ( !aRefImage.IsNull() && aRefImage->MustBeUpdated() ) + { + // image references to updated => also must be updated + anImage->MustBeUpdated(true); + aChanged = true; + } + } + } + } +} diff --git a/src/HYDROData/HYDROData_Tool.h b/src/HYDROData/HYDROData_Tool.h index 5be4ba3a..85824d6e 100644 --- a/src/HYDROData/HYDROData_Tool.h +++ b/src/HYDROData/HYDROData_Tool.h @@ -8,6 +8,7 @@ class QFile; class QStringList; +class Handle_HYDROData_Document; class HYDROData_Tool { @@ -17,6 +18,12 @@ public: const QStringList& theStrings, const QString& theSep = "\n" ); + /** + * Enables "MustBeUpdated" flag for Images that are depended on "MustBeUpdated" images. + * \param theDoc document where this operation is performed + */ + static void SetMustBeUpdatedImages( Handle_HYDROData_Document theDoc ); + }; inline bool ValuesEquals( const double& theFirst, const double& theSecond ) diff --git a/src/HYDROData/HYDROOperations_Factory.cxx b/src/HYDROData/HYDROOperations_Factory.cxx index c9c06966..ffe38aa0 100644 --- a/src/HYDROData/HYDROOperations_Factory.cxx +++ b/src/HYDROData/HYDROOperations_Factory.cxx @@ -7,7 +7,6 @@ #include #include #include -#include #include @@ -27,14 +26,6 @@ HYDROOperations_Factory* HYDROOperations_Factory::Factory() return FACTORY; } -ImageComposer_Operator* HYDROOperations_Factory::Operator(const QString theName) const -{ - if (myOps.contains(theName)) { - return myOps[theName]; - } - return NULL; -} - void HYDROOperations_Factory::Register( ImageComposer_Operator* theOperator) { @@ -49,18 +40,24 @@ HYDROOperations_Factory::HYDROOperations_Factory() } ImageComposer_Operator* HYDROOperations_Factory::Operator( - Handle(HYDROData_Image) theImage) const + Handle(HYDROData_Image) theImage ) const { // retreive operator instance by name - ImageComposer_Operator* anOp = Operator(theImage->OperatorName()); - if (!anOp) + ImageComposer_Operator* anOp = Operator( theImage->OperatorName() ); + if ( !anOp ) return anOp; + // fill arguments of the operator from theImage - theImage->Args(); - anOp->setBinArgs(theImage->Args()); + anOp->setBinArgs( theImage->Args() ); + return anOp; } +ImageComposer_Operator* HYDROOperations_Factory::Operator(const QString theName) const +{ + return myOps.contains( theName ) ? myOps[ theName ] : NULL; +} + Handle(HYDROData_Image) HYDROOperations_Factory::CreateImage( Handle(HYDROData_Document) theDoc, const ImageComposer_Operator* theOperator) { @@ -74,68 +71,3 @@ Handle(HYDROData_Image) HYDROOperations_Factory::CreateImage( } return anImage; } - -void HYDROOperations_Factory::UpdateImage( - Handle_HYDROData_Document theDoc, Handle(HYDROData_Image) theImage) -{ - // fill by arguments and process the operation - ImageComposer_Operator* anOp = Operator(theImage); - if (anOp) { // update image only if there is an operation - QTransform aTransform; - ImageComposer_Image anImage1; // first referenced image - if (theImage->NbReferences()) { - Handle(HYDROData_Image) anImage = - Handle(HYDROData_Image)::DownCast( theImage->Reference(0) ); - if( !anImage.IsNull() ) - { - anImage1 = anImage->Image(); - anImage1.setTransform(anImage->Trsf()); - aTransform = anImage1.transform(); - } - } - ImageComposer_Image anImage2; // second referenced image - if (theImage->NbReferences() > 1) { - Handle(HYDROData_Image) anImage = - Handle(HYDROData_Image)::DownCast( theImage->Reference(1) ); - if( !anImage.IsNull() ) - { - anImage2 = anImage->Image(); - anImage2.setTransform(anImage->Trsf()); - } - } - ImageComposer_Image aResImg = anOp->process(anImage1, anImage2); - theImage->SetImage(aResImg); - theImage->SetTrsf(aTransform); - } - // change the states of this and all depended images - theImage->MustBeUpdated(true); - SetMustBeUpdatedImages(theDoc); - theImage->MustBeUpdated(false); -} - -void HYDROOperations_Factory::SetMustBeUpdatedImages( - Handle_HYDROData_Document theDoc) const -{ - bool aChanged = true; - // iterate until there is no changes because images on all level of dependency must be updated - while(aChanged) { - aChanged = false; - HYDROData_Iterator anIter(theDoc, KIND_IMAGE); - for(; anIter.More(); anIter.Next()) { - Handle(HYDROData_Image) anImage = - Handle(HYDROData_Image)::DownCast(anIter.Current()); - if (!anImage->MustBeUpdated()) { - int a, aNBRefs = anImage->NbReferences(); - for(a = 0; a < aNBRefs; a++) { - Handle(HYDROData_Image) aRefImage = - Handle(HYDROData_Image)::DownCast(anImage->Reference(a)); - if (!aRefImage.IsNull() && aRefImage->MustBeUpdated()) { - // image references to updated => also must be updated - anImage->MustBeUpdated(true); - aChanged = true; - } - } - } - } - } -} diff --git a/src/HYDROData/HYDROOperations_Factory.h b/src/HYDROData/HYDROOperations_Factory.h index 7ef2c0e0..310280a7 100644 --- a/src/HYDROData/HYDROOperations_Factory.h +++ b/src/HYDROData/HYDROOperations_Factory.h @@ -44,15 +44,6 @@ public: HYDRODATA_EXPORT Handle(HYDROData_Image) CreateImage( Handle_HYDROData_Document theDoc, const ImageComposer_Operator* theOperator); - /** - * Updates an Image object in the data structure. If it is changed, - * sets "MustBeUpdated" flag to other depended images. - * \param theDoc document of this image (needed to update other images flags) - * \param theImage the updated image - */ - HYDRODATA_EXPORT void UpdateImage( - Handle_HYDROData_Document theDoc, Handle(HYDROData_Image) theImage); - /** * Returns the operator, initialized by the properties of theImage * \param theImage data structures object, that contains all arguments @@ -63,23 +54,17 @@ public: Handle(HYDROData_Image) theImage) const; -protected: - - //! Not public constructor that creates only one, global instance of this factory. - HYDROOperations_Factory(); - /** * Returns the appropriate operator by the name * \param theName name of the operator, equals to the operation_name constructor * \returns NULL if operator with such name is not registered yet */ - ImageComposer_Operator* Operator(const QString theName) const; - - /** - * Enables "MustBeUpdated" flag for Images that are depended on "MustBeUpdated" images. - * \param theDoc document where this operation is performed - */ - void SetMustBeUpdatedImages(Handle_HYDROData_Document theDoc) const; + HYDRODATA_EXPORT ImageComposer_Operator* Operator(const QString theName) const; + +protected: + + //! Not public constructor that creates only one, global instance of this factory. + HYDROOperations_Factory(); private: //! Map that stores all operators, isentified by strings diff --git a/src/HYDROGUI/HYDROGUI_ImportImageOp.cxx b/src/HYDROGUI/HYDROGUI_ImportImageOp.cxx index 86055e82..3ad1959d 100644 --- a/src/HYDROGUI/HYDROGUI_ImportImageOp.cxx +++ b/src/HYDROGUI/HYDROGUI_ImportImageOp.cxx @@ -300,8 +300,7 @@ bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags, module()->setObjectVisible( HYDROGUI_Tool::GetActiveGraphicsViewId( module() ), anImageObj, true ); if( myIsEdit ) - if( HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory() ) - aFactory->UpdateImage( doc(), anImageObj ); + anImageObj->Update(); theUpdateFlags = UF_Model | UF_Viewer | UF_GV_Forced; return true; diff --git a/src/HYDROGUI/HYDROGUI_TwoImagesOp.cxx b/src/HYDROGUI/HYDROGUI_TwoImagesOp.cxx index 19baa6a7..2e634382 100644 --- a/src/HYDROGUI/HYDROGUI_TwoImagesOp.cxx +++ b/src/HYDROGUI/HYDROGUI_TwoImagesOp.cxx @@ -160,54 +160,43 @@ bool HYDROGUI_TwoImagesOp::processApply( int& theUpdateFlags, return false; HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory(); - Handle(HYDROData_Image) aResult = 0; + + Handle(HYDROData_Image) aResult; + ImageComposer_Operator* anOperator = 0; if( myIsEdit ) { aResult = myEditedObject; aResult->ClearReferences(); + anOperator = aFactory->Operator( aResult ); } else { - ImageComposer_Operator* anOperator = 0; + QString anOperatorName; switch( myType ) { - case Fuse: anOperator = new ImageComposer_FuseOperator(); break; - case Cut: anOperator = new ImageComposer_CutOperator(); break; - case Split: anOperator = new ImageComposer_CropOperator(); break; + case Fuse: anOperatorName = ImageComposer_FuseOperator::Type(); break; + case Cut: anOperatorName = ImageComposer_CutOperator::Type(); break; + case Split: anOperatorName = ImageComposer_CropOperator::Type(); break; default: break; } - if( !anOperator ) - return false; + anOperator = aFactory->Operator( anOperatorName ); aResult = aFactory->CreateImage( doc(), anOperator ); } - if( aResult.IsNull() ) + if( aResult.IsNull() || !anOperator ) return false; + // Setting the operator arguments + anOperator->setArgs( aPanel->getColor() ); + aResult->SetArgs( anOperator->getBinArgs() ); + aResult->SetName( anImageName ); aResult->AppendReference( anObject1 ); aResult->AppendReference( anObject2 ); - // Setting the operator arguments (probably, it should be done somewhere else). - // For Fuse and Cut operators: setting background color. - // For Crop operator: setting background color and polyline path to crop. - if( ImageComposer_Operator* anOperator = aFactory->Operator( aResult ) ) - { - if( ImageComposer_CropOperator* aCropOperator = - dynamic_cast( anOperator ) ) - { - Handle(HYDROData_Polyline) aPolyline = Handle(HYDROData_Polyline)::DownCast( anObject2 ); - if( !aPolyline.IsNull() ) - aCropOperator->setArgs( aPanel->getColor(), aPolyline->painterPath() ); - } - else - anOperator->setArgs( aPanel->getColor() ); - aResult->SetArgs( anOperator->getBinArgs() ); - } - - aFactory->UpdateImage( doc(), aResult ); + aResult->Update(); if( anIsModifySelected ) { diff --git a/src/HYDROGUI/HYDROGUI_UpdateImageOp.cxx b/src/HYDROGUI/HYDROGUI_UpdateImageOp.cxx index 09d31a9d..0ac92a74 100644 --- a/src/HYDROGUI/HYDROGUI_UpdateImageOp.cxx +++ b/src/HYDROGUI/HYDROGUI_UpdateImageOp.cxx @@ -29,8 +29,6 @@ #include #include -#include - HYDROGUI_UpdateImageOp::HYDROGUI_UpdateImageOp( HYDROGUI_Module* theModule ) : HYDROGUI_Operation( theModule ) { @@ -53,10 +51,7 @@ void HYDROGUI_UpdateImageOp::startOperation() Handle(HYDROData_Image) anImage = Handle(HYDROData_Image)::DownCast( aSeq.Value( anIndex ) ); if( !anImage.IsNull() && anImage->MustBeUpdated() ) - { - HYDROOperations_Factory* aFactory = HYDROOperations_Factory::Factory(); - aFactory->UpdateImage( doc(), anImage ); - } + anImage->Update(); } commitDocOperation(); -- 2.39.2