Salome HOME
refs #1832
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
index 2cfd445de581bb468578876ac23992711f01a680..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>
@@ -98,6 +99,13 @@ void HYDROGUI_CalculationOp::startOperation()
   QStringList anEntryList;
   HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetGeometryObjects( module() );
   getNamesAndEntries( aSeq, aList, anEntryList );
+  
+  QStringList List1;
+  QStringList EList1;
+
+  //add intersection polylines
+  AddInterPolylinesToList(aList, anEntryList);
+  
   aPanel->setAllGeomObjects( aList, anEntryList );
 
   // Get all polylines
@@ -110,18 +118,18 @@ void HYDROGUI_CalculationOp::startOperation()
   {
     aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
 
-    if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed() )
+    if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed(false) )
     { 
-      // Check the polyline shape
-      TopoDS_Shape aPolylineShape = aPolylineObj->GetShape();
-      if ( !aPolylineShape.IsNull() && aPolylineShape.ShapeType() == TopAbs_WIRE ) {
+//      // Check the polyline shape
+//      TopoDS_Shape aPolylineShape = aPolylineObj->GetShape();
+//      if ( !aPolylineShape.IsNull() && aPolylineShape.ShapeType() == TopAbs_WIRE ) {
         aPolylineName = aPolylineObj->GetName();
         if ( !aPolylineName.isEmpty() )
         {
           aList.append( aPolylineName );
           anEntryList.append( HYDROGUI_DataObject::dataObjectEntry( aPolylineObj ) );
         }
-      }
+//      }
     }
   }
   aPanel->setPolylineNames( aList, anEntryList );
@@ -147,7 +155,10 @@ void HYDROGUI_CalculationOp::startOperation()
         aPanel->setBoundary( aPolylineName );
       }
 
-      aSeq = myEditedObject->GetGeometryObjects();
+      aSeq = myEditedObject->GetGeometryObjects();      
+      HYDROData_SequenceOfObjects anInterPolyList = myEditedObject->GetInterPolyObjects();
+      aSeq.Append(anInterPolyList);
+
       getNamesAndEntries( aSeq, aList, anEntryList );
       aPanel->includeGeomObjects( aList );
 
@@ -166,9 +177,31 @@ void HYDROGUI_CalculationOp::startOperation()
   aPanel->setObjectName( anObjectName );
   aPanel->setEditedObject( myEditedObject );
 
+  setGeomObjectsVisible( true );
+
   createPreview( false );
 }
 
+void HYDROGUI_CalculationOp::AddInterPolylinesToList(QStringList& theList, QStringList& theEntryList)
+{
+  HYDROData_Iterator anIter( doc(), KIND_POLYLINEXY );
+  Handle(HYDROData_PolylineXY) aPolylineObj;
+  QString aPolylineName;
+  for ( ; anIter.More(); anIter.Next() )
+  {
+    aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
+    if ( !aPolylineObj.IsNull())
+    { 
+      aPolylineName = aPolylineObj->GetName();
+      if ( !aPolylineName.isEmpty() && !theList.contains(aPolylineName) )
+      {
+        theList.append( aPolylineName );
+        theEntryList.append(HYDROGUI_DataObject::dataObjectEntry( aPolylineObj ));
+      }
+    }
+  }
+}
+
 void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq, 
                                                 QStringList& theNames, QStringList& theEntries ) const
 {
@@ -215,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& ) ) );
@@ -445,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 );
     }
   }
 }
@@ -460,7 +496,7 @@ void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZ
     {
       aPanel->refreshZonesBrowser();
     }
-    createPreview( false );
+    createPreview( false, false, false  );
   }
 }
 
@@ -508,6 +544,17 @@ void HYDROGUI_CalculationOp::onAddObjects()
       anAddedList.append( anObject->GetName() );
   }
 
+  for (int i = 0; i < aSelectedList.length(); i++)
+  {
+    Handle(HYDROData_PolylineXY) aPoly = Handle(HYDROData_PolylineXY)::DownCast( 
+      HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
+    if ( aPoly.IsNull() )
+      continue;
+
+    if ( myEditedObject->AddInterPoly( aPoly ) )
+      anAddedList.append( aPoly->GetName() );
+  }
+
   if ( !anAddedList.isEmpty() )
   {
     aPanel->includeGeomObjects( anAddedList );
@@ -538,6 +585,17 @@ void HYDROGUI_CalculationOp::onRemoveObjects()
     myEditedObject->RemoveGeometryObject( anObject );
   }
 
+  for (int i = 0; i < aSelectedList.length(); i++)
+  {
+    Handle(HYDROData_PolylineXY) aPoly = Handle(HYDROData_PolylineXY)::DownCast( 
+      HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
+    if ( aPoly.IsNull() )
+      continue;
+
+    setObjectVisibility( aPoly, false );
+    myEditedObject->RemoveInterPolyObject ( aPoly ); 
+  }
+
   module()->update( UF_OCCViewer );
   aPanel->excludeGeomObjects( aSelectedList );
 }
@@ -762,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 = 
@@ -866,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() );
@@ -959,7 +1021,7 @@ void HYDROGUI_CalculationOp::onHideZones( const int theIndex )
     closePreview( false );
     createPreview( false );
   }
-  if( theIndex==2 )
+  if( theIndex==3 )
   {
     setLandCoverMapVisible( true );
 
@@ -969,7 +1031,7 @@ void HYDROGUI_CalculationOp::onHideZones( const int theIndex )
     // Hide zones
     setZonesVisible( false );
   }
-  else if( theIndex==3 )
+  else if( theIndex==4 )
   {
     AssignDefaultZonesColors();
 
@@ -1045,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() );
@@ -1060,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);
               }
@@ -1074,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 = 
@@ -1143,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;
@@ -1154,7 +1201,9 @@ void HYDROGUI_CalculationOp::createPreview( const bool theLandCoverMap )
   else if ( !theLandCoverMap && myShowGeomObjects )
   {
     HYDROData_SequenceOfObjects aSeqGO = myEditedObject->GetGeometryObjects();
+    HYDROData_SequenceOfObjects aSeqP = myEditedObject->GetInterPolyObjects();
     aSeq.Append( aSeqGO );
+    aSeq.Append( aSeqP );
   }
 
   Handle(HYDROData_Entity) anEntity;
@@ -1182,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 );
 
@@ -1219,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();
   }
 }
 
@@ -1311,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() );
     }
   }
@@ -1390,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 =