2 #include "SMESHGUI_FunctionPreview.h"
3 #include <Expr_NamedUnknown.hxx>
4 #include <Expr_GeneralExpression.hxx>
5 #include <CASCatch_CatchSignals.hxx>
6 #include <CASCatch_Failure.hxx>
7 #include <CASCatch_ErrorHandler.hxx>
10 SMESHGUI_FunctionPreview::SMESHGUI_FunctionPreview( QWidget* p )
21 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
22 myCurve = insertCurve( QString() );
23 setCurvePen( myCurve, QPen( Qt::red, 1 ) );
26 SMESHGUI_FunctionPreview::~SMESHGUI_FunctionPreview()
30 bool SMESHGUI_FunctionPreview::isTableFunc() const
35 void SMESHGUI_FunctionPreview::tableFunc( SMESH::double_array& f ) const
40 QString SMESHGUI_FunctionPreview::function() const
45 void SMESHGUI_FunctionPreview::interval( double& xmin, double& xmax ) const
51 int SMESHGUI_FunctionPreview::pointsCount() const
56 void SMESHGUI_FunctionPreview::setIsExp( const bool exp, const bool update )
63 bool SMESHGUI_FunctionPreview::setParams( const QString& func,
64 const double xmin, const double xmax,
65 const int points, const bool update )
68 myTableFunc = SMESH::double_array();
69 myFunction = func.isEmpty() ? "0" : func;
71 myXmax = xmax<myXmin ? myXmax : xmax;
72 myPoints = points>0 ? points : 2;
73 bool res = init( func );
79 bool SMESHGUI_FunctionPreview::setParams( const SMESH::double_array& f, const double xmin, const double xmax,
84 if( myTableFunc.length()%2==1 )
85 myTableFunc.length( myTableFunc.length()-1 );
89 myXmax = xmax<myXmin ? myXmax : xmax;
90 myPoints = myTableFunc.length()/2;
95 return myTableFunc.length()>0;
98 bool SMESHGUI_FunctionPreview::createTable( SMESH::double_array& func )
100 if( myExpr.IsNull() )
106 double d = (myXmax-myXmin)/double(myPoints-1);
107 func.length( 2*myPoints );
109 for( int i=0, j=0; i<myPoints; j++ )
112 double t = myXmin + d*j, f = funcValue( t, ok );
122 func.length( func.length()-2*err );
126 void SMESHGUI_FunctionPreview::drawContents( QPainter* p )
128 SMESH::double_array f;
135 myIsDone = createTable( f );
137 int size = f.length()/2;
140 setAxisScale( curveXAxis( myCurve ), 0.0, 0.0 );
141 setAxisScale( curveYAxis( myCurve ), 0.0, 0.0 );
142 setCurveData( myCurve, 0, 0, 0 );
144 QwtPlot::drawContents( p );
148 double* x = new double[size], *y = new double[size];
149 double min_x, max_x, min_y, max_y;
150 for( int i=0; i<size; i++ )
153 y[i] = myIsExp ? pow( 10.0, f[2*i+1] ) : f[2*i+1];
154 if( i==0 || y[i]<min_y )
156 if( i==0 || y[i]>max_y )
158 if( i==0 || x[i]<min_x )
160 if( i==0 || x[i]>max_x )
164 setAxisScale( curveXAxis( myCurve ), min_x, max_x );
165 setAxisScale( curveYAxis( myCurve ), min_y, max_y );
166 setCurveData( myCurve, x, y, size );
171 QwtPlot::drawContents( p );
174 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
176 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
178 return sub->GetName()=="t";
181 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
183 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
184 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
187 if( name->GetName()!="t" )
191 res = isCorrectArg( sub );
196 bool SMESHGUI_FunctionPreview::init( const QString& str )
198 myExpr = ExprIntrp_GenExp::Create();
199 myExpr->Process( ( Standard_CString ) str.latin1() );
201 bool syntax = false, args = false;
202 if( myExpr->IsDone() )
205 args = isCorrectArg( myExpr->Expression() );
208 bool res = syntax && args;
214 double SMESHGUI_FunctionPreview::funcValue( const double t, bool& ok )
216 if( myExpr.IsNull() )
219 myValues.ChangeValue( 1 ) = t;
222 double res = calc( ok );
227 double SMESHGUI_FunctionPreview::calc( bool& ok )
229 OSD::SetSignal( true );
232 CASCatch_CatchSignals aCatchSignals;
233 aCatchSignals.Activate();
237 res = myExpr->Expression()->Evaluate( myVars, myValues );
239 CASCatch_CATCH(CASCatch_Failure) {
240 aCatchSignals.Deactivate();
241 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
245 aCatchSignals.Deactivate();
249 bool SMESHGUI_FunctionPreview::isDone() const