Salome HOME
LCM // Import/Export of SHP p.4
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportLandCoverMapOp.cxx
index 5490f57fd530b1866b24c009303ce17fa0064d15..d410018fb9d69426e6c65c38e2c31dd94ee5b922 100644 (file)
 #include <TopoDS.hxx>
 #include <TopoDS_Shape.hxx>
 #include <TopoDS_Wire.hxx>
-#include <qset.h>
+#include <QSet>
 
+#include <HYDROData_LandCoverMap.h>
+
+//TODO add definitions into TS file
 HYDROGUI_ImportLandCoverMapOp::HYDROGUI_ImportLandCoverMapOp( HYDROGUI_Module* theModule )
 : HYDROGUI_Operation( theModule )
 {
@@ -74,6 +77,8 @@ HYDROGUI_ImportLandCoverMapOp::HYDROGUI_ImportLandCoverMapOp( HYDROGUI_Module* t
 HYDROGUI_ImportLandCoverMapOp::~HYDROGUI_ImportLandCoverMapOp()
 {
   erasePreview();
+  myImporter.Free();
+  myAttrV.clear();
 }
 
 void HYDROGUI_ImportLandCoverMapOp::startOperation()
@@ -85,10 +90,6 @@ void HYDROGUI_ImportLandCoverMapOp::startOperation()
                        module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
   }
 
-  if ( !isApplyAndClose() ) {
-    return;
-  }
-
   HYDROGUI_ImportLandCoverMapDlg* aPanel = 
     ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
   if ( !aPanel ) {
@@ -184,7 +185,7 @@ void HYDROGUI_ImportLandCoverMapOp::onFileSelected()
       }
       //
       QApplication::restoreOverrideCursor();
-      commitDocOperation();
+      aPanel->setFirstPageState(true); 
     }
     else
     {
@@ -203,6 +204,7 @@ void HYDROGUI_ImportLandCoverMapOp::onFileSelected()
       commitDocOperation();
       myImporter.Free();
       //abort();
+      aPanel->setFirstPageState(false);
     }
     
   }
@@ -214,12 +216,8 @@ HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverMapOp::createInputPanel() const
   HYDROGUI_InputPanel* aPanel = new HYDROGUI_ImportLandCoverMapDlg( module(), getName() );
 
   connect( aPanel, SIGNAL( FileSelected( const QString& ) ), SLOT( onFileSelected() ) );
-
   connect( aPanel, SIGNAL( selectionChanged( const QStringList& ) ), this, SLOT( onSelectionChanged( const QStringList& ) ) );
-
   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
-  connect( aPanel, SIGNAL( Back( const int ) ), SLOT( onBack( const int ) ) );
-  connect( aPanel, SIGNAL( Finish( const int ) ), SLOT( onFinish( const int ) ) );
     
   return aPanel;
 }
@@ -233,67 +231,51 @@ bool HYDROGUI_ImportLandCoverMapOp::processApply( int& theUpdateFlags,
   if ( !aPanel ) {
     return false;
   }
-  
-  QStringList aSelectedtPolygons = aPanel->getSelectedPolygonNames();
-  aPanel->removePolygonNames( aSelectedtPolygons );
-    
-  if (!aSelectedtPolygons.empty())
+
+  Handle(HYDROData_LandCoverMap) aLCM =  Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
+  HYDROData_MapOfFaceToStricklerType aMapFace2ST;
+
+  QStringList aAttrV_T;
+  QStringList aSTL;
+  aPanel->GetAttribute2StricklerCorr(aAttrV_T, aSTL);
+
+  QVector<int> aSelIndices = aPanel->getSelectedPolygonIndices();
+  foreach ( int Ind, aSelIndices ) //check i-base.
   {
-    Handle(HYDROData_LandCoverMap) aLCM =  Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
-    TopoDS_Shape aResShape;
-    if (aSelectedtPolygons.size() > 1) 
+    TopoDS_Shape aShape = myPolygonFaces(Ind + 1);
+    if ( aShape.IsNull() ) 
+      continue;
+    QString aST = "";
+    if (aPanel->getAttrCheckBoxState())
     {
-      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( !aLCM.IsNull() ) 
-    {
-      QString aLCName = aPanel->getObjectName() + "_polygon";
-      int i = 0;
-      for( ;HYDROGUI_Tool::FindObjectByName(module(), aLCName); i++)
-        aLCName = aPanel->getObjectName() + "_polygon_" + QString::number(i);
-      aLCM->SetName( aLCName );
-      //TODO add color
-      //aLCM->SetFillingColor( aLCM->DefaultFillingColor() );
-      //aLCM->SetBorderColor( aLCM->DefaultBorderColor() );
-      
-      //aLCM->SetShape(aResShape);
-      aLCM->Show();
-            
-      //erasePreview();
-      
-      module()->setIsToUpdate( aLCM );
-      
+      HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[Ind];
+      int StricklerTypesInd = aAttrV_T.indexOf(QString(aV.myStrVal));
+      aST = aSTL.at(StricklerTypesInd);
     }
+    // else => ST is empty
+    aMapFace2ST.Add( TopoDS::Face( aShape ), aST ); //TODO get from tableW
   }
-  module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
 
-  if ( isApplyAndClose() )
-    erasePreview();
+  //
+  aLCM->StoreLandCovers(aMapFace2ST);
 
+  QString ObjName;
+  if ( !aPanel->getFileName().isEmpty() )
+    ObjName = aPanel->getObjectName();
+
+  if( !aLCM.IsNull() ) 
+  {
+    aLCM->SetName( ObjName );
+    aLCM->SetColor( Qt::gray );
+    aLCM->Show();
+    module()->setIsToUpdate( aLCM );
+  }
+  
+  module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
+
+  erasePreview();
   myImporter.Free();
+  myAttrV.clear();
 
   return true;
 }
@@ -316,14 +298,8 @@ void HYDROGUI_ImportLandCoverMapOp::onSelectionChanged( const QStringList& theSe
 
       bool isSelected = theSelectedNames.contains( aName );
       if ( ( isSelected && !aCtx->IsSelected( anObject) ) ||
-           ( !isSelected && aCtx->IsSelected( anObject) ) ) {
+           ( !isSelected && aCtx->IsSelected( anObject) ) )
         aCtx->AddOrRemoveSelected( anObject, Standard_False );
-      }
-      /*if (isSelected)
-      {
-        HYDROGUI_Shape* aHydroSh = myPolygonName2PrsShape.value( aName );
-        aHydroSh->setFillingColor(QColor(0,255,255), true, true);
-      }*/
     }
     aCtx->UpdateCurrentViewer();
   }
@@ -332,7 +308,6 @@ void HYDROGUI_ImportLandCoverMapOp::onSelectionChanged( const QStringList& theSe
 
 void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
 {
-  // Get panel
   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
   if ( !aPanel ) {
     return;
@@ -351,9 +326,8 @@ void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
     QStringList aSelectedNames;
     foreach ( QString aName, myPolygonName2PrsShape.keys() ) {
       bool isSelected = aCtx->IsSelected( myPolygonName2PrsShape.value(aName)->getAISObject() );
-      if ( isSelected ) {
+      if ( isSelected )
         aSelectedNames << aName;
-      }
     }
     aPanel->setSelectedPolygonNames( aSelectedNames );
   }
@@ -378,14 +352,12 @@ void HYDROGUI_ImportLandCoverMapOp::abortOperation()
   }
 
   erasePreview();
-
+  myImporter.Free();
   HYDROGUI_Operation::abortOperation();
 }
 
-
 void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
 {  
-  //TODO add acceptor
   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
   if ( !aPanel )
     return;
@@ -404,16 +376,20 @@ void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
 
   if (theIndex == 2)
   {
-    std::vector<HYDROData_ShapeFile::DBF_AttrValue> theAttrV;
+    //std::vector<HYDROData_ShapeFile::DBF_AttrValue> myAttrV;
     int Ind = myFieldList.indexOf(aPanel->getSelectedFieldName());
     if (Ind == -1)
-      return; //TODO add acceptor here!!
-    myImporter.DBF_GetAttributeList(Ind, theAttrV ); 
+    {
+      return;
+    }
+    //aPanel->setSecondPageState(true);
+    myAttrV.clear();
+    myImporter.DBF_GetAttributeList(Ind, myAttrV ); 
 
     mySetOfAttrValues.clear();
-    for (size_t i = 0; i < theAttrV.size(); i++)
+    for (size_t i = 0; i < myAttrV.size(); i++)
     {
-      HYDROData_ShapeFile::DBF_AttrValue aV = theAttrV[i];
+      HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[i];
       mySetOfAttrValues << QString(aV.myStrVal);  //take myStrVal by now..
     }
 
@@ -434,12 +410,51 @@ void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
         }
       }
     }
-    aPanel->FillCorrTable(mySetOfAttrValues.toList(), aSTSet.toList());
+    QList<QString> SetOfAttrValuesList = mySetOfAttrValues.toList();
+    QList<QString> aSTSetList = aSTSet.toList();
+    QVector<QColor> STColors;
+    STColors.reserve(aSTSetList.size());
+    foreach (QString str, aSTSetList)
+    {
+      QColor col = aDoc->GetAssociatedColor(str, NULL);
+      STColors.append (col);
+    }
+
+    aPanel->FillCorrTable(SetOfAttrValuesList, aSTSetList, STColors);
   }
 
 }
 
-void HYDROGUI_ImportLandCoverMapOp::onBack( const int theIndex )
+
+void HYDROGUI_ImportLandCoverMapOp::onApply()
 {
+  QApplication::setOverrideCursor( Qt::WaitCursor );
+  int anUpdateFlags = 0;
+  QString anErrorMsg;
+  QStringList aBrowseObjectsEntries;
+
+  bool aResult = false;
+  try
+  {
+    aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
+  }
+  catch (...)
+  {
+    SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "LCM_IMPORT_ERROR" ), "Cant import choosed polygons");
+    aResult = false;
+  }
+  
+  QApplication::restoreOverrideCursor();
 
+  if ( aResult )
+  {
+    module()->update( anUpdateFlags );
+    commit();
+    browseObjects( aBrowseObjectsEntries );
+  }
+  else
+  {
+    abort();
+  }
 }
+