2 #include "StdMeshersGUI_DistrPreview.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 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
16 myConv( CUT_NEGATIVE ),
20 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
21 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
22 myDensity = insertCurve( QString() );
23 myDistr = insertCurve( QString() );
24 myMsg = insertMarker( new QwtPlotMarker( this ) );
25 setMarkerPos( myMsg, 0.5, 0.5 );
26 setMarkerLabelPen( myMsg, QPen( Qt::red, 1 ) );
27 QFont f = markerFont( myMsg );
30 setMarkerFont( myMsg, f );
31 setCurvePen( myDensity, QPen( Qt::red, 1 ) );
34 setCurvePen( myDistr, QPen( dc, 1 ) );
35 setCurveSymbol( myDistr, QwtSymbol( QwtSymbol::XCross, QBrush( dc ), QPen( dc ), QSize( 5, 5 ) ) );
36 setAutoLegend( true );
38 setLegendPos( Qwt::Bottom );
39 setCurveTitle( myDensity, tr( "SMESH_DENSITY_FUNC" ) );
40 setCurveTitle( myDistr, tr( "SMESH_DISTR" ) );
43 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
47 bool StdMeshersGUI_DistrPreview::isTableFunc() const
52 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
57 QString StdMeshersGUI_DistrPreview::function() const
62 int StdMeshersGUI_DistrPreview::nbSeg() const
67 int StdMeshersGUI_DistrPreview::pointsCount() const
72 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
79 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
82 myTableFunc = SMESH::double_array();
83 myFunction = func.isEmpty() ? "0" : func;
84 myPoints = points>0 ? points : 2;
85 myNbSeg = nbSeg>0 ? nbSeg : 1;
86 bool res = init( func );
92 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
96 if( myTableFunc.length()%2==1 )
97 myTableFunc.length( myTableFunc.length()-1 );
100 myPoints = myTableFunc.length()/2;
101 myNbSeg = nbSeg>0 ? nbSeg : 1;
106 return myTableFunc.length()>0;
109 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
111 if( myExpr.IsNull() )
117 const double xmin = 0.0, xmax = 1.0;
119 double d = (xmax-xmin)/double(myPoints-1);
120 func.length( 2*myPoints );
122 for( int i=0, j=0; i<myPoints; j++ )
125 double t = xmin + d*j, f = funcValue( t, ok );
135 func.length( func.length()-2*err );
139 void StdMeshersGUI_DistrPreview::update()
141 SMESH::double_array graph, distr;
148 myIsDone = createTable( graph );
150 if( graph.length()>=2 )
152 StdMeshers::StdMeshers_NumberOfSegments_var h =
153 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
155 if( !CORBA::is_nil( h.in() ) )
157 SMESH::double_array* arr = 0;
159 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
161 arr = h->BuildDistributionExpr( myFunction.latin1(), myNbSeg, ( int )myConv );
170 bool correct = graph.length()>=2 && distr.length()>=2;
177 setMarkerLabel( myMsg, QString() );
179 int size = graph.length()/2;
180 double* x = new double[size], *y = new double[size];
181 double min_x, max_x, min_y, max_y;
182 for( int i=0; i<size; i++ )
186 if( !convert( y[i] ) )
188 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
189 delete[] x; delete[] y;
194 if( i==0 || y[i]<min_y )
196 if( i==0 || y[i]>max_y )
198 if( i==0 || x[i]<min_x )
200 if( i==0 || x[i]>max_x )
204 setAxisScale( curveXAxis( myDensity ), min_x, max_x );
205 setAxisScale( curveYAxis( myDensity ), min( 0.0, min_y ), max( 0.0, max_y ) );
206 setCurveData( myDensity, x, y, size );
213 size = distr.length();
214 x = new double[size];
215 y = new double[size];
216 for( int i=0; i<size; i++ )
221 setCurveData( myDistr, x, y, size );
226 OSD::SetSignal( true );
227 CASCatch_CatchSignals aCatchSignals;
228 aCatchSignals.Activate();
234 CASCatch_CATCH(CASCatch_Failure)
236 aCatchSignals.Deactivate();
237 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
239 aCatchSignals.Deactivate();
242 void StdMeshersGUI_DistrPreview::showError()
244 setAxisScale( curveXAxis( myDensity ), 0.0, 1.0 );
245 setAxisScale( curveYAxis( myDensity ), 0.0, 1.0 );
246 setCurveData( myDensity, 0, 0, 0 );
247 setCurveData( myDistr, 0, 0, 0 );
248 setMarkerLabel( myMsg, tr( "SMESH_INVALID_FUNCTION" ) );
252 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
254 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
256 return sub->GetName()=="t";
259 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
261 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
262 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
265 if( name->GetName()!="t" )
269 res = isCorrectArg( sub );
274 bool StdMeshersGUI_DistrPreview::init( const QString& str )
276 CASCatch_CatchSignals aCatchSignals;
277 aCatchSignals.Activate();
279 bool parsed_ok = true;
282 myExpr = ExprIntrp_GenExp::Create();
283 myExpr->Process( ( Standard_CString ) str.latin1() );
285 CASCatch_CATCH(CASCatch_Failure)
287 aCatchSignals.Deactivate();
288 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
291 aCatchSignals.Deactivate();
293 bool syntax = false, args = false;
294 if( parsed_ok && myExpr->IsDone() )
297 args = isCorrectArg( myExpr->Expression() );
300 bool res = parsed_ok && syntax && args;
306 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
308 if( myExpr.IsNull() )
311 myValues.ChangeValue( 1 ) = t;
314 double res = calc( ok );
319 double StdMeshersGUI_DistrPreview::calc( bool& ok )
321 OSD::SetSignal( true );
324 CASCatch_CatchSignals aCatchSignals;
325 aCatchSignals.Activate();
329 res = myExpr->Expression()->Evaluate( myVars, myValues );
331 CASCatch_CATCH(CASCatch_Failure) {
332 aCatchSignals.Deactivate();
333 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
337 aCatchSignals.Deactivate();
341 bool StdMeshersGUI_DistrPreview::isDone() const
346 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
348 CASCatch_CatchSignals aCatchSignals;
349 aCatchSignals.Activate();
360 CASCatch_CATCH(CASCatch_Failure)
362 aCatchSignals.Deactivate();
363 Handle(CASCatch_Failure) aFail = CASCatch_Failure::Caught();
375 aCatchSignals.Deactivate();