Salome HOME
patch on regression in land cover map coloring and transparency
[modules/hydro.git] / src / HYDROGUI / HYDROGUI_LandCoverMapOp.cxx
index 5e2c0b9e5705d41e573604f35d1cc276ac04dd94..cb073ade21cd7d91db68e65683ac16b88f1179f9 100644 (file)
 #include "HYDROGUI_Tool.h"
 #include "HYDROGUI_UpdateFlags.h"
 #include "HYDROGUI_DataObject.h"
+#include "HYDROGUI_ShapeLandCoverMap.h"
+#include "HYDROGUI_OCCSelector.h"
 
 #include <HYDROData_Iterator.h>
 #include <HYDROData_StricklerTable.h>
 #include <HYDROData_PolylineXY.h>
 #include <HYDROData_Object.h>
 
+#include <OCCViewer_ViewManager.h>
+#include <OCCViewer_ViewModel.h>
+#include <OCCViewer_ViewWindow.h>
+#include <OCCViewer_ViewPort3d.h>
+
+#include <LightApp_Application.h>
+#include <LightApp_SelectionMgr.h>
+#include <SUIT_ViewWindow.h>
+
+#include <TopoDS.hxx>
 #include <TopoDS_Face.hxx>
 #include <TopTools_ListOfShape.hxx>
 #include <TopTools_ListIteratorOfListOfShape.hxx>
+#include <AIS_Shape.hxx>
+
+#include <QApplication>
+#include <QMouseEvent>
 
 HYDROGUI_LandCoverMapOp::HYDROGUI_LandCoverMapOp( HYDROGUI_Module* theModule, const int theOperationId )
 : HYDROGUI_Operation( theModule ),
@@ -86,28 +102,47 @@ void HYDROGUI_LandCoverMapOp::startOperation()
   {
     if ( isApplyAndClose() )
       myEditedObject = Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::GetSelectedObject( module() ) );
-    if ( !myEditedObject.IsNull() )
+    
+    // Construct a list of names of all land cover map objects defined within the data model
+    QStringList aLandCoverMapNames;
+    HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
+    for( ; anIterator.More(); anIterator.Next() )
     {
-      anObjectName = myEditedObject->GetName();
+      Handle(HYDROData_LandCoverMap) aLandCoverObj =
+        Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );      
+      if ( !aLandCoverObj.IsNull() )
+        aLandCoverMapNames.append( aLandCoverObj->GetName() );
+    }
+    
+    //aLandCoverMapNames.sort();
+    aPanel->setObjectNames( aLandCoverMapNames );
 
-      // Construct a list of names of all land cover map objects defined within the data model
-      QStringList aLandCoverMapNames;
-      HYDROData_Iterator anIterator( doc(), KIND_LAND_COVER_MAP );
-      for( ; anIterator.More(); anIterator.Next() )
+    if ( myEditedObject.IsNull() )
+    {
+      if ( !aLandCoverMapNames.empty() )
       {
-        Handle(HYDROData_LandCoverMap) aLandCoverObj =
-          Handle(HYDROData_LandCoverMap)::DownCast( anIterator.Current() );    
-        if ( !aLandCoverObj.IsNull() )
-          aLandCoverMapNames.append( aLandCoverObj->GetName() );
+        anObjectName = aLandCoverMapNames.first();
+        if ( !anObjectName.isEmpty())
+        {
+          Handle(HYDROData_LandCoverMap) anObject =
+            Handle(HYDROData_LandCoverMap)::DownCast( HYDROGUI_Tool::FindObjectByName( module(), anObjectName ) );
+          if( !anObject.IsNull() )
+            myEditedObject = anObject;
+        }
       }
-
-      //aLandCoverMapNames.sort();
-      aPanel->setObjectNames( aLandCoverMapNames );
     }
+    else
+      anObjectName = myEditedObject->GetName();
   }  
   aPanel->setObjectName( anObjectName );
-  
+
+  closePreview();
+  if ( myOperationId != CreateLandCoverMapId )
+    onCreatePreview();
+
   aPanel->blockSignals( false );
+
+  module()->update( UF_OCCViewer | UF_FitAll );
 }
 
 void HYDROGUI_LandCoverMapOp::abortOperation()
@@ -115,6 +150,8 @@ void HYDROGUI_LandCoverMapOp::abortOperation()
   closePreview();
 
   HYDROGUI_Operation::abortOperation();
+
+  module()->update( UF_OCCViewer | UF_FitAll );
 }
 
 void HYDROGUI_LandCoverMapOp::commitOperation()
@@ -122,6 +159,8 @@ void HYDROGUI_LandCoverMapOp::commitOperation()
   closePreview();
 
   HYDROGUI_Operation::commitOperation();
+
+  module()->update( UF_OCCViewer | UF_FitAll );
 }
 
 HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
@@ -129,12 +168,6 @@ HYDROGUI_InputPanel* HYDROGUI_LandCoverMapOp::createInputPanel() const
   HYDROGUI_LandCoverMapDlg* aPanel = new HYDROGUI_LandCoverMapDlg( module(), getName(), myOperationId );
   connect( aPanel, SIGNAL( landCoverMapChanged( const QString& ) ),
            this, SLOT( onLandCoverMapChanged( const QString& ) ) );
-  connect( aPanel, SIGNAL( CreatePreview( const QStringList& ) ),
-           this,   SLOT( onCreatePreview( const QStringList& ) ) );
-  /*
-  connect( aPanel, SIGNAL( addPolylines() ), SLOT( onAddPolylines() ) );
-  connect( aPanel, SIGNAL( removePolylines() ), SLOT( onRemovePolylines() ) );
-  */
   return aPanel;
 }
 
@@ -187,15 +220,20 @@ bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
             myOperationId == MergeLandCoverId || 
             myOperationId == ChangeLandCoverTypeId )
   {
-    // TODO:
-    //Fill in aFacesSelectedInViewer list
+    if ( myPreviewPrs )
+    {
+      // Fill in aFacesSelectedInViewer list
+      Handle(AIS_InteractiveContext) aCtx;
+      getSelectedShapes( aFacesSelectedInViewer, aCtx );
+    }
   }
 
   // Get selected Strickler type
   QString aSelectedStricklerType;
   if ( myOperationId == CreateLandCoverMapId || 
        myOperationId == AddLandCoverId || 
-       myOperationId == MergeLandCoverId )
+       myOperationId == MergeLandCoverId ||
+       myOperationId == ChangeLandCoverTypeId )
   {
     aSelectedStricklerType = aPanel->getSelectedStricklerTypeName();
     if ( aSelectedStricklerType.isEmpty() )
@@ -208,6 +246,11 @@ bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
   // Create / find the new / edited land cover map object
   Handle(HYDROData_LandCoverMap) aLandCoverMapObj = myOperationId != CreateLandCoverMapId ? myEditedObject :
     Handle(HYDROData_LandCoverMap)::DownCast( doc()->CreateObject( KIND_LAND_COVER_MAP ) );
+  if ( aLandCoverMapObj.IsNull() )
+  {
+    theErrorMsg = tr( "LAND_COVER_MAP_UNDEFINED" );
+    return false;
+  }
 
   // Set land cover map name
   aLandCoverMapObj->SetName( anObjectName );
@@ -250,10 +293,20 @@ bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
       aLandCoverSplitted = aLandCoverMapObj->Split( aPolyline );
     else if ( !aFace.IsNull() )
     {
-      // TODO:
-      //Get the complete boundary of the object face as the splitting polyline
-      Handle(HYDROData_PolylineXY) aBoundaryPolyline;
-      aLandCoverSplitted = aLandCoverMapObj->Split( aBoundaryPolyline );
+      // Get the complete boundary of the object face as the splitting polyline
+      QList<TopoDS_Shape> aBoundShapes;
+      QStringList aBoundNames;
+      aFace->GetBoundaries( aBoundShapes, aBoundNames );
+
+      for( int i=0, n=aBoundShapes.size(); i<n; i++ )
+      {
+        TopoDS_Shape aShape = aBoundShapes[i];
+        if( aShape.IsNull() )
+          continue;
+
+        bool aSplitResult = aLandCoverMapObj->Split( aShape );
+        aLandCoverSplitted = ( i==0 ? aSplitResult : aLandCoverSplitted && aSplitResult );
+      }
     }
     if ( !aLandCoverSplitted )
     {
@@ -276,13 +329,22 @@ bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
       }
     }
   }
-    
-  /*if ( myOperationId == CreateLandCoverMapId )
-  {    
-    aLandCoverMapObj->SetFillingColor( aLandCoverMapObj->DefaultFillingColor() );
-    aLandCoverMapObj->SetBorderColor( aLandCoverMapObj->DefaultBorderColor() );
-  }*/
 
+  // Change Strickler type for land cover(s) inside edited land cover map
+  if ( myOperationId == ChangeLandCoverTypeId )
+  {
+    bool aLandCoverChangeType = false;
+    if ( !aFacesSelectedInViewer.IsEmpty() )
+    {
+      aLandCoverChangeType = aLandCoverMapObj->ChangeType( aFacesSelectedInViewer, aSelectedStricklerType );
+      if ( !aLandCoverChangeType )
+      {
+        theErrorMsg = tr( "LAND_COVER_TYPE_NOT_CHANGED" );
+        return false;
+      }
+    }
+  }
+    
   // Update land cover map object and close preview
   aLandCoverMapObj->Update();
 
@@ -302,6 +364,9 @@ bool HYDROGUI_LandCoverMapOp::processApply( int& theUpdateFlags,
 
   theUpdateFlags = UF_Model | UF_OCCViewer | UF_OCC_Forced | UF_VTKViewer;
 
+  if ( myOperationId == CreateLandCoverMapId )
+    module()->enableLCMActions();
+
   return true;
 }
 
@@ -314,35 +379,20 @@ void HYDROGUI_LandCoverMapOp::onLandCoverMapChanged( const QString& theName )
     if ( !myEditedObject.IsNull() )
     {
       // Show preview of the newly selected land cover map
-      QStringList aPolylineFaceNames;
-      onCreatePreview( aPolylineFaceNames );
+      closePreview();
+      onCreatePreview();
     }
   }
 }
 
-void HYDROGUI_LandCoverMapOp::onCreatePreview( const QStringList& thePolylineFaceNames )
+void HYDROGUI_LandCoverMapOp::onCreatePreview()
 {
-  /*
-  HYDROGUI_LandCoverDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverDlg*>( inputPanel() );
+  HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
   if ( !aPanel )
     return;
 
   QApplication::setOverrideCursor( Qt::WaitCursor );  
 
-  HYDROData_SequenceOfObjects aZonePolylines;
-  QStringList::const_iterator anIt = thePolylineNames.begin(), aLast = thePolylineNames.end();
-  for( ; anIt!=aLast; anIt++ )
-  {
-    QString aPolylineName = *anIt;
-    Handle(HYDROData_PolylineXY) aPolyline = Handle(HYDROData_PolylineXY)::DownCast(
-      HYDROGUI_Tool::FindObjectByName( module(), aPolylineName, KIND_POLYLINEXY ) );
-    if ( !aPolyline.IsNull() )
-      aZonePolylines.Append( aPolyline );
-  }
-  
-  TCollection_AsciiString anError;
-  TopoDS_Shape aZoneShape = HYDROData_LandCover::buildShape( aZonePolylines, anError );  
-
   LightApp_Application* anApp = module()->getApp();
   if ( !getPreviewManager() )
     setPreviewManager( ::qobject_cast<OCCViewer_ViewManager*>( 
@@ -354,28 +404,151 @@ void HYDROGUI_LandCoverMapOp::onCreatePreview( const QStringList& thePolylineFac
     {
       Handle(AIS_InteractiveContext) aCtx = aViewer->getAISContext();
       if ( !aCtx.IsNull() )
-        myPreviewPrs = new HYDROGUI_Shape( aCtx, NULL, getPreviewZLayer() );
+      {
+        disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
+          aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
+        disconnect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
+          aViewer, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));        
+
+        connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
+            this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
+        connect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
+          this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
+
+        LightApp_SelectionMgr* aSelectionMgr = module()->getApp()->selectionMgr();
+        if( aSelectionMgr )
+        {
+          QList<SUIT_Selector*> aSelectorList;
+          aSelectionMgr->selectors( aViewManager->getType(), aSelectorList );
+          QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
+          for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
+          {
+            HYDROGUI_OCCSelector* aHydroSelector = dynamic_cast<HYDROGUI_OCCSelector*>( *anIter );
+            if ( aHydroSelector )
+            {
+              disconnect( aHydroSelector->viewer(), SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
+              connect( this, SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
+            }
+          }
+        }
+        
+        connect( this, SIGNAL( selectionChanged() ), this, SLOT( onViewerSelectionChanged() ) );
+        myPreviewPrs = new HYDROGUI_ShapeLandCoverMap( module()->getOCCDisplayer(), aCtx, myEditedObject, getPreviewZLayer()/*, theIsScalarMapMode*/ );
+      }
+    }
+  }
+
+  if ( aViewManager && myPreviewPrs && !myEditedObject.IsNull() )
+  {
+    myPreviewPrs->update( false, false );      
+    if ( myOperationId == RemoveLandCoverId ||
+         myOperationId == MergeLandCoverId ||
+         myOperationId == ChangeLandCoverTypeId )
+      myPreviewPrs->setSelectionMode( AIS_Shape::SelectionMode( TopAbs_FACE ) ); 
+  }
+  
+  module()->update( UF_OCCViewer | UF_FitAll );
+
+  QApplication::restoreOverrideCursor();  
+}
+
+void HYDROGUI_LandCoverMapOp::onViewerSelectionChanged()
+{
+  HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
+  if ( !aPanel )
+    return;
+
+  Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
+  if ( !aCtx.IsNull() )
+  {
+    int aNbSelected = aCtx->NbSelected();
+
+    if ( myOperationId == RemoveLandCoverId || myOperationId == ChangeLandCoverTypeId )
+      // Enable Apply, Apply and Close buttons only if at least one face (land cover) is selected in the 3d viewer
+      aPanel->setApplyEnabled( aNbSelected > 0 );
+    else if ( myOperationId == MergeLandCoverId )
+      // Enable Apply, Apply and Close buttons only if at least two faces (land covers) are selected in the 3d viewer
+      aPanel->setApplyEnabled( aNbSelected > 1 );
+
+    if ( myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
+    {
+      if ( aNbSelected == 1 && !myEditedObject.IsNull() )
+      {
+        TopTools_ListOfShape aFacesSelectedInViewer;
+        getSelectedShapes( aFacesSelectedInViewer, aCtx );
+        if ( aFacesSelectedInViewer.Extent() == 1 )
+        {
+          QString aType = myEditedObject->StricklerType( TopoDS::Face( aFacesSelectedInViewer.First() ) );
+          if ( !aType.isEmpty() )
+            aPanel->setSelectedStricklerTypeName( aType );
+        }
+      }
     }
   }
+}
+
+void HYDROGUI_LandCoverMapOp::onMousePress(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
+{
+  myStartPnt.setX(theEvent->x()); myStartPnt.setY(theEvent->y());
+}
+
+void HYDROGUI_LandCoverMapOp::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* theEvent)
+{
+  if (theEvent->button() != Qt::LeftButton) return;
+  if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
+
+  OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
+  if (!aView )
+    return;
+
+  OCCViewer_ViewManager* aViewManager = getPreviewManager();
+  if ( !aViewManager )
+    return;
+  
+  OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer();
+  if ( !aViewer )
+    return;
+
+  Handle(AIS_InteractiveContext) aCtx = getInteractiveContext();
+  if ( aCtx.IsNull() )
+    return;
 
-  if ( aViewManager && myPreviewPrs )
+  myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
+
+  if (myStartPnt == myEndPnt)
   {
-    QColor aFillingColor = Qt::magenta;
-    QColor aBorderColor = Qt::transparent;
-    if ( !myEditedObject.IsNull() ) {
-      aFillingColor = myEditedObject->GetFillingColor();
-      aBorderColor = myEditedObject->GetBorderColor();
+    if ( !aViewer->isPreselectionEnabled() ) {
+      Handle(V3d_View) aView3d = aView->getViewPort()->getView();
+      if ( !aView3d.IsNull() ) {
+        aCtx->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
+      }
     }
 
-    myPreviewPrs->setFillingColor( aFillingColor, false, false );
-    myPreviewPrs->setBorderColor( aBorderColor, false, false );
+    Handle(StdSelect_ViewerSelector3d) aMainSelector = aCtx->MainSelector();
+    if ( aMainSelector.IsNull() )
+      return;
+    const Standard_Integer aDetectedNb = aMainSelector->NbPicked();
+    if ( aDetectedNb == 0 )
+    {
+      aCtx->ClearSelected( false );
+      emit deselection();
+    }
 
-    if( !aZoneShape.IsNull() )
-      myPreviewPrs->setShape( aZoneShape );
+    for (Standard_Integer aDetIter = 1; aDetIter <= aDetectedNb; ++aDetIter)
+    {
+      Handle(SelectMgr_EntityOwner) anOwner = aMainSelector->Picked (aDetIter);
+      aCtx->AddOrRemoveSelected( anOwner, Standard_False );
+    }
+  }
+  else
+  {
+    aCtx->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
+                      myEndPnt.x(), myEndPnt.y(),
+                      aView->getViewPort()->getView(), Standard_False );
   }
 
-  QApplication::restoreOverrideCursor();
-  */
+  aCtx->UpdateCurrentViewer();
+  emit selectionChanged();  
 }
 
 void HYDROGUI_LandCoverMapOp::closePreview()
@@ -385,4 +558,75 @@ void HYDROGUI_LandCoverMapOp::closePreview()
     delete myPreviewPrs;
     myPreviewPrs = 0;
   }
+
+  HYDROGUI_LandCoverMapDlg* aPanel = ::qobject_cast<HYDROGUI_LandCoverMapDlg*>( inputPanel() );
+  if ( !aPanel )
+    return;
+
+  if ( myOperationId == RemoveLandCoverId || myOperationId == MergeLandCoverId || myOperationId == ChangeLandCoverTypeId )
+    aPanel->setApplyEnabled( false );
+
+  OCCViewer_ViewManager* aViewManager = getPreviewManager();
+  if ( aViewManager )
+  {
+    if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() )
+    {
+      disconnect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
+        this, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
+      disconnect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)),
+        this, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));
+      connect(aViewManager, SIGNAL(mousePress(SUIT_ViewWindow*, QMouseEvent*)), 
+        aViewer, SLOT(onMousePress(SUIT_ViewWindow*, QMouseEvent*)));
+      connect(aViewManager, SIGNAL(mouseRelease(SUIT_ViewWindow*, QMouseEvent*)), 
+        aViewer, SLOT(onMouseRelease(SUIT_ViewWindow*, QMouseEvent*)));  
+
+      LightApp_SelectionMgr* aSelectionMgr = module()->getApp()->selectionMgr();
+      if( aSelectionMgr )
+      {
+        QList<SUIT_Selector*> aSelectorList;
+        aSelectionMgr->selectors( aViewManager->getType(), aSelectorList );
+        QList<SUIT_Selector*>::iterator anIter, anIterEnd = aSelectorList.end();
+        for( anIter = aSelectorList.begin(); anIter != anIterEnd; anIter++ )
+        {
+          HYDROGUI_OCCSelector* aHydroSelector = dynamic_cast<HYDROGUI_OCCSelector*>( *anIter );
+          if ( aHydroSelector )
+          {
+            disconnect( this, SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );
+            connect( aHydroSelector->viewer(), SIGNAL( deselection() ), aHydroSelector, SLOT( onDeselection() ) );            
+          }
+        }
+      }
+    }
+  }
+}
+
+Handle(AIS_InteractiveContext) HYDROGUI_LandCoverMapOp::getInteractiveContext()
+{
+  OCCViewer_ViewManager* aViewManager = getPreviewManager();
+  Handle(AIS_InteractiveContext) aCtx = NULL;
+  if ( aViewManager ) {
+    if ( OCCViewer_Viewer* aViewer = aViewManager->getOCCViewer() ) {
+      aCtx = aViewer->getAISContext();
+    }
+  }
+  return aCtx;
+}
+
+void HYDROGUI_LandCoverMapOp::getSelectedShapes( TopTools_ListOfShape& theSelectedShapes,
+                                                 Handle(AIS_InteractiveContext)& theCtx )
+{
+  if ( theCtx.IsNull() )
+    theCtx = getInteractiveContext();
+
+  if ( !theCtx.IsNull() && theCtx->NbSelected() > 0 )
+  {
+    for ( theCtx->InitSelected(); theCtx->MoreSelected(); theCtx->NextSelected() )
+    {
+      TopoDS_Shape aSelectedShape = theCtx->SelectedShape();
+      if ( aSelectedShape.IsNull() )
+        continue;
+
+      theSelectedShapes.Append( aSelectedShape );
+    }
+  }
 }