Salome HOME
Image composing.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportImageOp.cxx
index 2ab7cec68cf2b14e2520abbd4eecfdf4a3930a73..14c081255003be9a86e6ba8439faed75c4d14714 100644 (file)
@@ -29,8 +29,6 @@
 #include "HYDROGUI_Tool.h"
 #include "HYDROGUI_UpdateFlags.h"
 
-#include <HYDROData_Document.h>
-
 #include <GraphicsView_ViewManager.h>
 #include <GraphicsView_ViewPort.h>
 #include <GraphicsView_Viewer.h>
@@ -102,6 +100,20 @@ void HYDROGUI_ImportImageOp::startOperation()
   }
 }
 
+void HYDROGUI_ImportImageOp::abortOperation()
+{
+  closePreview();
+
+  HYDROGUI_Operation::abortOperation();
+}
+
+void HYDROGUI_ImportImageOp::commitOperation()
+{
+  closePreview();
+
+  HYDROGUI_Operation::commitOperation();
+}
+
 HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
 {
   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportImageDlg( module(), getName() );
@@ -112,10 +124,28 @@ HYDROGUI_InputPanel* HYDROGUI_ImportImageOp::createInputPanel() const
   return aPanel;
 }
 
-bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags )
+bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
+                                           QString& theErrorMsg )
 {
   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
 
+  QString anImageName = aPanel->getImageName();
+  if( anImageName.isEmpty() )
+    return false;
+
+  if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anImageName ) )
+  {
+    // check that there are no other objects with the same name in the document
+    Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anImageName );
+    if( !anObject.IsNull() )
+    {
+      theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anImageName );
+      return false;
+    }
+  }
+
+  QImage anImage = myPreviewPrs->getImage();
+
   HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
   bool anIsOk = aPanel->getTransformationDataMap( aMap );
   if( !anIsOk || !myPreviewPrs )
@@ -129,12 +159,12 @@ bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags )
   QPointF aPointB2 = aMap[ HYDROGUI_PrsImage::PointB ].second;
   QPointF aPointC2 = aMap[ HYDROGUI_PrsImage::PointC ].second;
 
-  double xa1 = aPointA1.x();
-  double ya1 = aPointA1.y();
-  double xb1 = aPointB1.x();
-  double yb1 = aPointB1.y();
-  double xc1 = aPointC1.x();
-  double yc1 = aPointC1.y();
+  int xa1 = aPointA1.x();
+  int ya1 = aPointA1.y();
+  int xb1 = aPointB1.x();
+  int yb1 = aPointB1.y();
+  int xc1 = aPointC1.x();
+  int yc1 = aPointC1.y();
 
   double xa2 = aPointA2.x();
   double ya2 = aPointA2.y();
@@ -143,14 +173,23 @@ bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags )
   double xc2 = aPointC2.x();
   double yc2 = aPointC2.y();
 
+  // first, check that three input points don't belong to a single line
+  if( ( yb1 - ya1 ) * ( xc1 - xa1 ) == ( yc1 - ya1 ) * ( xb1 - xa1 ) )
+  {
+    theErrorMsg = tr( "POINTS_A_B_C_BELONG_TO_SINGLE_LINE" );
+    return false;
+  }
+
   QTransform aTransform1( xa1, ya1, 1, xb1, yb1, 1, xc1, yc1, 1 );
   QTransform aTransform2( xa2, ya2, 1, xb2, yb2, 1, xc2, yc2, 1 );
 
-  QTransform aTransform = aTransform1.inverted() * aTransform2;
-
-  QImage anImage = myPreviewPrs->getImage();
-
-  closePreview();
+  bool anIsInvertible = false;
+  QTransform aTransform = aTransform1.inverted( &anIsInvertible ) * aTransform2;
+  if( !anIsInvertible )
+  {
+    theErrorMsg = tr( "TRANSFORMATION_MATRIX_CANNOT_BE_COMPUTED" );
+    return false;
+  }
 
   Handle(HYDROData_Image) anImageObj;
   if( myIsEdit )
@@ -161,11 +200,7 @@ bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags )
   if( anImageObj.IsNull() )
     return false;
 
-  if( !myIsEdit )
-  {
-    static int ImageId = 0;
-    anImageObj->SetName( QString( "Image_%1" ).arg( QString::number( ++ImageId ) ) );
-  }
+  anImageObj->SetName( anImageName );
 
   anImageObj->SetImage( anImage );
   anImageObj->SetTrsf( aTransform );
@@ -179,11 +214,6 @@ bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags )
   return true;
 }
 
-void HYDROGUI_ImportImageOp::processCancel()
-{
-  closePreview();
-}
-
 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
 {
   LightApp_Application* anApp = module()->getApp();
@@ -198,6 +228,7 @@ void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
     dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
   if( myPreviewViewManager )
   {
+    module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_Mapping );
     myPreviewViewManager->setTitle( tr( "MAPPING" ) );
     if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
     {
@@ -217,6 +248,16 @@ void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
 
   HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
 
+  QString anImageName;
+  if( myIsEdit )
+  {
+    if( !myEditedObject.IsNull() )
+      anImageName = myEditedObject->GetName();
+  }
+  else
+    anImageName = HYDROGUI_Tool::GenerateObjectName( module(), "Image" );
+  aPanel->setImageName( anImageName );
+
   aPanel->initializePointSelection();
   onPointSelected();
 }