Salome HOME
bos #18968 [CEA] Sometimes when clicking to add a face to a group two faces are added
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewModel.cxx
index 9de784810e69250fd77e09f0b2327e36216d7cd9..eb7ff1564746fbd1d0a4dcbc30827d83f24ee52a 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2016  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2020  CEA/DEN, EDF R&D, OPEN CASCADE
 //
 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
@@ -21,8 +21,6 @@
 //
 
 #include "OCCViewer_ViewModel.h"
-#include "OCCViewer.h"
-#include "OCCViewer_ViewWindow.h"
 #include "OCCViewer_ViewFrame.h"
 #include "OCCViewer_VService.h"
 #include "OCCViewer_ViewPort3d.h"
@@ -54,6 +52,7 @@
 #include <AIS_Axis.hxx>
 #include <Prs3d_Drawer.hxx>
 #include <AIS_ListIteratorOfListOfInteractive.hxx>
+#include <StdSelect_ViewerSelector3d.hxx>
 
 #include <Graphic3d_Texture2Dmanual.hxx>
 #include <Graphic3d_MaterialAspect.hxx>
 #include <V3d_DirectionalLight.hxx>
 #include <V3d_AmbientLight.hxx>
 
+#include <Basics_OCCTVersion.hxx>
+
+namespace
+{
+  void setCappingColor(const Handle(Graphic3d_ClipPlane)& plane, const QColor& color)
+  {
+    Quantity_Color qcolor( color.redF(), color.greenF(), color.blueF(), Quantity_TOC_RGB );
+#if OCC_VERSION_LARGE < 0x07040000
+    Graphic3d_MaterialAspect aspect;
+    aspect.SetColor( qcolor );
+    plane->SetCappingMaterial( aspect );
+#else
+    plane->SetCappingColor( qcolor );
+#endif
+  }
+}
+
 /*!
   Get data for supported background modes: gradient types, identifiers and supported image formats
 */
@@ -110,8 +126,11 @@ OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
   myIsRelative(true),
   myTopLayerId( 0 ),
   myTrihedronSize(100),
+#if OCC_VERSION_LARGE <= 0x07030000
+  myIsUseLocalSelection(false),
+#endif
   myClippingDlg (NULL),
-  myIsUseLocalSelection(false)
+  myFitter(0)
 {
   // init CasCade viewers
   myV3dViewer = OCCViewer_VService::CreateViewer( TCollection_ExtendedString("Viewer3d").ToExtString() );
@@ -122,7 +141,7 @@ OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
   myAISContext = new AIS_InteractiveContext( myV3dViewer );
   myAISContext->HighlightStyle(Prs3d_TypeOfHighlight_LocalSelected)->SetColor( Quantity_NOC_WHITE );
   myAISContext->HighlightStyle(Prs3d_TypeOfHighlight_Selected)->SetColor( Quantity_NOC_WHITE );
-  
+
   // display isoline on planar faces (box for ex.)
   myAISContext->IsoOnPlane( true );
 
@@ -166,6 +185,7 @@ OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
 
   // set projection type to orthographic
   myProjectionType = 0;
+  mySelectionStyle = OCCViewer_ViewWindow::RectStyle;
   // set stereo parameters
   myStereoType = 0;
   myAnaglyphFilter = 0;
@@ -240,6 +260,7 @@ void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
     view->initSketchers();
     view->setInteractionStyle( interactionStyle() );
     view->setProjectionType( projectionType() );
+    view->setSelectionStyle( selectionStyle() );
     view->setStereoType( stereoType() );
     view->setAnaglyphFilter( anaglyphFilter() );
     view->setStereographicFocus( stereographicFocusType(), stereographicFocusValue() );
@@ -352,17 +373,16 @@ void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* t
   myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
   bool aHasShift = (theEvent->modifiers() & Qt::ShiftModifier);
   
-  if (!aHasShift) {
-    myAISContext->ClearCurrents( false );
-    emit deselection();
-  }
-
   if (myStartPnt == myEndPnt)
   {
+    if (!aHasShift) {
+      myAISContext->ClearCurrents( false );
+      emit deselection();
+    }
     if ( !isPreselectionEnabled() ) {
       Handle(V3d_View) aView3d = aView->getViewPort()->getView();
       if ( !aView3d.IsNull() ) {
-       myAISContext->MoveTo( myEndPnt.x(), myEndPnt.y(), aView3d, Standard_True );
+             myAISContext->MoveTo( myEndPnt.x(), myEndPnt.y(), aView3d, Standard_True );
       }
     }
 
@@ -370,33 +390,34 @@ void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* t
       myAISContext->ShiftSelect( Standard_True );
     else 
       myAISContext->Select( Standard_True );
+    emit selectionChanged();
   }
-  else
-  {
-    if (aHasShift && myMultiSelectionEnabled)
-      myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
-                                myEndPnt.x(), myEndPnt.y(),
-                                aView->getViewPort()->getView(), Standard_False );
-    else
-      myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
-                           myEndPnt.x(), myEndPnt.y(),
-                           aView->getViewPort()->getView(), Standard_False );
 
-    int Nb = myAISContext->NbSelected();
-    if( Nb>1 && !myMultiSelectionEnabled )
-    {
-        myAISContext->InitSelected();
-        Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
-        if( !anOwner.IsNull() )
-        {
-            myAISContext->ClearSelected( Standard_False );
-            myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
-        }
-    }
-
-    myAISContext->UpdateCurrentViewer();
-  }
-  emit selectionChanged();
+  //else
+  //{
+  //  if (aHasShift && myMultiSelectionEnabled)
+  //    myAISContext->ShiftSelect(myStartPnt.x(), myStartPnt.y(),
+  //                              myEndPnt.x(), myEndPnt.y(),
+  //                              aView->getViewPort()->getView(), Standard_False );
+  //  else
+  //    myAISContext->Select(myStartPnt.x(), myStartPnt.y(),
+  //                         myEndPnt.x(), myEndPnt.y(),
+  //                         aView->getViewPort()->getView(), Standard_False );
+
+  //  int Nb = myAISContext->NbSelected();
+  //  if( Nb>1 && !myMultiSelectionEnabled )
+  //  {
+  //      myAISContext->InitSelected();
+  //      Handle( SelectMgr_EntityOwner ) anOwner = myAISContext->SelectedOwner();
+  //      if( !anOwner.IsNull() )
+  //      {
+  //          myAISContext->ClearSelected( Standard_False );
+  //          myAISContext->AddOrRemoveSelected( anOwner, Standard_False );
+  //      }
+  //  }
+
+  //  myAISContext->UpdateCurrentViewer();
+  //}
 }
 
 /*!
@@ -435,13 +456,17 @@ void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEven
     break;
   case  Qt::Key_N:
     if ( isPreselectionEnabled() ) {
+#if OCC_VERSION_LARGE <= 0x07030000
       if ( useLocalSelection() )
+#endif
        getAISContext()->HilightNextDetected( aView->getViewPort()->getView() );
     }
     break;
   case  Qt::Key_P:
     if ( isPreselectionEnabled() ) {
-      if ( getAISContext()->HasOpenedContext() )
+#if OCC_VERSION_LARGE <= 0x07030000
+      if ( useLocalSelection() )
+#endif
        getAISContext()->HilightPreviousDetected( aView->getViewPort()->getView() );
     }
     break;
@@ -537,6 +562,31 @@ void OCCViewer_Viewer::setProjectionType( const int theType )
   }
 }
 
+
+OCCViewer_ViewWindow::SelectionStyle OCCViewer_Viewer::selectionStyle() const
+{
+  return mySelectionStyle;
+}
+
+void OCCViewer_Viewer::setSelectionStyle(OCCViewer_ViewWindow::SelectionStyle theMode)
+{
+  if (mySelectionStyle != theMode) {
+    mySelectionStyle = theMode;
+    if (!myViewManager)
+      return;
+
+    QVector<SUIT_ViewWindow*> wins = myViewManager->getViews();
+    for (int i = 0; i < (int)wins.count(); i++)
+    {
+      OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>(wins.at(i));
+      if (win)
+        win->setSelectionStyle(theMode);
+    }
+  }
+}
+
+
+
 /*!
   \return stereo type
 */
@@ -879,12 +929,8 @@ void OCCViewer_Viewer::setClippingColor( const QColor& theColor )
   if( myInternalClipPlanes.IsEmpty() )
     return;
 
-  Graphic3d_MaterialAspect aMaterialAspect = Graphic3d_MaterialAspect();
-  aMaterialAspect.SetColor( Quantity_Color( theColor.redF(), theColor.greenF(),
-                                            theColor.blueF(), Quantity_TOC_RGB ) );
-
-  for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
-    myInternalClipPlanes.Value(i)->SetCappingMaterial( aMaterialAspect );
+  for ( Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt ( myInternalClipPlanes ); aPlaneIt.More(); aPlaneIt.Next() )
+    setCappingColor( aPlaneIt.Value(), theColor );
 
   update();
 }
@@ -934,9 +980,13 @@ void OCCViewer_Viewer::setClippingTextureParams( const bool isDefault, const QSt
   Handle(Graphic3d_Texture2Dmanual) aTexture =
     initClippingTexture( myDefaultTextureUsed, myClippingTexture,
                          myTextureModulated, myClippingTextureScale );
-
+#if OCC_VERSION_LARGE <= 0x07030000
   for( int i = 1; i <= myInternalClipPlanes.Size(); i++ )
     myInternalClipPlanes.Value(i)->SetCappingTexture( aTexture );
+#else
+  for ( Graphic3d_SequenceOfHClipPlane::Iterator aPlaneIt ( myInternalClipPlanes ); aPlaneIt.More(); aPlaneIt.Next() )
+    aPlaneIt.Value()->SetCappingTexture( aTexture );
+#endif
 
   update();
 }
@@ -1099,11 +1149,14 @@ void OCCViewer_Viewer::setDefaultLights()
   double aDz = SUIT_Session::session()->resourceMgr()->doubleValue( "OCCViewer", "light_dz", -1.0 );
 
   Handle(V3d_DirectionalLight) aLight =
-    new V3d_DirectionalLight( myV3dViewer, V3d_Zneg, OCCViewer::color( aColor ).Name(), Standard_True );
+    new V3d_DirectionalLight( V3d_Zneg, OCCViewer::color( aColor ).Name(), Standard_True );
+  myV3dViewer->AddLight( aLight );
   if( !( aDx == 0 && aDy == 0 && aDz == 0 ) )
     aLight->SetDirection( aDx, aDy, aDz );
   myV3dViewer->SetLightOn( aLight );
-  myV3dViewer->SetLightOn( new V3d_AmbientLight( myV3dViewer ) );
+  Handle(V3d_AmbientLight) ambLight = new V3d_AmbientLight();
+  myV3dViewer->AddLight( ambLight );
+  myV3dViewer->SetLightOn( ambLight );
 }
 
 /*!
@@ -1115,19 +1168,23 @@ void OCCViewer_Viewer::setDefaultLights()
 bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
                                   bool hilight, bool update )
 {
-  bool isInLocal = myAISContext->HasOpenedContext();
-  if( !obj.IsNull() )
-    if( !isInLocal )
-    {
-      if ( hilight && !myAISContext->IsSelected( obj ) )
-        myAISContext->AddOrRemoveCurrentObject( obj, false );
-      else if ( !hilight && myAISContext->IsSelected( obj ) )
-        myAISContext->AddOrRemoveCurrentObject( obj, false );
-    }
+  if( !obj.IsNull() ) {
+#if OCC_VERSION_LARGE <= 0x07030000
+    if( !myAISContext->HasOpenedContext() )
+      {
+#endif
+       if ( hilight && !myAISContext->IsSelected( obj ) )
+         myAISContext->AddOrRemoveSelected( obj, false );
+       else if ( !hilight && myAISContext->IsSelected( obj ) )
+         myAISContext->AddOrRemoveSelected( obj, false );
+#if OCC_VERSION_LARGE <= 0x07030000
+      }
+#endif
+  }
 
   if ( update )
     myV3dViewer->Redraw();
-    
+  
   return false;
 }
 
@@ -1137,20 +1194,23 @@ bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
 */
 bool OCCViewer_Viewer::unHighlightAll( bool updateviewer, bool unselect )
 {
+#if OCC_VERSION_LARGE <= 0x07030000
   if ( myAISContext->HasOpenedContext() ) {
+#endif
     if ( unselect ) {
       myAISContext->ClearSelected( updateviewer );
     } else {
       myAISContext->UnhilightSelected( updateviewer );
     }
+#if OCC_VERSION_LARGE <= 0x07030000
   } else {
-    if ( unselect ) {
+   if ( unselect ) {
       myAISContext->ClearCurrents( updateviewer );
     } else {
       myAISContext->UnhilightCurrents( updateviewer );
     }
   }
-
+#endif
   return false;
 }
 
@@ -1358,6 +1418,7 @@ OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
   return new OCCViewer_ViewWindow(0,  this);
 }
 
+#if OCC_VERSION_LARGE <= 0x07030000
 /*!
   Sets using local selection state
   \param theIsUseLocalSelection - state
@@ -1374,10 +1435,10 @@ bool OCCViewer_Viewer::useLocalSelection() const
 {
   if (myIsUseLocalSelection)
     return true;
-
   Handle(AIS_InteractiveContext) ic = getAISContext();
   return !ic.IsNull() && ic->HasOpenedContext();
 }
+#endif
 
 // obsolete  
 QColor OCCViewer_Viewer::backgroundColor( int theViewId ) const
@@ -1524,10 +1585,7 @@ Handle(Graphic3d_ClipPlane) OCCViewer_Viewer::createClipPlane(const gp_Pln& theP
   aGraphic3dPlane->SetCapping( Standard_True );
 
   // set capping color
-  Graphic3d_MaterialAspect aMaterialAspect = Graphic3d_MaterialAspect();
-  aMaterialAspect.SetColor( Quantity_Color( myClippingColor.redF(), myClippingColor.greenF(),
-                                            myClippingColor.blueF(), Quantity_TOC_RGB ) );
-  aGraphic3dPlane->SetCappingMaterial( aMaterialAspect );
+  setCappingColor( aGraphic3dPlane, myClippingColor );
 
   // set capping texture
   aGraphic3dPlane->SetCappingTexture( initClippingTexture( myDefaultTextureUsed, myClippingTexture,