Salome HOME
LCM // Import/Export of SHP p.6
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_DataModel.cxx
index 366f9c789b98fb52a0ec099edb072a925363bee0..23d8dc989bd03acc10bea47d8638a5ce7ebf8850 100644 (file)
@@ -44,6 +44,7 @@
 #include <HYDROData_River.h>
 #include <HYDROData_Stream.h>
 #include <HYDROData_StricklerTable.h>
+#include <HYDROData_LandCoverMap.h>
 
 #include <CAM_Module.h>
 #include <CAM_Study.h>
@@ -86,6 +87,14 @@ HYDROGUI_DataModel::~HYDROGUI_DataModel()
 {
 }
 
+bool HYDROGUI_DataModel::create( CAM_Study* theStudy )
+{
+    bool status = LightApp_DataModel::create( theStudy );
+    if ( status )
+        updateDocument();
+    return status;
+}
+
 bool HYDROGUI_DataModel::open( const QString& theURL,
                                CAM_Study* theStudy,
                                QStringList theFileList )
@@ -112,13 +121,14 @@ bool HYDROGUI_DataModel::open( const QString& theURL,
     {
       res = DocError_UnknownProblem;
     }
-    if( res != DocError_OK )
+    
+    if ( res != DocError_OK )
     {
       module()->application()->putInfo( tr( "LOAD_ERROR" ) );
       return false;
     }
 
-    if( !aStatesFullPath.isEmpty() )
+    if ( !aStatesFullPath.isEmpty() )
     {
       QFile aFile( aStatesFullPath );
       if( aFile.open( QFile::ReadOnly ) )
@@ -127,11 +137,13 @@ bool HYDROGUI_DataModel::open( const QString& theURL,
         aFile.close();
       }
     }
+
+    updateDocument();
   }
 
   // if the document open was successful, the data model update happens
   // in the set mode of the module
-  if( res == DocError_OK )
+  if ( res == DocError_OK )
     update( aStudyId );
 
   return true;
@@ -147,7 +159,7 @@ bool HYDROGUI_DataModel::save( QStringList& theFileList )
   QString aTmpDir;
   SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
   bool isMultiFile = false;
-  if( resMgr )
+  if ( resMgr )
     isMultiFile = resMgr->booleanValue( "Study", "multi_file", false );
 
   // save module data to temporary files
@@ -194,6 +206,9 @@ bool HYDROGUI_DataModel::saveAs( const QString& theURL,
 
 bool HYDROGUI_DataModel::close()
 {
+  HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
+  if ( aModule )
+      aModule->clearCache();
   return true;
 }
 
@@ -271,6 +286,9 @@ void HYDROGUI_DataModel::update( const int theStudyId )
   // STRICKLER TABLES
   LightApp_DataObject* aStricklerTablesRootObj = createObject( aNewRootObj, tr( partitionName( KIND_STRICKLER_TABLE ).toAscii() ) );
 
+  // LAND COVER MAPS
+  LightApp_DataObject* aLandCoversRootObj = createObject( aNewRootObj, tr( partitionName( KIND_LAND_COVER_MAP ).toAscii() ) );
+
   // CALCULATION CASES
   LightApp_DataObject* aCalculRootObj = createObject( aNewRootObj, tr( partitionName( KIND_CALCULATION ).toAscii() ) );
 
@@ -377,15 +395,25 @@ void HYDROGUI_DataModel::update( const int theStudyId )
         }
                case KIND_STRICKLER_TABLE:
         {
-          Handle(HYDROData_StricklerTable) anStricklerTableObj =
+          Handle(HYDROData_StricklerTable) aStricklerTableObj =
             Handle(HYDROData_StricklerTable)::DownCast( anObj );
-          if( !anStricklerTableObj.IsNull() ) {
-            obj = createObject( aStricklerTablesRootObj, anStricklerTableObj );
+          if( !aStricklerTableObj.IsNull() ) {
+            obj = createObject( aStricklerTablesRootObj, aStricklerTableObj );
           }
                  aNoStricklerTableObj++;
 
           break;
         }
+        case KIND_LAND_COVER_MAP:
+        {
+          Handle(HYDROData_LandCoverMap) aLandCoverMapObj =
+            Handle(HYDROData_LandCoverMap)::DownCast( anObj );
+          if( !aLandCoverMapObj.IsNull() ) {
+            obj = createObject( aLandCoversRootObj, aLandCoverMapObj );
+          }
+
+          break;
+        }
         case KIND_CALCULATION:
         {
           Handle(HYDROData_CalculationCase) aCalculObj =
@@ -651,7 +679,7 @@ bool HYDROGUI_DataModel::paste()
       Handle(HYDROData_Entity) aClone = getDocument()->CreateObject( aKind );
       if( !aClone.IsNull() )
       {
-        anObject->CopyTo( aClone );
+        anObject->CopyTo( aClone, true );
         anIsChanged = true;
 
         // remove Z layer
@@ -681,7 +709,8 @@ QString HYDROGUI_DataModel::partitionName( const ObjectKind theObjectKind )
     case KIND_OBSTACLE:          return "OBSTACLES";
     case KIND_ARTIFICIAL_OBJECT: return "ARTIFICIAL_OBJECTS";
     case KIND_NATURAL_OBJECT:    return "NATURAL_OBJECTS";
-       case KIND_STRICKLER_TABLE:   return "STRICKLER_TABLES";
+    case KIND_STRICKLER_TABLE:   return "STRICKLER_TABLES";
+    case KIND_LAND_COVER_MAP:    return "LAND_COVER_MAPS";
     default: break;
   }
   return QString();
@@ -705,7 +734,7 @@ LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject*
                     aKind == KIND_SHAPES_GROUP || aKind == KIND_SPLITTED_GROUP || aKind == KIND_ZONE ||
                     aKind == KIND_IMMERSIBLE_ZONE || aKind == KIND_REGION || aKind == KIND_BATHYMETRY ||
                     aKind == KIND_OBSTACLE || aKind == KIND_STREAM || aKind == KIND_CHANNEL ||
-                    aKind == KIND_DIGUE || aKind == KIND_DUMMY_3D;
+                    aKind == KIND_DIGUE || aKind == KIND_DUMMY_3D || aKind == KIND_LAND_COVER_MAP;
   if ( !visibility )
   {
     Handle(HYDROData_Profile) aProfObj = Handle(HYDROData_Profile)::DownCast( theModelObject );
@@ -720,18 +749,7 @@ LightApp_DataObject* HYDROGUI_DataModel::createObject( SUIT_DataObject*
 
   if ( visibility )
   {
-    SUIT_AbstractModel* treeModel = 0;
-    LightApp_Application* app = dynamic_cast<LightApp_Application*>( module()->application() );
-    if ( app )
-        treeModel = dynamic_cast<SUIT_AbstractModel*>( app->objectBrowser()->model() );
-
-    if ( treeModel )
-    {
-      HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
-      bool isVisible = aModule->isObjectVisible( -1, theModelObject );
-      Qtx::VisibilityState aVisState = isVisible ? Qtx::ShownState : Qtx::HiddenState;
-      treeModel->setVisibilityState( aResObj->text( aResObj->customData( Qtx::IdType ).toInt() ), aVisState, false );
-    }
+    setObjectVisibilityState( theModelObject, aResObj );
   }
 
   if ( theIsBuildTree )
@@ -761,8 +779,12 @@ LightApp_DataObject* HYDROGUI_DataModel::createZone( SUIT_DataObject*       theP
                                                      const bool             theIsBuildTree,
                                                      const bool             theIsInOperation )
 {
-  return buildObject( theParent, new HYDROGUI_Zone( theParent, theModelObject, theParentEntry, theIsInOperation ), 
-    theParentEntry, theIsBuildTree, theIsInOperation );
+  HYDROGUI_Zone* aZone = new HYDROGUI_Zone( theParent, theModelObject, theParentEntry, theIsInOperation );
+  LightApp_DataObject* aDataObj = buildObject( theParent, aZone, theParentEntry, theIsBuildTree, theIsInOperation );
+
+  setObjectVisibilityState( theModelObject, aZone );
+
+  return aDataObj;
 }
 
 LightApp_DataObject* HYDROGUI_DataModel::createRegion( SUIT_DataObject*         theParent,
@@ -779,25 +801,27 @@ void HYDROGUI_DataModel::createDefaultStricklerTable( const Handle(HYDROData_Doc
                                                       LightApp_DataObject*              theParent )
 {
   // Create default Strickler table object
-  Handle(HYDROData_StricklerTable) anStricklerTableObj =
+  Handle(HYDROData_StricklerTable) aStricklerTableObj =
     Handle(HYDROData_StricklerTable)::DownCast( theDocument->CreateObject(KIND_STRICKLER_TABLE) );     
-  if ( !anStricklerTableObj.IsNull() )
+  if ( !aStricklerTableObj.IsNull() )
   {
-    anStricklerTableObj->Import( HYDROGUI_Tool::ToAsciiString( tr( "DEFAULT_STRICKLER_TABLE_FILE" ) ) );
+    SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
+    QString defTablePath = resMgr->path( "resources", module()->name(), tr( "DEFAULT_STRICKLER_TABLE_FILE" ) );
+    aStricklerTableObj->Import( defTablePath );
        // Set name
     QString aStricklerTableName;
-    if ( anStricklerTableObj->GetName().isEmpty() )
+    if ( aStricklerTableObj->GetName().isEmpty() )
     {
       HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
       if ( aModule )
         aStricklerTableName = HYDROGUI_Tool::GenerateObjectName( aModule, tr( "DEFAULT_STRICKLER_TABLE_NAME" ) );
     }
-    if ( anStricklerTableObj->GetName() != aStricklerTableName )
-      anStricklerTableObj->SetName( aStricklerTableName );
+    if ( aStricklerTableObj->GetName() != aStricklerTableName )
+      aStricklerTableObj->SetName( aStricklerTableName );
 
-    anStricklerTableObj->Update();
+    aStricklerTableObj->Update();
 
-    LightApp_DataObject* obj = createObject( theParent, anStricklerTableObj );
+    LightApp_DataObject* obj = createObject( theParent, aStricklerTableObj );
   }
 }
 
@@ -977,7 +1001,7 @@ void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
       if( !aCaseRegion.IsNull() && !aCaseRegion->IsRemoved() )
         createRegion( aCaseRegionsSect, aCaseRegion, "", true, theIsInOperation );
     }
-
+   
 #ifdef DEB_GROUPS
     HYDROData_SequenceOfObjects aCalcGroups = aCaseObj->GetGeometryGroups();
     buildObjectPartition( aGuiObj, aCalcGroups, tr( "OBJECT_GROUPS" ), false );
@@ -1048,6 +1072,16 @@ void HYDROGUI_DataModel::buildObjectTree( SUIT_DataObject* theParent,
       createObject( aGuiObj, aBottomPolyline, aGuiObj->entry(), false );
     }
   }
+  else if ( anObjectKind == KIND_LAND_COVER_MAP )
+  {
+    Handle(HYDROData_LandCoverMap) aLandCoverMapObj =
+      Handle(HYDROData_LandCoverMap)::DownCast( aDataObj );
+
+    /*TODO: reference objects of the land cover map 
+    HYDROData_SequenceOfObjects aPolylines = aLandCoverMapObj->GetPolylines();
+    buildObjectPartition( aGuiObj, aPolylines, tr( "LAND_COVER_POLYLINES" ), true );*/
+  }
+  
 }
 
 void HYDROGUI_DataModel::buildCaseTree( SUIT_DataObject* theParent, Handle(HYDROData_CalculationCase) theCase )
@@ -1186,3 +1220,29 @@ bool HYDROGUI_DataModel::rename( Handle(HYDROData_Entity) theEntity, const QStri
   }
   return true;
 }
+
+void HYDROGUI_DataModel::updateDocument()
+{
+    // Sets the default strickler coefficient from preferences to document.
+    Handle(HYDROData_Document) aDoc = getDocument();
+    SUIT_ResourceMgr* resMgr = module()->application()->resourceMgr();
+    if ( resMgr && !aDoc.IsNull() )
+      aDoc->SetDefaultStricklerCoefficient( resMgr->doubleValue( "preferences", "default_strickler_coefficient", 0 ) );
+}
+
+void HYDROGUI_DataModel::setObjectVisibilityState( Handle(HYDROData_Entity) theModelObject,
+                                                   HYDROGUI_DataObject* theObject )
+{
+  SUIT_AbstractModel* treeModel = 0;
+  LightApp_Application* app = dynamic_cast<LightApp_Application*>( module()->application() );
+  if ( app )
+    treeModel = dynamic_cast<SUIT_AbstractModel*>( app->objectBrowser()->model() );
+
+  if ( treeModel )
+  {
+    HYDROGUI_Module* aModule = dynamic_cast<HYDROGUI_Module*>( module() );
+    bool isVisible = aModule->isObjectVisible( -1, theModelObject );
+    Qtx::VisibilityState aVisState = isVisible ? Qtx::ShownState : Qtx::HiddenState;
+    treeModel->setVisibilityState( theObject->text( theObject->customData( Qtx::IdType ).toInt() ), aVisState, false );
+  }
+}