Back to index

salome-gui  6.5.0
Plot2d_AnalyticalCurve.cxx
Go to the documentation of this file.
00001 // Copyright (C) 2007-2012  CEA/DEN, EDF R&D, OPEN CASCADE
00002 //
00003 // Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
00004 // CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
00005 //
00006 // This library is free software; you can redistribute it and/or
00007 // modify it under the terms of the GNU Lesser General Public
00008 // License as published by the Free Software Foundation; either
00009 // version 2.1 of the License.
00010 //
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014 // Lesser General Public License for more details.
00015 //
00016 // You should have received a copy of the GNU Lesser General Public
00017 // License along with this library; if not, write to the Free Software
00018 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
00019 //
00020 // See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
00021 //
00022 //  File   : Plot2d_AnalyticalCurve.cxx
00023 //  Author : Roman NIKOLAEV, Open CASCADE S.A.S. (roman.nikolaev@opencascade.com)
00024 
00025 #include "Plot2d_AnalyticalParser.h"
00026 #include "Plot2d_AnalyticalCurve.h"
00027 #include "Plot2d_PlotItems.h"
00028 #include "Plot2d_Object.h"
00029 
00030 #include <qwt_scale_div.h>
00031 
00032 
00033 //Init static data;
00034 
00035 int Plot2d_AnalyticalCurve::myNbCurves = 0;
00036 
00040 Plot2d_AnalyticalCurve::Plot2d_AnalyticalCurve() : 
00041   myAutoAssign(true),
00042   myColor( 0, 0, 0 ), 
00043   myMarker( Plot2d::Circle ), 
00044   myMarkerSize( 0 ), 
00045   myLine( Plot2d::Solid ), 
00046   myLineWidth( 0 ),
00047   myRangeBegin(0.0),
00048   myRangeEnd(100.0),
00049   myNbIntervals(100),
00050   myExpression(""),
00051   myAction(Plot2d_AnalyticalCurve::ActAddInView),
00052   myState(Plot2d_AnalyticalCurve::StateNeedUpdate),
00053   myCurve(0),
00054   myActive(true),
00055   myIsSelected(false)
00056 {
00057   myName = QString("Analytical Curve %1").arg(++myNbCurves);
00058 }
00059 
00060 
00064 Plot2d_AnalyticalCurve::~Plot2d_AnalyticalCurve()
00065 {
00066 }
00067 
00071 Plot2d_AnalyticalCurve::Plot2d_AnalyticalCurve( const Plot2d_AnalyticalCurve& curve )
00072 {
00073   myAutoAssign = curve.isAutoAssign();
00074   myColor      = curve.getColor();
00075   myMarker     = curve.getMarker();
00076   myMarkerSize = curve.getMarkerSize();
00077   myLine       = curve.getLine();
00078   myLineWidth  = curve.getLineWidth();
00079   myRangeBegin = curve.getRangeBegin();
00080   myRangeEnd   = curve.getRangeEnd();
00081   myNbIntervals= curve.getNbIntervals();
00082   myPoints     = curve.myPoints;
00083   myAction     = curve.getAction();
00084   myName       = curve.getName();
00085   myExpression = curve.getExpression();
00086   myState      = curve.state();
00087   myCurve      = curve.myCurve;
00088   myActive     = curve.isActive();
00089 }
00090 
00094 Plot2d_AnalyticalCurve& Plot2d_AnalyticalCurve::operator=( const Plot2d_AnalyticalCurve& curve )
00095 {
00096   myAutoAssign = curve.isAutoAssign();
00097   myColor      = curve.getColor();
00098   myMarker     = curve.getMarker();
00099   myMarkerSize = curve.getMarkerSize();
00100   myLine       = curve.getLine();
00101   myLineWidth  = curve.getLineWidth();
00102   myRangeBegin = curve.getRangeBegin();
00103   myRangeEnd   = curve.getRangeEnd();
00104   myNbIntervals= curve.getNbIntervals();
00105   myPoints     = curve.myPoints;
00106   myAction     = curve.getAction();
00107   myName       = curve.getName();
00108   myExpression = curve.getExpression();
00109   myState      = curve.state();
00110   myCurve      = curve.myCurve;
00111   myActive     = curve.isActive();
00112   return *this;
00113 }
00114 
00118 QwtPlotItem* Plot2d_AnalyticalCurve::plotItem()
00119 {
00120   if(!myCurve) {
00121     myCurve = new Plot2d_QwtPlotCurve(QString(""));
00122     updatePlotItem();
00123   }
00124   return myCurve;
00125 }
00126 
00130 void Plot2d_AnalyticalCurve::autoFill( const QwtPlot* thePlot )
00131 {
00132   QwtSymbol::Style typeMarker;
00133   QColor           color;
00134   Qt::PenStyle     typeLine;
00135   Plot2d::getNextMarker( QwtPlotItem::Rtti_PlotCurve, thePlot, typeMarker, color, typeLine );
00136   
00137   setColor( color );
00138   setLine( Plot2d::qwt2plotLine( typeLine ));
00139   setLineWidth(1);
00140   setMarker( Plot2d::qwt2plotMarker( typeMarker ) );
00141 }
00142 
00146 void Plot2d_AnalyticalCurve::updatePlotItem()
00147 {
00148   if ( !myCurve )
00149     return;
00150 
00151   Plot2d_QwtPlotCurve* aCurve = dynamic_cast<Plot2d_QwtPlotCurve*>(myCurve);
00152 
00153   if(!aCurve)
00154     return;
00155   
00156   Qt::PenStyle     ps = Plot2d::plot2qwtLine( getLine() );
00157   QwtSymbol::Style ms = Plot2d::plot2qwtMarker( getMarker() );
00158 
00159   QColor aColor = isSelected() ?  Plot2d_Object::selectionColor() : getColor();
00160   int lineW = getLineWidth(); 
00161   if ( isSelected() ) lineW += (lineW == 0 ? 3 : 2);
00162 
00163   int markerS = isSelected() ? getMarkerSize() + 2 : getMarkerSize();
00164 
00165   aCurve->setSelected(isSelected());
00166 
00167   aCurve->setPen( QPen(aColor , lineW, ps ) );
00168   aCurve->setSymbol( QwtSymbol( ms, QBrush( aColor ), 
00169                             QPen( aColor ), 
00170                             QSize( markerS , markerS ) ) );
00171 
00172   aCurve->setLegendPen(QPen(getColor(), getLineWidth(), ps ));
00173   aCurve->setLegendSymbol( QwtSymbol( ms, QBrush( getColor() ), 
00174                                   QPen( getColor() ), 
00175                                   QSize( getMarkerSize() , getMarkerSize() )));
00176 
00177   double *x, *y;
00178   long nb = getData( &x, &y );
00179   aCurve->setData( x, y, nb );
00180   aCurve->setTitle(getName());
00181 }
00182 
00183 
00187 void Plot2d_AnalyticalCurve::calculate() {
00188   if( state() == Plot2d_AnalyticalCurve::StateOk )
00189     return;
00190 
00191   if(myRangeBegin > myRangeEnd)
00192     return;
00193 
00194   Plot2d_AnalyticalParser* parser = Plot2d_AnalyticalParser::parser();
00195   double* x = 0;
00196   double* y = 0;
00197   int nb = parser->calculate(getExpression(), getRangeBegin(), getRangeEnd(),
00198                           getNbIntervals(),&x,&y);
00199   if( nb > 0 ) {
00200     myPoints.clear();
00201     for( int i = 0; i < nb; i++ ) {
00202       Plot2d_Point pnt(x[i], y[i]);
00203       myPoints.append(pnt);
00204     }
00205     delete x;
00206     delete y;
00207     myState = Plot2d_AnalyticalCurve::StateOk;
00208     setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
00209   }
00210 }
00214 bool Plot2d_AnalyticalCurve::checkCurve( const QwtPlot* thePlot) {
00215   if( !myExpression.isEmpty() && thePlot ) {
00216        const QwtScaleDiv* div = thePlot->axisScaleDiv(QwtPlot::xBottom);
00217        setRangeBegin(div->lowerBound());
00218        setRangeEnd(div->upperBound());
00219        calculate();
00220   }
00221   return myState == Plot2d_AnalyticalCurve::StateOk;
00222 }
00223 
00224 
00228 long Plot2d_AnalyticalCurve::getData( double** theX, double** theY ) const
00229 {
00230   int aNPoints = myPoints.size();
00231   *theX = new double[aNPoints];
00232   *theY = new double[aNPoints];
00233   for (int i = 0; i < aNPoints; i++) {
00234     (*theX)[i] = myPoints[i].x;
00235     (*theY)[i] = myPoints[i].y;
00236   }
00237   return aNPoints;
00238 }
00239 
00243 void Plot2d_AnalyticalCurve::setAutoAssign( bool on )
00244 {
00245   if( myAutoAssign != on ) {
00246     myAutoAssign = on;
00247     setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
00248   }
00249 }
00250 
00254 bool Plot2d_AnalyticalCurve::isAutoAssign() const
00255 {
00256   return myAutoAssign;
00257 }
00258 
00262 void Plot2d_AnalyticalCurve::setColor( const QColor& color )
00263 {
00264   if(myColor != color) {
00265     myColor = color;
00266     setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
00267   }
00268 }
00269 
00273 QColor Plot2d_AnalyticalCurve::getColor() const
00274 {
00275   return myColor;
00276 }
00277 
00278 
00282 void Plot2d_AnalyticalCurve::setMarker( Plot2d::MarkerType marker )
00283 {
00284   if(myMarker != marker) {
00285     myMarker = marker;
00286     setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
00287   }
00288 }
00289 
00293 Plot2d::MarkerType Plot2d_AnalyticalCurve::getMarker() const
00294 {
00295   return myMarker;
00296 }
00297 
00301 void Plot2d_AnalyticalCurve::setMarkerSize( const int theSize )
00302 {
00303   if( myMarkerSize != theSize ) {
00304     myMarkerSize = theSize < 0 ? 0 : theSize;
00305     setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
00306   }
00307 }
00308 
00312 int Plot2d_AnalyticalCurve::getMarkerSize() const
00313 {
00314   return myMarkerSize;
00315 }
00316 
00320 void Plot2d_AnalyticalCurve::setLine( Plot2d::LineType line )
00321 {
00322   if(myLine != line) {
00323     myLine = line;
00324     setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
00325   }
00326 }
00327 
00331 Plot2d::LineType Plot2d_AnalyticalCurve::getLine() const
00332 {
00333   return myLine;
00334 }
00335 
00336 
00340 void Plot2d_AnalyticalCurve::setLineWidth( const int lineWidth )
00341 {
00342   if( myLineWidth != lineWidth ) {
00343     myLineWidth = lineWidth < 0 ? 0 : lineWidth;
00344     setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
00345   }
00346 }
00347 
00351 int Plot2d_AnalyticalCurve::getLineWidth() const
00352 {
00353   return myLineWidth;
00354 }
00355 
00359 void Plot2d_AnalyticalCurve::setNbIntervals( const long nb )
00360 {
00361   if( myNbIntervals != nb ) {
00362     myNbIntervals = nb < 1 ? 1 : nb;
00363     myState = Plot2d_AnalyticalCurve::StateNeedUpdate;
00364   }
00365 }
00366 
00370 long Plot2d_AnalyticalCurve::getNbIntervals() const
00371 {
00372   return myNbIntervals;
00373 }
00374 
00378 void Plot2d_AnalyticalCurve::setRangeBegin( const double coord) {
00379   if( myRangeBegin != coord ) {
00380     myRangeBegin = coord;
00381     myState = Plot2d_AnalyticalCurve::StateNeedUpdate;
00382   }
00383 }
00384 
00388 double Plot2d_AnalyticalCurve::getRangeBegin() const {
00389   return myRangeBegin;
00390 }
00391 
00395 void Plot2d_AnalyticalCurve::setRangeEnd( const double coord) {
00396   if( myRangeEnd != coord ) {
00397     myRangeEnd = coord;
00398     myState = Plot2d_AnalyticalCurve::StateNeedUpdate;
00399   }
00400 }
00401 
00405 double Plot2d_AnalyticalCurve::getRangeEnd() const {
00406   return myRangeEnd;
00407 }
00408 
00412 void Plot2d_AnalyticalCurve::setExpression( const QString& expr ) {
00413   if( myExpression != expr ) {
00414     myExpression = expr;
00415     myState = Plot2d_AnalyticalCurve::StateNeedUpdate;
00416   }
00417 }
00418 
00422 QString Plot2d_AnalyticalCurve::getExpression() const {
00423   return  myExpression;
00424 }
00425 
00429 void Plot2d_AnalyticalCurve::setName( const QString& name ) {
00430   if( myName != name ) {    
00431     myName = name;
00432     setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
00433   }
00434 }
00435 
00439 QString Plot2d_AnalyticalCurve::getName() const {
00440   return myName;
00441 }
00442 
00443 
00447 void Plot2d_AnalyticalCurve::setAction(const int act) {
00448   if( act == Plot2d_AnalyticalCurve::ActNothing ) {
00449     myAction = act;
00450     return;
00451   }
00452   
00453   if(myAction != Plot2d_AnalyticalCurve::ActAddInView && 
00454      myAction != Plot2d_AnalyticalCurve::ActRemoveFromView) {
00455     myAction = act;  
00456   }
00457 }
00458 
00462 int Plot2d_AnalyticalCurve::getAction() const {
00463   return myAction;
00464 }
00465 
00469 int Plot2d_AnalyticalCurve::state() const {
00470   return myState;
00471 }
00472 
00476 void Plot2d_AnalyticalCurve::setActive(const bool on) {
00477   if( myActive != on ) {    
00478     if(myActive && !on)
00479       setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
00480     else if(!myActive && on) {
00481       setAction(Plot2d_AnalyticalCurve::ActAddInView);
00482       myState = Plot2d_AnalyticalCurve::StateNeedUpdate;
00483     }
00484     myActive = on;
00485   }  
00486 }
00487 
00491 bool Plot2d_AnalyticalCurve::isActive() const {
00492   return myActive;
00493 }
00494 
00495 
00499 void Plot2d_AnalyticalCurve::setSelected(const bool on) {
00500   if(myIsSelected != on) {
00501        myIsSelected  = on;
00502     setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
00503   }
00504 
00505 }
00506 
00510 bool Plot2d_AnalyticalCurve::isSelected() const {
00511   return myIsSelected;
00512 }