Salome HOME
The "MustBeUpdated" flag is moved to base data object class and implemented for calcu...
[modules/hydro.git] / src / HYDROData / HYDROData_Image.cxx
index ae6b08042693fc987ca8885b86ef58a9771f056b..3556d543d3a04f24ae84648eb63d03f7012a7458 100644 (file)
@@ -2,6 +2,7 @@
 #include "HYDROData_Image.h"
 
 #include "HYDROData_Document.h"
+#include "HYDROData_Lambert93.h"
 #include "HYDROData_Tool.h"
 #include "HYDROData_OperationsFactory.h"
 
@@ -19,7 +20,6 @@
 
 #include <QStringList>
 
-static const Standard_GUID GUID_MUST_BE_UPDATED("80f2bb81-3873-4631-8ddd-940d2119f000");
 static const Standard_GUID GUID_SELF_SPLITTED("997995aa-5c19-40bf-9a60-ab4b70ad04d8");
 static const Standard_GUID GUID_HAS_LOCAL_POINTS("FD8841AA-FC44-42fa-B6A7-0F682CCC6F27");
 static const Standard_GUID GUID_HAS_GLOBAL_POINTS("330D0E81-742D-4ea3-92D4-484877CFA7C1");
@@ -42,7 +42,7 @@ QStringList HYDROData_Image::DumpToPython( MapOfTreatedObjects& theTreatedObject
 {
   QStringList aResList;
 
-  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( this );
+  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
   if ( aDocument.IsNull() )
     return aResList;
                              
@@ -112,7 +112,8 @@ QStringList HYDROData_Image::DumpToPython( MapOfTreatedObjects& theTreatedObject
       aResList << QString( "%1.SetOperatorName( \"%2\" );" )
                   .arg( anImageName ).arg( anOperatorName );
 
-      ImageComposer_Operator* anImageOp = HYDROData_OperationsFactory::Factory()->Operator( this );
+      ImageComposer_Operator* anImageOp = 
+        HYDROData_OperationsFactory::Factory()->Operator( OperatorName() );
       if ( anImageOp )
       {
         // Dump operation arguments
@@ -144,19 +145,18 @@ QStringList HYDROData_Image::DumpToPython( MapOfTreatedObjects& theTreatedObject
 
     // Necessary to update image in case of composed operator
     aResList << QString( "" );
-    aResList << QString( "%1.Update( False );" ).arg( anImageName );
+    aResList << QString( "%1.Update();" ).arg( anImageName );
   }
 
   return aResList;
 }
 
-void HYDROData_Image::Update( const bool theIsForce )
+void HYDROData_Image::Update()
 {
   HYDROData_OperationsFactory* aFactory = HYDROData_OperationsFactory::Factory();
 
-  // Update image only if there is an operation
   ImageComposer_Operator* anOp = aFactory->Operator( OperatorName() );
-  if ( anOp ) 
+  if ( anOp ) // Update image if there is an operation
   {
     // Fill by arguments and process the operation
     QVariant anObj1, anObj2;
@@ -189,17 +189,18 @@ void HYDROData_Image::Update( const bool theIsForce )
     ImageComposer_Image aResImg = anOp->process( anObj1, anObj2 );
     SetImage( aResImg );
   }
+  else // Update image if it positioned relatively to other image
+  {
+    UpdateTrsf();
+  }
 
-  if ( theIsForce )
+  Handle(HYDROData_Document) aDocument = HYDROData_Document::Document( myLab );
+  if ( !aDocument.IsNull() )
   {
-    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 );
-    }
+    // Change the states of this and all depended images
+    SetToUpdate( true );
+    HYDROData_Tool::SetMustBeUpdatedImages( aDocument );
+    SetToUpdate( false );
   }
 }
 
@@ -331,8 +332,28 @@ void HYDROData_Image::UpdateTrsf()
     aRefTransform = aRefImage->Trsf();
   }
 
+  bool anIsByTwoPoints = IsByTwoPoints();
+
+  // Convert lambert coordinates to cartesian
+  if ( aTrsfMode == ManualLambert )
+  {
+    double aXCart = 0, aYCart = 0;
+
+    HYDROData_Lambert93::toXY( aTrsfPointA.x(), aTrsfPointA.y(), aXCart, aYCart );
+    aTrsfPointA = QPointF( aXCart, aYCart );
+
+    HYDROData_Lambert93::toXY( aTrsfPointB.x(), aTrsfPointB.y(), aXCart, aYCart );
+    aTrsfPointB = QPointF( aXCart, aYCart );
+
+    if ( !anIsByTwoPoints )
+    {
+      HYDROData_Lambert93::toXY( aTrsfPointC.x(), aTrsfPointC.y(), aXCart, aYCart );
+      aTrsfPointC = QPointF( aXCart, aYCart );
+    }
+  }
+
   // generate third points if needed
-  if ( IsByTwoPoints() )
+  if ( anIsByTwoPoints )
   {
     aPointC = generateThirdPoint( aPointA, aPointB, true ).toPoint();
     aTrsfPointC = generateThirdPoint( aTrsfPointA, aTrsfPointB, anIsRefImage );
@@ -368,11 +389,9 @@ void HYDROData_Image::UpdateTrsf()
   if( !anIsInvertible )
     return;
 
-  QTransform aResTransform;
+  QTransform aResTransform = aTransform1Inverted * aTransform2;
   if( anIsRefImage )
-    aResTransform = aTransform1Inverted * aTransform2 * aRefTransform;
-  else
-    aResTransform = aTransform1Inverted * aTransform2;
+    aResTransform *= aRefTransform;
 
   SetTrsf( aResTransform );
 }
@@ -388,9 +407,62 @@ bool HYDROData_Image::IsByTwoPoints() const
   return aPointC.x() < 0 && aPointC.y() < 0; 
 }
 
+bool HYDROData_Image::HasReferences() const
+{
+  Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
+  int aNbReferences = NbReferences();
+
+  return !aRefImage.IsNull() || aNbReferences > 0;
+}
+
+void HYDROData_Image::RemoveAllReferences()
+{
+  if ( !HasReferences() )
+    return;
+
+  Handle(HYDROData_Image) aRefImage = GetTrsfReferenceImage();
+  if ( !aRefImage.IsNull() )
+  {
+    RemoveTrsfReferenceImage();
+  }
+  else
+  {
+    ClearReferences();
+    SetOperatorName( "" );
+    SetArgs( "" );
+    SetIsSelfSplitted( false );
+  }
+
+  SetToUpdate( false );
+
+  bool anIsByTwoPoints = IsByTwoPoints();
+
+  QImage anImage = Image();
+  if ( anImage.isNull() )
+    return;
+
+  // Set local points to default position
+  QPoint aLocalPointA = QPoint( 0, 0 );
+  QPoint aLocalPointB = QPoint( anImage.width(), 0 );
+  QPoint aLocalPointC = anIsByTwoPoints ? QPoint( INT_MIN, INT_MIN ) : QPoint( 0, anImage.height() );
+
+  SetLocalPoints( aLocalPointA, aLocalPointB, aLocalPointC, false );
+
+  // Calculate global points
+  QTransform aTransform = Trsf();
+
+  QPointF aTrsfPointA = QPointF( aTransform.map( aLocalPointA ) );
+  QPointF aTrsfPointB = QPointF( aTransform.map( aLocalPointB ) );
+  QPointF aTrsfPointC = anIsByTwoPoints ? QPointF( INT_MIN, INT_MIN ) : 
+                                          QPointF( aTransform.map( aLocalPointC ) );
+
+  SetGlobalPoints( ManualCartesian, aTrsfPointA, aTrsfPointB, aTrsfPointC );
+}
+
 void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
                                       const QPoint& thePointB,
-                                      const QPoint& thePointC )
+                                      const QPoint& thePointC,
+                                      const bool    theIsUpdate )
 {
   Handle(TDataStd_RealArray) anArray;
   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
@@ -405,7 +477,8 @@ void HYDROData_Image::SetLocalPoints( const QPoint& thePointA,
 
   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_LOCAL_POINTS );
 
-  UpdateTrsf();
+  if ( theIsUpdate )
+    UpdateTrsf();
 }
 
 bool HYDROData_Image::GetLocalPoints( QPoint& thePointA,
@@ -439,7 +512,8 @@ bool HYDROData_Image::HasLocalPoints() const
 void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
                                        const QPointF&            thePointA,
                                        const QPointF&            thePointB,
-                                       const QPointF&            thePointC )
+                                       const QPointF&            thePointC,
+                                       const bool                theIsUpdate )
 {
   Handle(TDataStd_RealArray) anArray;
   if ( !myLab.FindChild( DataTag_TrsfPoints ).FindAttribute( TDataStd_RealArray::GetID(), anArray ) )
@@ -456,7 +530,8 @@ void HYDROData_Image::SetGlobalPoints( const TransformationMode& theMode,
 
   TDataStd_UAttribute::Set( myLab.FindChild( DataTag_TrsfPoints ), GUID_HAS_GLOBAL_POINTS );
 
-  UpdateTrsf();
+  if ( theIsUpdate )
+    UpdateTrsf();
 
   /*
   if( anIsRefImage )
@@ -518,12 +593,11 @@ bool HYDROData_Image::HasGlobalPoints() const
 void HYDROData_Image::SetReferencePoints( const Handle(HYDROData_Image)& theRefImage,
                                           const QPointF&                 thePointA,
                                           const QPointF&                 thePointB,
-                                          const QPointF&                 thePointC )
+                                          const QPointF&                 thePointC,
+                                          const bool                     theIsUpdate )
 {
   SetTrsfReferenceImage( theRefImage );
-  SetGlobalPoints( ReferenceImage, thePointA, thePointB, thePointC );
-
-  UpdateTrsf();
+  SetGlobalPoints( ReferenceImage, thePointA, thePointB, thePointC, theIsUpdate );
 }
 
 bool HYDROData_Image::GetReferencePoints( Handle(HYDROData_Image)& theRefImage,
@@ -588,6 +662,11 @@ Handle(HYDROData_Image) HYDROData_Image::GetTrsfReferenceImage() const
   return Handle(HYDROData_Image)::DownCast( GetReferenceObject( DataTag_TrsfImage ) );
 }
 
+void HYDROData_Image::RemoveTrsfReferenceImage()
+{
+  return RemoveReferenceObject( DataTag_TrsfImage );
+}
+
 void HYDROData_Image::AppendReference( const Handle(HYDROData_Entity)& theReferenced )
 {
   AddReferenceObject( theReferenced, 0 );
@@ -650,20 +729,6 @@ QByteArray HYDROData_Image::Args() const
   return QByteArray(aData, aLen);
 }
 
-void HYDROData_Image::MustBeUpdated(bool theFlag)
-{
-  if (theFlag) {
-    TDataStd_UAttribute::Set(myLab, GUID_MUST_BE_UPDATED);
-  } else {
-    myLab.ForgetAttribute(GUID_MUST_BE_UPDATED);
-  }
-}
-
-bool HYDROData_Image::MustBeUpdated() const
-{
-  return myLab.IsAttribute(GUID_MUST_BE_UPDATED);
-}
-
 void HYDROData_Image::SetIsSelfSplitted(bool theFlag)
 {
   if (theFlag) {