Salome HOME
refs #640: correct width for non-georeferenced profile
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_RecognizeContoursOp.cxx
index 9398945850893f66204f39d113af86b80f0ffdc5..c4961c39d71c503a272f74664850babf16f9bdf4 100644 (file)
@@ -23,6 +23,8 @@
 #include "HYDROGUI_Shape.h"
 #include "HYDROGUI_Tool.h"
 #include "HYDROGUI_UpdateFlags.h"
+#include "HYDROGUI_OCCSelector.h"
+#include "HYDROGUI_ZLayers.h"
 
 #include <HYDROData_Document.h>
 #include <HYDROData_GeomTool.h>
 #include <SUIT_Desktop.h>
 #include <SUIT_ViewManager.h>
 
+#include <BRepBuilderAPI_GTransform.hxx>
+#include <gp_GTrsf.hxx>
+
 #include <QDialog>
+#include <QDir>
 #include <QTemporaryFile>
 
 /**
@@ -74,6 +80,12 @@ void HYDROGUI_RecognizeContoursOp::startOperation()
 {
   HYDROGUI_Operation::startOperation();
   
+  // Set preview view manager
+  if ( !getPreviewManager() ) {
+    setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
+                       module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
+  }
+
   if ( !isApplyAndClose() ) {
     return;
   }
@@ -88,7 +100,7 @@ void HYDROGUI_RecognizeContoursOp::startOperation()
 
   // Create temporary graphics file
   QImage aQImage = myImage->Image();
-  myTmpImageFile = new QTemporaryFile( anImageName );
+  myTmpImageFile = new QTemporaryFile( QDir::tempPath() + QDir::separator() + anImageName );
   if ( !myTmpImageFile->open() || 
        !aQImage.save( myTmpImageFile->fileName(), "PNG", 100 ) ) {
     abort();
@@ -130,10 +142,10 @@ void HYDROGUI_RecognizeContoursOp::startOperation()
       // update the object browser
       module()->getApp()->updateObjectBrowser( true );
 
-      // select the picture
-      SUIT_DataOwnerPtrList aList( true );
-      aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aGeomPictureEntry ) ) );
-      selectionMgr()->setSelected(aList );
+      // browse the published GEOM picture
+      QStringList anEntries;
+      anEntries << aGeomPictureEntry;
+      browseObjects( anEntries );
 
       // Add GEOM picture object entry to the list of temporary geom objects
       myTmpGeomObjects << aGeomPictureEntry;
@@ -218,7 +230,6 @@ bool HYDROGUI_RecognizeContoursOp::processApply( int& theUpdateFlags,
       aPolylineObj->SetName( aName );
       aPolylineObj->ImportShape( aShape );
       aPolylineObj->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
-      aPolylineObj->Transform( myImage->Trsf() );
 
       aPolylineObj->Update();
       module()->setIsToUpdate( aPolylineObj );
@@ -330,34 +341,67 @@ void HYDROGUI_RecognizeContoursOp::updateRecognizedPolylines()
     Handle(AIS_InteractiveContext) aCtx = NULL;
 
     // Display preview
-    if ( !getPreviewManager() ) {
-      setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
-                          module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
-    }
-
     OCCViewer_ViewManager* aViewManager = getPreviewManager();
     if ( aViewManager ) {
       if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
+        // aViewer->enablePreselection( true );
+        // aViewer->enableSelection( true );
         aCtx = aViewer->getAISContext();
         connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
       }
     }
 
+    QTransform aTrsf = myImage->Trsf();
+    QImage anImage = myImage->Image();
+    QRectF aRect( QPointF( 0, 0 ), QPointF( anImage.width(), anImage.height() ) );
+    aRect = aTrsf.mapRect( aRect );
+    aTrsf.setMatrix( aTrsf.m11(), aTrsf.m12(),  aTrsf.m13(),
+                     aTrsf.m21(), -aTrsf.m22(), aTrsf.m23(),
+                     aTrsf.m31() + aRect.width() * 0.5, aTrsf.m32 () - aRect.height() * 0.5, aTrsf.m33() );
+
+    /*
+    QTransform aTrsf = myImage->Trsf();
+    gp_Mat aMat( aTrsf.m11(), aTrsf.m21(), 0, 
+                 aTrsf.m12(), -aTrsf.m22(), 0, 
+                 0,           0,           1 );
+    QImage anImage = myImage->Image();
+    QRectF aRect( QPointF( 0, 0 ), QPointF( anImage.width(), anImage.height() ) );
+    aRect = aTrsf.mapRect( aRect );
+    gp_XYZ aVec( aTrsf.m31() + aRect.width() * 0.5, 
+                 aTrsf.m32() - aRect.height() * 0.5, 0 );
+
+    BRepBuilderAPI_GTransform aBuilder( gp_GTrsf( aMat, aVec ) );
+    */
+
+    Handle(HYDROData_PolylineXY) aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( doc()->CreateObject( KIND_POLYLINEXY ) );
     QStringList aNamesList;
     for ( int i = 1; i <= aSubShapes.Length(); i++ ) {
-      const TopoDS_Shape& aSubShape = aSubShapes.Value( i );
-
+      TopoDS_Shape aSubShape = aSubShapes.Value( i );
+
+      // Transform the sub-shape
+      aPolylineObj->ImportShape( aSubShape );
+      aPolylineObj->Transform( aTrsf );
+
+      /*
+      aBuilder.Perform( aSubShape, Standard_True );
+      if ( aBuilder.IsDone() ) {
+        aSubShape = aBuilder.Shape();
+      }*/
+      
       HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
-      aShape->setShape( aSubShape, true, false );
+      aShape->setShape( aPolylineObj->GetShape(), true, false );
       aShape->setBorderColor( HYDROData_PolylineXY::DefaultWireColor(), false, false );
-
+      
       QString aPrefix = QString("%1_%2_%3").arg( myImage->GetName(), "Contour", QString::number( i ) );
       QString aName = HYDROGUI_Tool::GenerateObjectName( module(), aPrefix, QStringList(), true );
       myPolylineShapes.insert( aName, aShape);
       aNamesList << aName;
     }
 
+    aPolylineObj->Remove();
+
     if ( !aCtx.IsNull() ) {
+      UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
       aCtx->UpdateCurrentViewer();
     }
 
@@ -387,9 +431,28 @@ void HYDROGUI_RecognizeContoursOp::erasePreview()
 */
 void HYDROGUI_RecognizeContoursOp::onSelectionChanged( const QStringList& theSelectedNames )
 {
-  foreach ( QString aName, myPolylineShapes.keys() ) {
-    bool isSelected = theSelectedNames.contains( aName );
-    myPolylineShapes[aName]->highlight( isSelected, true );
+  Handle(AIS_InteractiveContext) aCtx = NULL;
+
+  OCCViewer_ViewManager* aViewManager = getPreviewManager();
+  if ( aViewManager ) {
+    if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
+      aCtx = aViewer->getAISContext();
+    }
+  }
+
+  if ( !aCtx.IsNull() ) {
+    foreach ( QString aName, myPolylineShapes.keys() ) {
+      Handle(AIS_InteractiveObject) anObject = 
+        myPolylineShapes.value(aName)->getAISObject();
+
+      bool isSelected = theSelectedNames.contains( aName );
+      if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
+           ( !isSelected && aCtx->IsSelected( anObject) ) ) {
+        aCtx->AddOrRemoveSelected( anObject, Standard_False );
+      }
+      // myPolylineShapes[aName]->highlight( isSelected, true );
+    }
+    aCtx->UpdateCurrentViewer();
   }
 }
 
@@ -405,9 +468,9 @@ void HYDROGUI_RecognizeContoursOp::onViewerSelectionChanged()
     return;
   }
 
-  Handle(AIS_InteractiveContext) aCtx = NULL;
 
   OCCViewer_ViewManager* aViewManager = getPreviewManager();
+  Handle(AIS_InteractiveContext) aCtx = NULL;
   if ( aViewManager ) {
     if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
       aCtx = aViewer->getAISContext();
@@ -418,7 +481,7 @@ void HYDROGUI_RecognizeContoursOp::onViewerSelectionChanged()
     QStringList aSelectedNames;
 
     foreach ( QString aName, myPolylineShapes.keys() ) {
-      bool isSelected = aCtx->IsSelected( myPolylineShapes[aName]->getAISObject() );
+      bool isSelected = aCtx->IsSelected( myPolylineShapes.value(aName)->getAISObject() );
       if ( isSelected ) {
         aSelectedNames << aName;
       }