Salome HOME
land cover object is removed from the data model
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
index ecb496c2191693508f8164b24adea23b82a7192d..edce573b2294e7e2bd3bc4afe9f68df1566c1e42 100644 (file)
@@ -56,7 +56,10 @@ HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool
   myIsEdit( theIsEdit ),
   myActiveViewManager( NULL ),
   myPreviewViewManager( NULL ),
-  myShowZones( false )
+  myShowGeomObjects( true ),
+  myShowLandCovers( false ),
+  myShowZones( false ),
+  myUpdateLandCoverZonesPrs( false )
 {
   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
 }
@@ -219,7 +222,10 @@ HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
   
   connect( aPanel, SIGNAL( orderChanged( bool& ) ), SLOT( onOrderChanged( bool& ) ) );
   connect( aPanel, SIGNAL( orderLandCoverChanged( bool& ) ), SLOT( onOrderLandCoverChanged( bool& ) ) );
-    
+
+  connect( aPanel, SIGNAL( ruleChanged( bool& ) ), SLOT( onRuleChanged( bool& ) ) );
+  connect( aPanel, SIGNAL( ruleLandCoverChanged( bool& ) ), SLOT( onRuleLandCoverChanged( bool& ) ) );
+
   connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
   connect( aPanel, SIGNAL( Back( const int ) ), SLOT( onHideZones( const int ) ) );
   //connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
@@ -242,6 +248,9 @@ HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
   connect( aPanel, SIGNAL( StricklerTableSelected( const QString & ) ), 
     SLOT( onStricklerTableSelected( const QString & ) ) );
 
+  connect( aPanel, SIGNAL( regenerateColors() ), this, 
+    SLOT( onRegenerateColors() ) );
+
   return aPanel;
 }
 
@@ -670,6 +679,31 @@ bool HYDROGUI_CalculationOp::confirmOrderChange() const
   return isConfirmed;
 }
 
+bool HYDROGUI_CalculationOp::confirmRuleChange() const
+{
+  // Check if the case is already modified or not
+  bool isConfirmed = myEditedObject->IsMustBeUpdated();
+  if ( !isConfirmed )
+  {
+    // If not modified check if the case has already defined regions with zones
+    HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( false );
+    if ( aSeq.Length() > 0 )
+    {
+      // If there are already defined zones then ask a user to confirm zones recalculation
+      isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
+                               tr( "RULE_CHANGED" ),
+                               tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_REGIONS" ),
+                               QMessageBox::Yes | QMessageBox::No,
+                               QMessageBox::No ) == QMessageBox::Yes );
+    }
+    else
+    {
+      isConfirmed = true; // No regions - no zones - nothing to recalculate
+    }
+  }
+  return isConfirmed;
+}
+
 bool HYDROGUI_CalculationOp::confirmModeChange() const
 {
   // Check if the case is already modified or not
@@ -724,28 +758,52 @@ bool HYDROGUI_CalculationOp::confirmContinueWithWarning( const HYDROData_Warning
   return ( anAnswer == QMessageBox::Yes );
 }
 
+bool HYDROGUI_CalculationOp::confirmLandCoverRegionsChange() const
+{
+  // Check if the case is already modified or not
+  bool isConfirmed = myEditedObject->IsMustBeUpdated();
+  if ( !isConfirmed )
+  {
+    // If not modified check if the case has already defined regions with land cover zones
+    HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( true );
+    if ( aSeq.Length() > 0 )
+    {
+      // If there are already defined land cover zones then ask a user to confirm land cover zones recalculation
+      isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
+                               tr( "REGIONS_CHANGED" ),
+                               tr( "CONFIRM_LAND_COVER_PARTITION_RECALCULATION_REGIONS" ),
+                               QMessageBox::Yes | QMessageBox::No,
+                               QMessageBox::No ) == QMessageBox::Yes );
+    }
+    else
+    {
+      isConfirmed = true; // No regions - no land cover zones - nothing to recalculate
+    }
+  }
+  return isConfirmed;
+}
+
 bool HYDROGUI_CalculationOp::confirmLandCoverModeChange() const
 {
   // Check if the case is already modified or not
   bool isConfirmed = myEditedObject->IsMustBeUpdated();
   if ( !isConfirmed )
   {
-    // If not modified check if the case has already defined regions with zones
-    // TODO: adapt HYDROData_CalculationCase class to process regions constructed for land covers
-    /*HYDROData_SequenceOfObjects aSeq = myEditedObject->GetLandCoverRegions();
+    // If not modified check if the case has already defined regions with land cover zones
+    HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( true );
     if ( aSeq.Length() > 0 )
-    {*/
-      // If there are already defined zones then ask a user to confirm zones recalculation
+    {
+      // If there are already defined land cover zones then ask a user to confirm land cover zones recalculation
       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
                                tr( "MODE_CHANGED" ),
                                tr( "CONFIRM_LAND_COVER_PARTITION_RECALCULATION_MODE" ),
                                QMessageBox::Yes | QMessageBox::No,
                                QMessageBox::No ) == QMessageBox::Yes );
-    /*}
+    }
     else
     {
-      isConfirmed = true; // No regions - no zones - nothing to recalculate
-    }*/
+      isConfirmed = true; // No regions - no land cover zones - nothing to recalculate
+    }
   }
   return isConfirmed;
 }
@@ -756,22 +814,46 @@ bool HYDROGUI_CalculationOp::confirmLandCoverOrderChange() const
   bool isConfirmed = myEditedObject->IsMustBeUpdated();
   if ( !isConfirmed )
   {
-    // If not modified check if the case has already defined regions with zones
-    // TODO: adapt HYDROData_CalculationCase class to process regions constructed for land covers
-    /*HYDROData_SequenceOfObjects aSeq = myEditedObject->GetLandCoverRegions();
+    // If not modified check if the case has already defined regions with land cover zones
+    HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( true );
     if ( aSeq.Length() > 0 )
-    {*/
-      // If there are already defined zones then ask a user to confirm zones recalculation
+    {
+      // If there are already defined land cover zones then ask a user to confirm land cover zones recalculation
       isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
                                tr( "ORDER_CHANGED" ),
                                tr( "CONFIRM_LAND_COVER_PARTITION_RECALCULATION_REGIONS" ),
                                QMessageBox::Yes | QMessageBox::No,
                                QMessageBox::No ) == QMessageBox::Yes );
-    /*}
+    }
     else
     {
-      isConfirmed = true; // No regions - no zones - nothing to recalculate
-    }*/
+      isConfirmed = true; // No regions - no land cover zones - nothing to recalculate
+    }
+  }
+  return isConfirmed;
+}
+
+bool HYDROGUI_CalculationOp::confirmLandCoverRuleChange() const
+{
+  // Check if the case is already modified or not
+  bool isConfirmed = myEditedObject->IsMustBeUpdated();
+  if ( !isConfirmed )
+  {
+    // If not modified check if the case has already defined regions with land cover zones
+    HYDROData_SequenceOfObjects aSeq = myEditedObject->GetRegions( true );
+    if ( aSeq.Length() > 0 )
+    {
+      // If there are already defined land cover zones then ask a user to confirm land cover zones recalculation
+      isConfirmed = ( SUIT_MessageBox::question( module()->getApp()->desktop(),
+                               tr( "RULE_CHANGED" ),
+                               tr( "CONFIRM_LAND_COVER_PARTITION_RECALCULATION_REGIONS" ),
+                               QMessageBox::Yes | QMessageBox::No,
+                               QMessageBox::No ) == QMessageBox::Yes );
+    }
+    else
+    {
+      isConfirmed = true; // No regions - no land cover zones - nothing to recalculate
+    }
   }
   return isConfirmed;
 }
@@ -877,6 +959,8 @@ void HYDROGUI_CalculationOp::onNext( const int theIndex )
     if ( !aPanel )
       return;
 
+    setLandCoversVisible( true );
+
     QStringList aList;
     QStringList anEntryList;
     HYDROData_SequenceOfObjects aSeq;
@@ -903,8 +987,10 @@ void HYDROGUI_CalculationOp::onNext( const int theIndex )
       }
     }
     aPanel->setStricklerTableNames( aList, anEntryList );
+    bool anUpdateState = myEditedObject->IsMustBeUpdated();
     if ( !aList.isEmpty() )
       aPanel->setStricklerTable( aList.at( 0 ), false );
+    myEditedObject->SetToUpdate( anUpdateState );
 
     // Fill in list widget with all available land covers
     aSeq = HYDROGUI_Tool::GetLandCovers( module() );
@@ -956,6 +1042,9 @@ void HYDROGUI_CalculationOp::onNext( const int theIndex )
 
     QApplication::setOverrideCursor( Qt::WaitCursor );
 
+    setGeomObjectsVisible( false );
+    setLandCoversVisible( false );
+
     QString aNewCaseName = aPanel->getObjectName();
     QString anOldCaseName = myEditedObject->GetName();
     bool isNameChanged = anOldCaseName != aNewCaseName;
@@ -966,7 +1055,7 @@ void HYDROGUI_CalculationOp::onNext( const int theIndex )
     // automatic names generation for regions and zones
     myEditedObject->SetName( aNewCaseName );
     
-    // Set parameters for automatic mode
+    // Zones: set parameters for automatic mode
     int aMode = aPanel->getMode();
     if ( aMode == HYDROData_CalculationCase::AUTOMATIC )
     {
@@ -998,6 +1087,37 @@ void HYDROGUI_CalculationOp::onNext( const int theIndex )
       }
     }
     aPanel->setEditZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
+
+    // Land cover zones: set parameters for automatic mode
+    aMode = aPanel->getLandCoverMode();
+    if ( aMode == HYDROData_CalculationCase::AUTOMATIC )
+    {
+      // Set objects in the specified order
+      if( myEditedObject->IsMustBeUpdated() )
+      {
+        myEditedObject->RemoveLandCovers();
+        foreach ( const QString& aName, aPanel->getAllLandCovers() )
+        {
+          Handle(HYDROData_LandCover) aLandCover = Handle(HYDROData_LandCover)::DownCast( 
+            HYDROGUI_Tool::FindObjectByName( module(), aName ) );
+          if ( aLandCover.IsNull() )
+          {
+            continue;
+          }
+          myEditedObject->AddLandCover( aLandCover );
+        }
+
+        // Clear priority rules
+        myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomLandCoverRules, true );
+        // Set priority rules
+        foreach ( const HYDROData_CustomRule& aRule, aPanel->getLandCoverRules() ) {
+          myEditedObject->AddRule( aRule.Object1, aRule.Priority,
+                                   aRule.Object2, HYDROData_Zone::Merge_Object,
+                                   HYDROData_CalculationCase::DataTag_CustomLandCoverRules );
+        }
+      }
+    }
+    aPanel->setEditLandCoverZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
          
     if ( myEditedObject->IsMustBeUpdated() )
     {
@@ -1014,7 +1134,7 @@ void HYDROGUI_CalculationOp::onNext( const int theIndex )
 
       anIsToUpdateOb = true;
 
-      myEditedObject->SetToUpdate( true );
+      myUpdateLandCoverZonesPrs = true;
     }
     else
     {
@@ -1038,43 +1158,10 @@ void HYDROGUI_CalculationOp::onNext( const int theIndex )
 
     QApplication::setOverrideCursor( Qt::WaitCursor );
 
-    // Set parameters for automatic mode
-    int aMode = aPanel->getLandCoverMode();
-    if ( aMode == HYDROData_CalculationCase::AUTOMATIC )
-    {
-      // Set objects in the specified order
-      if( myEditedObject->IsMustBeUpdated() )
-      {
-        myEditedObject->RemoveLandCovers();
-        foreach ( const QString& aName, aPanel->getAllLandCovers() )
-        {
-          Handle(HYDROData_LandCover) aLandCover = Handle(HYDROData_LandCover)::DownCast( 
-            HYDROGUI_Tool::FindObjectByName( module(), aName ) );
-          if ( aLandCover.IsNull() )
-          {
-            continue;
-          }
-          myEditedObject->AddLandCover( aLandCover );
-        }
-
-        // Clear priority rules
-        myEditedObject->ClearRules( HYDROData_CalculationCase::DataTag_CustomLandCoverRules, true );
-        // Set priority rules
-        foreach ( const HYDROData_CustomRule& aRule, aPanel->getLandCoverRules() ) {
-          myEditedObject->AddRule( aRule.Object1, aRule.Priority,
-                                   aRule.Object2, HYDROData_Zone::Merge_Object,
-                                   HYDROData_CalculationCase::DataTag_CustomLandCoverRules );
-        }
-      }
-    }
-    aPanel->setEditLandCoverZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
-
     bool anIsToUpdateOb = false;
-    bool anIsToUpdate = myEditedObject->IsMustBeUpdated();
-    if ( anIsToUpdate )
+    if ( myUpdateLandCoverZonesPrs )
     {
       myShowZones = true;
-      myEditedObject->Update();
       
       AssignDefaultZonesColors( true );
 
@@ -1086,7 +1173,9 @@ void HYDROGUI_CalculationOp::onNext( const int theIndex )
     closePreview( false );
     createPreview( true );
     
-    if ( !anIsToUpdate )
+    if ( myUpdateLandCoverZonesPrs )
+      myUpdateLandCoverZonesPrs = false;
+    else
     {
       // Hide zones
       setZonesVisible( false, false );
@@ -1109,11 +1198,15 @@ void HYDROGUI_CalculationOp::onHideZones( const int theIndex )
 {
   if( theIndex==1 )
   {
+    setGeomObjectsVisible( true );
+
     closePreview( false );
     createPreview( false );
   }
   if( theIndex==2 )
   {
+    setLandCoversVisible( true );
+
     closePreview( false );
     createPreview( true );
 
@@ -1170,6 +1263,30 @@ void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible, const bool theL
   }
 }
 
+void HYDROGUI_CalculationOp::setGeomObjectsVisible( bool theIsVisible )
+{
+  myShowGeomObjects = theIsVisible;
+
+  HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
+
+  HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
+  for ( ; anIter.More(); anIter.Next() ) {
+    setObjectVisibility( anIter.Value(), theIsVisible );
+  }
+}
+
+void HYDROGUI_CalculationOp::setLandCoversVisible( bool theIsVisible )
+{
+  myShowLandCovers = theIsVisible;
+
+  HYDROData_SequenceOfObjects aSeq = myEditedObject->GetLandCovers();
+
+  HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
+  for ( ; anIter.More(); anIter.Next() ) {
+    setObjectVisibility( anIter.Value(), theIsVisible );
+  }
+}
+
 void HYDROGUI_CalculationOp::AssignDefaultZonesColors( const bool theLandCover )
 {
   HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions( theLandCover );
@@ -1215,29 +1332,13 @@ QColor HYDROGUI_CalculationOp::GenerateDefaultZonesColor( int theIndex,
                                                           float theSaturation/* = 0.5*/,
                                                           float theValue/* = 0.95*/ ) const
 {
-  float aGoldenRatioConjugate = (float)(360./582.);
-  float aHue = (float)(rand()%100);
-  aHue += aGoldenRatioConjugate*theIndex;
-  aHue -= floor(aHue);
-
-  float aR = 0., aG = 0., aB = 0.;
-  int aHueInt = (int)(aHue*6.);
-  float aF = aHue*6. - aHueInt;
-  float aP = theValue * (1. - theSaturation);
-  float aQ = theValue * (1. - aF*theSaturation);
-  float aT = theValue * (1. - (1. - aF) * theSaturation);
-  switch (aHueInt)
-  {
-  case 0: { aR = theValue; aG = aT; aB = aP; break; }
-  case 1: { aR = aQ; aG = theValue; aB = aP; break; }
-  case 2: { aR = aP; aG = theValue; aB = aT; break; }
-  case 3: { aR = aP; aG = aQ; aB = theValue; break; }
-  case 4: { aR = aT; aG = aP; aB = theValue; break; }
-  case 5: { aR = theValue; aG = aP; aB = aQ; break; }
-  default: break;
-  }
-
-  QColor aColor = QColor( (int)(aR*256.), (int)(aG*256.), (int)(aB*256.) );
+  float aHue = ( rand()%1000 ) * 0.001f;
+
+  QColor aColor;
+  aColor.setHsl( (int)(aHue*255.), 128, 128 );
+  int r = aColor.red();
+  int g = aColor.green();
+  int b = aColor.blue();
   return ( aColor.isValid() ? aColor : HYDROData_ImmersibleZone::DefaultFillingColor() );
 }
 
@@ -1303,10 +1404,16 @@ void HYDROGUI_CalculationOp::createPreview( const bool theLandCover )
 {
   LightApp_Application* anApp = module()->getApp();
   HYDROData_SequenceOfObjects aSeq;
-  if ( theLandCover )
-    aSeq = myEditedObject->GetLandCovers();
-  else
-    aSeq = myEditedObject->GetGeometryObjects();
+  if ( theLandCover && myShowLandCovers )
+  {
+    HYDROData_SequenceOfObjects aSeqLC = myEditedObject->GetLandCovers();
+    aSeq.Append( aSeqLC );
+  }
+  else if ( !theLandCover && myShowGeomObjects )
+  {
+    HYDROData_SequenceOfObjects aSeqGO = myEditedObject->GetGeometryObjects();
+    aSeq.Append( aSeqGO );
+  }
 
   Handle(HYDROData_Entity) anEntity;
 
@@ -1566,7 +1673,7 @@ void HYDROGUI_CalculationOp::onAddLandCovers()
 
   // Add land covers selected in the module browser to the calculation case
   QStringList aSelectedList = aPanel->getSelectedAvailableLandCovers();
-  if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
+  if ( aSelectedList.isEmpty() || !confirmLandCoverRegionsChange() )
     return;
 
   QStringList anAddedList;
@@ -1597,7 +1704,7 @@ void HYDROGUI_CalculationOp::onRemoveLandCovers()
     return;
 
   QStringList aSelectedList = aPanel->getSelectedLandCovers();
-  if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
+  if ( aSelectedList.isEmpty() || !confirmLandCoverRegionsChange() )
     return;
 
   for (int i = 0; i < aSelectedList.length(); i++)
@@ -1654,3 +1761,35 @@ void HYDROGUI_CalculationOp::onOrderLandCoverChanged( bool& isConfirmed )
   if( isConfirmed )
     myEditedObject->SetToUpdate( true );
 }
+
+void HYDROGUI_CalculationOp::onRuleChanged( bool& isConfirmed )
+{
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( !aPanel )
+    return;
+
+  isConfirmed = confirmRuleChange();
+  if( isConfirmed )
+    myEditedObject->SetToUpdate( true );
+}
+
+void HYDROGUI_CalculationOp::onRuleLandCoverChanged( bool& isConfirmed )
+{
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( !aPanel )
+    return;
+
+  isConfirmed = confirmLandCoverRuleChange();
+  if( isConfirmed )
+    myEditedObject->SetToUpdate( true );
+}
+
+void HYDROGUI_CalculationOp::onRegenerateColors()
+{
+  // For geometry zones
+  AssignDefaultZonesColors( false );
+  setZonesVisible( false, false );
+  setZonesVisible( true, false );
+}