1 // Copyright (C) 2007-2024 CEA, EDF, OPEN CASCADE
3 // Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
4 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
6 // This library is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU Lesser General Public
8 // License as published by the Free Software Foundation; either
9 // version 2.1 of the License, or (at your option) any later version.
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 // Lesser General Public License for more details.
16 // You should have received a copy of the GNU Lesser General Public
17 // License along with this library; if not, write to the Free Software
18 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
23 // File : StdMeshersGUI_DistrPreview.cxx
24 // Author : Open CASCADE S.A.S.
27 #include "StdMeshersGUI_DistrPreview.h"
30 #include <qwt_plot_curve.h>
31 #include <qwt_plot_marker.h>
32 #include <qwt_plot_grid.h>
33 #include <qwt_symbol.h>
34 #include <qwt_legend.h>
37 #include <Expr_NamedUnknown.hxx>
38 #include <Expr_GeneralExpression.hxx>
40 #include <Standard_Failure.hxx>
41 #include <Standard_ErrorHandler.hxx>
49 // SALOME KERNEL includes
50 #include <Basics_Utils.hxx>
51 // SALOME GUI includes
52 #include <Plot2d_PlotItems.h>
54 Plot2d_QwtLegendLabel* getLegendLabel( QwtPlotCurve* plotCurve )
56 const QVariant itemInfo = plotCurve->plot()->itemToInfo( plotCurve );
57 QwtLegend* legend = dynamic_cast<QwtLegend*>( plotCurve->plot()->legend() );
58 QWidget* widget = legend->legendWidget( itemInfo );
59 QwtLegendLabel* label = dynamic_cast<QwtLegendLabel*>( widget );
60 Plot2d_QwtLegendLabel* anItem = (Plot2d_QwtLegendLabel*)label;
64 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
69 myConv( CUT_NEGATIVE ),
74 Kernel_Utils::Localizer loc;
75 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
76 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
78 QwtAbstractLegend* absLegend = legend();
79 QwtLegend* legend = 0;
81 legend = new Plot2d_QwtLegend( this );
82 legend->setFrameStyle( QFrame::Box | QFrame::Sunken );
85 legend = dynamic_cast<QwtLegend*>( absLegend );
87 insertLegend( legend, QwtPlot::BottomLegend );
89 myDensity = new QwtPlotCurve( QString() );
90 myDensity->attach( this );
91 QPen densityPen = QPen( Qt::red, 1 );
92 myDensity->setPen( QPen( Qt::red, 1 ) );
93 if( Plot2d_QwtLegendLabel* anItem = getLegendLabel( myDensity ) ) {
94 anItem->setPen( densityPen );
98 myDistr = new QwtPlotCurve( QString() );
99 myDistr->attach( this );
100 QPen distrPen = QPen( Qt::blue, 1 );
101 QwtSymbol* distrSymbol = new QwtSymbol( QwtSymbol::XCross, QBrush( Qt::blue ),
102 QPen( Qt::blue ), QSize( 5, 5 ) );
103 myDistr->setPen( distrPen );
104 myDistr->setSymbol( distrSymbol );
105 if( Plot2d_QwtLegendLabel* anItem = getLegendLabel( myDistr ) ) {
106 anItem->setPen( distrPen );
107 anItem->setSymbol( distrSymbol );
111 myMsg = new QwtPlotMarker();
112 myMsg->attach( this );
113 myMsg->setValue( 0.5, 0.5 );
114 QwtText mt = myMsg->label();
115 mt.setBorderPen( QPen( Qt::red, 1 ) );
117 f.setPointSize( 14 ); //f.setBold( true );
119 myMsg->setLabel( mt );
121 enableAxis(QwtPlot::yLeft, false);
122 enableAxis(QwtPlot::yRight, true);
125 axisFont.setPointSize( 8 );
126 setAxisFont(QwtPlot::yRight, axisFont);
127 setAxisFont(QwtPlot::xBottom, axisFont);
129 myDensity->setYAxis(QwtPlot::yRight);
130 myDistr->setYAxis(QwtPlot::yRight);
131 myMsg->setYAxis(QwtPlot::yRight);
132 myDensity->setTitle( tr( "SMESH_DENSITY_FUNC" ) );
133 myDistr->setTitle( tr( "SMESH_DISTR" ) );
135 QwtPlotGrid* aGrid = new QwtPlotGrid();
136 QPen aMajPen = aGrid->majorPen();
137 aMajPen.setStyle( Qt::DashLine );
138 aGrid->setPen( aMajPen );
140 aGrid->enableX( true );
141 aGrid->enableY( true );
143 aGrid->attach( this );
146 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
150 bool StdMeshersGUI_DistrPreview::isTableFunc() const
155 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
160 QString StdMeshersGUI_DistrPreview::function() const
165 int StdMeshersGUI_DistrPreview::nbSeg() const
170 int StdMeshersGUI_DistrPreview::pointsCount() const
175 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
182 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
185 myTableFunc = SMESH::double_array();
186 myFunction = func.isEmpty() ? "0" : func;
187 myPoints = points>0 ? points : 2;
188 myNbSeg = nbSeg>0 ? nbSeg : 1;
189 bool res = init( func );
195 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
199 if( myTableFunc.length()%2==1 )
200 myTableFunc.length( myTableFunc.length()-1 );
203 myPoints = myTableFunc.length()/2;
204 myNbSeg = nbSeg>0 ? nbSeg : 1;
209 return myTableFunc.length()>0;
212 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
214 if( myExpr.IsNull() )
220 const double xmin = 0.0, xmax = 1.0;
222 double d = (xmax-xmin)/double(myPoints-1);
223 func.length( 2*myPoints );
225 for( int i=0, j=0; i<myPoints; j++ )
228 double t = xmin + d*j, f = funcValue( t, ok );
238 func.length( func.length()-2*err );
242 void StdMeshersGUI_DistrPreview::update()
244 Kernel_Utils::Localizer loc;
245 SMESH::double_array graph, distr;
252 myIsDone = createTable( graph );
254 if( graph.length()>=2 )
256 StdMeshers::StdMeshers_NumberOfSegments_var h =
257 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
259 if( !CORBA::is_nil( h.in() ) )
261 SMESH::double_array* arr = 0;
263 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
265 arr = h->BuildDistributionExpr( myFunction.toLatin1().data(), myNbSeg, ( int )myConv );
274 bool correct = graph.length()>=2 && distr.length()>=2;
282 QwtText mt = myMsg->label();
283 mt.setText( QString() );
284 myMsg->setLabel( mt );
287 int size = graph.length()/2;
288 double* x = new double[size], *y = new double[size];
289 double min_x = 0, max_x = 0, min_y = 0, max_y = 0;
290 for( int i=0; i<size; i++ )
294 if( !convert( y[i] ) )
296 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
297 delete[] x; delete[] y;
303 if ( std::fabs(y[i]) >= HUGE_VAL)
304 y[i] = HUGE_VAL/100.;
306 if ( std::isinf(y[i]))
307 y[i] = std::numeric_limits<double>::max()/100.;
311 if( i==0 || y[i]<min_y )
313 if( i==0 || y[i]>max_y )
315 if( i==0 || x[i]<min_x )
317 if( i==0 || x[i]>max_x )
321 setAxisScale( myDensity->xAxis(), min_x, max_x );
322 setAxisScale( myDensity->yAxis(),
323 std::min( 0.0, min_y ),
324 std::max( 0.0, max_y )
326 myDensity->setSamples( x, y, size );
333 size = distr.length();
334 x = new double[size];
335 y = new double[size];
336 for( int i=0; i<size; i++ )
341 myDistr->setSamples( x, y, size );
349 } catch(Standard_Failure&) {
353 void StdMeshersGUI_DistrPreview::showError()
355 setAxisScale( myDensity->xAxis(), 0.0, 1.0 );
356 setAxisScale( myDensity->yAxis(), 0.0, 1.0 );
357 myDensity->setSamples( 0, 0, 0 );
358 myDistr->setSamples( 0, 0, 0 );
359 QwtText mt = myMsg->label();
360 mt.setText( tr( "SMESH_INVALID_FUNCTION" ) );
361 myMsg->setLabel( mt );
365 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
367 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
369 return sub->GetName()=="t";
372 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
374 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
375 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
378 if( name->GetName()!="t" )
382 res = isCorrectArg( sub );
387 bool StdMeshersGUI_DistrPreview::init( const QString& str )
389 Kernel_Utils::Localizer loc;
390 bool parsed_ok = true;
393 myExpr = ExprIntrp_GenExp::Create();
394 myExpr->Process( ( Standard_CString ) str.toLatin1().data() );
395 } catch(Standard_Failure&) {
399 bool syntax = false, args = false;
400 if( parsed_ok && myExpr->IsDone() )
403 args = isCorrectArg( myExpr->Expression() );
406 bool res = parsed_ok && syntax && args;
412 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
414 if( myExpr.IsNull() )
417 myValues.ChangeValue( 1 ) = t;
420 double res = calc( ok );
425 double StdMeshersGUI_DistrPreview::calc( bool& ok )
432 res = myExpr->Expression()->Evaluate( myVars, myValues );
433 } catch(Standard_Failure&) {
441 bool StdMeshersGUI_DistrPreview::isDone() const
446 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
455 // in StdMeshers_NumberOfSegments.cc
456 // const double PRECISION = 1e-7;
460 } catch(Standard_Failure&) {