1 // Copyright (C) 2005 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
2 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License.
9 // This library is distributed in the hope that it will be useful
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 // See http://www.salome-platform.org/
21 #include "SMESHGUI_FunctionPreview.h"
22 #include <Expr_NamedUnknown.hxx>
23 #include <Expr_GeneralExpression.hxx>
24 #include <CASCatch_CatchSignals.hxx>
25 #include <CASCatch_Failure.hxx>
26 #include <CASCatch_ErrorHandler.hxx>
29 SMESHGUI_FunctionPreview::SMESHGUI_FunctionPreview( QWidget* p )
40 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
41 myCurve = insertCurve( QString() );
42 setCurvePen( myCurve, QPen( Qt::red, 1 ) );
45 SMESHGUI_FunctionPreview::~SMESHGUI_FunctionPreview()
49 bool SMESHGUI_FunctionPreview::isTableFunc() const
54 void SMESHGUI_FunctionPreview::tableFunc( SMESH::double_array& f ) const
59 QString SMESHGUI_FunctionPreview::function() const
64 void SMESHGUI_FunctionPreview::interval( double& xmin, double& xmax ) const
70 int SMESHGUI_FunctionPreview::pointsCount() const
75 void SMESHGUI_FunctionPreview::setIsExp( const bool exp, const bool update )
82 bool SMESHGUI_FunctionPreview::setParams( const QString& func,
83 const double xmin, const double xmax,
84 const int points, const bool update )
87 myTableFunc = SMESH::double_array();
88 myFunction = func.isEmpty() ? "0" : func;
90 myXmax = xmax<myXmin ? myXmax : xmax;
91 myPoints = points>0 ? points : 2;
92 bool res = init( func );
98 bool SMESHGUI_FunctionPreview::setParams( const SMESH::double_array& f, const double xmin, const double xmax,
103 if( myTableFunc.length()%2==1 )
104 myTableFunc.length( myTableFunc.length()-1 );
108 myXmax = xmax<myXmin ? myXmax : xmax;
109 myPoints = myTableFunc.length()/2;
114 return myTableFunc.length()>0;
117 bool SMESHGUI_FunctionPreview::createTable( SMESH::double_array& func )
119 if( myExpr.IsNull() )
125 double d = (myXmax-myXmin)/double(myPoints-1);
126 func.length( 2*myPoints );
128 for( int i=0, j=0; i<myPoints; j++ )
131 double t = myXmin + d*j, f = funcValue( t, ok );
141 func.length( func.length()-2*err );
145 void SMESHGUI_FunctionPreview::drawContents( QPainter* p )
147 SMESH::double_array f;
154 myIsDone = createTable( f );
156 int size = f.length()/2;
159 setAxisScale( curveXAxis( myCurve ), 0.0, 0.0 );
160 setAxisScale( curveYAxis( myCurve ), 0.0, 0.0 );
161 setCurveData( myCurve, 0, 0, 0 );
163 QwtPlot::drawContents( p );
167 double* x = new double[size], *y = new double[size];
168 double min_x, max_x, min_y, max_y;
169 for( int i=0; i<size; i++ )
172 y[i] = myIsExp ? pow( 10.0, f[2*i+1] ) : f[2*i+1];
173 if( i==0 || y[i]<min_y )
175 if( i==0 || y[i]>max_y )
177 if( i==0 || x[i]<min_x )
179 if( i==0 || x[i]>max_x )
183 setAxisScale( curveXAxis( myCurve ), min_x, max_x );
184 setAxisScale( curveYAxis( myCurve ), min_y, max_y );
185 setCurveData( myCurve, x, y, size );
190 QwtPlot::drawContents( p );
193 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
195 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
197 return sub->GetName()=="t";
200 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
202 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
203 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
206 if( name->GetName()!="t" )
210 res = isCorrectArg( sub );
215 bool SMESHGUI_FunctionPreview::init( const QString& str )
217 myExpr = ExprIntrp_GenExp::Create();
218 myExpr->Process( ( Standard_CString ) str.latin1() );
220 bool syntax = false, args = false;
221 if( myExpr->IsDone() )
224 args = isCorrectArg( myExpr->Expression() );
227 bool res = syntax && args;
233 double SMESHGUI_FunctionPreview::funcValue( const double t, bool& ok )
235 if( myExpr.IsNull() )
238 myValues.ChangeValue( 1 ) = t;
241 double res = calc( ok );
246 double SMESHGUI_FunctionPreview::calc( bool& ok )
248 OSD::SetSignal( true );
251 CASCatch_CatchSignals aCatchSignals;
252 aCatchSignals.Activate();
256 res = myExpr->Expression()->Evaluate( myVars, myValues );
258 CASCatch_CATCH(CASCatch_Failure) {
259 aCatchSignals.Deactivate();
260 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
264 aCatchSignals.Deactivate();
268 bool SMESHGUI_FunctionPreview::isDone() const