Salome HOME
refs #585: polylines operations (split/merge)
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportLandcoverOp.cxx
index 56ca83951489b9fb50ce4201381885aef4dc6f8c..79179eb31500dd7aa075eb3954f6ec2ea83bb4ed 100644 (file)
 #include "HYDROGUI_UpdateFlags.h"
 #include "HYDROGUI_Tool.h"
 #include "HYDROGUI_ImportLandCoverDlg.h"
+#include "HYDROGUI_Shape.h"
+#include <HYDROData_LandCover.h>
+#include <HYDROGUI_ZLayers.h>
 
 #include <HYDROGUI_DataObject.h>
-#include <HYDROData_Bathymetry.h>
 #include <HYDROData_Iterator.h>
 
 #include <HYDROData_Profile.h>
 #include <QFileInfo>
 #include <SUIT_MessageBox.h>
 
+#include <OCCViewer_ViewManager.h>
+#include <OCCViewer_ViewModel.h>
+
+#include <SalomeApp_Study.h>
+
+#include <LightApp_Application.h>
+#include <LightApp_DataOwner.h>
+#include <LightApp_Displayer.h>
+#include <LightApp_SelectionMgr.h>
+
+#include <SUIT_Desktop.h>
+#include <SUIT_ViewManager.h>
+
 #include <BRep_Builder.hxx>
 #include <TopoDS.hxx>
 #include <TopoDS_Shape.hxx>
 #include <TopoDS_Wire.hxx>
-#include <BRepBuilderAPI_MakeEdge2d.hxx>
+#include <BRepBuilderAPI_MakeEdge.hxx>
 #include <BRepBuilderAPI_MakeWire.hxx>
 #include <BRepBuilderAPI_MakeFace.hxx>
-#include <gp_Pnt2d.hxx>
-#include <BRepTools.hxx>
 #include <gp_Pln.hxx>
-#include <QLineEdit>
+#include <BRepLib.hxx>
+#include <ShapeFix_Shape.hxx>
 
 
 HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModule )
@@ -64,21 +78,29 @@ HYDROGUI_ImportLandCoverOp::HYDROGUI_ImportLandCoverOp( HYDROGUI_Module* theModu
 
 HYDROGUI_ImportLandCoverOp::~HYDROGUI_ImportLandCoverOp()
 {
+  erasePreview();
 }
 
 void HYDROGUI_ImportLandCoverOp::startOperation()
 {
   HYDROGUI_Operation::startOperation();
 
-  /*myFileDlg = new SUIT_FileDlg( module()->getApp()->desktop(), true );
-  myFileDlg->setWindowTitle( getName() );
-  myFileDlg->setFileMode( SUIT_FileDlg::ExistingFiles );
-  myFileDlg->setFilter( tr("LANDCOVER_FILTER") );
+  if ( !getPreviewManager() ) {
+    setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
+                       module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
+  }
+
+  if ( !isApplyAndClose() ) {
+    return;
+  }
 
-  connect( myFileDlg, SIGNAL( accepted() ), this, SLOT( onApply() ) );
-  connect( myFileDlg, SIGNAL( rejected() ), this, SLOT( onCancel() ) );
+  HYDROGUI_ImportLandCoverDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
+  if ( !aPanel ) {
+    return;
+  }
 
-  myFileDlg->exec();*/
+  aPanel->reset();
 }
 
 
@@ -93,13 +115,76 @@ HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverOp::createInputPanel() const
   return aPanel;
 }
 
-void HYDROGUI_ImportLandCoverOp::onApply()
+bool HYDROGUI_ImportLandCoverOp::processApply( int& theUpdateFlags,
+                                                QString& theErrorMsg,
+                                                QStringList& theBrowseObjectsEntries )
 {
-  module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
+
+  HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
+  if ( !aPanel ) {
+    return false;
+  }
   
-  QApplication::restoreOverrideCursor();
-}
+  QStringList aSelectedtPolygons = aPanel->getSelectedPolygonNames();
+  aPanel->removePolygonNames( aSelectedtPolygons );
+    
+  if (!aSelectedtPolygons.empty())
+  {
+    Handle(HYDROData_LandCover) aLC =  Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
+    TopoDS_Shape aResShape;
+    if (aSelectedtPolygons.size() > 1) 
+    {
+      TopoDS_Compound cmp;
+      BRep_Builder BB;
+      BB.MakeCompound(cmp);
+
+      foreach ( QString aName, aSelectedtPolygons ) {
+        TopoDS_Shape aShape = myPolygonName2PrsShape.value( aName )->getTopoShape();
+        if ( aShape.IsNull() ) 
+          continue;
+        BB.Add(cmp, aShape);
+        HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aName );
+        delete aShapeToDelete;
+      }
+      aResShape = cmp;
+    }
+    else
+    {         
+      TopoDS_Shape aShape = myPolygonName2PrsShape.value( aSelectedtPolygons.first() )->getTopoShape();
+      if ( !aShape.IsNull() ) 
+      {
+        HYDROGUI_Shape* aShapeToDelete = myPolygonName2PrsShape.take( aSelectedtPolygons.first() );
+        delete aShapeToDelete;
+       aResShape = aShape;
+      }
+    }
+    if( !aLC.IsNull() ) 
+    {
+      QString aLCName = aPanel->getObjectName() + "_polygon";
+      int i = 0;
+      for( ;HYDROGUI_Tool::FindObjectByName(module(), aLCName); i++)
+        aLCName = aPanel->getObjectName() + "_polygon_" + QString::number(i);
+      aLC->SetName( aLCName );
+      aLC->SetFillingColor( HYDROData_LandCover::DefaultFillingColor() );
+      aLC->SetBorderColor( HYDROData_LandCover::DefaultBorderColor() );
+      
+      aLC->setShape(aResShape);
+      aLC->Show();
+            
+      //erasePreview();
+      
+      module()->setIsToUpdate( aLC );
+      
+    }
+  }
+  module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
 
+  if ( isApplyAndClose() )
+    erasePreview();
+
+  return true;
+}
 
 
 void HYDROGUI_ImportLandCoverOp::Parse(SHPHandle theHandle)
@@ -121,6 +206,10 @@ void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face
   int nParts = anObj->nParts;
   gp_Pln pln(gp_Pnt(0,0,0), gp_Dir(0,0,1));
   BRepBuilderAPI_MakeFace aFBuilder(pln);
+
+  //Handle(ShapeFix_Shape) sfs = new ShapeFix_Shape;
+  //sfs->FixFaceTool()->FixOrientationMode() = 1;
+  
   for ( int i = 0 ; i < nParts ; i++ )
   { 
     BRepBuilderAPI_MakeWire aBuilder;
@@ -133,33 +222,27 @@ void HYDROGUI_ImportLandCoverOp::ProcessSHP(SHPObject* anObj, int i, TopoDS_Face
 
     for ( int k = StartIndex; k < EndIndex - 1  ; k++ )
     {
-      gp_Pnt2d P1 (anObj->padfX[k], anObj->padfY[k]);
-      gp_Pnt2d P2 (anObj->padfX[k+1], anObj->padfY[k+1]);
-      BRepBuilderAPI_MakeEdge2d aMakeEdge(P1, P2);
+      gp_Pnt P1 (anObj->padfX[k], anObj->padfY[k], 0);
+      gp_Pnt P2 (anObj->padfX[k+1], anObj->padfY[k+1], 0);
+      BRepBuilderAPI_MakeEdge aMakeEdge(P1, P2);
       aBuilder.Add(TopoDS::Edge(aMakeEdge.Shape()));
     }
+    
     aBuilder.Build();
     W = TopoDS::Wire(aBuilder.Shape());
+    W.Reverse();
     aFBuilder.Add(W);
   }
 
   aFBuilder.Build();
-  F = aFBuilder.Face();
-
-  //TODO build curve 3d
-
-  /*aPolylineXY->SetWireColor( HYDROData_PolylineXY::DefaultWireColor() );
-  aPolylineXY->SetName( theFileName + "_PolyXY_" + QString::number(theInd) );
-  
-  aPolylineXY->Update();
-  
-  size_t anActiveViewId = HYDROGUI_Tool::GetActiveGraphicsViewId( module() );
-  if ( anActiveViewId == 0 )
-    anActiveViewId = HYDROGUI_Tool::GetActiveOCCViewId( module() );
-  
-  module()->setObjectVisible( anActiveViewId, aPolylineXY, true );
-  
-  module()->setIsToUpdate( aPolylineXY );*/
+  TopoDS_Face DF = aFBuilder.Face();
+  BRepLib::BuildCurves3d(DF);
+  if(!DF.IsNull()) 
+  {
+    //sfs->Init ( DF );
+    //sfs->Perform();
+    F = DF; //TopoDS::Face(sfs->Shape());
+  }
 }
 
 void HYDROGUI_ImportLandCoverOp::onFileSelected()
@@ -167,19 +250,17 @@ void HYDROGUI_ImportLandCoverOp::onFileSelected()
   HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
   if ( !aPanel )
     return;
-
+  
   QString anObjectName = aPanel->getObjectName().simplified();
-   anObjectName = aPanel->getFileName();
-   if ( !anObjectName.isEmpty() ) {
-       anObjectName = QFileInfo( anObjectName ).baseName();
-   }
+  anObjectName = aPanel->getFileName();
+  if ( !anObjectName.isEmpty() ) 
+      anObjectName = QFileInfo( anObjectName ).baseName();
 
-   if ( anObjectName.isEmpty() ) {
-     anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_BATHYMETRY_NAME" ) );
-   }
-   aPanel->setObjectName( anObjectName );
+  if ( anObjectName.isEmpty() ) 
+    anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_LANDCOVER_NAME" ) );
+  aPanel->setObjectName( anObjectName );
 
-  QString aFileName = aPanel->myFileName->text();
+  QString aFileName = aPanel->getFileName();
   if ( aFileName.isEmpty() )
   {
     abort();
@@ -193,29 +274,77 @@ void HYDROGUI_ImportLandCoverOp::onFileSelected()
     SHPHandle aHSHP;
     aHSHP = SHPOpen( aFileName.toAscii().data(), "rb" );
     Parse(aHSHP);
-  
+    
     startDocOperation();
     QStringList aPolygonsList;
-    for (int i = 0; i < mySHPObjects.size(); i++)
+    for (int i = 1; i < mySHPObjects.size(); i++)
       aPolygonsList.append("polygon_" + QString::number(i));
-    aPanel->setPolylineNames(aPolygonsList);
-    TopoDS_Compound cmp;
-    BRep_Builder BB;
-    BB.MakeCompound(cmp);
-    TopoDS_Face F;
+    aPanel->setPolygonNames(aPolygonsList);
+
+    SalomeApp_Study* aStudy = dynamic_cast<SalomeApp_Study*>( module()->getApp()->activeStudy() );
+    if ( !aStudy ) {
+      return;
+    }
+
+    erasePreview();
+
+    Handle(AIS_InteractiveContext) aCtx = NULL;
+
+    LightApp_Application* anApp = module()->getApp();
+    if ( !getPreviewManager() )
+      setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
+    OCCViewer_ViewManager* aViewManager = getPreviewManager();
+
+    if ( aViewManager )
+    {
+      if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
+      {
+        aCtx = aViewer->getAISContext();
+        connect( aViewer, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
+      }
+    }
+
+    QApplication::setOverrideCursor(Qt::WaitCursor);
+
+    TopTools_SequenceOfShape aFaces;
+    TopoDS_Face aF;
     if (aHSHP->nShapeType == 5)
     {
-      for (int i = 0; i < 10/*mySHPObjects.size()*/; i++) {
-         ProcessSHP(mySHPObjects[i], i, F);
-         BB.Add(cmp, F);
+      for (int i = 0; i < mySHPObjects.size(); i++) 
+      {
+         ProcessSHP(mySHPObjects[i], i, aF);
+         aFaces.Append(aF);
       }
-      ///to hydro_landcover
-      // BRepTools::Write(cmp, "d:/h1.brep");
     }
     else
       SUIT_MessageBox::warning( module()->getApp()->desktop(), "Import Land cover", "Cannot land cover;\nThe shape type is not polygon" );
-    
+    QApplication::restoreOverrideCursor();
+
+    Handle(HYDROData_LandCover) aLC = Handle(HYDROData_LandCover)::DownCast( doc()->CreateObject( KIND_LAND_COVER ) );
+    for ( int i = 1; i <= aFaces.Length(); i++ ) 
+    {
+      TopoDS_Face aFace = TopoDS::Face(aFaces.Value( i ));
+
+      aLC->setShape( aFace );      
+      
+      if ( aViewManager && !aCtx.IsNull() )
+      {
+        HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
+
+        aShape->setFillingColor( HYDROData_LandCover::DefaultFillingColor(), false, false );
+        aShape->setBorderColor( HYDROData_LandCover::DefaultBorderColor(), false, false );
+        if( !aFace.IsNull() )
+          aShape->setShape( aLC->GetShape() );
+        myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
+      }
+    }
+    aLC->Remove();
+
+    if ( !aCtx.IsNull() ) {
+      UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
+      aCtx->UpdateCurrentViewer();
+    }
+
     commitDocOperation();
     
     for (size_t i = 0; i < mySHPObjects.size(); i++ )
@@ -227,5 +356,83 @@ void HYDROGUI_ImportLandCoverOp::onFileSelected()
   
 }
 
+void HYDROGUI_ImportLandCoverOp::onSelectionChanged( const QStringList& theSelectedNames )
+{
+  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, myPolygonName2PrsShape.keys() ) {
+      Handle(AIS_InteractiveObject) anObject = 
+        myPolygonName2PrsShape.value(aName)->getAISObject();
+
+      bool isSelected = theSelectedNames.contains( aName );
+      if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
+           ( !isSelected && aCtx->IsSelected( anObject) ) ) {
+        aCtx->AddOrRemoveSelected( anObject, Standard_False );
+      }
+    }
+    aCtx->UpdateCurrentViewer();
+  }
+}
+
+
+void HYDROGUI_ImportLandCoverOp::onViewerSelectionChanged()
+{
+  // Get panel
+  HYDROGUI_ImportLandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverDlg*>( inputPanel() );
+  if ( !aPanel ) {
+    return;
+  }
+
+  OCCViewer_ViewManager* aViewManager = getPreviewManager();
+  Handle(AIS_InteractiveContext) aCtx = NULL;
+  if ( aViewManager ) {
+    if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
+      aCtx = aViewer->getAISContext();
+    }
+  }
+  
+  if ( !aCtx.IsNull() )
+  {
+    QStringList aSelectedNames;
+    foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
+      bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
+      if ( isSelected ) {
+        aSelectedNames << aName;
+      }
+    }
+    aPanel->setSelectedPolygonNames( aSelectedNames );
+  }
+}
+
+
+void HYDROGUI_ImportLandCoverOp::erasePreview()
+{
+  foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
+    delete aShape;
+  }
+
+  myPolygonName2PrsShape.clear();
+}
+
+
+void HYDROGUI_ImportLandCoverOp::abortOperation()
+{
+  LightApp_Application* anApp = module()->getApp();
+  if ( anApp ) {
+    anApp->disconnect( this );
+  }
+
+  erasePreview();
+
+  HYDROGUI_Operation::abortOperation();
+}