Salome HOME
Updated copyright comment
[modules/gui.git] / src / Plot2d / Plot2d_Curve.cxx
old mode 100755 (executable)
new mode 100644 (file)
index 25ed5ac..a5470fc
@@ -1,14 +1,44 @@
+// Copyright (C) 2007-2024  CEA, EDF, OPEN CASCADE
+//
+// Copyright (C) 2003-2007  OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN,
+// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS
+//
+// This library is free software; you can redistribute it and/or
+// modify it under the terms of the GNU Lesser General Public
+// License as published by the Free Software Foundation; either
+// version 2.1 of the License, or (at your option) any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+// Lesser General Public License for more details.
+//
+// You should have received a copy of the GNU Lesser General Public
+// License along with this library; if not, write to the Free Software
+// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
+//
+// See http://www.salome-platform.org/ or email : webmaster.salome@opencascade.com
+//
+//  File   : Plot2d_Curve.cxx
+//  Author : Vadim SANDLER, Open CASCADE S.A.S. (vadim.sandler@opencascade.com)
+
 #include "Plot2d_Curve.h"
-#include <qcolor.h>
+#include "Plot2d_PlotItems.h"
+#include <qwt_plot_curve.h>
+
+const int DEFAULT_LINE_WIDTH  =  0;     // (default) line width
+const int DEFAULT_MARKER_SIZE =  9;     // default marker size
 
 /*!
   Constructor
 */
 Plot2d_Curve::Plot2d_Curve()
-: myHorTitle( "" ), myVerTitle( "" ), 
-myHorUnits( "" ), myVerUnits( "" ), 
-myAutoAssign( true ), myColor( 0,0,0 ), myMarker( Circle ), myLine( Solid ), myLineWidth( 0 ),
-myYAxis( QwtPlot::yLeft )
+: Plot2d_Object(),
+  myColor( 0, 0, 0 ), 
+  myMarker( Plot2d::Circle ), 
+  myMarkerSize( 0 ), 
+  myLine( Plot2d::Solid ), 
+  myLineWidth( 0 )
 {
 }
 
@@ -20,313 +50,316 @@ Plot2d_Curve::~Plot2d_Curve()
 }
 
 /*!
-  Copy constructor. Makes deep copy of data.
+  Copy constructor. Makes deep copy of data
 */
 Plot2d_Curve::Plot2d_Curve( const Plot2d_Curve& curve )
+: Plot2d_Object( curve )
 {
-  myAutoAssign = curve.isAutoAssign();
-  myHorTitle   = curve.getHorTitle();
-  myVerTitle   = curve.getVerTitle();
-  myHorUnits   = curve.getHorUnits();
-  myVerUnits   = curve.getVerUnits();
   myColor      = curve.getColor();
   myMarker     = curve.getMarker();
+  myMarkerSize = curve.getMarkerSize();
   myLine       = curve.getLine();
   myLineWidth  = curve.getLineWidth();
-  myPoints     = curve.getPointList();
 }
 
 /*!
-  operator=. Makes deep copy of data.
+  operator=. Makes deep copy of data
 */
 Plot2d_Curve& Plot2d_Curve::operator=( const Plot2d_Curve& curve )
 {
-  myAutoAssign = curve.isAutoAssign();
-  myHorTitle   = curve.getHorTitle();
-  myVerTitle   = curve.getVerTitle();
-  myHorUnits   = curve.getHorUnits();
-  myVerUnits   = curve.getVerUnits();
+  Plot2d_Object::operator=(curve);
   myColor      = curve.getColor();
   myMarker     = curve.getMarker();
+  myMarkerSize = curve.getMarkerSize();
   myLine       = curve.getLine();
   myLineWidth  = curve.getLineWidth();
-  myPoints     = curve.getPointList();
   return *this;
 }
 
 /*!
-  Sets curve's horizontal title
+  Get typeid for the plot2d curve class
 */
-void Plot2d_Curve::setHorTitle( const QString& title )
+int Plot2d_Curve::rtti()
 {
-  myHorTitle = title;
+  return QwtPlotItem::Rtti_PlotCurve;
 }
 
 /*!
-  Gets curve's horizontal title
+  Create plot object for the curve
 */
-QString Plot2d_Curve::getHorTitle() const
+QwtPlotItem* Plot2d_Curve::createPlotItem()
 {
-  return myHorTitle;
+  QwtPlotCurve* aCurve = new Plot2d_QwtPlotCurve( getVerTitle(), getYAxis() );
+  updatePlotItem( aCurve );
+  return aCurve;
 }
 
 /*!
-  Sets curve's vertical title
+  Auto fill parameters of object by plot view
 */
-void Plot2d_Curve::setVerTitle( const QString& title )
+void Plot2d_Curve::autoFill( const QwtPlot* thePlot )
 {
-  myVerTitle = title;
-}
+  QwtSymbol::Style typeMarker;
+  QColor           color;
+  Qt::PenStyle     typeLine;
+  Plot2d::getNextMarker( rtti(), thePlot, typeMarker, color, typeLine );
 
-/*!
-  Gets curve's vertical title
-*/
-QString Plot2d_Curve::getVerTitle() const
-{
-  return myVerTitle;
+  setColor( color );
+  setLine( Plot2d::qwt2plotLine( typeLine ), DEFAULT_LINE_WIDTH );
+  setMarker( Plot2d::qwt2plotMarker( typeMarker ) );
 }
 
 /*!
-  Sets curve's horizontal units
+  Updates curve fields
 */
-void Plot2d_Curve::setHorUnits( const QString& units )
+void Plot2d_Curve::updatePlotItem( QwtPlotItem* theItem )
 {
-  myHorUnits = units;
-}
+  if ( theItem->rtti() != rtti() )
+    return;
 
-/*!
-  Gets curve's horizontal units
-*/
-QString Plot2d_Curve::getHorUnits() const
-{
-  return myHorUnits;
-}
+  Plot2d_QwtPlotCurve* aCurve = dynamic_cast<Plot2d_QwtPlotCurve*>( theItem );
+  if ( !aCurve )
+    return;
 
-/*!
-  Sets curve's vertical units
-*/
-void Plot2d_Curve::setVerUnits( const QString& units )
-{
-  myVerUnits = units;
-}
+  Plot2d_Object::updatePlotItem( theItem );
 
-/*!
-  Gets curve's vertical units
-*/
-QString Plot2d_Curve::getVerUnits() const
-{
-  return myVerUnits;
-}
+  Qt::PenStyle     ps = Plot2d::plot2qwtLine( getLine() );
+  QwtSymbol::Style ms = Plot2d::plot2qwtMarker( getMarker() );
+  
+  QColor aColor = isSelected() ?  Plot2d_Object::selectionColor() : getColor();
+  int lineW = getLineWidth(); 
+  if ( isSelected() ) lineW += (lineW == 0 ? 3 : 2);
 
-/*!
-  Adds one point for curve.
-*/
-void Plot2d_Curve::addPoint(double theX, double theY)
-{
-  Plot2d_Point aPoint;
-  aPoint.x = theX;
-  aPoint.y = theY;
-  myPoints.append(aPoint);
-}
+  int markerS = isSelected() ? getMarkerSize() + 2 : getMarkerSize();
 
-/*!
-  Insert one point for curve on some position.
-*/
-void Plot2d_Curve::insertPoint(int thePos, double theX, double theY)
-{
-  Plot2d_Point aPoint;
-  aPoint.x = theX;
-  aPoint.y = theY;
-
-  QValueList<Plot2d_Point>::iterator aIt;
-  int aCurrent = 0;
-  for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
-    if (thePos == aCurrent) {
-      myPoints.insert(aIt, aPoint);
-      return;
+  aCurve->setSelected(isSelected());
+
+  aCurve->setPen( QPen(aColor , lineW, ps ) );
+  aCurve->setSymbol( new QwtSymbol( ms, QBrush( aColor ), 
+                                    QPen( aColor ),
+                                    QSize( markerS , markerS ) ) );
+
+  aCurve->setLegendPen(QPen(getColor(), getLineWidth(), ps ));
+  aCurve->setLegendSymbol( new QwtSymbol( ms, QBrush( getColor() ),
+                                          QPen( getColor() ),
+                                          QSize( getMarkerSize() , getMarkerSize() )));
+  
+  double *x, *y, *min, *max;
+  long nb = getData( &x, &y );
+  if(nb > 0 && x && y) {
+    aCurve->setSamples( x, y, nb );
+    delete [] x;
+    delete [] y;
+    QList<int> idx;
+    getDeviationData(min, max, idx);
+    if(idx.size() > 0 && min && max) {
+      aCurve->setDeviationData(min,max,idx);
+      delete min;
+      delete max;
+    } else {
+      aCurve->clearDeviationData();
     }
-    aCurrent++;  
+  } else {
+    aCurve->setSamples( NULL, NULL, 0 );
   }
-  myPoints.append(aPoint);
 }
 
 /*!
-  Delete one point for curve on some position.
+  Sets curve's color ( and resets AutoAssign flag )
 */
-void Plot2d_Curve::deletePoint(int thePos)
+void Plot2d_Curve::setColor( const QColor& color )
 {
-  QValueList<Plot2d_Point>::iterator aIt;
-  int aCurrent = 0;
-  for(aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
-    if (thePos == aCurrent) {
-      myPoints.remove(aIt);
-      return;
-    }
-    aCurrent++;  
-  }
+  myColor = color;
+  setAutoAssign( false );
 }
 
 /*!
-  Remove all points for curve.
+  Gets curve's color
 */
-void Plot2d_Curve::clearAllPoints()
+QColor Plot2d_Curve::getColor() const
 {
-  myPoints.clear();
+  return myColor;
 }
 
 /*!
-  Gets curve's data : abscissas of points
+  Sets marker type and size ( and resets AutoAssign flag )
 */
-pointList Plot2d_Curve::getPointList() const
+void Plot2d_Curve::setMarker( Plot2d::MarkerType marker, const int markerSize )
 {
-  return myPoints;
+  setMarker( marker );
+  setMarkerSize( markerSize );
+  setAutoAssign( false );
 }
 
 /*!
-  Sets curve's data. 
+  Sets marker type ( and resets AutoAssign flag )
 */
-void Plot2d_Curve::setData( const double* hData, const double* vData, long size )
+void Plot2d_Curve::setMarker( Plot2d::MarkerType marker )
 {
-  clearAllPoints();
-  for(long i = 0; i < size; i++) addPoint(hData[i], vData[i]);
+  myMarker = marker;
+  setAutoAssign( false );
 }
 
-/*!
-  Gets curve's data : abscissas of points
-*/
-double* Plot2d_Curve::horData() const
+/* Sets Qwt marker type
+ */
+void Plot2d_Curve::setMarkerStyle( QwtSymbol::Style style)
 {
-  int aNPoints = nbPoints();
-  double* aX = new double[aNPoints];
-  for (int i = 0; i < aNPoints; i++) {
-    aX[i] = myPoints[i].x;
-  }
-  return aX;
+  myMarkerStyle = style;
 }
 
 /*!
-  Gets curve's data : ordinates of points
+  Gets marker type
 */
-double* Plot2d_Curve::verData() const
+Plot2d::MarkerType Plot2d_Curve::getMarker() const
 {
-  int aNPoints = nbPoints();
-  double* aY = new double[aNPoints];
-  for (int i = 0; i < aNPoints; i++) {
-    aY[i] = myPoints[i].y;
-  }
-  return aY;
+  return myMarker;
 }
 
-/*!
-  Gets curve's data : number of points
-*/
-int Plot2d_Curve::nbPoints() const
+
+/* Gets Qwt marker type
+ */
+QwtSymbol::Style Plot2d_Curve::getMarkerStyle() const
 {
-  return myPoints.count();
+  return myMarkerStyle;
 }
 
+
+
 /*!
-  Returns true if curve has no data
+  Sets new marker size ( and resets AutoAssign flag )
 */
-bool Plot2d_Curve::isEmpty() const
+void Plot2d_Curve::setMarkerSize( const int theSize )
 {
-  return myPoints.isEmpty();
+  myMarkerSize = theSize < 0 ? 0 : theSize;
+  setAutoAssign( false );
 }
 
 /*!
-  Sets curve's AutoAssign flag - in this case attributes will be set automatically
+  Gets marker size
 */
-void Plot2d_Curve::setAutoAssign( bool on )
+int Plot2d_Curve::getMarkerSize() const
 {
-  myAutoAssign = on;
+  return myMarkerSize;
 }
 
 /*!
-  Gets curve's AutoAssign flag state
+  Sets line type and width ( and resets AutoAssign flag )
+  NOTE : A line width of 0 will produce a 1 pixel wide line using a fast algorithm for diagonals. 
+         A line width of 1 will also produce a 1 pixel wide line, but uses a slower more accurate 
+         algorithm for diagonals. 
+         For horizontal and vertical lines a line width of 0 is the same as a line width of 1.
 */
-bool Plot2d_Curve::isAutoAssign() const
+void Plot2d_Curve::setLine( Plot2d::LineType line, const int lineWidth )
 {
-  return myAutoAssign;
+  setLine( line );
+  setLineWidth( lineWidth );
+  setAutoAssign( false );
 }
 
 /*!
-  Sets curve's color ( and resets AutoAssign flag )
+  Sets line type ( and resets AutoAssign flag )
 */
-void Plot2d_Curve::setColor( const QColor& color )
+void Plot2d_Curve::setLine( Plot2d::LineType line )
 {
-  myColor = color;
-  myAutoAssign = false;
+  myLine = line;
+  setAutoAssign( false );
 }
 
 /*!
-  Gets curve's color
+  Gets line type
 */
-QColor Plot2d_Curve::getColor() const
+Plot2d::LineType Plot2d_Curve::getLine() const
 {
-  return myColor;
+  return myLine;
 }
 
 /*!
-  Sets curve's marker ( and resets AutoAssign flag )
+  Sets line width ( and resets AutoAssign flag )
 */
-void Plot2d_Curve::setMarker( MarkerType marker )
+void Plot2d_Curve::setLineWidth( const int lineWidth )
 {
-  myMarker = marker;
-  myAutoAssign = false;
+  myLineWidth = lineWidth < 0 ? 0 : lineWidth;
+  setAutoAssign( false );
 }
 
 /*!
-  Gets curve's marker
+  Gets line width
 */
-Plot2d_Curve::MarkerType Plot2d_Curve::getMarker() const
+int Plot2d_Curve::getLineWidth() const
 {
-  return myMarker;
+  return myLineWidth;
 }
-
 /*!
-  Sets curve's line type and width ( and resets AutoAssign flag )
-  NOTE : A line width of 0 will produce a 1 pixel wide line using a fast algorithm for diagonals. 
-         A line width of 1 will also produce a 1 pixel wide line, but uses a slower more accurate 
-         algorithm for diagonals. 
-         For horizontal and vertical lines a line width of 0 is the same as a line width of 1.
+  Sets deviation data on the curve.
 */
-void Plot2d_Curve::setLine( LineType line, const int lineWidth )
-{
-  myLine = line;
-  myLineWidth = lineWidth;
-  if ( myLineWidth < 0 ) myLineWidth = 0;
-  myAutoAssign = false;
+void Plot2d_Curve::setDeviationData( const double* min, const double* max,const QList<int>& idx) {
+  for( int i = 0; i < idx.size(); i++ ) {
+    if(idx[i] < myPoints.size()) {
+      myPoints[idx[i]].setDeviation(min[i], max[i]);
+    }
+  }
 }
 
 /*!
-  Gets curve's line type
+  Gets object's data
 */
-Plot2d_Curve::LineType Plot2d_Curve::getLine() const
+void Plot2d_Curve::getDeviationData( double*& theMin, double*& theMax, QList<int>& idx) const
 {
-  return myLine;
+  int aNb = 0;
+  idx.clear();
+  for (int i = 0; i < nbPoints(); i++)
+    if(myPoints[i].hasDeviation())
+      aNb++;
+  if(aNb) {
+    double min, max;
+    theMin = new double[aNb];
+    theMax = new double[aNb];
+    for (int i = 0; i < nbPoints(); i++)
+      if(myPoints[i].hasDeviation()) {
+        myPoints[i].deviation(min,max);
+        theMin[i] = min;
+        theMax[i] = max;
+        idx.push_back(i);
+      }
+  }
 }
 
 /*!
-  Gets curve's line width
+  Clear deviation data on the curve.
 */
-int Plot2d_Curve::getLineWidth() const
-{
-  return myLineWidth;
+void Plot2d_Curve::clearDeviationData() {
+  for( int i=0; i < myPoints.size(); i++ )
+    myPoints[i].clearDeviation();
 }
 
 /*!
-  Sets curve's y axis
+  Gets object's minimal ordinate
 */
-void Plot2d_Curve::setYAxis(QwtPlot::Axis theYAxis)
+double Plot2d_Curve::getMinY() const
 {
-  if(theYAxis == QwtPlot::yLeft || theYAxis == QwtPlot::yRight)
-    myYAxis = theYAxis;
+  double aMinY = 1e150;
+  pointList::const_iterator aIt;
+  double coeff = 0.0;
+  for (aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {    
+    aMinY = qMin( aMinY, myScale * (*aIt).y );
+    if((*aIt).minDeviation(coeff))
+      aMinY = qMin( aMinY, coeff );
+  }
+  return aMinY;
 }
 
 /*!
-  Gets curve's y axis
+  Gets object's maximal ordinate
 */
-QwtPlot::Axis Plot2d_Curve::getYAxis() const
+double Plot2d_Curve::getMaxY() const
 {
-  return myYAxis;
+  double aMaxY = -1e150;
+  pointList::const_iterator aIt;
+  double coeff = 0.0;
+  for (aIt = myPoints.begin(); aIt != myPoints.end(); ++aIt) {
+    aMaxY = qMax( aMaxY, myScale * (*aIt).y);
+    if((*aIt).maxDeviation(coeff))
+      aMaxY = qMax( aMaxY, coeff);
+  }
+  return aMaxY;
 }