1 // Copyright (C) 2007-2019 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
22 // File : Plot2d_AnalyticalCurve.cxx
23 // Author : Roman NIKOLAEV, Open CASCADE S.A.S. (roman.nikolaev@opencascade.com)
25 #include "Plot2d_AnalyticalParser.h"
26 #include "Plot2d_AnalyticalCurve.h"
27 #include "Plot2d_PlotItems.h"
28 #include "Plot2d_Object.h"
30 #include <qwt_scale_div.h>
35 int Plot2d_AnalyticalCurve::myNbCurves = 0;
40 Plot2d_AnalyticalCurve::Plot2d_AnalyticalCurve() :
43 myMarker( Plot2d::Circle ),
45 myLine( Plot2d::Solid ),
51 myAction(Plot2d_AnalyticalCurve::ActAddInView),
52 myState(Plot2d_AnalyticalCurve::StateNeedUpdate),
57 myName = QString("Analytical Curve %1").arg(++myNbCurves);
64 Plot2d_AnalyticalCurve::~Plot2d_AnalyticalCurve()
69 Copy constructor. Makes deep copy of data
71 Plot2d_AnalyticalCurve::Plot2d_AnalyticalCurve( const Plot2d_AnalyticalCurve& curve )
73 myAutoAssign = curve.isAutoAssign();
74 myColor = curve.getColor();
75 myMarker = curve.getMarker();
76 myMarkerSize = curve.getMarkerSize();
77 myLine = curve.getLine();
78 myLineWidth = curve.getLineWidth();
79 myRangeBegin = curve.getRangeBegin();
80 myRangeEnd = curve.getRangeEnd();
81 myNbIntervals= curve.getNbIntervals();
82 myPoints = curve.myPoints;
83 myAction = curve.getAction();
84 myName = curve.getName();
85 myExpression = curve.getExpression();
86 myState = curve.state();
87 myCurve = curve.myCurve;
88 myActive = curve.isActive();
92 operator=. Makes deep copy of data
94 Plot2d_AnalyticalCurve& Plot2d_AnalyticalCurve::operator=( const Plot2d_AnalyticalCurve& curve )
96 myAutoAssign = curve.isAutoAssign();
97 myColor = curve.getColor();
98 myMarker = curve.getMarker();
99 myMarkerSize = curve.getMarkerSize();
100 myLine = curve.getLine();
101 myLineWidth = curve.getLineWidth();
102 myRangeBegin = curve.getRangeBegin();
103 myRangeEnd = curve.getRangeEnd();
104 myNbIntervals= curve.getNbIntervals();
105 myPoints = curve.myPoints;
106 myAction = curve.getAction();
107 myName = curve.getName();
108 myExpression = curve.getExpression();
109 myState = curve.state();
110 myCurve = curve.myCurve;
111 myActive = curve.isActive();
116 Create plot object for the curve
118 QwtPlotItem* Plot2d_AnalyticalCurve::plotItem()
121 myCurve = new Plot2d_QwtPlotCurve(QString(""));
128 Auto fill parameters of object by plot view
130 void Plot2d_AnalyticalCurve::autoFill( const QwtPlot* thePlot )
132 QwtSymbol::Style typeMarker;
134 Qt::PenStyle typeLine;
135 Plot2d::getNextMarker( QwtPlotItem::Rtti_PlotCurve, thePlot, typeMarker, color, typeLine );
138 setLine( Plot2d::qwt2plotLine( typeLine ));
140 setMarker( Plot2d::qwt2plotMarker( typeMarker ) );
146 void Plot2d_AnalyticalCurve::updatePlotItem()
151 Plot2d_QwtPlotCurve* aCurve = dynamic_cast<Plot2d_QwtPlotCurve*>(myCurve);
156 Qt::PenStyle ps = Plot2d::plot2qwtLine( getLine() );
157 QwtSymbol::Style ms = Plot2d::plot2qwtMarker( getMarker() );
159 QColor aColor = isSelected() ? Plot2d_Object::selectionColor() : getColor();
160 int lineW = getLineWidth();
161 if ( isSelected() ) lineW += (lineW == 0 ? 3 : 2);
163 int markerS = isSelected() ? getMarkerSize() + 2 : getMarkerSize();
165 aCurve->setSelected(isSelected());
167 aCurve->setPen( QPen(aColor , lineW, ps ) );
168 aCurve->setSymbol( new QwtSymbol( ms, QBrush( aColor ),
170 QSize( markerS , markerS ) ) );
172 aCurve->setLegendPen(QPen(getColor(), getLineWidth(), ps ));
173 aCurve->setLegendSymbol( new QwtSymbol( ms, QBrush( getColor() ),
175 QSize( getMarkerSize() , getMarkerSize() )));
178 long nb = getData( &x, &y );
179 aCurve->setSamples( x, y, nb );
180 aCurve->setTitle(getName());
185 Calculate the curve points.
187 void Plot2d_AnalyticalCurve::calculate() {
188 if( state() == Plot2d_AnalyticalCurve::StateOk )
191 if(myRangeBegin > myRangeEnd)
194 Plot2d_AnalyticalParser* parser = Plot2d_AnalyticalParser::parser();
197 int nb = parser->calculate(getExpression(), getRangeBegin(), getRangeEnd(),
198 getNbIntervals(),&x,&y);
201 for( int i = 0; i < nb; i++ ) {
202 Plot2d_Point pnt(x[i], y[i]);
203 myPoints.append(pnt);
207 myState = Plot2d_AnalyticalCurve::StateOk;
208 setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
212 Checks that this curve can be computed for the input QwtPlot
214 bool Plot2d_AnalyticalCurve::checkCurve( const QwtPlot* thePlot) {
215 if( !myExpression.isEmpty() && thePlot ) {
216 const QwtScaleDiv div = thePlot->axisScaleDiv(QwtPlot::xBottom);
217 setRangeBegin(div.lowerBound());
218 setRangeEnd(div.upperBound());
221 return myState == Plot2d_AnalyticalCurve::StateOk;
228 long Plot2d_AnalyticalCurve::getData( double** theX, double** theY ) const
230 int aNPoints = myPoints.size();
231 *theX = new double[aNPoints];
232 *theY = new double[aNPoints];
233 for (int i = 0; i < aNPoints; i++) {
234 (*theX)[i] = myPoints[i].x;
235 (*theY)[i] = myPoints[i].y;
241 Sets curves's AutoAssign flag - in this case attributes will be set automatically
243 void Plot2d_AnalyticalCurve::setAutoAssign( bool on )
245 if( myAutoAssign != on ) {
247 setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
252 Gets curve's AutoAssign flag state
254 bool Plot2d_AnalyticalCurve::isAutoAssign() const
262 void Plot2d_AnalyticalCurve::setColor( const QColor& color )
264 if(myColor != color) {
266 setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
273 QColor Plot2d_AnalyticalCurve::getColor() const
280 Sets marker type ( and resets AutoAssign flag )
282 void Plot2d_AnalyticalCurve::setMarker( Plot2d::MarkerType marker )
284 if(myMarker != marker) {
286 setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
293 Plot2d::MarkerType Plot2d_AnalyticalCurve::getMarker() const
301 void Plot2d_AnalyticalCurve::setMarkerSize( const int theSize )
303 if( myMarkerSize != theSize ) {
304 myMarkerSize = theSize < 0 ? 0 : theSize;
305 setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
312 int Plot2d_AnalyticalCurve::getMarkerSize() const
320 void Plot2d_AnalyticalCurve::setLine( Plot2d::LineType line )
324 setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
331 Plot2d::LineType Plot2d_AnalyticalCurve::getLine() const
340 void Plot2d_AnalyticalCurve::setLineWidth( const int lineWidth )
342 if( myLineWidth != lineWidth ) {
343 myLineWidth = lineWidth < 0 ? 0 : lineWidth;
344 setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
351 int Plot2d_AnalyticalCurve::getLineWidth() const
357 Sets number of points
359 void Plot2d_AnalyticalCurve::setNbIntervals( const long nb )
361 if( myNbIntervals != nb ) {
362 myNbIntervals = nb < 1 ? 1 : nb;
363 myState = Plot2d_AnalyticalCurve::StateNeedUpdate;
368 Gets number of points
370 long Plot2d_AnalyticalCurve::getNbIntervals() const
372 return myNbIntervals;
376 Sets X coordinate of the first curve points
378 void Plot2d_AnalyticalCurve::setRangeBegin( const double coord) {
379 if( myRangeBegin != coord ) {
380 myRangeBegin = coord;
381 myState = Plot2d_AnalyticalCurve::StateNeedUpdate;
386 Gets X coordinate of the first curve points
388 double Plot2d_AnalyticalCurve::getRangeBegin() const {
393 Sets X coordinate of the last curve points
395 void Plot2d_AnalyticalCurve::setRangeEnd( const double coord) {
396 if( myRangeEnd != coord ) {
398 myState = Plot2d_AnalyticalCurve::StateNeedUpdate;
403 Gets X coordinate of the last curve points
405 double Plot2d_AnalyticalCurve::getRangeEnd() const {
410 Sets the curve expression.
412 void Plot2d_AnalyticalCurve::setExpression( const QString& expr ) {
413 if( myExpression != expr ) {
415 myState = Plot2d_AnalyticalCurve::StateNeedUpdate;
420 Gets the curve expression.
422 QString Plot2d_AnalyticalCurve::getExpression() const {
429 void Plot2d_AnalyticalCurve::setName( const QString& name ) {
430 if( myName != name ) {
432 setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
439 QString Plot2d_AnalyticalCurve::getName() const {
445 Sets the curve action.
447 void Plot2d_AnalyticalCurve::setAction(const int act) {
448 if( act == Plot2d_AnalyticalCurve::ActNothing ) {
453 if(myAction != Plot2d_AnalyticalCurve::ActAddInView &&
454 myAction != Plot2d_AnalyticalCurve::ActRemoveFromView) {
460 Gets the curve action.
462 int Plot2d_AnalyticalCurve::getAction() const {
467 Gets the curve state.
469 int Plot2d_AnalyticalCurve::state() const {
474 Sets the curve active status.
476 void Plot2d_AnalyticalCurve::setActive(const bool on) {
477 if( myActive != on ) {
479 setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
480 else if(!myActive && on) {
481 setAction(Plot2d_AnalyticalCurve::ActAddInView);
482 myState = Plot2d_AnalyticalCurve::StateNeedUpdate;
489 Gets the curve active status.
491 bool Plot2d_AnalyticalCurve::isActive() const {
497 Sets curve's selected property.
499 void Plot2d_AnalyticalCurve::setSelected(const bool on) {
500 if(myIsSelected != on) {
502 setAction(Plot2d_AnalyticalCurve::ActUpdateInView);
508 Gets curve's selected property.
510 bool Plot2d_AnalyticalCurve::isSelected() const {