Salome HOME
Merge branch 'BR_LAND_COVER_MAP' of ssh://git.salome-platform.org/modules/hydro into...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ImportLandCoverMapOp.cxx
index d410018fb9d69426e6c65c38e2c31dd94ee5b922..9708f6563ee4437f6e19aac542ffec6a09aded9d 100644 (file)
 #include "HYDROGUI_Tool.h"
 #include "HYDROGUI_ImportLandCoverMapDlg.h"
 #include "HYDROGUI_Shape.h"
-#include <HYDROData_LandCoverMap.h>
 #include <HYDROGUI_ZLayers.h>
-
 #include <HYDROGUI_DataObject.h>
 #include <HYDROData_Iterator.h>
 #include <HYDROData_ShapeFile.h>
 #include <HYDROData_Profile.h>
+#include <HYDROData_LandCoverMap.h>
+#include <HYDROData_StricklerTable.h>
 
 #include <SUIT_Desktop.h>
 #include <SUIT_FileDlg.h>
-#include <LightApp_Application.h>
-
-#include <QApplication>
-#include <QFile>
-#include <QFileInfo>
 #include <SUIT_MessageBox.h>
+#include <SUIT_Desktop.h>
+#include <SUIT_ViewManager.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 <HYDROData_StricklerTable.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 <QSet>
+#include <QFile>
+#include <QFileInfo>
 
-#include <HYDROData_LandCoverMap.h>
+#include <QApplication>
+
+#define MAX_LCM_NAME_INDEX 1000
 
-//TODO add definitions into TS file
 HYDROGUI_ImportLandCoverMapOp::HYDROGUI_ImportLandCoverMapOp( HYDROGUI_Module* theModule )
 : HYDROGUI_Operation( theModule )
 {
@@ -78,23 +74,19 @@ HYDROGUI_ImportLandCoverMapOp::~HYDROGUI_ImportLandCoverMapOp()
 {
   erasePreview();
   myImporter.Free();
-  myAttrV.clear();
+  myAttrValue.clear();
 }
 
 void HYDROGUI_ImportLandCoverMapOp::startOperation()
 {
   HYDROGUI_Operation::startOperation();
 
-  if ( !getPreviewManager() ) {
-    setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
-                       module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
-  }
+  if ( !getPreviewManager() )
+    setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( module()->getApp()->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
 
-  HYDROGUI_ImportLandCoverMapDlg* aPanel = 
-    ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
-  if ( !aPanel ) {
+  HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
+  if ( !aPanel )
     return;
-  }
 
   aPanel->reset();
 }
@@ -169,15 +161,16 @@ void HYDROGUI_ImportLandCoverMapOp::onFileSelected()
         if ( aViewManager && !aCtx.IsNull() )
         {
           HYDROGUI_Shape* aShape = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
-          //Green color for now..
-          aShape->setFillingColor(QColor(0,255,0), false, false);
-          aShape->setBorderColor(QColor(0,255,0), false, false);
+          //Preview color is grey
+          aShape->setFillingColor(QColor(50,50,50), false, false);
+          aShape->setBorderColor(QColor(50,50,50), false, false);
           if( !aFace.IsNull() )
             aShape->setShape( aFace);
 
-          myPolygonName2PrsShape.insert( "polygon_" + QString::number(i), aShape);
+          myPolygonName2PrsShape.insert( tr( "DEF_POLYGON_NAME" ) + "_" + QString::number(i), aShape);
         }
       }
+
       if ( !aCtx.IsNull() ) 
       {
         UpdateZLayersOfHilightPresentationsOfDisplayedObjects( aCtx, Graphic3d_ZLayerId_TopOSD );
@@ -186,6 +179,19 @@ void HYDROGUI_ImportLandCoverMapOp::onFileSelected()
       //
       QApplication::restoreOverrideCursor();
       aPanel->setFirstPageState(true); 
+      //
+      //Try to load DBF-database...
+      QString aDBFFileName;
+      aDBFFileName = myFileName.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
+      bool DBF_Stat = myImporter.DBF_OpenDBF(aDBFFileName);
+      // TODO:
+      // add MSG BOX if stat is bad
+      if (DBF_Stat)
+      {
+        myFieldList = myImporter.DBF_GetFieldList();
+        aPanel->setAttributeNames(myFieldList);
+      }
+      aPanel->setDbfState(DBF_Stat);
     }
     else
     {
@@ -193,22 +199,20 @@ void HYDROGUI_ImportLandCoverMapOp::onFileSelected()
       aPanel->setPolygonNames(QStringList());
       aPanel->setObjectName("");
       QApplication::restoreOverrideCursor();
-      QString aMess = "Cannot import land cover;\n";
+      QString aMess = tr( "CANNT_IMPORT_LCM" ) + "\n"; 
       if (aStat == -1)
-        aMess += "Cannot open SHP file";
+        aMess += tr( "CANNT_OPEN_SHP" );
       else if (aStat == -2)
-        aMess += "Cannot open SHX file";
+        aMess += tr( "CANNT_OPEN_SHX" );
       else 
-        aMess += "The shape type of file is " + myImporter.GetShapeTypeName(aShapeTypeOfFile);
+        aMess += tr ("SHP_TYPEFORMAT_MESS") + myImporter.GetShapeTypeName(aShapeTypeOfFile);
       SUIT_MessageBox::warning( module()->getApp()->desktop(), tr( "IMPORT_LANDCOVER" ), aMess);
       commitDocOperation();
       myImporter.Free();
       //abort();
       aPanel->setFirstPageState(false);
     }
-    
   }
-  
 }
 
 HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverMapOp::createInputPanel() const
@@ -223,24 +227,23 @@ HYDROGUI_InputPanel* HYDROGUI_ImportLandCoverMapOp::createInputPanel() const
 }
 
 bool HYDROGUI_ImportLandCoverMapOp::processApply( int& theUpdateFlags,
-                                                QString& theErrorMsg,
-                                                QStringList& theBrowseObjectsEntries )
+                                                  QString& theErrorMsg,
+                                                  QStringList& theBrowseObjectsEntries )
 {
 
   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
-  if ( !aPanel ) {
+  if ( !aPanel ) 
     return false;
-  }
 
-  Handle(HYDROData_LandCoverMap) aLCM =  Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
+  myLCM = Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
   HYDROData_MapOfFaceToStricklerType aMapFace2ST;
 
   QStringList aAttrV_T;
   QStringList aSTL;
-  aPanel->GetAttribute2StricklerCorr(aAttrV_T, aSTL);
+  aPanel->getValAttr2StricklerTypeCorr(aAttrV_T, aSTL);
 
   QVector<int> aSelIndices = aPanel->getSelectedPolygonIndices();
-  foreach ( int Ind, aSelIndices ) //check i-base.
+  foreach ( int Ind, aSelIndices )
   {
     TopoDS_Shape aShape = myPolygonFaces(Ind + 1);
     if ( aShape.IsNull() ) 
@@ -248,34 +251,49 @@ bool HYDROGUI_ImportLandCoverMapOp::processApply( int& theUpdateFlags,
     QString aST = "";
     if (aPanel->getAttrCheckBoxState())
     {
-      HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[Ind];
-      int StricklerTypesInd = aAttrV_T.indexOf(QString(aV.myStrVal));
-      aST = aSTL.at(StricklerTypesInd);
+      HYDROData_ShapeFile::DBF_AttrValue aDataVal = myAttrValue[Ind];
+      int aStricklerTypesInd = aAttrV_T.indexOf(QString(aDataVal.myStrVal));
+      aST = aSTL.at(aStricklerTypesInd);
     }
     // else => ST is empty
-    aMapFace2ST.Add( TopoDS::Face( aShape ), aST ); //TODO get from tableW
+    aMapFace2ST.Add( TopoDS::Face( aShape ), aST );
   }
 
   //
-  aLCM->StoreLandCovers(aMapFace2ST);
+  myLCM->StoreLandCovers(aMapFace2ST);
 
-  QString ObjName;
+  QString anObjName;
   if ( !aPanel->getFileName().isEmpty() )
-    ObjName = aPanel->getObjectName();
+    anObjName = aPanel->getObjectName();
+  
+  Handle_HYDROData_Document aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
 
-  if( !aLCM.IsNull() ) 
+  //check if name of LCM is already exists
+  QSet<QString> aNameList;
+  if (aDoc)
   {
-    aLCM->SetName( ObjName );
-    aLCM->SetColor( Qt::gray );
-    aLCM->Show();
-    module()->setIsToUpdate( aLCM );
+    HYDROData_Iterator anIt( aDoc, KIND_LAND_COVER_MAP );
+    for( ; anIt.More(); anIt.Next() )
+      aNameList << anIt.Current()->GetName();
+  }
+
+  QString aNewName = anObjName;
+  for ( int i = 1; i < MAX_LCM_NAME_INDEX && aNameList.contains(aNewName); i++)
+    aNewName = anObjName + "_" + QString::number(i);
+
+  if( !myLCM.IsNull() ) 
+  {
+    myLCM->SetName( aNewName );
+    myLCM->SetColor( Qt::gray );
+    myLCM->Show();
+    module()->setIsToUpdate( myLCM );
   }
   
   module()->update( UF_Model | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init );
 
   erasePreview();
   myImporter.Free();
-  myAttrV.clear();
+  myAttrValue.clear();
 
   return true;
 }
@@ -309,9 +327,8 @@ void HYDROGUI_ImportLandCoverMapOp::onSelectionChanged( const QStringList& theSe
 void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
 {
   HYDROGUI_ImportLandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_ImportLandCoverMapDlg*>( inputPanel() );
-  if ( !aPanel ) {
+  if ( !aPanel )
     return;
-  }
 
   OCCViewer_ViewManager* aViewManager = getPreviewManager();
   Handle(AIS_InteractiveContext) aCtx = NULL;
@@ -336,9 +353,8 @@ void HYDROGUI_ImportLandCoverMapOp::onViewerSelectionChanged()
 
 void HYDROGUI_ImportLandCoverMapOp::erasePreview()
 {
-  foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape ) {
+  foreach ( HYDROGUI_Shape* aShape, myPolygonName2PrsShape )
     delete aShape;
-  }
 
   myPolygonName2PrsShape.clear();
 }
@@ -347,9 +363,8 @@ void HYDROGUI_ImportLandCoverMapOp::erasePreview()
 void HYDROGUI_ImportLandCoverMapOp::abortOperation()
 {
   LightApp_Application* anApp = module()->getApp();
-  if ( anApp ) {
+  if ( anApp )
     anApp->disconnect( this );
-  }
 
   erasePreview();
   myImporter.Free();
@@ -362,46 +377,35 @@ void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
   if ( !aPanel )
     return;
 
-  if (theIndex == 1)
-  {
-      //Try to load DBF-database...
-      QString theDBFFileName;
-      theDBFFileName = myFileName.simplified().replace( ".shp", ".dbf", Qt::CaseInsensitive);
-      bool DBF_Stat = myImporter.DBF_OpenDBF(theDBFFileName);
-      // TODO:
-      // add MSG BOX if stat is bad
-      myFieldList = myImporter.DBF_GetFieldList();
-      aPanel->setAttributeNames(myFieldList);
-  }
-
   if (theIndex == 2)
   {
-    //std::vector<HYDROData_ShapeFile::DBF_AttrValue> myAttrV;
-    int Ind = myFieldList.indexOf(aPanel->getSelectedFieldName());
-    if (Ind == -1)
-    {
+    //std::vector<HYDROData_ShapeFile::DBF_AttrValue> myAttrValue;
+    int anIndOfSelField = myFieldList.indexOf(aPanel->getSelectedFieldName());
+    if (anIndOfSelField == -1)
       return;
-    }
     //aPanel->setSecondPageState(true);
-    myAttrV.clear();
-    myImporter.DBF_GetAttributeList(Ind, myAttrV ); 
+    myAttrValue.clear();
+    myImporter.DBF_GetAttributeList(anIndOfSelField, myAttrValue ); 
 
     mySetOfAttrValues.clear();
-    for (size_t i = 0; i < myAttrV.size(); i++)
+    for (size_t i = 0; i < myAttrValue.size(); i++)
     {
-      HYDROData_ShapeFile::DBF_AttrValue aV = myAttrV[i];
-      mySetOfAttrValues << QString(aV.myStrVal);  //take myStrVal by now..
+      HYDROData_ShapeFile::DBF_AttrValue aV = myAttrValue[i];
+      mySetOfAttrValues << QString(aV.myStrVal);  //take myStrVal for now..
     }
 
     //Collect all strickler_types
     QSet<QString> aSTSet;
     Handle_HYDROData_Document aDoc = HYDROData_Document::Document( application()->activeStudy()->id() );
+    Handle(HYDROData_StricklerTable) DefStricklerTableObj;
     if ( aDoc )
     {
       HYDROData_Iterator It( aDoc, KIND_STRICKLER_TABLE );
       for( ; It.More(); It.Next() )
       {
-        Handle(HYDROData_StricklerTable) aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( It.Current() );      
+        Handle(HYDROData_StricklerTable) aStricklerTableObj = Handle(HYDROData_StricklerTable)::DownCast( It.Current() );
+        if (DefStricklerTableObj.IsNull())
+          DefStricklerTableObj = aStricklerTableObj;
         if ( !aStricklerTableObj.IsNull())
         {
           const QStringList& aStricklerList = aStricklerTableObj->GetTypes();
@@ -410,17 +414,35 @@ void HYDROGUI_ImportLandCoverMapOp::onNext( const int theIndex )
         }
       }
     }
-    QList<QString> SetOfAttrValuesList = mySetOfAttrValues.toList();
+
+    QList<QString> aSetOfAttrValuesList = mySetOfAttrValues.toList();
     QList<QString> aSTSetList = aSTSet.toList();
-    QVector<QColor> STColors;
-    STColors.reserve(aSTSetList.size());
+    QVector<QColor> aSTColors;
+    aSTColors.reserve(aSTSetList.size());
     foreach (QString str, aSTSetList)
     {
       QColor col = aDoc->GetAssociatedColor(str, NULL);
-      STColors.append (col);
+      aSTColors.append (col);
     }
 
-    aPanel->FillCorrTable(SetOfAttrValuesList, aSTSetList, STColors);
+    //add an empty Strickler type
+    aSTSetList.prepend(""); 
+    aSTColors.prepend(QColor(Qt::gray));
+    
+    QVector<int> aCurCBIndices(aSetOfAttrValuesList.size());
+    if (DefStricklerTableObj->GetAttrName() == aPanel->getSelectedFieldName())
+      for (int i = 0; i < aSetOfAttrValuesList.size(); i++)
+      {
+        QString aST = DefStricklerTableObj->GetType(aSetOfAttrValuesList[i]);
+        int anIndOfSt = aSTSetList.indexOf(aST);
+        aCurCBIndices[i] = anIndOfSt;
+      }
+    else
+      //TODO add warning ???
+      for (int i = 0; i < aSetOfAttrValuesList.size(); i++)
+        aCurCBIndices[i] = 0;
+
+    aPanel->FillCorrespondenceTable(aSetOfAttrValuesList, aSTSetList, aCurCBIndices, aSTColors);    
   }
 
 }
@@ -440,7 +462,7 @@ void HYDROGUI_ImportLandCoverMapOp::onApply()
   }
   catch (...)
   {
-    SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "LCM_IMPORT_ERROR" ), "Cant import choosed polygons");
+    SUIT_MessageBox::critical( module()->getApp()->desktop(), tr( "LCM_IMPORT_ERROR" ), tr ("CANNT_IMPORT_POLYGONS_FROM_SHP"));
     aResult = false;
   }
   
@@ -454,6 +476,9 @@ void HYDROGUI_ImportLandCoverMapOp::onApply()
   }
   else
   {
+    Handle(HYDROData_Entity) LCM = Handle(HYDROData_Entity)::DownCast( myLCM );
+    LCM->Remove();
+    module()->setObjectRemoved( myLCM );
     abort();
   }
 }