Salome HOME
Merge branch 'ASL_TREE_STATE' into asl/hydro_porting_741
[modules/gui.git] / src / OCCViewer / OCCViewer_ViewModel.cxx
index a4f2bb86d3b361ce5b14b26e0d8ca79c05696dce..366d3bb0cc53954d530318213857a9872fda4120 100755 (executable)
@@ -1,4 +1,4 @@
-// Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
+// Copyright (C) 2007-2014  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
@@ -6,7 +6,7 @@
 // This library is free software; you can redistribute it and/or
 // modify it under the terms of the GNU Lesser General Public
 // License as published by the Free Software Foundation; either
-// version 2.1 of the License.
+// version 2.1 of the License, or (at your option) any later version.
 //
 // This library is distributed in the hope that it will be useful,
 // but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -25,6 +25,7 @@
 #include "OCCViewer_ViewFrame.h"
 #include "OCCViewer_VService.h"
 #include "OCCViewer_ViewPort3d.h"
+#include "OCCViewer_ClippingDlg.h"
 
 #include "SUIT_ViewWindow.h"
 #include "SUIT_ViewManager.h"
@@ -32,6 +33,8 @@
 #include "SUIT_Session.h"
 #include "SUIT_ResourceMgr.h"
 
+#include "ViewerData_AISShape.hxx"
+
 #include "QtxActionToolMgr.h"
 #include "QtxBackgroundTool.h"
 
 #include <Prs3d_Drawer.hxx>
 #include <Prs3d_DatumAspect.hxx>
 #include <Prs3d_LineAspect.hxx>
-#include <Prs3d_LengthAspect.hxx>
-#include <Prs3d_AngleAspect.hxx>
 #include <Prs3d_TextAspect.hxx>
 
 #include <Visual3d_View.hxx>
 
-#include <Basics_OCCTVersion.hxx>
 
 // VSR: Uncomment below line to allow texture background support in OCC viewer
 #define OCC_ENABLE_TEXTURED_BACKGROUND
@@ -96,37 +96,33 @@ OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
   myBackgrounds(4, Qtx::BackgroundData( Qt::black )),
   myIsRelative(true),
   myTopLayerId( 0 ),
-  myTrihedronSize(100)
+  myTrihedronSize(100),
+  myClippingDlg (NULL)
 {
   // init CasCade viewers
-  myV3dViewer = OCCViewer_VService::Viewer3d( "", (short*) "Viewer3d", "", 1000.,
-                                              V3d_XposYnegZpos, true, true );
-
-  myV3dViewer->Init();
-
-  myV3dCollector = OCCViewer_VService::Viewer3d( "", (short*) "Collector3d", "", 1000.,
-                                                 V3d_XposYnegZpos, true, true );
-  myV3dCollector->Init();
+  myV3dViewer = OCCViewer_VService::CreateViewer( TCollection_ExtendedString("Viewer3d").ToExtString() );
+  //myV3dViewer->Init(); // to avoid creation of the useless perspective view (see OCCT issue 0024267)
+  myV3dViewer->SetDefaultLights();
+
+#if OCC_VERSION_LARGE <= 0x06060000 // Porting to OCCT higher 6.6.0 version
+  myV3dCollector = OCCViewer_VService::CreateViewer( TCollection_ExtendedString("Collector3d").ToExtString() );
+  //myV3dCollector->Init(); // to avoid creation of the useless perspective view (see OCCT issue 0024267)
+  myV3dCollector->SetDefaultLights();
+#endif
 
   // init selector
-  myAISContext = new AIS_InteractiveContext( myV3dViewer, myV3dCollector);
-
+#if OCC_VERSION_LARGE <= 0x06060000 
+  myAISContext = new AIS_InteractiveContext( myV3dViewer, myV3dCollector );
+#else
+  myAISContext = new AIS_InteractiveContext( myV3dViewer );
+#endif
   myAISContext->SelectionColor( Quantity_NOC_WHITE );
   
   // display isoline on planar faces (box for ex.)
   myAISContext->IsoOnPlane( true );
-
-  double h = QApplication::desktop()->screenGeometry( QApplication::desktop()->primaryScreen() ).height() / 300. ;
-  Handle(Prs3d_Drawer) drawer = myAISContext->DefaultDrawer();
-  Handle(Prs3d_TextAspect) ta = drawer->TextAspect();
-  ta->SetHeight(100); // VSR: workaround for CAS.CADE bug (is it really needed ???)
-  ta->SetHeight(h);
-  drawer->SetTextAspect(ta);
-  drawer->AngleAspect()->SetTextAspect(ta);
-  drawer->LengthAspect()->SetTextAspect(ta);
   
   /* create trihedron */
-  if( DisplayTrihedron )
+  if ( DisplayTrihedron )
   {
     Handle(Geom_Axis2Placement) anAxis = new Geom_Axis2Placement(gp::XOY());
     myTrihedron = new AIS_Trihedron(anAxis);
@@ -138,15 +134,12 @@ OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
     myTrihedron->SetSize(100);
     Handle(AIS_Drawer) drawer = myTrihedron->Attributes();
     if (drawer->HasDatumAspect()) {
-        Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
-        daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
-        daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
-        daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
-    }
-
-    myAISContext->Display(myTrihedron);
-    myAISContext->Deactivate(myTrihedron);
+      Handle(Prs3d_DatumAspect) daspect = drawer->DatumAspect();
+      daspect->FirstAxisAspect()->SetColor(Quantity_Color(1.0, 0.0, 0.0, Quantity_TOC_RGB));
+      daspect->SecondAxisAspect()->SetColor(Quantity_Color(0.0, 1.0, 0.0, Quantity_TOC_RGB));
+      daspect->ThirdAxisAspect()->SetColor(Quantity_Color(0.0, 0.0, 1.0, Quantity_TOC_RGB));
     }
+  }
 
   // set interaction style to standard
   myInteractionStyle = 0;
@@ -154,14 +147,12 @@ OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
   // set zooming style to standard
   myZoomingStyle = 0;
 
+  // preselection
+  myPreselectionEnabled = true;
+
   // selection
   mySelectionEnabled = true;
   myMultiSelectionEnabled = true;
-  
-  
-  SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
-  if(resMgr)
-    myShowStaticTrihedron = resMgr->booleanValue( "OCCViewer", "show_static_trihedron", true );
 }
 
 /*!
@@ -169,6 +160,11 @@ OCCViewer_Viewer::OCCViewer_Viewer( bool DisplayTrihedron)
 */
 OCCViewer_Viewer::~OCCViewer_Viewer() 
 {
+  myAISContext.Nullify();
+  myV3dViewer.Nullify();
+#if OCC_VERSION_LARGE <= 0x06060000
+  myV3dCollector.Nullify();
+#endif
 }
 
 /*!
@@ -217,11 +213,16 @@ void OCCViewer_Viewer::initView( OCCViewer_ViewWindow* view )
     view->initSketchers();
     view->setInteractionStyle( interactionStyle() );
     view->setZoomingStyle( zoomingStyle() );
+    view->enablePreselection( isPreselectionEnabled() );
+    view->enableSelection( isSelectionEnabled() );
     
     OCCViewer_ViewPort3d* vp3d = view->getViewPort();
     if ( vp3d )
     {
       vp3d->getView()->SetSurfaceDetail(V3d_TEX_ALL);
+      // connect signal from viewport
+      connect(vp3d, SIGNAL(vpClosed(OCCViewer_ViewPort3d*)), this, SLOT(onViewClosed(OCCViewer_ViewPort3d*)));
+      connect(vp3d, SIGNAL(vpMapped(OCCViewer_ViewPort3d*)), this, SLOT(onViewMapped(OCCViewer_ViewPort3d*)));
     }
   }
 }
@@ -240,6 +241,7 @@ SUIT_ViewWindow* OCCViewer_Viewer::createView( SUIT_Desktop* theDesktop )
   initView( vw );
   // set default background for view window
   vw->setBackground( background(0) ); // 0 means MAIN_VIEW (other views are not yet created here)
+
   return view;
 }
 
@@ -283,7 +285,9 @@ void OCCViewer_Viewer::onMouseMove(SUIT_ViewWindow* theWindow, QMouseEvent* theE
 
   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
 
-  if ( isSelectionEnabled() ) {
+  myCurPnt.setX(theEvent->x()); myCurPnt.setY(theEvent->y());
+
+  if ( isSelectionEnabled() && isPreselectionEnabled() ) {
     if (aView->getViewPort()->isBusy()) {
       QCoreApplication::processEvents();
       return; // Check that the ViewPort initialization completed
@@ -308,7 +312,7 @@ void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* t
   if (!theWindow->inherits("OCCViewer_ViewWindow")) return;
 
   OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*) theWindow;
-  if (!aView || aView->interactionStyle() != SUIT_ViewModel::STANDARD)
+  if (!aView )
     return;
 
   myEndPnt.setX(theEvent->x()); myEndPnt.setY(theEvent->y());
@@ -318,6 +322,13 @@ void OCCViewer_Viewer::onMouseRelease(SUIT_ViewWindow* theWindow, QMouseEvent* t
 
   if (myStartPnt == myEndPnt)
   {
+    if ( !isPreselectionEnabled() ) {
+      Handle(V3d_View) aView3d = aView->getViewPort()->getView();
+      if ( !aView3d.IsNull() ) {
+       myAISContext->MoveTo(myEndPnt.x(), myEndPnt.y(), aView3d);
+      }
+    }
+
     if (aHasShift && myMultiSelectionEnabled)
       myAISContext->ShiftSelect();
     else
@@ -365,11 +376,39 @@ void OCCViewer_Viewer::onKeyPress(SUIT_ViewWindow* theWindow, QKeyEvent* theEven
     return;
 
   emit deselection();
+
+  if ( !isPreselectionEnabled() ) {
+    Handle(V3d_View) aView3d = aView->getViewPort()->getView();
+    if ( !aView3d.IsNull() ) {
+      myAISContext->MoveTo(myCurPnt.x(), myCurPnt.y(), aView3d);
+    }
+  }
+
   myAISContext->Select();
 
   emit selectionChanged();
 }
 
+void OCCViewer_Viewer::onViewClosed(OCCViewer_ViewPort3d*)
+{
+  Standard_Integer aViewsNb = 0;
+  for ( myV3dViewer->InitActiveViews(); myV3dViewer->MoreActiveViews(); myV3dViewer->NextActiveViews())
+    ++aViewsNb;
+  if ( aViewsNb < 2 ) {
+    //clean up presentations before last view is closed
+    myAISContext->RemoveAll(Standard_False);
+  }
+}
+
+void OCCViewer_Viewer::onViewMapped(OCCViewer_ViewPort3d* viewPort)
+{
+  setTrihedronShown( true );
+  bool showStaticTrihedron = true;
+  SUIT_ResourceMgr* resMgr = SUIT_Session::session()->resourceMgr();
+  if ( resMgr ) showStaticTrihedron = resMgr->booleanValue( "3DViewer", "show_static_trihedron", true );
+  viewPort->showStaticTrihedron( showStaticTrihedron );
+}
+
 int OCCViewer_Viewer::getTopLayerId()
 {
 #if OCC_VERSION_LARGE > 0x06050200
@@ -437,12 +476,50 @@ void OCCViewer_Viewer::setZoomingStyle( const int theStyle )
 }
 
 /*!
-  Sets selection enabled status
+  \return true if preselection is enabled
+*/
+bool OCCViewer_Viewer::isPreselectionEnabled() const 
+{ 
+  return myPreselectionEnabled; 
+}
+
+/*!
+  Enables/disables preselection
+  \param isEnabled - new status
+*/
+void OCCViewer_Viewer::enablePreselection(bool isEnabled)
+{
+  myPreselectionEnabled = isEnabled;
+
+  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->enablePreselection( isEnabled );
+    }
+  }
+}
+
+/*!
+  \return true if selection is enabled
+*/
+bool OCCViewer_Viewer::isSelectionEnabled() const 
+{ 
+  return mySelectionEnabled; 
+}
+
+/*!
+  Enables/disables selection
   \param isEnabled - new status
 */
 void OCCViewer_Viewer::enableSelection(bool isEnabled)
 {
   mySelectionEnabled = isEnabled;
+
   //!! To be done for view windows
   if ( !myViewManager )
     return;
@@ -451,9 +528,18 @@ void OCCViewer_Viewer::enableSelection(bool isEnabled)
   for ( int i = 0; i < (int)wins.count(); i++ )
   {
     OCCViewer_ViewWindow* win = ::qobject_cast<OCCViewer_ViewWindow*>( wins.at( i ) );
-    if ( win )
+    if ( win ) {
       win->updateEnabledDrawMode();
+      win->enableSelection( isEnabled );
+    }
   }
+
+  
+  //clear current selection in the viewer
+  if(!isEnabled) {
+    myAISContext->ClearSelected( Standard_True );
+  }
+
 }
 
 /*!
@@ -611,12 +697,22 @@ bool OCCViewer_Viewer::highlight( const Handle(AIS_InteractiveObject)& obj,
   Unhilights all objects in viewer
   \param updateviewer - update current viewer
 */
-bool OCCViewer_Viewer::unHighlightAll( bool updateviewer )
+bool OCCViewer_Viewer::unHighlightAll( bool updateviewer, bool unselect )
 {
-  if ( myAISContext->HasOpenedContext() )
-    myAISContext->ClearSelected( updateviewer );
-  else
-    myAISContext->ClearCurrents( updateviewer );
+  if ( myAISContext->HasOpenedContext() ) {
+    if ( unselect ) {
+      myAISContext->ClearSelected( updateviewer );
+    } else {
+      myAISContext->UnhilightSelected( updateviewer );
+    }
+  } else {
+    if ( unselect ) {
+      myAISContext->ClearCurrents( updateviewer );
+    } else {
+      myAISContext->UnhilightCurrents( updateviewer );
+    }
+  }
+
   return false;
 }
 
@@ -631,12 +727,14 @@ bool OCCViewer_Viewer::isInViewer( const Handle(AIS_InteractiveObject)& obj,
   AIS_ListOfInteractive List;
   myAISContext->DisplayedObjects(List);
 
+#if OCC_VERSION_LARGE <= 0x06060000
   if( !onlyInViewer )
   {
     AIS_ListOfInteractive List1;
     myAISContext->ObjectsInCollector(List1);
     List.Append(List1);
   }
+#endif
 
   AIS_ListIteratorOfListOfInteractive ite(List);
   for ( ; ite.More(); ite.Next() )
@@ -733,10 +831,13 @@ void OCCViewer_Viewer::setTrihedronShown( const bool on )
   if ( myTrihedron.IsNull() )
     return;
 
-  if ( on )
+  if ( on ) {
     myAISContext->Display( myTrihedron );
-  else
+    myAISContext->Deactivate(myTrihedron);
+  }
+  else {
     myAISContext->Erase( myTrihedron );
+  }
 }
 
 /*!
@@ -798,7 +899,7 @@ void OCCViewer_Viewer::isos( int& u, int& v ) const
  */
 OCCViewer_ViewWindow* OCCViewer_Viewer::createSubWindow()
 {
-  return new OCCViewer_ViewWindow( 0,  this);
+  return new OCCViewer_ViewWindow(0,  this);
 }
 
 // obsolete  
@@ -832,18 +933,10 @@ void OCCViewer_Viewer::setBackground( int theViewId, const Qtx::BackgroundData&
 /*!
   Set the show static trihedron flag
 */
-void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on) {
-  if(myShowStaticTrihedron != on) {
-    OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
-    if(!aView)
-      return;
-
-    OCCViewer_ViewPort3d* vp3d = aView->getViewPort();
-    if(vp3d) {
-      myShowStaticTrihedron = on;
-      vp3d->updateStaticTriedronVisibility();
-    }
-  }
+void OCCViewer_Viewer::setStaticTrihedronDisplayed(const bool on)
+{
+  OCCViewer_ViewWindow* aView = (OCCViewer_ViewWindow*)(myViewManager->getActiveView());
+  if ( aView ) aView->showStaticTrihedron( on );
 }
 
 /*!
@@ -865,18 +958,7 @@ bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize
   if ( view3d.IsNull() )
     return false;
 
-  double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
-  double aMaxSide;
-
-  view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
-
-  if ( Xmin == RealFirst() || Ymin == RealFirst() || Zmin == RealFirst() ||
-       Xmax == RealLast()  || Ymax == RealLast()  || Zmax == RealLast() )
-    return false;
-
-  aMaxSide = Xmax - Xmin;
-  if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
-  if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
+  double aMaxSide = computeSceneSize( view3d );
 
   // IPAL21687
   // The boundary box of the view may be initialized but nullified
@@ -884,20 +966,44 @@ bool OCCViewer_Viewer::computeTrihedronSize( double& theNewSize, double& theSize
   if ( aMaxSide < Precision::Confusion() )
     return false;
 
-  float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("OCCViewer","trihedron_size", 100.);
+  float aSizeInPercents = SUIT_Session::session()->resourceMgr()->doubleValue("3DViewer","trihedron_size", 100.);
 
   static float EPS = 5.0E-3;
   theSize = getTrihedron()->Size();
   theNewSize = aMaxSide*aSizeInPercents / 100.0;
 
-  return fabs( theNewSize - theSize ) > theSize * EPS ||
-         fabs( theNewSize - theSize) > theNewSize * EPS;
+  return fabs( theNewSize - theSize ) > theSize    * EPS ||
+         fabs( theNewSize - theSize ) > theNewSize * EPS;
+}
+
+/*!
+ * Compute scene size
+ */
+double OCCViewer_Viewer::computeSceneSize(const Handle(V3d_View)& view3d) const
+{
+  double aMaxSide = 0;
+  double Xmin = 0, Ymin = 0, Zmin = 0, Xmax = 0, Ymax = 0, Zmax = 0;
+
+  view3d->View()->MinMaxValues( Xmin, Ymin, Zmin, Xmax, Ymax, Zmax );
+
+  if ( Xmin != RealFirst() && Ymin != RealFirst() && Zmin != RealFirst() &&
+       Xmax != RealLast()  && Ymax != RealLast()  && Zmax != RealLast() )
+  {
+    aMaxSide = Xmax - Xmin;
+    if ( aMaxSide < Ymax -Ymin ) aMaxSide = Ymax -Ymin;
+    if ( aMaxSide < Zmax -Zmin ) aMaxSide = Zmax -Zmin;
+  }
+
+  return aMaxSide;
 }
 
 /*! 
  * Update the size of the trihedron
  */
 void OCCViewer_Viewer::updateTrihedron() {
+  if ( myTrihedron.IsNull() )
+    return;
+
   if(myIsRelative){
     double newSz, oldSz;
     
@@ -908,3 +1014,95 @@ void OCCViewer_Viewer::updateTrihedron() {
     myTrihedron->SetSize(myTrihedronSize);
   }
 }
+
+/*!
+  Set number of isolines
+  \param u - u-isolines (first parametric co-ordinate)
+  \param v - v-isolines (second parametric co-ordinate)
+*/
+void OCCViewer_Viewer::setSelectionOptions( bool isPreselectionEnabled, bool isSelectionEnabled )
+{
+  myPreselectionEnabled = isPreselectionEnabled;
+  mySelectionEnabled = isSelectionEnabled;
+  //clear current selection in the viewer
+  
+  if(!mySelectionEnabled) {
+    myAISContext->ClearSelected( Standard_True );
+  }
+}
+
+
+/*!
+  Applies clipping planes to clippable objects
+*/
+void OCCViewer_Viewer::setClipPlanes(ClipPlanesList theList)
+{
+  // 1. Remove existing clipping planes
+  myClipPlanes.clear();
+  myInternalClipPlanes.Clear();
+
+  // 2. Create new clipping planes
+  ClipPlanesList::iterator inIt = theList.begin();
+  for (;inIt != theList.end(); inIt++ )
+  {
+    OCCViewer_ClipPlane aPlane = *inIt;
+
+    double aDx = 0.0, aDy = 0.0, aDz = 0.0;
+    aPlane.OrientationToXYZ( aDx, aDy, aDz );
+
+    gp_Pnt anOrigin( aPlane.X, aPlane.Y, aPlane.Z );
+    gp_Dir aDirection( aDx, aDy, aDz );
+
+    Handle(Graphic3d_ClipPlane) aGraphic3dPlane = new Graphic3d_ClipPlane( gp_Pln( anOrigin, aDirection ) );
+    aGraphic3dPlane->SetOn( aPlane.IsOn );
+
+    myInternalClipPlanes.Append( aGraphic3dPlane );
+    myClipPlanes.push_back( aPlane );
+  }
+
+  // 3. Apply clipping planes
+  AIS_ListOfInteractive aList;
+  myAISContext->DisplayedObjects (aList);
+  for ( AIS_ListIteratorOfListOfInteractive anIter (aList); anIter.More(); anIter.Next() ) {
+    Handle(AIS_InteractiveObject) anObj = anIter.Value();
+    Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (anObj);
+    if (!aShape.IsNull() && aShape->IsClippable()) {
+      aShape->SetClipPlanes(myInternalClipPlanes);
+    }
+  }
+}
+
+/*!
+  Returns the clipping planes applied to the displayed objects.
+*/
+ClipPlanesList OCCViewer_Viewer::getClipPlanes() const {
+  return myClipPlanes;
+}
+/*!
+  Applies clipping planes to given object objects
+*/
+void OCCViewer_Viewer::applyExistingClipPlanesToObject (const Handle(AIS_InteractiveObject)& theObject)
+{
+  Handle(ViewerData_AISShape) aShape = Handle(ViewerData_AISShape)::DownCast (theObject);
+  if (!aShape.IsNull() && aShape->IsClippable())
+  {
+    aShape->SetClipPlanes (myInternalClipPlanes);
+  }
+}
+
+/*!
+  Returns the pointer to the clipping dialog box.
+*/
+OCCViewer_ClippingDlg* OCCViewer_Viewer::getClippingDlg() const{
+  return myClippingDlg;
+}
+
+
+/*!
+  Stores pointer to the clipping dialog box.
+*/
+void OCCViewer_Viewer::setClippingDlg(OCCViewer_ClippingDlg* theDlg) {
+  if(myClippingDlg != theDlg) {
+    myClippingDlg = theDlg;
+  }
+}