1 // Copyright (C) 2007-2014 CEA/DEN, EDF R&D, 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 #if (OCC_VERSION_MAJOR << 16 | OCC_VERSION_MINOR << 8 | OCC_VERSION_MAINTENANCE) > 0x060100
44 #include <Standard_Failure.hxx>
47 #include <Standard_ErrorHandler.hxx>
56 // SALOME KERNEL includes
57 #include <Basics_Utils.hxx>
58 // SALOME GUI includes
59 #include <Plot2d_PlotItems.h>
61 Plot2d_QwtLegendLabel* getLegendLabel( QwtPlotCurve* plotCurve )
63 const QVariant itemInfo = plotCurve->plot()->itemToInfo( plotCurve );
64 QwtLegend* legend = dynamic_cast<QwtLegend*>( plotCurve->plot()->legend() );
65 QWidget* widget = legend->legendWidget( itemInfo );
66 QwtLegendLabel* label = dynamic_cast<QwtLegendLabel*>( widget );
67 if( Plot2d_QwtLegendLabel* anItem = (Plot2d_QwtLegendLabel*)label )
71 StdMeshersGUI_DistrPreview::StdMeshersGUI_DistrPreview( QWidget* p, StdMeshers::StdMeshers_NumberOfSegments_ptr h )
77 myConv( CUT_NEGATIVE ),
81 Kernel_Utils::Localizer loc;
82 myHypo = StdMeshers::StdMeshers_NumberOfSegments::_duplicate( h );
83 myVars.ChangeValue( 1 ) = new Expr_NamedUnknown( "t" );
85 QwtAbstractLegend* absLegend = legend();
86 QwtLegend* legend = 0;
88 legend = new Plot2d_QwtLegend( this );
89 legend->setFrameStyle( QFrame::Box | QFrame::Sunken );
92 legend = dynamic_cast<QwtLegend*>( absLegend );
94 insertLegend( legend, QwtPlot::BottomLegend );
96 myDensity = new QwtPlotCurve( QString() );
97 myDensity->attach( this );
98 QPen densityPen = QPen( Qt::red, 1 );
99 myDensity->setPen( QPen( Qt::red, 1 ) );
100 if( Plot2d_QwtLegendLabel* anItem = getLegendLabel( myDensity ) ) {
101 anItem->setPen( densityPen );
105 myDistr = new QwtPlotCurve( QString() );
106 myDistr->attach( this );
107 QPen distrPen = QPen( Qt::blue, 1 );
108 QwtSymbol* distrSymbol = new QwtSymbol( QwtSymbol::XCross, QBrush( Qt::blue ),
109 QPen( Qt::blue ), QSize( 5, 5 ) );
110 myDistr->setPen( distrPen );
111 myDistr->setSymbol( distrSymbol );
112 if( Plot2d_QwtLegendLabel* anItem = getLegendLabel( myDistr ) ) {
113 anItem->setPen( distrPen );
114 anItem->setSymbol( distrSymbol );
118 myMsg = new QwtPlotMarker();
119 myMsg->attach( this );
120 myMsg->setValue( 0.5, 0.5 );
121 QwtText mt = myMsg->label();
122 mt.setBorderPen( QPen( Qt::red, 1 ) );
124 f.setPointSize( 14 ); //f.setBold( true );
126 myMsg->setLabel( mt );
128 enableAxis(QwtPlot::yLeft, false);
129 enableAxis(QwtPlot::yRight, true);
132 axisFont.setPointSize( 8 );
133 setAxisFont(QwtPlot::yRight, axisFont);
134 setAxisFont(QwtPlot::xBottom, axisFont);
136 myDensity->setYAxis(QwtPlot::yRight);
137 myDistr->setYAxis(QwtPlot::yRight);
138 myMsg->setYAxis(QwtPlot::yRight);
139 myDensity->setTitle( tr( "SMESH_DENSITY_FUNC" ) );
140 myDistr->setTitle( tr( "SMESH_DISTR" ) );
142 QwtPlotGrid* aGrid = new QwtPlotGrid();
143 QPen aMajPen = aGrid->majorPen();
144 aMajPen.setStyle( Qt::DashLine );
145 aGrid->setPen( aMajPen );
147 aGrid->enableX( true );
148 aGrid->enableY( true );
150 aGrid->attach( this );
153 StdMeshersGUI_DistrPreview::~StdMeshersGUI_DistrPreview()
157 bool StdMeshersGUI_DistrPreview::isTableFunc() const
162 void StdMeshersGUI_DistrPreview::tableFunc( SMESH::double_array& f ) const
167 QString StdMeshersGUI_DistrPreview::function() const
172 int StdMeshersGUI_DistrPreview::nbSeg() const
177 int StdMeshersGUI_DistrPreview::pointsCount() const
182 void StdMeshersGUI_DistrPreview::setConversion( Conversion conv, const bool upd )
189 bool StdMeshersGUI_DistrPreview::setParams( const QString& func, const int nbSeg, const int points, const bool upd )
192 myTableFunc = SMESH::double_array();
193 myFunction = func.isEmpty() ? "0" : func;
194 myPoints = points>0 ? points : 2;
195 myNbSeg = nbSeg>0 ? nbSeg : 1;
196 bool res = init( func );
202 bool StdMeshersGUI_DistrPreview::setParams( const SMESH::double_array& f, const int nbSeg, const bool upd )
206 if( myTableFunc.length()%2==1 )
207 myTableFunc.length( myTableFunc.length()-1 );
210 myPoints = myTableFunc.length()/2;
211 myNbSeg = nbSeg>0 ? nbSeg : 1;
216 return myTableFunc.length()>0;
219 bool StdMeshersGUI_DistrPreview::createTable( SMESH::double_array& func )
221 if( myExpr.IsNull() )
227 const double xmin = 0.0, xmax = 1.0;
229 double d = (xmax-xmin)/double(myPoints-1);
230 func.length( 2*myPoints );
232 for( int i=0, j=0; i<myPoints; j++ )
235 double t = xmin + d*j, f = funcValue( t, ok );
245 func.length( func.length()-2*err );
249 void StdMeshersGUI_DistrPreview::update()
251 Kernel_Utils::Localizer loc;
252 SMESH::double_array graph, distr;
259 myIsDone = createTable( graph );
261 if( graph.length()>=2 )
263 StdMeshers::StdMeshers_NumberOfSegments_var h =
264 StdMeshers::StdMeshers_NumberOfSegments::_narrow( myHypo );
266 if( !CORBA::is_nil( h.in() ) )
268 SMESH::double_array* arr = 0;
270 arr = h->BuildDistributionTab( myTableFunc, myNbSeg, ( int )myConv );
272 arr = h->BuildDistributionExpr( myFunction.toLatin1().data(), myNbSeg, ( int )myConv );
281 bool correct = graph.length()>=2 && distr.length()>=2;
289 QwtText mt = myMsg->label();
290 mt.setText( QString() );
291 myMsg->setLabel( mt );
294 int size = graph.length()/2;
295 double* x = new double[size], *y = new double[size];
296 double min_x, max_x, min_y, max_y;
297 for( int i=0; i<size; i++ )
301 if( !convert( y[i] ) )
303 min_x = 0.0; max_x = 1.0; min_y = 0.0; max_y = 1.0;
304 delete[] x; delete[] y;
310 if ( std::fabs(y[i]) >= HUGE_VAL)
311 y[i] = HUGE_VAL/100.;
314 y[i] = std::numeric_limits<double>::max()/100.;
318 if( i==0 || y[i]<min_y )
320 if( i==0 || y[i]>max_y )
322 if( i==0 || x[i]<min_x )
324 if( i==0 || x[i]>max_x )
328 setAxisScale( myDensity->xAxis(), min_x, max_x );
329 setAxisScale( myDensity->yAxis(),
334 std::min( 0.0, min_y ),
335 std::max( 0.0, max_y )
338 myDensity->setSamples( x, y, size );
345 size = distr.length();
346 x = new double[size];
347 y = new double[size];
348 for( int i=0; i<size; i++ )
353 myDistr->setSamples( x, y, size );
363 } catch(Standard_Failure) {
364 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
368 void StdMeshersGUI_DistrPreview::showError()
370 setAxisScale( myDensity->xAxis(), 0.0, 1.0 );
371 setAxisScale( myDensity->yAxis(), 0.0, 1.0 );
372 myDensity->setSamples( 0, 0, 0 );
373 myDistr->setSamples( 0, 0, 0 );
374 QwtText mt = myMsg->label();
375 mt.setText( tr( "SMESH_INVALID_FUNCTION" ) );
376 myMsg->setLabel( mt );
380 bool isCorrectArg( const Handle( Expr_GeneralExpression )& expr )
382 Handle( Expr_NamedUnknown ) sub = Handle( Expr_NamedUnknown )::DownCast( expr );
384 return sub->GetName()=="t";
387 for( int i=1, n=expr->NbSubExpressions(); i<=n && res; i++ )
389 Handle( Expr_GeneralExpression ) sub = expr->SubExpression( i );
390 Handle( Expr_NamedUnknown ) name = Handle( Expr_NamedUnknown )::DownCast( sub );
393 if( name->GetName()!="t" )
397 res = isCorrectArg( sub );
402 bool StdMeshersGUI_DistrPreview::init( const QString& str )
404 Kernel_Utils::Localizer loc;
405 bool parsed_ok = true;
410 myExpr = ExprIntrp_GenExp::Create();
411 myExpr->Process( ( Standard_CString ) str.toLatin1().data() );
412 } catch(Standard_Failure) {
413 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
417 bool syntax = false, args = false;
418 if( parsed_ok && myExpr->IsDone() )
421 args = isCorrectArg( myExpr->Expression() );
424 bool res = parsed_ok && syntax && args;
430 double StdMeshersGUI_DistrPreview::funcValue( const double t, bool& ok )
432 if( myExpr.IsNull() )
435 myValues.ChangeValue( 1 ) = t;
438 double res = calc( ok );
443 double StdMeshersGUI_DistrPreview::calc( bool& ok )
452 res = myExpr->Expression()->Evaluate( myVars, myValues );
453 } catch(Standard_Failure) {
454 Handle(Standard_Failure) aFail = Standard_Failure::Caught();
462 bool StdMeshersGUI_DistrPreview::isDone() const
467 bool StdMeshersGUI_DistrPreview::convert( double& v ) const
478 // in StdMeshers_NumberOfSegments.cc
479 // const double PRECISION = 1e-7;
483 } catch(Standard_Failure) {
484 Handle(Standard_Failure) aFail = Standard_Failure::Caught();