Salome HOME
refs #1832
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
index 734964c9fa2199390477d3713b69ac47ae955ea6..739df20fcb81f4ce75dc5bf11039f2ffe6a6844a 100644 (file)
@@ -33,6 +33,7 @@
 #include <HYDROData_Object.h>
 #include <HYDROData_Tool.h>
 #include <HYDROData_StricklerTable.h>
+#include <HYDROData_BCPolygon.h>
 
 #include <OCCViewer_ViewManager.h>
 #include <OCCViewer_ViewModel.h>
@@ -247,6 +248,9 @@ HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
   connect( aPanel, SIGNAL( addGroups() ), SLOT( onAddGroups() ) );
   connect( aPanel, SIGNAL( removeGroups() ), SLOT( onRemoveGroups() ) );
 
+  connect( aPanel, SIGNAL( addBoundaryPolygons() ), SLOT( onAddBoundaryPolygons() ) );
+  connect( aPanel, SIGNAL( removeBoundaryPolygons() ), SLOT( onRemoveBoundaryPolygons() ) );
+
   connect( aPanel, SIGNAL( orderChanged( bool& ) ), SLOT( onOrderChanged( bool& ) ) );
 
   connect( aPanel, SIGNAL( ruleChanged( bool& ) ), SLOT( onRuleChanged( bool& ) ) );
@@ -477,7 +481,7 @@ void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem,
         ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
       if ( aPanel )
         aPanel->refreshZonesBrowser();
-      createPreview( false );
+      createPreview( false, false, false );
     }
   }
 }
@@ -492,7 +496,7 @@ void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZ
     {
       aPanel->refreshZonesBrowser();
     }
-    createPreview( false );
+    createPreview( false, false, false  );
   }
 }
 
@@ -816,6 +820,10 @@ void HYDROGUI_CalculationOp::onNext( const int theIndex )
     setAvailableGroups();
   }
   else if( theIndex==2 )
+  {
+    setAvailableBoundaryPolygons();
+  }
+  else if( theIndex==3 )
   {
     // Land cover map panel
      HYDROGUI_CalculationDlg* aPanel = 
@@ -920,7 +928,7 @@ void HYDROGUI_CalculationOp::onNext( const int theIndex )
     closePreview( false );
     createPreview( true );
   }
-  else if( theIndex==3 )
+  else if( theIndex==4 )
   {
     HYDROGUI_CalculationDlg* aPanel = 
       ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
@@ -1013,7 +1021,7 @@ void HYDROGUI_CalculationOp::onHideZones( const int theIndex )
     closePreview( false );
     createPreview( false );
   }
-  if( theIndex==2 )
+  if( theIndex==3 )
   {
     setLandCoverMapVisible( true );
 
@@ -1023,7 +1031,7 @@ void HYDROGUI_CalculationOp::onHideZones( const int theIndex )
     // Hide zones
     setZonesVisible( false );
   }
-  else if( theIndex==3 )
+  else if( theIndex==4 )
   {
     AssignDefaultZonesColors();
 
@@ -1099,8 +1107,7 @@ void HYDROGUI_CalculationOp::AssignDefaultZonesColors()
     {
       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
       if ( !aCtx.IsNull() )
-      {
-        int aCounter = 0;        
+      {     
         for ( ; aRegionsIter.More(); aRegionsIter.Next() )
         {
           aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
@@ -1114,9 +1121,9 @@ void HYDROGUI_CalculationOp::AssignDefaultZonesColors()
               Handle(HYDROData_Zone) aZone = Handle(HYDROData_Zone)::DownCast( aZonesIter.Value() );
               if ( !aZone.IsNull() )
               {
-                QColor aFillingColor = GenerateDefaultZonesColor(++aCounter);
+                QColor aFillingColor = HYDROData_Tool::GenerateRandColor();
                 while (aFillingColor == Qt::red)
-                  aFillingColor = GenerateDefaultZonesColor(++aCounter);
+                  aFillingColor = HYDROData_Tool::GenerateRandColor();
                 
                 aZone->SetColor(aFillingColor);
               }
@@ -1128,20 +1135,6 @@ void HYDROGUI_CalculationOp::AssignDefaultZonesColors()
   }
 }
 
-QColor HYDROGUI_CalculationOp::GenerateDefaultZonesColor( int theIndex,
-                                                          float theSaturation/* = 0.5*/,
-                                                          float theValue/* = 0.95*/ ) const
-{
-  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 : Qt::darkBlue );
-}
-
 void HYDROGUI_CalculationOp::setRules( HYDROData_CalculationCase::DataTag theDataTag )
 {
   HYDROGUI_CalculationDlg* aPanel = 
@@ -1197,7 +1190,7 @@ bool HYDROGUI_CalculationOp::createRegion( const QList<SUIT_DataObject*>& theZon
   return aRetValue;
 }
 
-void HYDROGUI_CalculationOp::createPreview( const bool theLandCoverMap )
+void HYDROGUI_CalculationOp::createPreview( const bool theLandCoverMap, bool fitAllFlag, bool onTopViewAndFit )
 {
   LightApp_Application* anApp = module()->getApp();
   HYDROData_SequenceOfObjects aSeq;
@@ -1238,7 +1231,8 @@ void HYDROGUI_CalculationOp::createPreview( const bool theLandCoverMap )
   }
 
   // Get a boundary polyline if any
-  aSeq.Append( myEditedObject->GetBoundaryPolyline() );
+  if (Handle(HYDROData_PolylineXY) aBPoly = myEditedObject->GetBoundaryPolyline())
+    aSeq.Append( aBPoly );
 
   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
 
@@ -1275,16 +1269,23 @@ void HYDROGUI_CalculationOp::createPreview( const bool theLandCoverMap )
       HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
       for ( ; anIter.More(); anIter.Next() )
       {
-        setObjectVisibility( anIter.Value(), true );
+        const Handle(HYDROData_Entity)& ent = anIter.Value();
+        if (!ent.IsNull())
+          setObjectVisibility( ent, true );
       }
 
       //Process the draw events for viewer
       QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
-      if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
-        vw->onTopView();
     }
 
-    module()->update( UF_OCCViewer | UF_FitAll );
+    int UpdateFlags = UF_OCCViewer;
+    if (fitAllFlag)
+      UpdateFlags |= UF_FitAll;
+    module()->update( UpdateFlags );
+
+    OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView();
+    if ( onTopViewAndFit && vw )
+      vw->onTopView();
   }
 }
 
@@ -1367,11 +1368,11 @@ void HYDROGUI_CalculationOp::setAvailableGroups()
   HYDROData_SequenceOfObjects anObjs = myEditedObject->GetGeometryObjects();
   for( int anIndex = 1, aLength = anObjs.Length(); anIndex <= aLength; anIndex++ )
   {
-    Handle_HYDROData_Object anObj = Handle_HYDROData_Object::DownCast( anObjs.Value( anIndex ) );
+    Handle(HYDROData_Object) anObj = Handle(HYDROData_Object)::DownCast( anObjs.Value( anIndex ) );
     HYDROData_SequenceOfObjects aGroups = anObj->GetGroups();
     for( int aGIndex = 1, aGLength = aGroups.Length(); aGIndex <= aGLength; aGIndex++ )
     {
-      Handle_HYDROData_ShapesGroup aGroup = Handle_HYDROData_ShapesGroup::DownCast( aGroups.Value( aGIndex ) );
+      Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( aGroups.Value( aGIndex ) );
       aGroupsNames.append( aGroup->GetName() );
     }
   }
@@ -1446,6 +1447,110 @@ void HYDROGUI_CalculationOp::onRemoveGroups()
   aPanel->excludeGroups( aSelectedList );
 }
 
+
+void HYDROGUI_CalculationOp::setAvailableBoundaryPolygons()
+{
+  HYDROGUI_CalculationDlg* aPanel = ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+
+  HYDROData_Iterator anIter( doc(), KIND_BC_POLYGON );
+  Handle(HYDROData_BCPolygon) aBCP;
+  QStringList aNames;
+  QVector<int> aTypes; 
+  for (;anIter.More(); anIter.Next())
+  {
+    aBCP = Handle(HYDROData_BCPolygon)::DownCast( anIter.Current() );
+    aNames.append(aBCP->GetName());
+    aTypes.append(aBCP->GetBoundaryType());
+  }
+  HYDROData_SequenceOfObjects aBCPSeq = myEditedObject->GetBoundaryPolygons();
+
+  QStringList aListCut, aListIS;
+  for (int i=1; i<=aBCPSeq.Size();i++)
+  {
+    int type = Handle(HYDROData_BCPolygon)::DownCast(aBCPSeq(i))->GetBoundaryType();
+    if (type == 1)
+      aListCut << aBCPSeq(i)->GetName();
+    else if (type == 2 || type == 3)
+      aListIS << aBCPSeq(i)->GetName();
+  }
+
+  aPanel->setAvailableBoundaryPolygons( aNames, aTypes );
+  aPanel->includeBoundaryPolygons( aListCut );
+  aPanel->includeISBoundaryPolygons( aListIS );
+}
+
+void HYDROGUI_CalculationOp::onRemoveBoundaryPolygons()
+{
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( !aPanel )
+    return;
+
+  QStringList aSelectedListCut = aPanel->getSelectedBoundaryPolygons();
+  QStringList aSelectedListIS = aPanel->getSelectedISBoundaryPolygons();
+
+  if ( aSelectedListCut.isEmpty() && aSelectedListIS.isEmpty() )
+    return;
+
+  for (int i = 0; i < aSelectedListCut.length(); i++)
+  {
+    Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( 
+      HYDROGUI_Tool::FindObjectByName( module(), aSelectedListCut.at(i) ) );
+    if ( aBCPoly.IsNull() )
+      continue;
+    myEditedObject->RemoveBoundaryPolygon( aBCPoly );
+  }
+
+  for (int i = 0; i < aSelectedListIS.length(); i++)
+  {
+    Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( 
+      HYDROGUI_Tool::FindObjectByName( module(), aSelectedListIS.at(i) ) );
+    if ( aBCPoly.IsNull() )
+      continue;
+    myEditedObject->RemoveBoundaryPolygon( aBCPoly );
+  }
+
+  aPanel->excludeBoundaryPolygons( aSelectedListCut );
+  aPanel->excludeISBoundaryPolygons( aSelectedListIS );
+}
+
+void HYDROGUI_CalculationOp::onAddBoundaryPolygons()
+{
+  HYDROGUI_CalculationDlg* aPanel = ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( !aPanel )
+    return;
+
+  QStringList aSelectedList = aPanel->getSelectedAvailableBoundaryPolygons();
+  if ( aSelectedList.isEmpty())
+    return;
+
+  QStringList anAddedListCut, anAddedListIS;
+  for (int i = 0; i < aSelectedList.length(); i++)
+  {
+    Handle(HYDROData_BCPolygon) aBCPoly = Handle(HYDROData_BCPolygon)::DownCast( 
+      HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
+    if ( aBCPoly.IsNull() )
+      continue;
+   
+   if ( myEditedObject->AddBoundaryPolygon( aBCPoly ) )
+   {
+     QString aName = aBCPoly->GetName();
+     int type = aBCPoly->GetBoundaryType(); 
+     if (type == 1)
+       anAddedListCut.append(aName);
+     else if (type == 2 || type == 3)
+       anAddedListIS.append(aName);
+   }
+
+  }
+
+  if ( !anAddedListCut.isEmpty() )
+    aPanel->includeBoundaryPolygons(anAddedListCut);
+  if ( !anAddedListIS.isEmpty() )
+    aPanel->includeISBoundaryPolygons(anAddedListIS);
+}
+
 void HYDROGUI_CalculationOp::onChangeMode( int theMode )
 {
   HYDROGUI_CalculationDlg* aPanel =