Salome HOME
merge trunk on BR_quadtree 20140820
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_ChannelOp.cxx
index fe4153171d39ec37f91261cc31417c2488687514..e820deb641e97d42beba677505cbd82b080385ca 100644 (file)
@@ -23,6 +23,7 @@
 #include "HYDROGUI_ChannelOp.h"
 
 #include "HYDROGUI_DataModel.h"
+#include <HYDROGUI_DataObject.h>
 #include "HYDROGUI_ChannelDlg.h"
 #include "HYDROGUI_Module.h"
 #include "HYDROGUI_Shape.h"
@@ -59,41 +60,42 @@ void HYDROGUI_ChannelOp::startOperation()
 {
   HYDROGUI_Operation::startOperation();
 
-  // We start operation in the document
-  startDocOperation();
-
   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
 
   aPanel->blockSignals( true );
 
   aPanel->reset();
 
-  myEditedObject = getObjectToEdit();
+  myEditedObject.Nullify();
 
   QString aSelectedGuideLine, aSelectedProfile;
 
   QString anObjectName = HYDROGUI_Tool::GenerateObjectName( module(), tr( "DEFAULT_CHANNEL_NAME" ) );
-  if ( myIsEdit && !myEditedObject.IsNull() )
+  if ( myIsEdit )
   {
-    anObjectName = myEditedObject->GetName();
+    myEditedObject =
+      Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
+    if ( !myEditedObject.IsNull() )
+    {
+      anObjectName = myEditedObject->GetName();
 
-    Handle(HYDROData_Polyline3D) aRefGuideLine = myEditedObject->GetGuideLine();
-    if ( !aRefGuideLine.IsNull() )
-      aSelectedGuideLine = aRefGuideLine->GetName();
+      Handle(HYDROData_Polyline3D) aRefGuideLine = myEditedObject->GetGuideLine();
+      if ( !aRefGuideLine.IsNull() )
+        aSelectedGuideLine = aRefGuideLine->GetName();
 
-    Handle(HYDROData_Profile) aRefProfile = myEditedObject->GetProfile();
-    if ( !aRefProfile.IsNull() )
-      aSelectedProfile = aRefProfile->GetName();
+      Handle(HYDROData_Profile) aRefProfile = myEditedObject->GetProfile();
+      if ( !aRefProfile.IsNull() )
+        aSelectedProfile = aRefProfile->GetName();
+    }
   }
 
   // collect information about existing 3d polylines
   QStringList aGuideLines;
-  QStringList anAllGuideLines = HYDROGUI_Tool::FindExistingObjectsNames( doc(), KIND_POLYLINE );
-  foreach( QString aGuideLine, anAllGuideLines )
+  HYDROData_Iterator aPolylinesIt( doc(), KIND_POLYLINE );
+  for ( ; aPolylinesIt.More(); aPolylinesIt.Next() )
   {
-    Handle( HYDROData_Polyline3D ) aPolyline3d = 
-      Handle( HYDROData_Polyline3D )::DownCast( 
-    HYDROGUI_Tool::FindObjectByName( module(), aGuideLine, KIND_POLYLINE ) );
+    Handle(HYDROData_Polyline3D) aPolyline3d = 
+      Handle(HYDROData_Polyline3D)::DownCast( aPolylinesIt.Current() );
     if( !aPolyline3d.IsNull() )
     {
       TopoDS_Shape aShape = aPolyline3d->GetShape3D();
@@ -101,7 +103,7 @@ void HYDROGUI_ChannelOp::startOperation()
       {
         TopoDS_Wire aWire = TopoDS::Wire( aShape );
         if( !aWire.Closed() )
-          aGuideLines.append( aGuideLine );
+          aGuideLines.append( aPolyline3d->GetName() );
       }
     }
   }
@@ -119,22 +121,18 @@ void HYDROGUI_ChannelOp::startOperation()
 
   aPanel->blockSignals( false );
 
-  onCreatePreview( true );
+  onCreatePreview();
 }
 
-
 void HYDROGUI_ChannelOp::abortOperation()
 {
   erasePreview();
-  abortDocOperation();
-
   HYDROGUI_Operation::abortOperation();
 }
 
 void HYDROGUI_ChannelOp::commitOperation()
 {
   erasePreview();
-
   HYDROGUI_Operation::commitOperation();
 }
 
@@ -146,7 +144,8 @@ HYDROGUI_InputPanel* HYDROGUI_ChannelOp::createInputPanel() const
 }
 
 bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
-                                       QString& theErrorMsg )
+                                       QString& theErrorMsg,
+                                       QStringList& theBrowseObjectsEntries )
 {
   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
   if ( !aPanel )
@@ -159,7 +158,7 @@ bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
     return false;
   }
 
-  if( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
+  if ( !myIsEdit || ( !myEditedObject.IsNull() && myEditedObject->GetName() != anObjectName ) )
   {
     // check that there are no other objects with the same name in the document
     Handle(HYDROData_Entity) anObject = HYDROGUI_Tool::FindObjectByName( module(), anObjectName );
@@ -170,21 +169,47 @@ bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
     }
   }
 
+  
   if ( myEditedObject.IsNull() )
-    return false;
+    myEditedObject = createNewObject(); // Create new data model object
 
   myEditedObject->SetName( anObjectName );
 
   if ( !myIsEdit )
   {
-    myEditedObject->SetFillingColor( HYDROData_Channel::DefaultFillingColor() );
-    myEditedObject->SetBorderColor( HYDROData_Channel::DefaultBorderColor() );
+    myEditedObject->SetFillingColor( getDefaultFillingColor() );
+    myEditedObject->SetBorderColor( getDefaultBorderColor() );
   }
 
+  QString aGuideLineName = aPanel->getGuideLineName();
+  QString aProfileName = aPanel->getProfileName();
+  if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
+  {
+    myEditedObject->RemoveGuideLine();
+    myEditedObject->RemoveProfile();
+  }
+  else
+  {
+    Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
+      HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
+    myEditedObject->SetGuideLine( aGuideLine );
+
+    Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
+      HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
+    myEditedObject->SetProfile( aProfile );
+  }
+
+  if ( myEditedObject->IsMustBeUpdated() )
+    myEditedObject->Update();
+
   erasePreview();
 
   if( !myIsEdit )
+  {
     module()->setObjectVisible( HYDROGUI_Tool::GetActiveOCCViewId( module() ), myEditedObject, true );
+    QString anEntry = HYDROGUI_DataObject::dataObjectEntry( myEditedObject );
+    theBrowseObjectsEntries.append( anEntry );
+  }
 
   module()->setIsToUpdate( myEditedObject );
 
@@ -193,53 +218,42 @@ bool HYDROGUI_ChannelOp::processApply( int& theUpdateFlags,
   return true;
 }
 
-Handle(HYDROData_Channel) HYDROGUI_ChannelOp::getObjectToEdit() const
+Handle(HYDROData_Channel) HYDROGUI_ChannelOp::createNewObject() const
+{
+  return Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
+}
+
+QColor HYDROGUI_ChannelOp::getDefaultFillingColor() const
 {
-  return myIsEdit ? Handle(HYDROData_Channel)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) ) :
-                    Handle(HYDROData_Channel)::DownCast( doc()->CreateObject( KIND_CHANNEL ) );
+  return HYDROData_Channel::DefaultFillingColor();
 }
 
-void HYDROGUI_ChannelOp::onCreatePreview( const bool theIsInit )
+QColor HYDROGUI_ChannelOp::getDefaultBorderColor() const
+{
+  return HYDROData_Channel::DefaultBorderColor();
+}
+
+void HYDROGUI_ChannelOp::onCreatePreview()
 {
   HYDROGUI_ChannelDlg* aPanel = ::qobject_cast<HYDROGUI_ChannelDlg*>( inputPanel() );
-  if ( !aPanel || myEditedObject.IsNull() )
+  if ( !aPanel )
     return;
 
   QString aGuideLineName = aPanel->getGuideLineName();
   QString aProfileName = aPanel->getProfileName();
   if ( aGuideLineName.isEmpty() || aProfileName.isEmpty() )
   {
-    if ( !theIsInit )
-    {
-      myEditedObject->RemoveGuideLine();
-      myEditedObject->RemoveProfile();
-      myEditedObject->Update();
-    }
-
     erasePreview();
     return;
   }
 
-  // Update channel data
-  if ( !theIsInit )
-  {
-    Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
-      HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
-    myEditedObject->SetGuideLine( aGuideLine );
-
-    Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
-      HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
-    myEditedObject->SetProfile( aProfile );
-
-    if ( myEditedObject->IsMustBeUpdated() )
-      myEditedObject->Update();
-  }
-
   LightApp_Application* anApp = module()->getApp();
   
   if ( !getPreviewManager() )
+  {
     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
                        anApp->getViewManager( OCCViewer_Viewer::Type(), true ) ) );
+  }
 
   OCCViewer_ViewManager* aViewManager = getPreviewManager();
   if ( aViewManager && !myPreviewPrs )
@@ -248,14 +262,40 @@ void HYDROGUI_ChannelOp::onCreatePreview( const bool theIsInit )
     {
       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
       if ( !aCtx.IsNull() )
-        myPreviewPrs = new HYDROGUI_Shape( aCtx, myEditedObject, getPreviewZLayer() );
+      {
+        myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
+
+        QColor aFillingColor = getDefaultFillingColor();
+        QColor aBorderColor = getDefaultBorderColor();
+        if ( !myEditedObject.IsNull() )
+        {
+          aFillingColor = myEditedObject->GetFillingColor();
+          aBorderColor = myEditedObject->GetBorderColor();
+        }
+
+        myPreviewPrs->setFillingColor( aFillingColor, false, false );
+        myPreviewPrs->setBorderColor( aBorderColor, false, false );
+      }
     }
   }
 
   if ( !aViewManager || !myPreviewPrs )
     return;
 
-  myPreviewPrs->update( true );
+  Handle(HYDROData_Polyline3D) aGuideLine = Handle(HYDROData_Polyline3D)::DownCast(
+    HYDROGUI_Tool::FindObjectByName( module(), aGuideLineName, KIND_POLYLINE ) );
+
+  Handle(HYDROData_Profile) aProfile = Handle(HYDROData_Profile)::DownCast(
+    HYDROGUI_Tool::FindObjectByName( module(), aProfileName, KIND_PROFILE ) );
+
+  HYDROData_Channel::PrsDefinition aPrsDef;
+  if ( !HYDROData_Channel::CreatePresentations( aGuideLine, aProfile, aPrsDef ) )
+  {
+    erasePreview();
+    return;
+  }
+
+  myPreviewPrs->setShape( aPrsDef.myPrs2D );
 }
 
 void HYDROGUI_ChannelOp::erasePreview()