Salome HOME
copyrights in HYDRO files are updated
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
index ff036a4ffec0c4d11c774f0e9f0277e91e1ccf67..0e9967790d033461efd7ca603991ec99c7879af3 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2013  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2015  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
 #include "HYDROGUI_Module.h"
 #include "HYDROGUI_Tool.h"
 #include "HYDROGUI_UpdateFlags.h"
+#include "HYDROGUI_Zone.h"
+#include "HYDROGUI_Region.h"
 
-#include <HYDROData_Polyline.h>
+#include <HYDROData_PolylineXY.h>
+#include <HYDROData_ShapesGroup.h>
 #include <HYDROData_Iterator.h>
-#include <HYDROData_ImmersibleZone.h>
+#include <HYDROData_Object.h>
+#include <HYDROData_Tool.h>
 
 #include <OCCViewer_ViewManager.h>
 #include <OCCViewer_ViewModel.h>
 
 #include <LightApp_Application.h>
 #include <LightApp_UpdateFlags.h>
+#include <LightApp_SelectionMgr.h>
+#include <LightApp_DataOwner.h>
+
+#include <SUIT_MessageBox.h>
+#include <SUIT_Desktop.h>
+#include <SUIT_DataBrowser.h>
 
 #include <QApplication>
+#include <QKeySequence>
+#include <QShortcut>
 
 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
 : HYDROGUI_Operation( theModule ),
@@ -58,62 +70,154 @@ HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
 void HYDROGUI_CalculationOp::startOperation()
 {
   HYDROGUI_Operation::startOperation();
+  
+  // Begin transaction
+  startDocOperation();
 
   HYDROGUI_CalculationDlg* aPanel = 
     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
   if ( !aPanel )
     return;
 
-  myRegionsList.clear();
-  aPanel->reset();
+  SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
+  QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
+  QShortcut* aShortcut;
+  foreach( aShortcut, aShortcuts )
+  {
+    if ( aShortcut->key() == 
+      QKeySequence(((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
+      SUIT_DataBrowser::RenameShortcut ) ) )
+    {
+      aShortcut->setEnabled( false );
+    }
+  }
 
-  QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
+  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_POLYLINEXY );
+  Handle(HYDROData_PolylineXY) aPolylineObj;
+  QString aPolylineName;
+  for ( ; anIter.More(); anIter.Next() )
+  {
+    aPolylineObj = Handle(HYDROData_PolylineXY)::DownCast( anIter.Current() );
+
+    if ( !aPolylineObj.IsNull() && aPolylineObj->IsClosed() )
+    { 
+      // 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 );
 
-  QStringList aSelectedObjects;
+  QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CALCULATION_CASE_NAME" ) );
 
   myEditedObject.Nullify();
   if ( myIsEdit )
   {
-    myEditedObject = Handle(HYDROData_Calculation)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
+    myEditedObject = Handle(HYDROData_CalculationCase)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
     if ( !myEditedObject.IsNull() )
     {
+      aPanel->setMode( myEditedObject->GetAssignmentMode() );
       anObjectName = myEditedObject->GetName();
-
-      HYDROData_SequenceOfObjects aRefObjects = myEditedObject->GetGeometryObjects();
-      HYDROData_SequenceOfObjects::Iterator anIter( aRefObjects );
-      for ( ; anIter.More(); anIter.Next() )
+      aPolylineObj = myEditedObject->GetBoundaryPolyline();
+      if ( aPolylineObj.IsNull() )
       {
-        Handle(HYDROData_Object) aRefbject = 
-          Handle(HYDROData_Object)::DownCast( anIter.Value() );
-        if ( aRefbject.IsNull() )
-          continue;
+        aPanel->setBoundary( QString() );
+      }
+      else
+      {
+        aPolylineName = aPolylineObj->GetName();
+        aPanel->setBoundary( aPolylineName );
+      }
 
-        QString aRefObjectName = aRefbject->GetName();
-        if ( aRefObjectName.isEmpty() )
-          continue;
+      aSeq = myEditedObject->GetGeometryObjects();
+      getNamesAndEntries( aSeq, aList, anEntryList );
+      aPanel->includeGeomObjects( aList );
 
-        aSelectedObjects.append( aRefObjectName );
+      // set rules
+      HYDROData_ListOfRules aRules;
+      Handle(HYDROData_Object) anObject1, anObject2;
+      HYDROData_PriorityType aPriority;
+      HYDROData_Zone::MergeAltitudesType aMergeType;
+      for ( int anIndex = 0; ; anIndex++ )
+      {
+        if ( myEditedObject->GetRule( anIndex, anObject1, aPriority, anObject2, aMergeType ) ) {
+          HYDROData_CustomRule aRule;
+          aRule.Object1 = anObject1;
+          aRule.Object2 = anObject2;
+          aRule.Priority = aPriority;
+          aRule.MergeType = aMergeType;
+
+          aRules << aRule;
+        }
+        else
+          break;
       }
+      aPanel->setRules( aRules );
     }
   }
+  else
+  {
+    myEditedObject =
+      Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
+    myEditedObject->SetName( anObjectName );
+    myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)aPanel->getMode() );
+  }
 
   aPanel->setObjectName( anObjectName );
-  aPanel->setSelectedGeomObjects( aSelectedObjects );
+  aPanel->setEditedObject( myEditedObject );
 
   createPreview();
 }
 
+void HYDROGUI_CalculationOp::getNamesAndEntries( const HYDROData_SequenceOfObjects& theSeq, 
+                                                QStringList& theNames, QStringList& theEntries ) const
+{
+  theNames.clear();
+  theEntries.clear();
+  HYDROData_SequenceOfObjects::Iterator anIter( theSeq );
+  for ( ; anIter.More(); anIter.Next() )
+  {
+    Handle(HYDROData_Entity) anEntity = anIter.Value();
+    //if ( !HYDROData_Tool::IsGeometryObject( anEntity ) )
+    //  continue;
+
+    theNames.append( anEntity->GetName() );
+    theEntries.append( HYDROGUI_DataObject::dataObjectEntry( anEntity ) );
+  }
+}
+
 void HYDROGUI_CalculationOp::abortOperation()
 {
   closePreview();
-
+  // Abort transaction
+  abortDocOperation();
   HYDROGUI_Operation::abortOperation();
+  module()->getApp()->updateObjectBrowser();
 }
 
 void HYDROGUI_CalculationOp::commitOperation()
 {
   closePreview();
-
+  // Commit transaction
+  commitDocOperation();
   HYDROGUI_Operation::commitOperation();
 }
 
@@ -122,108 +226,599 @@ HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
 
   // Connect signals and slots
+  connect( aPanel, SIGNAL( changeMode( int ) ), SLOT( onChangeMode( int ) ) );
+  connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
+  connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
+  connect( aPanel, SIGNAL( addGroups() ), SLOT( onAddGroups() ) );
+  connect( aPanel, SIGNAL( removeGroups() ), SLOT( onRemoveGroups() ) );
+  connect( aPanel, SIGNAL( Next( const int ) ), SLOT( onNext( const int ) ) );
+  connect( aPanel, SIGNAL( Back( const int ) ), SLOT( onHideZones() ) );
+  //connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
+  connect( aPanel, SIGNAL( setMergeType( int, QString& ) ), SLOT( onSetMergeType( int, QString& ) ) );
+  connect( aPanel, SIGNAL( moveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ),
+    SLOT( onMoveZones( SUIT_DataObject*, const QList<SUIT_DataObject*>& ) ) );
+  connect( aPanel, SIGNAL( createRegion( const QList<SUIT_DataObject*>& ) ),
+    SLOT( onCreateRegion( const QList<SUIT_DataObject*>& ) ) );
+  connect( aPanel, SIGNAL( clickedInZonesBrowser( SUIT_DataObject* ) ),
+    SLOT( onClickedInZonesBrowser( SUIT_DataObject* ) ) );
+  connect( aPanel, SIGNAL( objectsSelected() ), 
+           SLOT( onObjectsSelected() ) );
+  connect( aPanel, SIGNAL( boundarySelected( const QString & ) ), 
+    SLOT( onBoundarySelected( const QString & ) ) );
 
   return aPanel;
 }
 
-bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
-                                           QString& theErrorMsg )
+void HYDROGUI_CalculationOp::onBoundarySelected ( const QString & theObjName )
+{
+  bool anIsToUpdateViewer = false;
+
+  // Remove the old boundary from the operation viewer
+  Handle(HYDROData_PolylineXY) aPrevPolyline = 
+    myEditedObject->GetBoundaryPolyline();
+  if ( !aPrevPolyline.IsNull() )
+  {
+    setObjectVisibility( aPrevPolyline, false );
+    anIsToUpdateViewer = true;
+  }
+
+  // Set the selected boundary polyline to the calculation case
+  Handle(HYDROData_PolylineXY) aNewPolyline = Handle(HYDROData_PolylineXY)::DownCast(
+    HYDROGUI_Tool::FindObjectByName( module(), theObjName, KIND_POLYLINEXY ) );
+  myEditedObject->SetBoundaryPolyline( aNewPolyline );
+
+  if ( myPreviewViewManager )
+  {
+    OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
+    if ( aViewer )
+    {
+      if ( !aNewPolyline.IsNull() )
+      {
+        Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
+        if ( !aCtx.IsNull() )
+        {
+          setObjectVisibility( aNewPolyline, true );
+          anIsToUpdateViewer = true;
+        }
+      }
+
+      if ( anIsToUpdateViewer )
+        module()->update( UF_OCCViewer );
+    }
+  }
+}
+
+void HYDROGUI_CalculationOp::onObjectsSelected()
 {
   HYDROGUI_CalculationDlg* aPanel = 
     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
-  if ( !aPanel )
-    return false;
 
-  QString anObjectName = aPanel->getObjectName().simplified();
-  if ( anObjectName.isEmpty() )
+  QStringList aSelectedObjs = aPanel->getSelectedGeomObjects();
+  QMap<QString, bool> aSelectedObjsMap;
+  foreach( QString aName, aSelectedObjs )
+    aSelectedObjsMap[aName] = true;
+
+
+  // Select the appropriate geometry object shape in the viewer
+  selectionMgr()->clearSelected();
+
+  // Unhighlight all objects except selected
+  HYDROGUI_Shape* aShape = 0, *aLastShape = 0;
+  Handle(HYDROData_Entity) anEntity;
+  HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
+  HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
+  bool isSelected;
+  QString aName;
+  for ( ; anIter.More(); anIter.Next() )
   {
-    theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
-    return false;
+    anEntity = anIter.Value();
+    if ( !anEntity.IsNull() )
+    {
+      aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, anEntity );
+      if ( aShape )
+      {
+        aName = anEntity->GetName();
+        isSelected = aSelectedObjsMap.contains( aName );
+        aShape->highlight( isSelected, false );
+        aShape->update( false, false );
+        aLastShape = aShape;
+      }
+    }
   }
+  if( aLastShape )
+    aLastShape->update( true, false );
+}
 
-  // check that there are no other objects with the same name in the document
-  if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
+void HYDROGUI_CalculationOp::onClickedInZonesBrowser( SUIT_DataObject* theItem )
+{
+  HYDROGUI_Region* aRegionItem = dynamic_cast<HYDROGUI_Region*>(theItem);
+  HYDROGUI_Zone* aZoneItem;
+  selectionMgr()->clearSelected();
+  if ( aRegionItem )
   {
-    Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
-    if ( !anObject.IsNull() )
+    // Select a region in preview
+    SUIT_DataOwnerPtrList aList( true );
+    DataObjectList aZones = aRegionItem->children();
+    for ( int i = 0; i < aZones.length(); i++ )
     {
-      theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
-      return false;
+      aZoneItem = dynamic_cast<HYDROGUI_Zone*>(aZones.at(i));
+      if ( aZoneItem )
+      {
+        aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
+      }
     }
+    selectionMgr()->setSelected( aList );
   }
+  else
+  {
+    // select a single zone
+    aZoneItem = dynamic_cast<HYDROGUI_Zone*>(theItem);
+    if ( aZoneItem )
+    {
+      SUIT_DataOwnerPtrList aList( true );
+      aList.append( SUIT_DataOwnerPtr( new LightApp_DataOwner( aZoneItem->entry() ) ) );
+      selectionMgr()->setSelected( aList );
+    }
+  }
+}
 
-  Handle(HYDROData_Document) aDocument = doc();
+void HYDROGUI_CalculationOp::onMoveZones( SUIT_DataObject* theRegionItem, const QList<SUIT_DataObject*>& theZonesList )
+{
+  HYDROGUI_Region* aRegion = dynamic_cast<HYDROGUI_Region*>(theRegionItem);
+  if ( aRegion )
+  {
+    QList<HYDROGUI_Zone*> aZonesList;
+    HYDROGUI_Zone* aZone;
+    // Get a list of dropped zones
+    for ( int i = 0; i < theZonesList.length(); i++ )
+    {
+      aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
+      if ( aZone )
+      {
+        aZonesList.append( aZone );
+      }
+    }
+    if ( aZonesList.length() > 0 )
+    {
+      aRegion->addZones( aZonesList );
+      HYDROGUI_CalculationDlg* aPanel = 
+        ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+      if ( aPanel )
+      {
+        aPanel->refreshZonesBrowser();
+      }
+      createPreview();
+    }
+  }
+}
 
-  Handle(HYDROData_Calculation) aCalculObj = myIsEdit ? myEditedObject :
-    Handle(HYDROData_Calculation)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
-  if ( aCalculObj.IsNull() )
-    return false;
+void HYDROGUI_CalculationOp::onCreateRegion( const QList<SUIT_DataObject*>& theZonesList )
+{
+  QList<HYDROGUI_Zone*> aZonesList;
+  HYDROGUI_Zone* aZone;
+  // Get a list of dropped zones
+  for ( int i = 0; i < theZonesList.length(); i++ )
+  {
+    aZone = dynamic_cast<HYDROGUI_Zone*>( theZonesList.at( i ) );
+    if ( aZone )
+    {
+      aZonesList.append( aZone );
+    }
+  }
+  if ( aZonesList.length() > 0 )
+  {
+    module()->getDataModel()->createNewRegion( myEditedObject, aZonesList );
+    HYDROGUI_CalculationDlg* aPanel = 
+      ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+    if ( aPanel )
+    {
+      aPanel->refreshZonesBrowser();
+    }
+    createPreview();
+  }
+}
+
+void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString& theAltitudeName )
+{
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( aPanel )
+  {
+    HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
+    if ( aZone )
+    {
+      aZone->setMergeType( theMergeType, theAltitudeName );
+      HYDROGUI_Shape* aShape = module()->getObjectShape( HYDROGUI_Module::VMR_PreviewCaseZones, aZone->modelObject() );
+      if ( aShape )
+      {
+        aShape->update( true, false );
+      }
+    }
+    aPanel->refreshZonesBrowser();
+  }
+}
 
-  aCalculObj->SetName( anObjectName );
+void HYDROGUI_CalculationOp::onAddObjects()
+{
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( !aPanel )
+    return;
 
-  QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
-  HYDROData_SequenceOfObjects aGeomObjects = 
-    HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
+  // Add geometry objects selected in the module browser to the calculation case
+  QStringList aSelectedList = aPanel->getSelectedAvailableGeomObjects();
+  if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
+    return;
 
-  aCalculObj->SetGeometryObjects( aGeomObjects );
+  QStringList anAddedList;
+  for (int i = 0; i < aSelectedList.length(); i++)
+  {
+    Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
+      HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
+    if ( anObject.IsNull() )
+      continue;
 
-  theUpdateFlags = UF_Model;
+    if ( myEditedObject->AddGeometryObject( anObject ) )
+      anAddedList.append( anObject->GetName() );
+  }
 
-  return true;
+  if ( !anAddedList.isEmpty() )
+  {
+    aPanel->includeGeomObjects( anAddedList );
+    createPreview();
+  }
 }
 
-void HYDROGUI_CalculationOp::onSplitZones()
+void HYDROGUI_CalculationOp::onRemoveObjects()
 {
-  myRegionsList.clear();
-
+  // Remove selected objects from the calculation case
   HYDROGUI_CalculationDlg* aPanel = 
     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
   if ( !aPanel )
     return;
 
-  QApplication::setOverrideCursor( Qt::WaitCursor );
+  QStringList aSelectedList = aPanel->getSelectedGeomObjects();
+  if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
+    return;
+
+  for (int i = 0; i < aSelectedList.length(); i++)
+  {
+    Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
+      HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
+    if ( anObject.IsNull() )
+      continue;
 
-  QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
-  HYDROData_SequenceOfObjects aGeomObjects = 
-    HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
+    setObjectVisibility( anObject, false );
+    myEditedObject->RemoveGeometryObject( anObject );
+  }
 
-  QStringList aResSplittedZones;
+  module()->update( UF_OCCViewer );
+  aPanel->excludeGeomObjects( aSelectedList );
+}
 
-  HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
-    HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
+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_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
+  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( "MODE_CHANGED" ),
+                               tr( "CONFIRM_SPLITTING_ZONES_RECALCULATION_MODE" ),
+                               QMessageBox::Yes | QMessageBox::No,
+                               QMessageBox::No ) == QMessageBox::Yes );
+    }
+    else
+    {
+      isConfirmed = true; // No regions - no zones - nothing to recalculate
+    }
+  }
+  return isConfirmed;
+}
 
-  QStringList aUsedNames;
+bool HYDROGUI_CalculationOp::confirmContinueWithWarning( const HYDROData_Warning& theWarning ) const
+{
+  HYDROData_WarningType aType = theWarning.Type;
+  if ( aType == WARN_OK ) {
+    return true;
+  }
 
-  HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
-  while( anIter.hasNext() )
+  QString aTitle;
+  QString aMsg;
+  switch ( aType )
   {
-    Region aRegion;
-    aRegion.SplitData = anIter.next();
+    case WARN_EMPTY_REGIONS:
+      aTitle = tr( "EMPTY_REGIONS" );
+      aMsg = tr( "CONFIRM_CONTINUE_WITH_OBJECTS_NOT_INCLUDED_TO_REGION" ).arg( theWarning.Data );
+      break;
+    default:
+      aTitle = tr( "WARNING" );
+      aMsg = theWarning.Data;
+  }
 
-    aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
-    aRegion.BorderColor  = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
 
-    aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
+  int anAnswer = SUIT_MessageBox::warning( module()->getApp()->desktop(),
+                                           aTitle, aMsg,
+                                           QMessageBox::Yes | QMessageBox::No,
+                                           QMessageBox::No );
 
-    aUsedNames.append( aRegion.RegionName );
+  return ( anAnswer == QMessageBox::Yes );
+}
 
-    aResSplittedZones.append( aRegion.RegionName );
+bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
+                                           QString& theErrorMsg,
+                                           QStringList& theBrowseObjectsEntries )
+{
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( !aPanel )
+    return false;
 
-    myRegionsList.append( aRegion );
+  if( !myIsEdit )
+  {
+    QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
+    theBrowseObjectsEntries.append( anEntry );
+  }
+
+  // For manual mode priority rules are redundant
+  if ( aPanel->getMode() == HYDROData_CalculationCase::MANUAL ) {
+    myEditedObject->ClearRules( false );
+  }
+  theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer | UF_VTK_Forced | UF_VTK_Init;
+
+  return true;
+}
+
+void HYDROGUI_CalculationOp::onApply()
+{
+  // Check warnings
+  HYDROData_Warning aWarning = myEditedObject->GetLastWarning();
+  if ( aWarning.Type != WARN_OK ) {
+    if ( !confirmContinueWithWarning( aWarning ) ) {
+      // Go back to the first page
+      HYDROGUI_CalculationDlg* aPanel = 
+        ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+      if ( aPanel ) {
+        aPanel->onFirstPage();
+      }
+      return;
+    }
   }
-  
-  createPreview();
 
+  QApplication::setOverrideCursor( Qt::WaitCursor );
+
+  int anUpdateFlags = 0;
+  QString anErrorMsg;
+  QStringList aBrowseObjectsEntries;
+
+  bool aResult = false;
+  
+  try
+  {
+    aResult = processApply( anUpdateFlags, anErrorMsg, aBrowseObjectsEntries );
+  }
+  catch ( Standard_Failure )
+  {
+    Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
+    anErrorMsg = aFailure->GetMessageString();
+    aResult = false;
+  }
+  catch ( ... )
+  {
+    aResult = false;
+  }
+  
   QApplication::restoreOverrideCursor();
+
+  if ( aResult )
+  {
+    module()->update( anUpdateFlags );
+    commit();
+    browseObjects( aBrowseObjectsEntries );
+  }
+  else
+  {
+    abort();
+    QString aMsg = tr( "INPUT_VALID_DATA" );
+    if( !anErrorMsg.isEmpty() )
+      aMsg.prepend( anErrorMsg + "\n" );
+    SUIT_MessageBox::critical( module()->getApp()->desktop(),
+                               tr( "INSUFFICIENT_INPUT_DATA" ),
+                               aMsg ); 
+  }
+}
+
+void HYDROGUI_CalculationOp::onNext( const int theIndex )
+{
+  if( theIndex==1 )
+  {
+    setAvailableGroups();
+  }
+  else if( theIndex==2 )
+  {
+    HYDROGUI_CalculationDlg* aPanel = 
+      ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+    if ( !aPanel )
+      return;
+
+    QApplication::setOverrideCursor( Qt::WaitCursor );
+  
+    QString aNewCaseName = aPanel->getObjectName();
+    QString anOldCaseName = myEditedObject->GetName();
+    bool isNameChanged = anOldCaseName != aNewCaseName;
+  
+    bool anIsToUpdateOb = isNameChanged;
+
+    // At first we must to update the case name because of 
+    // automatic names generation for regions and zones
+    myEditedObject->SetName( aNewCaseName );
+    
+    // Set parameters for automatic mode
+    int aMode = aPanel->getMode();
+    if ( aMode == HYDROData_CalculationCase::AUTOMATIC ) {
+      // Set objects in the specified order
+      myEditedObject->RemoveGeometryObjects();
+      foreach ( const QString& aName, aPanel->getAllGeomObjects() ) {
+        Handle(HYDROData_Object) anObject = Handle(HYDROData_Object)::DownCast( 
+          HYDROGUI_Tool::FindObjectByName( module(), aName ) );
+        if ( anObject.IsNull() ) {
+          continue;
+        }
+
+        myEditedObject->AddGeometryObject( anObject );
+      }
+
+      // Clear priority rules
+      //@ASL if ( myEditedObject->GetRulesCount() > 0 ) {
+        myEditedObject->ClearRules( true );
+      //@ASL }
+      // Set priority rules
+      foreach ( const HYDROData_CustomRule& aRule, aPanel->getRules() ) {
+        myEditedObject->AddRule( aRule.Object1, aRule.Priority,
+                                 aRule.Object2, aRule.MergeType );
+      }
+    }
+    aPanel->setEditZonesEnabled( aMode == HYDROData_CalculationCase::MANUAL );
+         
+    if ( myEditedObject->IsMustBeUpdated() )
+    {
+      myShowZones = true;
+      myEditedObject->Update();
+      
+      //aPanel->setEditedObject( myEditedObject );
+      aPanel->refreshZonesBrowser();
+    
+      closePreview();
+      createPreview();
+
+      anIsToUpdateOb = true;
+    }
+    else
+    {
+      setZonesVisible( true );
+
+      if ( isNameChanged ) {
+        module()->getDataModel()->updateObjectTree( myEditedObject );
+      }
+    }
+
+    if ( anIsToUpdateOb ) {
+      SUIT_DataBrowser* anObjBrowser = ((LightApp_Application*)module()->application())->objectBrowser();
+      if ( anObjBrowser ) {
+        anObjBrowser->updateTree( module()->getDataModel()->getDataObject( myEditedObject ), false );
+      }
+    }
+
+    QApplication::restoreOverrideCursor();
+  }
+}
+
+void HYDROGUI_CalculationOp::onHideZones()
+{
+  setZonesVisible( false );
+}
+
+void HYDROGUI_CalculationOp::setZonesVisible( bool theIsVisible )
+{
+  myShowZones = theIsVisible;
+  HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
+  HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
+  HYDROData_SequenceOfObjects aZones;
+  Handle(HYDROData_Region) aRegion;
+  if ( myPreviewViewManager ) 
+  {
+    if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
+    {
+      Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
+      if ( !aCtx.IsNull() )
+      {
+        for ( ; aRegionsIter.More(); aRegionsIter.Next() )
+        {
+          aRegion = Handle(HYDROData_Region)::DownCast( aRegionsIter.Value() );
+          if ( !aRegion.IsNull() )
+          {
+            aZones = aRegion->GetZones();
+            HYDROData_SequenceOfObjects::Iterator aZonesIter( aZones );
+            for ( ; aZonesIter.More(); aZonesIter.Next() )
+            {
+              setObjectVisibility( aZonesIter.Value(), theIsVisible );
+            }
+          }
+        }
+      }
+
+      module()->update( UF_OCCViewer );
+    }
+  }
 }
 
 void HYDROGUI_CalculationOp::createPreview()
 {
   LightApp_Application* anApp = module()->getApp();
+  HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryObjects();
+  Handle(HYDROData_Entity) anEntity;
+
+  if ( myShowZones )
+  {
+    // Gather zones for displaying
+    HYDROData_SequenceOfObjects aRegions = myEditedObject->GetRegions();
+    HYDROData_SequenceOfObjects::Iterator aRegionsIter( aRegions );
+    HYDROData_SequenceOfObjects aZones;
+    Handle(HYDROData_Region) aRegion;
+    for ( ; aRegionsIter.More(); aRegionsIter.Next() )
+    {
+      anEntity = aRegionsIter.Value();
+      if ( !anEntity.IsNull() )
+      {
+        aRegion = Handle(HYDROData_Region)::DownCast( anEntity );
+        if ( !aRegion.IsNull() )
+        {
+          aZones = aRegion->GetZones();
+          aSeq.Append( aZones );
+        }
+      }
+    }
+  }
+
+  // Get a boundary polyline if any
+  aSeq.Append( myEditedObject->GetBoundaryPolyline() );
+
+  module()->removeViewShapes( HYDROGUI_Module::VMR_PreviewCaseZones );
 
   if ( !myActiveViewManager )
   {
-    if ( myRegionsList.isEmpty() )
+    if ( aSeq.IsEmpty() )
       return;
 
     myActiveViewManager = anApp->activeViewManager();
@@ -251,28 +846,31 @@ void HYDROGUI_CalculationOp::createPreview()
     Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
     if ( !aCtx.IsNull() )
     {
-      RegionsList::iterator anIter = myRegionsList.begin();
-      for ( ; anIter != myRegionsList.end(); ++anIter )
+      HYDROData_SequenceOfObjects::Iterator anIter( aSeq );
+      for ( ; anIter.More(); anIter.Next() )
       {
-        Region& aRegion = *anIter;
-        if ( aRegion.Shape )
-        {
-          aRegion.Shape->erase( false );
-          delete aRegion.Shape;
-        }
-
-        aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
-
-        aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
-        aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
-        aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
+        setObjectVisibility( anIter.Value(), true );
       }
 
       //Process the draw events for viewer
-      QApplication::processEvents();
+      QApplication::processEvents( QEventLoop::ExcludeUserInputEvents );
       if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
         vw->onTopView();
     }
+
+    module()->update( UF_OCCViewer | UF_FitAll );
+  }
+}
+
+void HYDROGUI_CalculationOp::setObjectVisibility( Handle(HYDROData_Entity) theEntity, const bool theIsVisible )
+{
+  if ( theEntity.IsNull() || !myPreviewViewManager ) {
+    return;
+  }
+
+  OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
+  if ( aViewer ) {
+    module()->setObjectVisible( (size_t)aViewer, theEntity, theIsVisible );
   }
 }
 
@@ -283,20 +881,35 @@ void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager
 
 void HYDROGUI_CalculationOp::closePreview()
 {
-  RegionsList::iterator anIter= myRegionsList.begin();
-  for ( ; anIter != myRegionsList.end(); ++anIter )
+  SUIT_DataBrowser* aOb = ((LightApp_Application*)module()->application())->objectBrowser();
+  QList<QShortcut*> aShortcuts = aOb->findChildren<QShortcut*>();
+  QShortcut* aShortcut;
+  foreach( aShortcut, aShortcuts )
   {
-    Region& aRegion = *anIter;
-    if ( aRegion.Shape )
+    if ( aShortcut->key() == 
+      QKeySequence( ((LightApp_Application*)module()->application())->objectBrowser()->shortcutKey( 
+      SUIT_DataBrowser::RenameShortcut ) ) )
     {
-      aRegion.Shape->erase( false );
-      delete aRegion.Shape;
-      aRegion.Shape = NULL;
+      aShortcut->setEnabled( true );
     }
   }
 
+
   if( myPreviewViewManager )
   {
+    // Hide all the displayed objects in the preview view
+    OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer();
+    if ( aViewer ) {
+      size_t aViewId = (size_t)aViewer;
+      HYDROData_Iterator anIterator( doc() );
+      for( ; anIterator.More(); anIterator.Next() ) {
+        Handle(HYDROData_Entity) anObject = anIterator.Current();
+        if( !anObject.IsNull() ) {
+          module()->setObjectVisible( aViewId, anObject, false );
+        }
+      }
+    }
+
     disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
                 this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
 
@@ -311,4 +924,111 @@ void HYDROGUI_CalculationOp::closePreview()
   }
 }
 
+void HYDROGUI_CalculationOp::setAvailableGroups()
+{
+  HYDROGUI_CalculationDlg* aPanel = 
+      ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+
+  HYDROData_SequenceOfObjects aSeq = myEditedObject->GetGeometryGroups();
+  QStringList aList, anEntryList;
+  getNamesAndEntries( aSeq, aList, anEntryList );
+
+  QStringList aGroupsNames;
+
+  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 ) );
+    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 ) );
+      aGroupsNames.append( aGroup->GetName() );
+    }
+  }
+  if( myEditedObject->IsMustBeUpdated() ) {
+    for( int anIndex = 1, aLength = aSeq.Length(); anIndex <= aLength; anIndex++ ) {
+      Handle(HYDROData_ShapesGroup) aGeomGroup =
+        Handle(HYDROData_ShapesGroup)::DownCast( aSeq.Value( anIndex ) );
+      if ( !aGeomGroup.IsNull() && !aGroupsNames.contains( aGeomGroup->GetName() ) ) {
+        myEditedObject->RemoveGeometryGroup( aGeomGroup );
+      }
+    }
+  }
+
+  aPanel->setAvailableGroups( aGroupsNames );
+  aPanel->includeGroups( aList );
+
+  bool isUpdated = myEditedObject->IsMustBeUpdated();
+}
+
+void HYDROGUI_CalculationOp::onAddGroups()
+{
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( !aPanel )
+    return;
+
+  // Add geometry objects selected in the module browser to the calculation case
+  QStringList aSelectedList = aPanel->getSelectedAvailableGroups();
+  if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
+    return;
+
+  QStringList anAddedList;
+  for (int i = 0; i < aSelectedList.length(); i++)
+  {
+    Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
+      HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at( i ) ) );
+    if ( aGroup.IsNull() )
+      continue;
+
+    if ( myEditedObject->AddGeometryGroup( aGroup ) )
+      anAddedList.append( aGroup->GetName() );
+  }
+
+  if ( !anAddedList.isEmpty() )
+  {
+    aPanel->includeGroups( anAddedList );
+  }
+}
+
+void HYDROGUI_CalculationOp::onRemoveGroups()
+{
+  // Remove selected objects from the calculation case
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( !aPanel )
+    return;
+
+  QStringList aSelectedList = aPanel->getSelectedGroups();
+  if ( aSelectedList.isEmpty() || !confirmRegionsChange() )
+    return;
+
+  for (int i = 0; i < aSelectedList.length(); i++)
+  {
+    Handle(HYDROData_ShapesGroup) aGroup = Handle(HYDROData_ShapesGroup)::DownCast( 
+      HYDROGUI_Tool::FindObjectByName( module(), aSelectedList.at(i) ) );
+    if ( aGroup.IsNull() )
+      continue;
+
+    myEditedObject->RemoveGeometryGroup( aGroup );
+  }
+
+  aPanel->excludeGroups( aSelectedList );
+}
+
+void HYDROGUI_CalculationOp::onChangeMode( int theMode )
+{
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( !aPanel )
+    return;
 
+  if ( !confirmModeChange() ) {
+    aPanel->setMode( myEditedObject->GetAssignmentMode() );
+    return;
+  }
+
+  myEditedObject->SetAssignmentMode( (HYDROData_CalculationCase::AssignmentMode)theMode );
+  aPanel->setMode( theMode );
+}