Salome HOME
Drawing of zones in OCC view improved.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportImageOp.cxx
index 52ea65d9c32263bb5033fef06b230f79b6709471..13fc3a7923b1513fedc9cfc0f64d2cce22f9db49 100644 (file)
@@ -183,22 +183,8 @@ bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
     }
   }
 
-  if( !myPreviewPrs )
-    return false;
-
-  QImage anImage = myPreviewPrs->getImage();
-
   bool anIsRefImage = aPanel->getTransformationMode() == HYDROGUI_ImportImageDlg::RefImage;
 
-  QTransform aRefTransform;
-  if( anIsRefImage && myRefPreviewPrs )
-  {
-    Handle(HYDROData_Image) aRefImageObj =
-      Handle(HYDROData_Image)::DownCast( myRefPreviewPrs->getObject() );
-    if( !aRefImageObj.IsNull() )
-      aRefTransform = aRefImageObj->Trsf();
-  }
-
   HYDROGUI_ImportImageDlg::TransformationDataMap aMap;
   if( !aPanel->getTransformationDataMap( aMap ) )
     return false;
@@ -272,7 +258,7 @@ bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
 
   QTransform aTransform;
   if( anIsRefImage )
-    aTransform = aTransform1Inverted * aTransform3 * aRefTransform;
+    aTransform = aTransform1Inverted * aTransform3 * myRefTransform;
   else
     aTransform = aTransform1Inverted * aTransform2;
 
@@ -287,7 +273,7 @@ bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
 
   anImageObj->SetName( anImageName );
 
-  anImageObj->SetImage( anImage );
+  anImageObj->SetImage( myImage );
   anImageObj->SetTrsf( aTransform );
 
   if( anIsRefImage )
@@ -300,6 +286,13 @@ bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
   anImageObj->SetTrsfPoints( aPointA1, aPointB1, aPointC1,
                              aPointA2, aPointB2, aPointC2 );
 
+  if( !myIsEdit )
+  {
+    // Set imported file name for image
+    QString aFilePath = aPanel->getFileName();
+    anImageObj->SetFilePath( aFilePath );
+  }
+   
   // must be done after all checks and before calling SetVisible() method below
   closePreview();
 
@@ -307,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;
@@ -316,49 +308,74 @@ bool HYDROGUI_ImportImageOp::processApply( int& theUpdateFlags,
 
 void HYDROGUI_ImportImageOp::onCreatePreview( QImage theImage )
 {
-  LightApp_Application* anApp = module()->getApp();
+  HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
 
-  myActiveViewManager = anApp->activeViewManager();
+  myImage = theImage;
 
-  myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
-  myPreviewPrs->setImage( theImage );
-  myPreviewPrs->compute();
+  if( myPreviewPrs ) // if the image is changed by choosing another file
+  {
+    myPreviewPrs->setImage( myImage );
+    myPreviewPrs->compute();
 
-  myPreviewViewManager =
-    dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
-  if( myPreviewViewManager )
+    if( myPreviewViewManager )
+    {
+      if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
+      {
+        if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
+        {
+          aViewPort->onBoundingRectChanged();
+          aViewPort->fitAll();
+        }
+      }
+    }
+  }
+  else
   {
-    module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
-    myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
-    if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
+    LightApp_Application* anApp = module()->getApp();
+
+    myActiveViewManager = anApp->activeViewManager();
+
+    myPreviewPrs = new HYDROGUI_PrsImage( myIsEdit ? myEditedObject : 0 );
+    myPreviewPrs->setImage( myImage );
+    myPreviewPrs->compute();
+
+    myPreviewViewManager =
+      dynamic_cast<GraphicsView_ViewManager*>( anApp->createViewManager( GraphicsView_Viewer::Type() ) );
+    if( myPreviewViewManager )
     {
-      if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
+      connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
+               this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
+
+      module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_TransformImage );
+      myPreviewViewManager->setTitle( tr( "TRANSFORM_IMAGE" ) );
+      if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
       {
-        //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
+        if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
+        {
+          //aViewPort->setMousePositionEnabled( true ); //ouv: temporarily commented
 
-        aViewPort->addItem( myPreviewPrs );
-        aViewPort->fitAll();
+          aViewPort->addItem( myPreviewPrs );
+          aViewPort->fitAll();
 
-        myPreviewPrs->setIsTransformationPointPreview( true );
+          myPreviewPrs->setIsTransformationPointPreview( true );
+        }
+        connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
+                 this, SLOT( onPointSelected() ) );
       }
-      connect( aViewer, SIGNAL( selectionChanged( GV_SelectionChangeStatus ) ),
-               this, SLOT( onPointSelected() ) );
     }
-  }
-
-  HYDROGUI_ImportImageDlg* aPanel = (HYDROGUI_ImportImageDlg*)inputPanel();
 
-  QString anImageName;
-  if( myIsEdit )
-  {
-    if( !myEditedObject.IsNull() )
-      anImageName = myEditedObject->GetName();
+    QString anImageName;
+    if( myIsEdit )
+    {
+      if( !myEditedObject.IsNull() )
+        anImageName = myEditedObject->GetName();
+    }
+    else
+      anImageName = HYDROGUI_Tool::GenerateObjectName( module(), "Image" );
+    aPanel->setImageName( anImageName );
   }
-  else
-    anImageName = HYDROGUI_Tool::GenerateObjectName( module(), "Image" );
-  aPanel->setImageName( anImageName );
 
-  aPanel->setImageSize( theImage.size() );
+  aPanel->setImageSize( myImage.size() );
 
   aPanel->initializePointSelection();
   onPointSelected( false );
@@ -386,6 +403,8 @@ void HYDROGUI_ImportImageOp::onPointCoordChanged( bool theIsRef,
 
 void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
 {
+  myRefTransform.reset();
+
   GraphicsView_ViewPort* aViewPort = 0;
   if( myPreviewViewManager )
     if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
@@ -412,6 +431,7 @@ void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
   if( !anImageObj.IsNull() )
   {
     anImage = anImageObj->Image();
+    myRefTransform = anImageObj->Trsf();
 
     myRefPreviewPrs = new HYDROGUI_PrsImage( anImageObj );
     myRefPreviewPrs->setImage( anImage );
@@ -443,6 +463,11 @@ void HYDROGUI_ImportImageOp::onRefImageActivated( const QString& theName )
   onPointSelected( true );
 }
 
+void HYDROGUI_ImportImageOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
+{
+  closePreview();
+}
+
 void HYDROGUI_ImportImageOp::onPointSelected()
 {
   onPointSelected( myRefPreviewPrs && myRefPreviewPrs->isSelected() );
@@ -474,6 +499,14 @@ void HYDROGUI_ImportImageOp::onPointSelected( bool theIsRefImage )
 
 void HYDROGUI_ImportImageOp::closePreview()
 {
+  // It's very strange, but without calling this method (it's quite safe) a crash is stably reproduced.
+  // Scenario: create any non-Graphics view, activate import op, click apply.
+  // Result: a few SIGSEGVs coming from processEvents(), then crash.
+  if( myPreviewViewManager )
+    if( GraphicsView_Viewer* aViewer = myPreviewViewManager->getViewer() )
+      if( GraphicsView_ViewPort* aViewPort = aViewer->getActiveViewPort() )
+        aViewPort->onBoundingRectChanged();
+
   if( myPreviewPrs )
   {
     delete myPreviewPrs;
@@ -488,6 +521,9 @@ void HYDROGUI_ImportImageOp::closePreview()
 
   if( myPreviewViewManager )
   {
+    disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
+                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
+
     module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
     myPreviewViewManager = 0;
   }