Salome HOME
Qt4 porting: add axis grids to plot 2d preview.
[modules/smesh.git] / src / StdMeshersGUI / StdMeshersGUI_DistrPreview.cxx
index f763fbd9c27b032f4e0099c6eb16da0a9bddd268..fc3787b9e5ced9b857475817e641d1e99d7d3348 100644 (file)
@@ -1,11 +1,49 @@
-
+// Copyright (C) 2005  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// 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.
+//
+// This library is distributed in the hope that it will be useful
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+// File   : StdMeshersGUI_DistrPreview.cxx
+// Author : Open CASCADE S.A.S.
+//
+
+// SMESH includes
 #include "StdMeshersGUI_DistrPreview.h"
+
+// Qwt includes
+#include <qwt_plot_curve.h>
+#include <qwt_plot_marker.h>
+#include <qwt_plot_grid.h>
+#include <qwt_symbol.h>
+#include <qwt_legend.h>
+
+// OCCT includes
 #include <Expr_NamedUnknown.hxx>
 #include <Expr_GeneralExpression.hxx>
-#include <CASCatch_CatchSignals.hxx>
-#include <CASCatch_Failure.hxx> 
-#include <CASCatch_ErrorHandler.hxx>
-#include <OSD.hxx>
+
+#if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
+#define NO_CAS_CATCH
+#endif
+
+#include <Standard_Failure.hxx>
+
+#ifdef NO_CAS_CATCH
+#include <Standard_ErrorHandler.hxx>
+#endif
 
 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
 : QwtPlot( p ),
@@ -19,20 +57,44 @@ StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::
 {
   myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
   myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
-  myCurve1 = insertCurve( QString() );
-  myCurve2 = insertCurve( QString() );
-  myMsg = insertMarker( new QwtPlotMarker( this ) );
-  setMarkerPos( myMsg, 0.5, 0.5 );
-  setMarkerLabelPen( myMsg, QPen( Qt::red, 1 ) );
-  QFont f = markerFont( myMsg );
-  f.setPointSize( 14 );
-  f.setBold( true );
-  setMarkerFont( myMsg, f );
-  setCurvePen( myCurve1, QPen( Qt::red, 1 ) );
+  myDensity = new QwtPlotCurve( QString() );
+  myDensity->attach( this );
+  myDistr = new QwtPlotCurve( QString() );
+  myDistr->attach( this );
+  myMsg = new QwtPlotMarker();
+  myMsg->attach( this );
+  myMsg->setValue( 0.5, 0.5 );
+  QwtText mt = myMsg->label();
+  mt.setBackgroundPen( QPen( Qt::red, 1 ) );
+  QFont f = mt.font();
+  f.setPointSize( 14 ); f.setBold( true );
+  mt.setFont( f );
+  myMsg->setLabel( mt );
+  myDensity->setPen( QPen( Qt::red, 1 ) );
 
   QColor dc = Qt::blue;
-  setCurvePen( myCurve2, QPen( dc, 1 ) );
-  setCurveSymbol( myCurve2, QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
+  myDistr->setPen( QPen( dc, 1 ) );
+  myDistr->setSymbol( QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
+
+  QwtLegend* l = legend();
+  if ( !l ) {
+    l = new QwtLegend( this );
+    l->setFrameStyle( QFrame::Box | QFrame::Sunken );
+  }
+  insertLegend( l, QwtPlot::BottomLegend );
+
+  myDensity->setTitle( tr( "SMESH_DENSITY_FUNC" ) );
+  myDistr->setTitle( tr( "SMESH_DISTR" ) );
+  
+  QwtPlotGrid* aGrid = new QwtPlotGrid();
+  QPen aMajPen = aGrid->majPen();
+  aMajPen.setStyle( Qt::DashLine );
+  aGrid->setPen( aMajPen );
+
+  aGrid->enableX( true );
+  aGrid->enableY( true );
+
+  aGrid->attach( this );
 }
 
 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
@@ -153,7 +215,7 @@ void StdMeshersGUI_DistrPreview::update()
       if( isTableFunc() )
        arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
       else
-       arr = h->BuildDistributionExpr( myFunction.latin1(), myNbSeg, ( int )myConv );
+       arr = h->BuildDistributionExpr( myFunction.toLatin1().data(), myNbSeg, ( int )myConv );
       if( arr )
       {
        distr = *arr;
@@ -169,7 +231,11 @@ void StdMeshersGUI_DistrPreview::update()
     return;
   }
   else
-    setMarkerLabel( myMsg, QString() );
+  {
+    QwtText mt = myMsg->label();
+    mt.setText( QString() );
+    myMsg->setLabel( mt );
+  }
 
   int size = graph.length()/2;
   double* x = new double[size], *y = new double[size];
@@ -196,9 +262,9 @@ void StdMeshersGUI_DistrPreview::update()
       max_x = x[i];
   }
 
-  setAxisScale( curveXAxis( myCurve1 ), min_x, max_x );
-  setAxisScale( curveYAxis( myCurve1 ), min( 0.0, min_y ), max( 0.0, max_y ) );
-  setCurveData( myCurve1, x, y, size );
+  setAxisScale( myDensity->xAxis(), min_x, max_x );
+  setAxisScale( myDensity->yAxis(), std::min( 0.0, min_y ), std::max( 0.0, max_y ) );
+  myDensity->setData( x, y, size );
   if( x )
     delete[] x;
   if( y )
@@ -213,20 +279,30 @@ void StdMeshersGUI_DistrPreview::update()
     x[i] = distr[i];
     y[i] = 0;
   }
-  setCurveData( myCurve2, x, y, size );
+  myDistr->setData( x, y, size );
   delete[] x;
   delete[] y;
   x = y = 0;
-  replot();
+
+  try {   
+#ifdef NO_CAS_CATCH
+    OCC_CATCH_SIGNALS;
+#endif
+    replot();
+  } catch(Standard_Failure) {
+    Handle(Standard_Failure) aFail = Standard_Failure::Caught();
+  }
 }
 
 void StdMeshersGUI_DistrPreview::showError()
 {
-  setAxisScale( curveXAxis( myCurve1 ), 0.0, 1.0 );
-  setAxisScale( curveYAxis( myCurve1 ), 0.0, 1.0 );
-  setCurveData( myCurve1, 0, 0, 0 );
-  setCurveData( myCurve2, 0, 0, 0 );
-  setMarkerLabel( myMsg, tr( "SMESH_INVALID_FUNCTION" ) );
+  setAxisScale( myDensity->xAxis(), 0.0, 1.0 );
+  setAxisScale( myDensity->yAxis(), 0.0, 1.0 );
+  myDensity->setData( 0, 0, 0 );
+  myDistr->setData( 0, 0, 0 );
+  QwtText mt = myMsg->label();
+  mt.setText( tr( "SMESH_INVALID_FUNCTION" ) );
+  myMsg->setLabel( mt );
   replot();
 }
 
@@ -254,22 +330,17 @@ bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
 
 bool StdMeshersGUI_DistrPreview::init( const QString& str )
 {
-  CASCatch_CatchSignals aCatchSignals;
-  aCatchSignals.Activate();
-
   bool parsed_ok = true;
-  CASCatch_TRY
-  {
+  try {
+#ifdef NO_CAS_CATCH
+    OCC_CATCH_SIGNALS;
+#endif
     myExpr = ExprIntrp_GenExp::Create();
-    myExpr->Process( ( Standard_CString ) str.latin1() );
-  }
-  CASCatch_CATCH(CASCatch_Failure)
-  {
-    aCatchSignals.Deactivate();
-    Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
+    myExpr->Process( ( Standard_CString ) str.toLatin1().data() );
+  } catch(Standard_Failure) {
+    Handle(Standard_Failure) aFail = Standard_Failure::Caught();
     parsed_ok = false;
   }
-  aCatchSignals.Deactivate();
 
   bool syntax = false, args = false;
   if( parsed_ok && myExpr->IsDone() )
@@ -299,23 +370,20 @@ double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
 
 double StdMeshersGUI_DistrPreview::calc( bool& ok )
 {
-  OSD::SetSignal( true );
   double res = 0.0;
 
-  CASCatch_CatchSignals aCatchSignals;
-  aCatchSignals.Activate();
-
   ok = true;
-  CASCatch_TRY {   
+  try {   
+#ifdef NO_CAS_CATCH
+    OCC_CATCH_SIGNALS;
+#endif
     res = myExpr->Expression()->Evaluate( myVars, myValues );
-  }
-  CASCatch_CATCH(CASCatch_Failure) {
-    aCatchSignals.Deactivate();
-    Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
+  } catch(Standard_Failure) {
+    Handle(Standard_Failure) aFail = Standard_Failure::Caught();
     ok = false;
     res = 0.0;
   }
-  aCatchSignals.Deactivate();
+
   return res;
 }
 
@@ -326,22 +394,22 @@ bool StdMeshersGUI_DistrPreview::isDone() const
 
 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
 {
-  CASCatch_CatchSignals aCatchSignals;
-  aCatchSignals.Activate();
-
   bool ok = true;
   switch( myConv )
   {
   case EXPONENT:
     {
-      CASCatch_TRY
-      { 
+      try { 
+#ifdef NO_CAS_CATCH
+        OCC_CATCH_SIGNALS;
+#endif
+       // in StdMeshers_NumberOfSegments.cc
+       // const double PRECISION = 1e-7;
+       //
+       if(v < -7) v = -7.0;
        v = pow( 10.0, v );
-      }
-      CASCatch_CATCH(CASCatch_Failure)
-      {
-       aCatchSignals.Deactivate();
-       Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
+      } catch(Standard_Failure) {
+       Handle(Standard_Failure) aFail = Standard_Failure::Caught();
        v = 0.0;
        ok = false;
       }
@@ -353,6 +421,6 @@ bool StdMeshersGUI_DistrPreview::convert( double& v ) const
       v = 0;
     break;
   }
-  aCatchSignals.Deactivate();
+
   return ok;
 }