Salome HOME
Image positioning by two points.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
index 3e1a5dc83dfa21297172dbdade6877075455d123..3dab9422c0eb0fecebb2a2df68669719be3c1248 100644 (file)
 #include "HYDROGUI_Module.h"
 #include "HYDROGUI_Tool.h"
 #include "HYDROGUI_UpdateFlags.h"
+#include "HYDROGUI_Zone.h"
 
 #include <HYDROData_Polyline.h>
 #include <HYDROData_Iterator.h>
-#include <HYDROData_Zone.h>
+#include <HYDROData_ImmersibleZone.h>
+#include <HYDROData_Object.h>
 
 #include <OCCViewer_ViewManager.h>
 #include <OCCViewer_ViewModel.h>
@@ -39,6 +41,9 @@
 #include <LightApp_Application.h>
 #include <LightApp_UpdateFlags.h>
 
+#include <SUIT_MessageBox.h>
+#include <SUIT_Desktop.h>
+
 #include <QApplication>
 
 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
@@ -58,6 +63,9 @@ HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
 void HYDROGUI_CalculationOp::startOperation()
 {
   HYDROGUI_Operation::startOperation();
+  
+  // Begin transaction
+  startDocOperation();
 
   HYDROGUI_CalculationDlg* aPanel = 
     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
@@ -69,108 +77,68 @@ void HYDROGUI_CalculationOp::startOperation()
 
   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
 
-  QStringList aSelectedZones, aRegions;
+  QStringList aSelectedObjects;
 
   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() )
     {
       anObjectName = myEditedObject->GetName();
-
-      Handle(HYDROData_Polyline) aBoundaryPolyline = myEditedObject->GetBoundaryPolyline();
-      if ( !aBoundaryPolyline.IsNull() )
-      {
-        QString aPolylineName = aBoundaryPolyline->GetName();
-        aPanel->setPolylineName( aPolylineName );
-      }
-
-      HYDROData_SequenceOfObjects aRefZones = myEditedObject->GetZones();
-      HYDROData_SequenceOfObjects::Iterator anIter( aRefZones );
-      for ( ; anIter.More(); anIter.Next() )
-      {
-        Handle(HYDROData_Zone) aRefZone = 
-          Handle(HYDROData_Zone)::DownCast( anIter.Value() );
-        if ( aRefZone.IsNull() )
-          continue;
-
-        QString aRefZoneName = aRefZone->GetName();
-        if ( aRefZoneName.isEmpty() )
-          continue;
-
-        aSelectedZones.append( aRefZoneName );
-      }
-
-      HYDROData_SequenceOfObjects aDataRegions = myEditedObject->GetRegions();
-      anIter.Init( aDataRegions );
-      for ( ; anIter.More(); anIter.Next() )
-      {
-        Handle(HYDROData_Region) aDataRegion = 
-          Handle(HYDROData_Region)::DownCast( anIter.Value() );
-        if ( aDataRegion.IsNull() )
-          continue;
-
-        QString aRegionName = aDataRegion->GetName();
-        if ( aRegionName.isEmpty() )
-          continue;
-
-        Region aRegion;
-        // aRegion.SplitData.Path = aSplitZone->GetPainterPath();
-        // aRegion.SplitData.ZoneNames ???
-
-        aRegion.FillingColor = aDataRegion->GetFillingColor();
-        aRegion.BorderColor  = aDataRegion->GetBorderColor();
-
-        aRegion.RegionName = aRegionName;
-
-        aRegion.DataRegion = aDataRegion;
-
-        myRegionsList.append( aRegion );
-
-        aRegions.append( aRegionName );
-      }
+      updateGeomObjectsList(aPanel);
     }
   }
-
-  // collect information about existing zones
-  QStringList aZones;
-
-  HYDROData_Iterator anIter( doc(), KIND_ZONE );
-  for ( ; anIter.More(); anIter.Next() )
+  else
   {
-    Handle(HYDROData_Zone) aZoneObj = 
-      Handle(HYDROData_Zone)::DownCast( anIter.Current() );
-    if ( aZoneObj.IsNull() )
-      continue;
-
-    QString aZoneName = aZoneObj->GetName();
-    if ( aZoneName.isEmpty() )
-      continue;
-
-    aZones.append( aZoneName );
+    myEditedObject =
+      Handle(HYDROData_CalculationCase)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
+    myEditedObject->SetName(anObjectName);
   }
 
   aPanel->setObjectName( anObjectName );
-
-  aPanel->setZones( aZones );
-  aPanel->setSelectedZones( aSelectedZones );
-  aPanel->setRegions( aRegions );
+  aPanel->setEditedObject( myEditedObject );
+//  aPanel->setSelectedGeomObjects( aSelectedObjects );
 
   createPreview();
 }
 
+void HYDROGUI_CalculationOp::updateGeomObjectsList( HYDROGUI_CalculationDlg* thePanel ) 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 );
+  for ( ; anIter.More(); anIter.Next() )
+  {
+    anEntity = anIter.Value();
+    if ( !anEntity.IsNull() )
+    {
+      anObject = Handle(HYDROData_Object)::DownCast( anEntity );
+      if ( !anObject.IsNull() )
+      {
+        aList.append( anObject->GetName() );
+      }
+    }
+  }
+  thePanel->setSelectedGeomObjects( aList );
+}
+
 void HYDROGUI_CalculationOp::abortOperation()
 {
   closePreview();
-
+  // Abort transaction
+  abortDocOperation();
   HYDROGUI_Operation::abortOperation();
 }
 
 void HYDROGUI_CalculationOp::commitOperation()
 {
   closePreview();
-
+  // Commit transaction
+  commitDocOperation();
   HYDROGUI_Operation::commitOperation();
 }
 
@@ -179,95 +147,176 @@ HYDROGUI_InputPanel* HYDROGUI_CalculationOp::createInputPanel() const
   HYDROGUI_CalculationDlg* aPanel = new HYDROGUI_CalculationDlg( module(), getName() );
 
   // Connect signals and slots
-  connect( aPanel, SIGNAL( SplitZones() ), this, SLOT( onSplitZones() ) );
+  connect( aPanel, SIGNAL( addObjects() ), SLOT( onAddObjects() ) );
+  connect( aPanel, SIGNAL( removeObjects() ), SLOT( onRemoveObjects() ) );
+  connect( aPanel, SIGNAL( splitZones() ), SLOT( onSplitZones() ) );
+  connect( aPanel, SIGNAL( clicked( SUIT_DataObject* ) ), SLOT( onSelected( SUIT_DataObject* ) ) );
+  connect( aPanel, SIGNAL( setMergeType( int, QString ) ), SLOT( onSetMergeType( int, QString ) ) );
 
   return aPanel;
 }
 
-bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
-                                           QString& theErrorMsg )
+void HYDROGUI_CalculationOp::onSetMergeType( int theMergeType, QString theBathymetryName )
 {
   HYDROGUI_CalculationDlg* aPanel = 
     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
-  if ( !aPanel )
-    return false;
-
-  QString anObjectName = aPanel->getObjectName().simplified();
-  if ( anObjectName.isEmpty() )
+  if ( aPanel )
   {
-    theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
-    return false;
+    HYDROGUI_Zone* aZone = aPanel->getCurrentZone();
+    if ( aZone )
+    {
+      aZone->setMergeType( theMergeType, theBathymetryName );
+    }
+    aPanel->refreshZonesBrowser();
   }
+}
 
-  // check that there are no other objects with the same name in the document
-  if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
+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++ )
   {
-    Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
-    if ( !anObject.IsNull() )
+    anEntity = aSeq.Value( anIndex );
+    if ( !anEntity.IsNull() )
     {
-      theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
-      return false;
+      anObject = Handle(HYDROData_Object)::DownCast( anEntity );
+      if( !anObject.IsNull() )
+      {
+        if (myEditedObject->AddGeometryObject( anObject ))
+        {
+          aList.append( anObject->GetName() );
+        }
+      }
     }
   }
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( aPanel )
+  {
+    updateGeomObjectsList( aPanel );
+  }
+}
 
-  Handle(HYDROData_Document) aDocument = doc();
+void HYDROGUI_CalculationOp::onRemoveObjects()
+{
+  // Remove selected objects from the calculation case
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( aPanel )
+  {
+    QStringList aList = aPanel->getSelectedGeomObjects();
+    Handle(HYDROData_Object) anObject;
+    Handle(HYDROData_Entity) anEntity;
+    for (int i = 0; i < aList.length(); i++)
+    {
+      anEntity = HYDROGUI_Tool::FindObjectByName( module(), aList.at(i) );
+      if ( !anEntity.IsNull() )
+      {
+        anObject = Handle(HYDROData_Object)::DownCast( anEntity );
+        if ( !anObject.IsNull() )
+        {
+          myEditedObject->RemoveGeometryObject( anObject );
+        }
+      }
+    }
+    updateGeomObjectsList( aPanel );
+  }
+}
 
-  Handle(HYDROData_Calculation) aCalculObj = myIsEdit ? myEditedObject :
-    Handle(HYDROData_Calculation)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
-  if ( aCalculObj.IsNull() )
+bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
+                                           QString& theErrorMsg )
+{
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( !aPanel )
     return false;
 
-  aCalculObj->SetName( anObjectName );
-
-  QString aPolylineName = aPanel->getPolylineName();
-  Handle(HYDROData_Polyline) aBndPolyline = Handle(HYDROData_Polyline)::DownCast(
-      HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINE ) );
+  //QString anObjectName = aPanel->getObjectName().simplified();
+  //if ( anObjectName.isEmpty() )
+  //{
+  //  theErrorMsg = tr( "INCORRECT_OBJECT_NAME" );
+  //  return false;
+  //}
+
+  //// check that there are no other objects with the same name in the document
+  //if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
+  //{
+  //  Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
+  //  if ( !anObject.IsNull() )
+  //  {
+  //    theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
+  //    return false;
+  //  }
+  //}
+
+  //Handle(HYDROData_Document) aDocument = doc();
+
+  //Handle(HYDROData_CalculationCase) aCalculObj = myIsEdit ? myEditedObject :
+  //  Handle(HYDROData_CalculationCase)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
+  //if ( aCalculObj.IsNull() )
+  //  return false;
+
+  //aCalculObj->SetName( anObjectName );
+
+  //QStringList aRefObjectNames = aPanel->getSelectedGeomObjects();
+  //HYDROData_SequenceOfObjects aGeomObjects = 
+  //  HYDROGUI_Tool::FindObjectsByNames( module(), aRefObjectNames );
 
-  aCalculObj->SetBoundaryPolyline( aBndPolyline );
+  theUpdateFlags = UF_Model;
 
-  QStringList aRefZoneNames = aPanel->getSelectedZones();
-  HYDROData_SequenceOfObjects aRefZones = 
-    HYDROGUI_Tool::FindObjectsByNames( module(), aRefZoneNames, KIND_ZONE );
+  return true;
+}
 
-  aCalculObj->SetZones( aRefZones );
+void HYDROGUI_CalculationOp::onApply()
+{
+  QApplication::setOverrideCursor( Qt::WaitCursor );
 
-  HYDROData_SequenceOfObjects aRegions;
+  int anUpdateFlags = 0;
+  QString anErrorMsg;
 
-  RegionsList::iterator anIter = myRegionsList.begin();
-  for ( ; anIter != myRegionsList.end(); ++anIter )
+  bool aResult = false;
+  
+  try
   {
-    const Region& aRegion = *anIter;
-
-    if ( !aRegion.DataRegion.IsNull() )
-    {
-      //No need to create new but use old zone
-      aRegions.Append( aRegion.DataRegion );
-      continue;
-    }
-
-    Handle(HYDROData_Region) aDataRegion =
-      Handle(HYDROData_Region)::DownCast( aDocument->CreateObject( KIND_REGION ) );
-    if( aDataRegion.IsNull() )
-      continue;
-
-    // Fill the zone data
-    aDataRegion->SetName( aRegion.RegionName );
-    aDataRegion->SetBorderColor( aRegion.BorderColor );
-    aDataRegion->SetFillingColor( aRegion.FillingColor );
-
-    aRegions.Append( aDataRegion );
+    aResult = processApply( anUpdateFlags, anErrorMsg );
   }
+  catch ( Standard_Failure )
+  {
+    Handle(Standard_Failure) aFailure = Standard_Failure::Caught();
+    anErrorMsg = aFailure->GetMessageString();
+    aResult = false;
+  }
+  catch ( ... )
+  {
+    aResult = false;
+  }
+  
+  QApplication::restoreOverrideCursor();
 
-  aCalculObj->SetRegions( aRegions );
-
-  theUpdateFlags = UF_Model;
-
-  return true;
+  if ( aResult )
+  {
+    module()->update( anUpdateFlags );
+    commit();
+  }
+  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::onSplitZones()
 {
-  myRegionsList.clear();
+  //myRegionsList.clear();
 
   HYDROGUI_CalculationDlg* aPanel = 
     ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
@@ -276,42 +325,38 @@ void HYDROGUI_CalculationOp::onSplitZones()
 
   QApplication::setOverrideCursor( Qt::WaitCursor );
 
-  QString aPolylineName = aPanel->getPolylineName();
-  Handle(HYDROData_Polyline) aBndPolyline = Handle(HYDROData_Polyline)::DownCast(
-      HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINE ) );
-
-  QStringList aZoneNames = aPanel->getSelectedZones();
-  HYDROData_SequenceOfObjects aZones = 
-    HYDROGUI_Tool::FindObjectsByNames( module(), aZoneNames, KIND_ZONE );
+  //QStringList aGeomObjectNames = aPanel->getSelectedGeomObjects();
+  //HYDROData_SequenceOfObjects aGeomObjects = 
+  //  HYDROGUI_Tool::FindObjectsByNames( module(), aGeomObjectNames );
 
-  QStringList aResSplittedZones;
+  //QStringList aResSplittedZones;
 
-  HYDROGUI_SplitZonesTool::SplitDataList aSplittedZones =
-    HYDROGUI_SplitZonesTool::SplitZones( aZones, aBndPolyline );
+  //HYDROData_SplitToZonesTool::SplitDataList aSplittedZones =
+  //  HYDROData_SplitToZonesTool::SplitToZones( aGeomObjects );
 
-  QString aSplitZonesPrefix = aPanel->getSplitZonesPrefix();
-  QStringList aUsedNames;
+  //QStringList aUsedNames;
 
-  HYDROGUI_SplitZonesTool::SplitDataListIterator anIter( aSplittedZones );
-  while( anIter.hasNext() )
-  {
-    Region aRegion;
-    aRegion.SplitData = anIter.next();
+  //HYDROData_SplitToZonesTool::SplitDataListIterator anIter( aSplittedZones );
+  //while( anIter.hasNext() )
+  //{
+  //  Region aRegion;
+  //  aRegion.SplitData = anIter.next();
 
-    aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ZoneNames );
-    aRegion.BorderColor  = QColor( HYDROData_Zone::DefaultBorderColor() );
+  //  aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ObjectNames );
+  //  aRegion.BorderColor  = QColor( HYDROData_ImmersibleZone::DefaultBorderColor() );
 
-    aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), aSplitZonesPrefix, aUsedNames );
+  //  aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), "Region", aUsedNames );
 
-    aUsedNames.append( aRegion.RegionName );
+  //  aUsedNames.append( aRegion.RegionName );
 
-    aResSplittedZones.append( aRegion.RegionName );
+  //  aResSplittedZones.append( aRegion.RegionName );
 
-    myRegionsList.append( aRegion );
-  }
+  //  myRegionsList.append( aRegion );
+  //}
+  //
 
-  aPanel->setRegions( aResSplittedZones );
-  
+  myEditedObject->SplitGeometryObjects();
+  aPanel->setEditedObject( myEditedObject );
   createPreview();
 
   QApplication::restoreOverrideCursor();
@@ -356,13 +401,16 @@ void HYDROGUI_CalculationOp::createPreview()
       {
         Region& aRegion = *anIter;
         if ( aRegion.Shape )
+        {
+          aRegion.Shape->erase( false );
           delete aRegion.Shape;
+        }
 
-        aRegion.Shape = new HYDROGUI_Shape( aCtx );
+        aRegion.Shape = new HYDROGUI_Shape( aCtx, NULL );
 
-        aRegion.Shape->setFillingColor( aRegion.FillingColor, false );
-        aRegion.Shape->setBorderColor( aRegion.BorderColor, false );
-        aRegion.Shape->setPath( aRegion.SplitData.Path, true );
+        aRegion.Shape->setFillingColor( aRegion.FillingColor, false, false );
+        aRegion.Shape->setBorderColor( aRegion.BorderColor, false, false );
+        aRegion.Shape->setFace( aRegion.SplitData.Face(), true, false );
       }
 
       //Process the draw events for viewer
@@ -386,6 +434,7 @@ void HYDROGUI_CalculationOp::closePreview()
     Region& aRegion = *anIter;
     if ( aRegion.Shape )
     {
+      aRegion.Shape->erase( false );
       delete aRegion.Shape;
       aRegion.Shape = NULL;
     }