Salome HOME
Add tests for drag and drop algo.
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_StreamOp.cxx
index f23b1c03a614b67907772463e8d9081efa6a05c6..b85cfb80a3f283f0a9029555f5cfe3dbcad33d8a 100755 (executable)
@@ -55,7 +55,6 @@
 HYDROGUI_StreamOp::HYDROGUI_StreamOp( HYDROGUI_Module* theModule, bool theIsEdit )
 : HYDROGUI_Operation( theModule ), 
   myIsEdit( theIsEdit ),
-  myViewManager( NULL ),
   myPreviewPrs( NULL )
 {
   setName( theIsEdit ? tr( "EDIT_STREAM" ) : tr( "CREATE_STREAM" ) );
@@ -80,6 +79,9 @@ void HYDROGUI_StreamOp::startOperation()
   // Get/create the edited object
   if( myIsEdit ) {
     myEditedObject = Handle(HYDROData_Stream)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
+    if ( !myEditedObject.IsNull() && myEditedObject->IsMustBeUpdated() ) {
+      myEditedObject->Update();
+    }
   } else {
     myEditedObject = Handle(HYDROData_Stream)::DownCast( doc()->CreateObject( KIND_STREAM ) );
   }
@@ -174,6 +176,12 @@ bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
   // Set the object name
   myEditedObject->SetName( anObjectName );
 
+  if ( !myIsEdit )
+  {
+    myEditedObject->SetFillingColor( HYDROData_Stream::DefaultFillingColor() );
+    myEditedObject->SetBorderColor( HYDROData_Stream::DefaultBorderColor() );
+  }
+
   // Erase preview
   erasePreview();
 
@@ -182,8 +190,10 @@ bool HYDROGUI_StreamOp::processApply( int& theUpdateFlags,
     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
   }
 
+  module()->setIsToUpdate( myEditedObject );
+
   // Set update flags
-  theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced;
+  theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
 
   return true;
 }
@@ -195,22 +205,22 @@ void HYDROGUI_StreamOp::createPreview()
   }
 
   LightApp_Application* anApp = module()->getApp();
-  if ( !myViewManager ) {
-    myViewManager = ::qobject_cast<OCCViewer_ViewManager*>( 
-      anApp->getViewManager( OCCViewer_Viewer::Type(), true ) );
-  }
+  if ( !getPreviewManager() )
+    setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
+                       anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
 
-  if ( myViewManager && !myPreviewPrs ) {
-    if ( OCCViewer_Viewer* aViewer = myViewManager->getOCCViewer() ) {
+  OCCViewer_ViewManager* aViewManager = getPreviewManager();
+  if ( aViewManager && !myPreviewPrs ) {
+    if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
       if ( !aCtx.IsNull() ) {
-        myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject );
+        myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject, getPreviewZLayer() );
       }
     }
   }
 
   if ( myPreviewPrs ) {
-    myPreviewPrs->update();  
+    myPreviewPrs->update( true );
   }
 }
 
@@ -245,13 +255,13 @@ void HYDROGUI_StreamOp::onAddProfiles()
   QStringList anInvalidProfiles, anExistingProfiles, aHasNoIntersectionProfiles;
 
   HYDROData_SequenceOfObjects aVerifiedProfiles;
-  HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() );
-  gp_Ax2 aX2(gp::XOY());
-  gp_Ax3 aX3(aX2);
-  gp_Pln aPln(aX3);
-  BRepBuilderAPI_MakeFace aMkr(aPln);
-  if(!aMkr.IsDone())   return;
-  const TopoDS_Face& aPlane = TopoDS::Face(aMkr.Shape());
+  HYDROData_SequenceOfObjects aSelectedProfiles = HYDROGUI_Tool::GetSelectedObjects( module() ); 
+  Handle(HYDROData_PolylineXY) aHydAxis = myEditedObject->GetHydraulicAxis();
+  if ( aHydAxis.IsNull() )
+    return; 
+  TopoDS_Face aPlane;
+  if(!myEditedObject->BuildFace(aHydAxis, aPlane))
+    return;
   Standard_Real aPar(.0);
   for( int i = 1, n = aSelectedProfiles.Length(); i <= n; i++ ) {
     Handle(HYDROData_Profile) aProfile = 
@@ -298,7 +308,7 @@ void HYDROGUI_StreamOp::onAddProfiles()
       Handle(HYDROData_Profile)::DownCast( aVerifiedProfiles.Value( i ) );
     myEditedObject->AddProfile( aProfile );
   }
-  myEditedObject->Update();
+  myEditedObject->UpdatePrs();
 
   // Update the panel
   updatePanelData();
@@ -313,19 +323,39 @@ void HYDROGUI_StreamOp::onRemoveProfiles( const QStringList& theProfilesToRemove
     return;
   }
 
+  bool isRemoved = false;
+
+  // Take the Object Browser selection into account
+  HYDROData_SequenceOfObjects aSelectedObjects = HYDROGUI_Tool::GetSelectedObjects( module() ); 
+  for( int i = 1, n = aSelectedObjects.Length(); i <= n; i++ ) {
+    Handle(HYDROData_Profile) aProfile = 
+      Handle(HYDROData_Profile)::DownCast( aSelectedObjects.Value( i ) );
+    if ( !aProfile.IsNull() && !theProfilesToRemove.contains(aProfile->GetName()) ) {
+      if ( myEditedObject->RemoveProfile( aProfile ) ) {
+        isRemoved = true;
+      }
+    }
+  }
+
   // Remove profiles
   foreach( const QString& aProfileName, theProfilesToRemove ) {
     Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
       HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
-    myEditedObject->RemoveProfile( aProfile );
+    if ( myEditedObject->RemoveProfile( aProfile ) ) {
+      isRemoved = true;
+    }
   }
-  myEditedObject->Update();
 
-  // Update the panel
-  updatePanelData();
+  if ( isRemoved ) {
+    // Update the edited stream object
+    myEditedObject->UpdatePrs();
 
-  // Update preview
-  createPreview();
+    // Update the panel
+    updatePanelData();
+
+    // Update preview
+    createPreview();
+  }
 }
 
 void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
@@ -338,6 +368,19 @@ void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
   Handle(HYDROData_PolylineXY) anAxis = Handle(HYDROData_PolylineXY)::DownCast(
     HYDROGUI_Tool::FindObjectByName( module(), theNewAxis, KIND_POLYLINEXY ) );
 
+  // Prepare data for intersection check
+  TopoDS_Face aPlane;
+  if ( !myEditedObject->BuildFace(anAxis, aPlane) ) {
+    SUIT_MessageBox::critical( module()->getApp()->desktop(), 
+                              tr( "BAD_SELECTED_POLYLINE_TLT" ),
+                              tr( "BAD_SELECTED_POLYLINE_MSG" ).arg( theNewAxis ) );
+    // To restore the old axis
+    updatePanelData();
+    return;
+  }
+
+  Standard_Real aPar(.0);
+
   // Get list of profiles which do not intersect the axis
   QStringList aHasNoIntersectionProfiles;
   HYDROData_SequenceOfObjects aCurrentProfiles = myEditedObject->GetProfiles();
@@ -345,7 +388,9 @@ void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
     Handle(HYDROData_Profile) aProfile =
       Handle(HYDROData_Profile)::DownCast( aCurrentProfiles.Value( anIndex ) );
     if ( !aProfile.IsNull() ) {
-      // TODO check intersection
+      if ( !HYDROData_Stream::HasIntersection( anAxis, aProfile, aPlane, aPar ) ) {
+        aHasNoIntersectionProfiles << aProfile->GetName();
+      }
     }
   }
 
@@ -370,7 +415,7 @@ void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
   } else {
     // Set axis
     myEditedObject->SetHydraulicAxis( anAxis );
-    myEditedObject->Update();
+    myEditedObject->UpdatePrs();
 
     // Update the panel
     updatePanelData();
@@ -383,7 +428,7 @@ void HYDROGUI_StreamOp::onAxisChanged( const QString& theNewAxis )
 void HYDROGUI_StreamOp::updatePanelData()
 {
   HYDROGUI_StreamDlg* aPanel = ::qobject_cast<HYDROGUI_StreamDlg*>( inputPanel() );
-  if ( !aPanel ) {
+  if ( !aPanel || myEditedObject.IsNull() ) {
     return;
   }