Salome HOME
Selector implementation for OCC viewer.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_CalculationOp.cxx
index 25aac2717abbc34b2da26389c67bd370c649fda6..fd48bc169f2a73f5f80a6feb9beaa1e4c6394b30 100644 (file)
 #include "HYDROGUI_Tool.h"
 #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 <OCCViewer_ViewWindow.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()
@@ -51,34 +64,122 @@ void HYDROGUI_CalculationOp::startOperation()
   if ( !aPanel )
     return;
 
+  myRegionsList.clear();
   aPanel->reset();
 
   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), "Case" );
 
+  QStringList aSelectedZones, aRegions;
+
   myEditedObject.Nullify();
   if ( myIsEdit )
   {
     myEditedObject = Handle(HYDROData_Calculation)::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.Shape = aDataRegion->Face();
+
+        aRegion.FillingColor = aDataRegion->GetFillingColor();
+        aRegion.BorderColor  = aDataRegion->GetBorderColor();
+
+        aRegion.RegionName = aRegionName;
+
+        aRegion.DataRegion = aDataRegion;
+
+        myRegionsList.append( aRegion );
+
+        aRegions.append( aRegionName );
+      }
+    }
+  }
+
+  // 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->setRegions( aRegions );
+
+  createPreview();
 }
 
 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;
 }
 
@@ -98,23 +199,217 @@ 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 );
 
+  QString aPolylineName = aPanel->getPolylineName();
+  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 aRegions;
+
+  RegionsList::iterator anIter = myRegionsList.begin();
+  for ( ; anIter != myRegionsList.end(); ++anIter )
+  {
+    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->SetFace( aRegion.SplitData.Face() );
+
+    aDataRegion->SetName( aRegion.RegionName );
+    aDataRegion->SetBorderColor( aRegion.BorderColor );
+    aDataRegion->SetFillingColor( aRegion.FillingColor );
+
+    aRegions.Append( aDataRegion );
+  }
+
+  aCalculObj->SetRegions( aRegions );
+
   theUpdateFlags = UF_Model;
 
   return true;
 }
 
+void HYDROGUI_CalculationOp::onSplitZones()
+{
+  myRegionsList.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() )
+  {
+    Region aRegion;
+    aRegion.SplitData = anIter.next();
+
+    aRegion.FillingColor = HYDROGUI_Tool::GenerateFillingColor( module(), aRegion.SplitData.ZoneNames );
+    aRegion.BorderColor  = QColor( HYDROData_Zone::DefaultBorderColor() );
+
+    aRegion.RegionName = HYDROGUI_Tool::GenerateObjectName( module(), aSplitZonesPrefix, aUsedNames );
+
+    aUsedNames.append( aRegion.RegionName );
+
+    aResSplittedZones.append( aRegion.RegionName );
+
+    myRegionsList.append( aRegion );
+  }
+
+  aPanel->setRegions( aResSplittedZones );
+  
+  createPreview();
+
+  QApplication::restoreOverrideCursor();
+}
+
+void HYDROGUI_CalculationOp::createPreview()
+{
+  LightApp_Application* anApp = module()->getApp();
+
+  if ( !myActiveViewManager )
+  {
+    if ( myRegionsList.isEmpty() )
+      return;
+
+    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() )
+    {
+      RegionsList::iterator anIter = myRegionsList.begin();
+      for ( ; anIter != myRegionsList.end(); ++anIter )
+      {
+        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 );
+      }
+
+      //Process the draw events for viewer
+      QApplication::processEvents();
+      if ( OCCViewer_ViewWindow* vw = (OCCViewer_ViewWindow*)myPreviewViewManager->getActiveView() )
+        vw->onTopView();
+    }
+  }
+}
+
+void HYDROGUI_CalculationOp::onLastViewClosed( SUIT_ViewManager* theViewManager )
+{
+  closePreview();
+}
+
+void HYDROGUI_CalculationOp::closePreview()
+{
+  RegionsList::iterator anIter= myRegionsList.begin();
+  for ( ; anIter != myRegionsList.end(); ++anIter )
+  {
+    Region& aRegion = *anIter;
+    if ( aRegion.Shape )
+    {
+      aRegion.Shape->erase( false );
+      delete aRegion.Shape;
+      aRegion.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;
+  }
+}
+