Salome HOME
Feature #86: The hierarchy in the Object Browser (T 19).
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
index 5607f4f435345d96cf037b983f7e82508c690ac1..707a308655bc1726839d9f5aa9a70db953a4e33b 100644 (file)
@@ -76,10 +76,39 @@ void HYDROGUI_CalculationOp::startOperation()
     return;
 
   aPanel->reset();
+  QStringList aList;
+  QStringList anEntryList;
+  HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetGeometryObjects( module() );
+  getNamesAndEntries( aSeq, aList, anEntryList );
+  aPanel->setAllGeomObjects( aList, anEntryList );
+
+  // Get all polylines
+  aList.clear();
+  anEntryList.clear();
+  HYDROData_Iterator anIter( doc(), KIND_POLYLINE );
+  Handle(HYDROData_Polyline) aPolylineObj;
+  QString aPolylineName;
+  for ( ; anIter.More(); anIter.Next() )
+  {
+    aPolylineObj = Handle(HYDROData_Polyline)::DownCast( anIter.Current() );
+
+    if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed() )
+    { 
+      // Check the polyline shape
+      TopoDS_Shape aPolylineShape = aPolylineObj->GetTopShape();
+      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 );
 
-  QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
-
-  QStringList aSelectedObjects;
+  QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CALCULATION_CASE_NAME" ) );
 
   myEditedObject.Nullify();
   if ( myIsEdit )
@@ -88,7 +117,19 @@ void HYDROGUI_CalculationOp::startOperation()
     if ( !myEditedObject.IsNull() )
     {
       anObjectName = myEditedObject->GetName();
-      updateGeomObjectsList(aPanel);
+      aPolylineObj = myEditedObject->GetBoundaryPolyline();
+      if ( aPolylineObj.IsNull() )
+      {
+        aPanel->setBoundary( QString() );
+      }
+      else
+      {
+        aPolylineName = aPolylineObj->GetName();
+        aPanel->setBoundary( aPolylineName );
+      }
+      aSeq = myEditedObject->GetGeometryObjects();
+      getNamesAndEntries( aSeq, aList, anEntryList );
+      aPanel->includeGeomObjects( aList );
     }
   }
   else
@@ -104,27 +145,29 @@ void HYDROGUI_CalculationOp::startOperation()
   createPreview();
 }
 
-void HYDROGUI_CalculationOp::updateGeomObjectsList( HYDROGUI_CalculationDlg* thePanel ) const
+void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq, 
+                                                QStringList& theNames, QStringList& theEntries ) const
 {
   Handle(HYDROData_Object) anObject;
   Handle(HYDROData_Entity) anEntity;
-  QStringList aList;
-  // Update the list in the dialog
-  HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
-  HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
+  theNames.clear();
+  theEntries.clear();
+  HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
   for ( ; anIter.More(); anIter.Next() )
   {
     anEntity = anIter.Value();
     if ( !anEntity.IsNull() )
     {
-      anObject = Handle(HYDROData_Object)::DownCast( anEntity );
+      // Temporary solution will be revised later
+      //anObject = Handle(HYDROData_Object)::DownCast( anEntity );
+      anObject = Handle(HYDROData_ImmersibleZone)::DownCast( anEntity );
       if ( !anObject.IsNull() )
       {
-        aList.append( anObject->GetName() );
+        theNames.append( anObject->GetName() );
+        theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anObject ) );
       }
     }
   }
-  thePanel->setGeomObjects( aList );
 }
 
 void HYDROGUI_CalculationOp::abortOperation()
@@ -162,10 +205,55 @@ HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
     SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
   connect( aPanel, SIGNAL( objectSelected( const QString & ) ), 
     SLOT( onObjectSelected( const QString & ) ) );
+  connect( aPanel, SIGNAL( boundarySelected( const QString & ) ), 
+    SLOT( onBoundarySelected( const QString & ) ) );
 
   return aPanel;
 }
 
+void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
+{
+  // Set the selected boundary polyline to the calculation case
+  Handle(HYDROData_Polyline) anObject;
+  Handle(AIS_InteractiveContext) aCtx;
+  if ( myPreviewViewManager ) 
+  {
+    if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
+    {
+      aCtx = aViewer->getAISContext();
+    }
+  }
+  // Remove the old boundary from the operation viewer
+  anObject = myEditedObject->GetBoundaryPolyline();
+  if ( !anObject.IsNull() )
+  {
+    module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
+  }
+
+  if ( theObjName.trimmed().isEmpty() )
+  {
+    // No polyline is selected
+    myEditedObject->RemoveBoundaryPolyline();
+  }
+  else
+  {
+    Handle(HYDROData_Entity) anEntity = 
+      HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINE );
+    if ( !anEntity.IsNull() )
+    {
+      anObject = Handle(HYDROData_Polyline)::DownCast( anEntity );
+      if ( !anObject.IsNull() )
+      {
+        myEditedObject->SetBoundaryPolyline( anObject );
+        if ( !aCtx.IsNull() )
+        {
+          showObject( anEntity, aCtx );
+        }
+      }
+    }
+  }
+}
+
 void HYDROGUI_CalculationOp::onObjectSelected ( const QString & theObjName )
 {
   // Select the appropriate geometry object shape in the viewer
@@ -322,33 +410,39 @@ void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theBathy
 
 void HYDROGUI_CalculationOp::onAddObjects()
 {
-  // Add geometry objects selected in the module browser to the calculation case
-  Handle(HYDROData_Object) anObject;
-  Handle(HYDROData_Entity) anEntity;
-  QStringList aList;
-  HYDROData_SequenceOfObjects aSeq = HYDROGUI_Tool::GetSelectedObjects( module() );
-  for( Standard_Integer anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ )
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( aPanel )
   {
-    anEntity = aSeq.Value( anIndex );
-    if ( !anEntity.IsNull() )
+    // Add geometry objects selected in the module browser to the calculation case
+    QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
+    if ( ( !aSelectedList.isEmpty() ) && ( confirmRegionsChange() ) )
     {
-      anObject = Handle(HYDROData_Object)::DownCast( anEntity );
-      if( !anObject.IsNull() )
+      Handle(HYDROData_Object) anObject;
+      Handle(HYDROData_Entity) anEntity;
+      QStringList aList;
+      for (int i = 0; i < aSelectedList.length(); i++)
       {
-        if (myEditedObject->AddGeometryObject( anObject ))
+        anEntity = HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) );
+        if ( !anEntity.IsNull() )
         {
-          aList.append( anObject->GetName() );
+          anObject = Handle(HYDROData_Object)::DownCast( anEntity );
+          if ( !anObject.IsNull() )
+          {
+            if (myEditedObject->AddGeometryObject( anObject ))
+            {
+              aList.append( anObject->GetName() );
+            }
+          }
         }
       }
+      if ( !aList.isEmpty() )
+      {
+        aPanel->includeGeomObjects( aList );
+        createPreview();
+      }
     }
   }
-  HYDROGUI_CalculationDlg* aPanel = 
-    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
-  if ( aPanel )
-  {
-    updateGeomObjectsList( aPanel );
-  }
-  createPreview();
 }
 
 void HYDROGUI_CalculationOp::onRemoveObjects()
@@ -359,25 +453,56 @@ void HYDROGUI_CalculationOp::onRemoveObjects()
   if ( aPanel )
   {
     QStringList aList = aPanel->getSelectedGeomObjects();
-    Handle(HYDROData_Object) anObject;
-    Handle(HYDROData_Entity) anEntity;
-    for (int i = 0; i < aList.length(); i++)
+    if ( ( !aList.isEmpty() ) && ( confirmRegionsChange() ) )
     {
-      anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
-      if ( !anEntity.IsNull() )
+      Handle(HYDROData_Object) anObject;
+      Handle(HYDROData_Entity) anEntity;
+      for (int i = 0; i < aList.length(); i++)
       {
-        anObject = Handle(HYDROData_Object)::DownCast( anEntity );
-        if ( !anObject.IsNull() )
+        anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
+        if ( !anEntity.IsNull() )
         {
-          module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
-          myEditedObject->RemoveGeometryObject( anObject );
+          anObject = Handle(HYDROData_Object)::DownCast( anEntity );
+          if ( !anObject.IsNull() )
+          {
+            module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anObject );
+            myEditedObject->RemoveGeometryObject( anObject );
+          }
         }
       }
+      if ( !aList.isEmpty() )
+      {
+        aPanel->excludeGeomObjects( aList );
+      }
     }
-    updateGeomObjectsList( aPanel );
   }
 }
 
+bool HYDROGUI_CalculationOp::confirmRegionsChange() 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();
+    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( "REGIONS_CHANGED" ),
+                               tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION" ),
+                               QMessageBox::Yes | QMessageBox::No,
+                               QMessageBox::No ) == QMessageBox::Yes );
+    }
+    else
+    {
+      isConfirmed = true; // No regions - no zones - nothing to recalculate
+    }
+  }
+  return isConfirmed;
+}
+
 bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
                                            QString& theErrorMsg )
 {
@@ -386,7 +511,7 @@ bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
   if ( !aPanel )
     return false;
 
-  theUpdateFlags = UF_Model;
+  theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
 
   return true;
 }
@@ -436,19 +561,28 @@ void HYDROGUI_CalculationOp::onApply()
 
 void HYDROGUI_CalculationOp::onSplitZones()
 {
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( !aPanel )
+    return;
+
   QApplication::setOverrideCursor( Qt::WaitCursor );
 
+  QString aNewCaseName = aPanel->getObjectName();
+  QString anOldCaseName = myEditedObject->GetName();
+
+  bool anIsToUpdateOb = myIsEdit && anOldCaseName != aNewCaseName;
+  
+  // At first we must to update the case name because of 
+  // automatic names generation for regions and zones
+  myEditedObject->SetName( aNewCaseName );
+  
   if ( myEditedObject->IsMustBeUpdated() )
   {
     myShowZones = true;
     myEditedObject->SplitGeometryObjects();
 
-    HYDROGUI_CalculationDlg* aPanel = 
-      ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
-    if ( aPanel )
-    {
-      aPanel->setEditedObject( myEditedObject );
-    }
+    aPanel->setEditedObject( myEditedObject );
 
     createPreview();
   }
@@ -457,6 +591,9 @@ void HYDROGUI_CalculationOp::onSplitZones()
     setZonesVisible( true );
   }
 
+  if ( anIsToUpdateOb )
+    module()->getApp()->updateObjectBrowser( false );
+
   QApplication::restoreOverrideCursor();
 }
 
@@ -472,27 +609,30 @@ void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
   HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
   HYDROData_SequenceOfObjects aZones;
   Handle(HYDROData_Region) aRegion;
-  if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
+  if ( myPreviewViewManager ) 
   {
-    Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
-    if ( !aCtx.IsNull() )
+    if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
     {
-      for ( ; aRegionsIter.More(); aRegionsIter.Next() )
+      Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
+      if ( !aCtx.IsNull() )
       {
-        aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
-        if ( !aRegion.IsNull() )
+        for ( ; aRegionsIter.More(); aRegionsIter.Next() )
         {
-          aZones = aRegion->GetZones();
-          HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
-          for ( ; aZonesIter.More(); aZonesIter.Next() )
+          aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
+          if ( !aRegion.IsNull() )
           {
-            if ( theIsVisible )
+            aZones = aRegion->GetZones();
+            HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
+            for ( ; aZonesIter.More(); aZonesIter.Next() )
             {
-              showObject( aZonesIter.Value(), aCtx );
-            }
-            else
-            {
-              module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
+              if ( theIsVisible )
+              {
+                showObject( aZonesIter.Value(), aCtx );
+              }
+              else
+              {
+                module()->removeObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZonesIter.Value() );
+              }
             }
           }
         }
@@ -529,6 +669,9 @@ void HYDROGUI_CalculationOp::createPreview()
     }
   }
 
+  // Get a boundary polyline if any
+  aSeq.Append( myEditedObject->GetBoundaryPolyline() );
+
   module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
 
   if ( !myActiveViewManager )