Salome HOME
Modify creation of curves: 1) using QDockWidget instead of QDialog; 2) selection...
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
index 2e36bf50f5f5ab4ea51937c6cfe505af466b4bb0..5d4a465b695d194b9e13b965bfebd34e515d6ae5 100644 (file)
 #include "HYDROGUI_UpdateFlags.h"
 
 #include <HYDROData_Polyline.h>
+#include <HYDROData_Iterator.h>
+#include <HYDROData_Zone.h>
+
+#include <OCCViewer_ViewManager.h>
+#include <OCCViewer_ViewModel.h>
 
 #include <LightApp_Application.h>
 #include <LightApp_UpdateFlags.h>
 
+#include <QApplication>
+
 HYDROGUI_CalculationOp::HYDROGUI_CalculationOp( HYDROGUI_Module* theModule, bool theIsEdit )
 : HYDROGUI_Operation( theModule ),
-  myIsEdit( theIsEdit )
+  myIsEdit( theIsEdit ),
+  myActiveViewManager( NULL ),
+  myPreviewViewManager( NULL )
 {
   setName( myIsEdit ? tr( "EDIT_CALCULATION" ) : tr( "CREATE_CALCULATION" ) );
 }
 
 HYDROGUI_CalculationOp::~HYDROGUI_CalculationOp()
 {
+  closePreview();
 }
 
 void HYDROGUI_CalculationOp::startOperation()
@@ -53,10 +63,13 @@ void HYDROGUI_CalculationOp::startOperation()
   if ( !aPanel )
     return;
 
+  mySplittedZones.clear();
   aPanel->reset();
 
   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
 
+  QStringList aSelectedZones, aSplittedZones;
+
   myEditedObject.Nullify();
   if ( myIsEdit )
   {
@@ -71,25 +84,87 @@ void HYDROGUI_CalculationOp::startOperation()
         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 aSplitZones = myEditedObject->GetSplittedZones();
+      anIter.Init( aSplitZones );
+      for ( ; anIter.More(); anIter.Next() )
+      {
+        Handle(HYDROData_Zone) aSplitZone = 
+          Handle(HYDROData_Zone)::DownCast( anIter.Value() );
+        if ( aSplitZone.IsNull() )
+          continue;
+
+        QString aSplitZoneName = aSplitZone->GetName();
+        if ( aSplitZoneName.isEmpty() )
+          continue;
+
+        aSplittedZones.append( aSplitZoneName );
+      }
     }
   }
 
+  // collect information about existing zones
+  QStringList aZones;
+
+  HYDROData_Iterator anIter( doc(), KIND_ZONE );
+  for ( ; anIter.More(); anIter.Next() )
+  {
+    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 );
+  }
+
   aPanel->setObjectName( anObjectName );
+
+  aPanel->setZones( aZones );
+  aPanel->setSelectedZones( aSelectedZones );
+  aPanel->setSplittedZones( aSplittedZones );
 }
 
 void HYDROGUI_CalculationOp::abortOperation()
 {
+  closePreview();
+
   HYDROGUI_Operation::abortOperation();
 }
 
 void HYDROGUI_CalculationOp::commitOperation()
 {
+  closePreview();
+
   HYDROGUI_Operation::commitOperation();
 }
 
 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() ) );
+
   return aPanel;
 }
 
@@ -109,34 +184,230 @@ bool HYDROGUI_CalculationOp::processApply( int&     theUpdateFlags,
   }
 
   // check that there are no other objects with the same name in the document
-  Handle(HYDROData_Object) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
-  if ( !anObject.IsNull() )
+  if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
   {
-    theErrorMsg = tr( "OBJECT_EXISTS_IN_DOCUMENT" ).arg( anObjectName );
-    return false;
+    Handle(HYDROData_Object) 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_Calculation) aCalculObj = myIsEdit ? myEditedObject :
-    Handle(HYDROData_Calculation)::DownCast( doc()->CreateObject( KIND_CALCULATION ) );
+    Handle(HYDROData_Calculation)::DownCast( aDocument->CreateObject( KIND_CALCULATION ) );
   if ( aCalculObj.IsNull() )
     return false;
 
   aCalculObj->SetName( anObjectName );
 
-  Handle(HYDROData_Polyline) aBndPolyline;
-
   QString aPolylineName = aPanel->getPolylineName();
-  if ( !aPolylineName.isEmpty() )
-  {
-    aBndPolyline = Handle(HYDROData_Polyline)::DownCast(
+  Handle(HYDROData_Polyline) aBndPolyline = Handle(HYDROData_Polyline)::DownCast(
       HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINE ) );
-  }
 
   aCalculObj->SetBoundaryPolyline( aBndPolyline );
 
+  QStringList aRefZoneNames = aPanel->getSelectedZones();
+  HYDROData_SequenceOfObjects aRefZones = 
+    HYDROGUI_Tool::FindObjectsByNames( module(), aRefZoneNames, KIND_ZONE );
+
+  aCalculObj->SetZones( aRefZones );
+
+  HYDROData_SequenceOfObjects aSplittedZones;
+
+  SplittedZonesList::iterator anIter = mySplittedZones.begin();
+  for ( ; anIter != mySplittedZones.end(); ++anIter )
+  {
+    const SplittedZone& aSplittedZone = *anIter;
+
+    Handle(HYDROData_Polyline) aPolyline =
+      Handle(HYDROData_Polyline)::DownCast( aDocument->CreateObject( KIND_POLYLINE ) );
+    Handle(HYDROData_Zone) aDtaZone =
+      Handle(HYDROData_Zone)::DownCast( aDocument->CreateObject( KIND_ZONE ) );
+
+    if( aPolyline.IsNull() || aDtaZone.IsNull() )
+      continue;
+
+    // Fill the polyline data
+    aPolyline->SetName( aSplittedZone.PolylineName );
+    aPolyline->setDimension( 2 );
+
+    QList<PolylineSection> aPolylineData;
+    for( int i = 0, n = aSplittedZone.SplitData.Path.elementCount(); i < n; i++ )
+    {
+      const QPainterPath::Element anElement = aSplittedZone.SplitData.Path.elementAt( i );
+      switch( anElement.type )
+      {
+        case QPainterPath::MoveToElement:
+          aPolylineData.append( PolylineSection() );
+          break;
+        case QPainterPath::LineToElement:
+          if( !aPolylineData.isEmpty() )
+          {
+            PolylineSection& aSection = aPolylineData.last();
+            aSection.myCoords << anElement.x;
+            aSection.myCoords << anElement.y;
+          }
+          break;
+        case QPainterPath::CurveToElement: // currently not supported
+        default:
+          break;
+      }
+    }
+    aPolyline->setPolylineData( aPolylineData );
+
+    // Fill the zone data
+    aDtaZone->SetName( aSplittedZone.ZoneName );
+    aDtaZone->SetPolyline( aPolyline );
+    aDtaZone->SetBorderColor( aSplittedZone.BorderColor );
+    aDtaZone->SetFillingColor( aSplittedZone.FillingColor );
+
+    aSplittedZones.Append( aDtaZone );
+  }
+
+  aCalculObj->SetSplittedZones( aSplittedZones );
+
   theUpdateFlags = UF_Model;
 
   return true;
 }
 
+void HYDROGUI_CalculationOp::onSplitZones()
+{
+  mySplittedZones.clear();
+
+  HYDROGUI_CalculationDlg* aPanel = 
+    ::qobject_cast<HYDROGUI_CalculationDlg*>( inputPanel() );
+  if ( !aPanel )
+    return;
+
+  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 aResSplittedZones;
+
+  HYDROGUI_SplitZonesTool::SplitDataList aSplittedZones =
+    HYDROGUI_SplitZonesTool::SplitZones( aZones, aBndPolyline );
+
+  QString aSplitZonesPrefix = aPanel->getSplitZonesPrefix();
+  QStringList aUsedNames;
+
+  HYDROGUI_SplitZonesTool::SplitDataListIterator anIter( aSplittedZones );
+  while( anIter.hasNext() )
+  {
+    SplittedZone aSplittedZone;
+    aSplittedZone.SplitData = anIter.next();
+
+    aSplittedZone.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aSplittedZone.SplitData.ZoneNames );
+    aSplittedZone.BorderColor  = QColor( HYDROData_Zone::DefaultBorderColor() );
+
+    aSplittedZone.ZoneName = HYDROGUI_Tool::GenerateObjectName( module(), aSplitZonesPrefix + "Zone", aUsedNames );
+    aSplittedZone.PolylineName = HYDROGUI_Tool::GenerateObjectName( module(), aSplitZonesPrefix + "Poly", aUsedNames );
+
+    aUsedNames.append( aSplittedZone.ZoneName );
+    aUsedNames.append( aSplittedZone.PolylineName );
+
+    aResSplittedZones.append( aSplittedZone.ZoneName );
+
+    mySplittedZones.append( aSplittedZone );
+  }
+
+  aPanel->setSplittedZones( aResSplittedZones );
+  
+  createPreview();
+
+  QApplication::restoreOverrideCursor();
+}
+
+void HYDROGUI_CalculationOp::createPreview()
+{
+  LightApp_Application* anApp = module()->getApp();
+
+  if ( !myActiveViewManager )
+  {
+    myActiveViewManager = anApp->activeViewManager();
+  }
+
+  if ( !myPreviewViewManager )
+  {
+    myPreviewViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
+      anApp->createViewManager( OCCViewer_Viewer::Type() ) );
+    if ( myPreviewViewManager )
+    {
+      connect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
+               this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
+
+      module()->setViewManagerRole( myPreviewViewManager, HYDROGUI_Module::VMR_PreviewCaseZones );
+      myPreviewViewManager->setTitle( tr( "PREVIEW_CASE_ZONES" ) );
+    }
+  }
+
+  if ( !myPreviewViewManager )
+    return;
+
+  if ( OCCViewer_Viewer* aViewer = myPreviewViewManager->getOCCViewer() )
+  {
+    Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
+    if ( !aCtx.IsNull() )
+    {
+      SplittedZonesList::iterator anIter = mySplittedZones.begin();
+      for ( ; anIter != mySplittedZones.end(); ++anIter )
+      {
+        SplittedZone& aSplittedZone = *anIter;
+        if ( aSplittedZone.Shape )
+          delete aSplittedZone.Shape;
+
+        aSplittedZone.Shape = new HYDROGUI_Shape( aCtx );
+
+        aSplittedZone.Shape->setFillingColor( aSplittedZone.FillingColor, false );
+        aSplittedZone.Shape->setBorderColor( aSplittedZone.BorderColor, false );
+        aSplittedZone.Shape->setPath( aSplittedZone.SplitData.Path, true );
+      }
+    }
+  }
+}
+
+void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
+{
+  closePreview();
+}
+
+void HYDROGUI_CalculationOp::closePreview()
+{
+  SplittedZonesList::iterator anIter= mySplittedZones.begin();
+  for ( ; anIter != mySplittedZones.end(); ++anIter )
+  {
+    SplittedZone& aSplittedZone = *anIter;
+    if ( aSplittedZone.Shape )
+    {
+      delete aSplittedZone.Shape;
+      aSplittedZone.Shape = NULL;
+    }
+  }
+
+  if( myPreviewViewManager )
+  {
+    disconnect( myPreviewViewManager, SIGNAL( lastViewClosed( SUIT_ViewManager* ) ),
+                this, SLOT( onLastViewClosed( SUIT_ViewManager* ) ) );
+
+    module()->getApp()->removeViewManager( myPreviewViewManager ); // myPreviewViewManager is deleted here
+    myPreviewViewManager = NULL;
+  }
+
+  if( myActiveViewManager )
+  {
+    HYDROGUI_Tool::SetActiveViewManager( module(), myActiveViewManager );
+    myActiveViewManager = NULL;
+  }
+}
+