qwt_plot_curve.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337
  1. /* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
  2. * Qwt Widget Library
  3. * Copyright (C) 1997 Josef Wilgen
  4. * Copyright (C) 2002 Uwe Rathmann
  5. *
  6. * This library is free software; you can redistribute it and/or
  7. * modify it under the terms of the Qwt License, Version 1.0
  8. *****************************************************************************/
  9. #ifndef QWT_PLOT_CURVE_H
  10. #define QWT_PLOT_CURVE_H
  11. #include "qwt_global.h"
  12. #include "qwt_plot_seriesitem.h"
  13. #include "qwt_series_data.h"
  14. #include "qwt_text.h"
  15. #include <qpen.h>
  16. #include <qstring.h>
  17. class QPainter;
  18. class QPolygonF;
  19. class QwtScaleMap;
  20. class QwtSymbol;
  21. class QwtCurveFitter;
  22. /*!
  23. \brief A plot item, that represents a series of points
  24. A curve is the representation of a series of points in the x-y plane.
  25. It supports different display styles, interpolation ( f.e. spline )
  26. and symbols.
  27. \par Usage
  28. <dl><dt>a) Assign curve properties</dt>
  29. <dd>When a curve is created, it is configured to draw black solid lines
  30. with in QwtPlotCurve::Lines style and no symbols.
  31. You can change this by calling
  32. setPen(), setStyle() and setSymbol().</dd>
  33. <dt>b) Connect/Assign data.</dt>
  34. <dd>QwtPlotCurve gets its points using a QwtSeriesData object offering
  35. a bridge to the real storage of the points ( like QAbstractItemModel ).
  36. There are several convenience classes derived from QwtSeriesData, that also store
  37. the points inside ( like QStandardItemModel ). QwtPlotCurve also offers
  38. a couple of variations of setSamples(), that build QwtSeriesData objects from
  39. arrays internally.</dd>
  40. <dt>c) Attach the curve to a plot</dt>
  41. <dd>See QwtPlotItem::attach()
  42. </dd></dl>
  43. \par Example:
  44. see examples/bode
  45. \sa QwtPointSeriesData, QwtSymbol, QwtScaleMap
  46. */
  47. class QWT_EXPORT QwtPlotCurve:
  48. public QwtPlotSeriesItem, public QwtSeriesStore<QPointF>
  49. {
  50. public:
  51. /*!
  52. Curve styles.
  53. \sa setStyle(), style()
  54. */
  55. enum CurveStyle
  56. {
  57. /*!
  58. Don't draw a curve. Note: This doesn't affect the symbols.
  59. */
  60. NoCurve = -1,
  61. /*!
  62. Connect the points with straight lines. The lines might
  63. be interpolated depending on the 'Fitted' attribute. Curve
  64. fitting can be configured using setCurveFitter().
  65. */
  66. Lines,
  67. /*!
  68. Draw vertical or horizontal sticks ( depending on the
  69. orientation() ) from a baseline which is defined by setBaseline().
  70. */
  71. Sticks,
  72. /*!
  73. Connect the points with a step function. The step function
  74. is drawn from the left to the right or vice versa,
  75. depending on the QwtPlotCurve::Inverted attribute.
  76. */
  77. Steps,
  78. /*!
  79. Draw dots at the locations of the data points. Note:
  80. This is different from a dotted line (see setPen()), and faster
  81. as a curve in QwtPlotCurve::NoStyle style and a symbol
  82. painting a point.
  83. */
  84. Dots,
  85. /*!
  86. Styles >= QwtPlotCurve::UserCurve are reserved for derived
  87. classes of QwtPlotCurve that overload drawCurve() with
  88. additional application specific curve types.
  89. */
  90. UserCurve = 100
  91. };
  92. /*!
  93. Attribute for drawing the curve
  94. \sa setCurveAttribute(), testCurveAttribute(), curveFitter()
  95. */
  96. enum CurveAttribute
  97. {
  98. /*!
  99. For QwtPlotCurve::Steps only.
  100. Draws a step function from the right to the left.
  101. */
  102. Inverted = 0x01,
  103. /*!
  104. Only in combination with QwtPlotCurve::Lines
  105. A QwtCurveFitter tries to
  106. interpolate/smooth the curve, before it is painted.
  107. \note Curve fitting requires temporary memory
  108. for calculating coefficients and additional points.
  109. If painting in QwtPlotCurve::Fitted mode is slow it might be better
  110. to fit the points, before they are passed to QwtPlotCurve.
  111. */
  112. Fitted = 0x02
  113. };
  114. //! Curve attributes
  115. typedef QFlags<CurveAttribute> CurveAttributes;
  116. /*!
  117. Attributes how to represent the curve on the legend
  118. \sa setLegendAttribute(), testLegendAttribute(),
  119. QwtPlotItem::legendData(), legendIcon()
  120. */
  121. enum LegendAttribute
  122. {
  123. /*!
  124. QwtPlotCurve tries to find a color representing the curve
  125. and paints a rectangle with it.
  126. */
  127. LegendNoAttribute = 0x00,
  128. /*!
  129. If the style() is not QwtPlotCurve::NoCurve a line
  130. is painted with the curve pen().
  131. */
  132. LegendShowLine = 0x01,
  133. /*!
  134. If the curve has a valid symbol it is painted.
  135. */
  136. LegendShowSymbol = 0x02,
  137. /*!
  138. If the curve has a brush a rectangle filled with the
  139. curve brush() is painted.
  140. */
  141. LegendShowBrush = 0x04
  142. };
  143. //! Legend attributes
  144. typedef QFlags<LegendAttribute> LegendAttributes;
  145. /*!
  146. Attributes to modify the drawing algorithm.
  147. The default setting enables ClipPolygons | FilterPoints
  148. \sa setPaintAttribute(), testPaintAttribute()
  149. */
  150. enum PaintAttribute
  151. {
  152. /*!
  153. Clip polygons before painting them. In situations, where points
  154. are far outside the visible area (f.e when zooming deep) this
  155. might be a substantial improvement for the painting performance
  156. */
  157. ClipPolygons = 0x01,
  158. /*!
  159. Tries to reduce the data that has to be painted, by sorting out
  160. duplicates, or paintings outside the visible area. Might have a
  161. notable impact on curves with many close points.
  162. Only a couple of very basic filtering algorithms are implemented.
  163. */
  164. FilterPoints = 0x02,
  165. /*!
  166. Minimize memory usage that is temporarily needed for the
  167. translated points, before they get painted.
  168. This might slow down the performance of painting
  169. */
  170. MinimizeMemory = 0x04,
  171. /*!
  172. Render the points to a temporary image and paint the image.
  173. This is a very special optimization for Dots style, when
  174. having a huge amount of points.
  175. With a reasonable number of points QPainter::drawPoints()
  176. will be faster.
  177. */
  178. ImageBuffer = 0x08
  179. };
  180. //! Paint attributes
  181. typedef QFlags<PaintAttribute> PaintAttributes;
  182. explicit QwtPlotCurve( const QString &title = QString() );
  183. explicit QwtPlotCurve( const QwtText &title );
  184. virtual ~QwtPlotCurve();
  185. virtual int rtti() const;
  186. void setPaintAttribute( PaintAttribute, bool on = true );
  187. bool testPaintAttribute( PaintAttribute ) const;
  188. void setLegendAttribute( LegendAttribute, bool on = true );
  189. bool testLegendAttribute( LegendAttribute ) const;
  190. #ifndef QWT_NO_COMPAT
  191. void setRawSamples( const double *xData, const double *yData, int size );
  192. void setSamples( const double *xData, const double *yData, int size );
  193. void setSamples( const QVector<double> &xData, const QVector<double> &yData );
  194. #endif
  195. void setSamples( const QVector<QPointF> & );
  196. void setSamples( QwtSeriesData<QPointF> * );
  197. int closestPoint( const QPoint &pos, double *dist = NULL ) const;
  198. double minXValue() const;
  199. double maxXValue() const;
  200. double minYValue() const;
  201. double maxYValue() const;
  202. void setCurveAttribute( CurveAttribute, bool on = true );
  203. bool testCurveAttribute( CurveAttribute ) const;
  204. void setPen( const QColor &, qreal width = 0.0, Qt::PenStyle = Qt::SolidLine );
  205. void setPen( const QPen & );
  206. const QPen &pen() const;
  207. void setBrush( const QBrush & );
  208. const QBrush &brush() const;
  209. void setBaseline( double );
  210. double baseline() const;
  211. void setStyle( CurveStyle style );
  212. CurveStyle style() const;
  213. void setSymbol( QwtSymbol * );
  214. const QwtSymbol *symbol() const;
  215. void setCurveFitter( QwtCurveFitter * );
  216. QwtCurveFitter *curveFitter() const;
  217. virtual void drawSeries( QPainter *,
  218. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
  219. const QRectF &canvasRect, int from, int to ) const;
  220. virtual QwtGraphic legendIcon( int index, const QSizeF & ) const;
  221. protected:
  222. void init();
  223. virtual void drawCurve( QPainter *, int style,
  224. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
  225. const QRectF &canvasRect, int from, int to ) const;
  226. virtual void drawSymbols( QPainter *, const QwtSymbol &,
  227. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
  228. const QRectF &canvasRect, int from, int to ) const;
  229. virtual void drawLines( QPainter *,
  230. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
  231. const QRectF &canvasRect, int from, int to ) const;
  232. virtual void drawSticks( QPainter *,
  233. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
  234. const QRectF &canvasRect, int from, int to ) const;
  235. virtual void drawDots( QPainter *,
  236. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
  237. const QRectF &canvasRect, int from, int to ) const;
  238. virtual void drawSteps( QPainter *,
  239. const QwtScaleMap &xMap, const QwtScaleMap &yMap,
  240. const QRectF &canvasRect, int from, int to ) const;
  241. virtual void fillCurve( QPainter *,
  242. const QwtScaleMap &, const QwtScaleMap &,
  243. const QRectF &canvasRect, QPolygonF & ) const;
  244. void closePolyline( QPainter *,
  245. const QwtScaleMap &, const QwtScaleMap &, QPolygonF & ) const;
  246. private:
  247. class PrivateData;
  248. PrivateData *d_data;
  249. };
  250. //! boundingRect().left()
  251. inline double QwtPlotCurve::minXValue() const
  252. {
  253. return boundingRect().left();
  254. }
  255. //! boundingRect().right()
  256. inline double QwtPlotCurve::maxXValue() const
  257. {
  258. return boundingRect().right();
  259. }
  260. //! boundingRect().top()
  261. inline double QwtPlotCurve::minYValue() const
  262. {
  263. return boundingRect().top();
  264. }
  265. //! boundingRect().bottom()
  266. inline double QwtPlotCurve::maxYValue() const
  267. {
  268. return boundingRect().bottom();
  269. }
  270. Q_DECLARE_OPERATORS_FOR_FLAGS( QwtPlotCurve::PaintAttributes )
  271. Q_DECLARE_OPERATORS_FOR_FLAGS( QwtPlotCurve::LegendAttributes )
  272. Q_DECLARE_OPERATORS_FOR_FLAGS( QwtPlotCurve::CurveAttributes )
  273. #endif