qwt_plot_curve.h 10.1 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13
/* -*- mode: C++ ; c-file-style: "stroustrup" -*- *****************************
 * Qwt Widget Library
 * Copyright (C) 1997   Josef Wilgen
 * Copyright (C) 2002   Uwe Rathmann
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the Qwt License, Version 1.0
 *****************************************************************************/

#ifndef QWT_PLOT_CURVE_H
#define QWT_PLOT_CURVE_H

#include "qwt_global.h"
Bryant's avatar
Bryant committed
14 15
#include "qwt_plot_seriesitem.h"
#include "qwt_series_data.h"
pixhawk's avatar
pixhawk committed
16
#include "qwt_text.h"
Bryant's avatar
Bryant committed
17 18
#include <qpen.h>
#include <qstring.h>
pixhawk's avatar
pixhawk committed
19 20

class QPainter;
Bryant's avatar
Bryant committed
21
class QPolygonF;
pixhawk's avatar
pixhawk committed
22 23 24 25 26
class QwtScaleMap;
class QwtSymbol;
class QwtCurveFitter;

/*!
Bryant's avatar
Bryant committed
27
  \brief A plot item, that represents a series of points
pixhawk's avatar
pixhawk committed
28

Bryant's avatar
Bryant committed
29 30 31
  A curve is the representation of a series of points in the x-y plane.
  It supports different display styles, interpolation ( f.e. spline )
  and symbols.
pixhawk's avatar
pixhawk committed
32 33

  \par Usage
Bryant's avatar
Bryant committed
34
  <dl><dt>a) Assign curve properties</dt>
pixhawk's avatar
pixhawk committed
35
  <dd>When a curve is created, it is configured to draw black solid lines
Bryant's avatar
Bryant committed
36 37
  with in QwtPlotCurve::Lines style and no symbols. 
  You can change this by calling
pixhawk's avatar
pixhawk committed
38
  setPen(), setStyle() and setSymbol().</dd>
Bryant's avatar
Bryant committed
39 40 41 42 43 44 45 46 47
  <dt>b) Connect/Assign data.</dt>
  <dd>QwtPlotCurve gets its points using a QwtSeriesData object offering
  a bridge to the real storage of the points ( like QAbstractItemModel ).
  There are several convenience classes derived from QwtSeriesData, that also store
  the points inside ( like QStandardItemModel ). QwtPlotCurve also offers
  a couple of variations of setSamples(), that build QwtSeriesData objects from
  arrays internally.</dd>
  <dt>c) Attach the curve to a plot</dt>
  <dd>See QwtPlotItem::attach()
pixhawk's avatar
pixhawk committed
48 49 50
  </dd></dl>

  \par Example:
Bryant's avatar
Bryant committed
51
  see examples/bode
pixhawk's avatar
pixhawk committed
52

Bryant's avatar
Bryant committed
53
  \sa QwtPointSeriesData, QwtSymbol, QwtScaleMap
pixhawk's avatar
pixhawk committed
54
*/
Bryant's avatar
Bryant committed
55 56
class QWT_EXPORT QwtPlotCurve: 
    public QwtPlotSeriesItem, public QwtSeriesStore<QPointF>
pixhawk's avatar
pixhawk committed
57 58
{
public:
59 60
    /*!
        Curve styles.
Bryant's avatar
Bryant committed
61
        \sa setStyle(), style()
pixhawk's avatar
pixhawk committed
62
    */
Bryant's avatar
Bryant committed
63 64 65 66 67 68 69 70 71 72 73 74
    enum CurveStyle
    {
        /*!
           Don't draw a curve. Note: This doesn't affect the symbols.
        */
        NoCurve = -1,

        /*!
           Connect the points with straight lines. The lines might
           be interpolated depending on the 'Fitted' attribute. Curve
           fitting can be configured using setCurveFitter().
        */
pixhawk's avatar
pixhawk committed
75
        Lines,
Bryant's avatar
Bryant committed
76 77 78 79 80

        /*!
           Draw vertical or horizontal sticks ( depending on the 
           orientation() ) from a baseline which is defined by setBaseline().
        */
pixhawk's avatar
pixhawk committed
81
        Sticks,
Bryant's avatar
Bryant committed
82 83 84 85 86 87

        /*!
           Connect the points with a step function. The step function
           is drawn from the left to the right or vice versa,
           depending on the QwtPlotCurve::Inverted attribute.
        */
pixhawk's avatar
pixhawk committed
88
        Steps,
Bryant's avatar
Bryant committed
89 90 91 92 93 94 95

        /*!
           Draw dots at the locations of the data points. Note:
           This is different from a dotted line (see setPen()), and faster
           as a curve in QwtPlotCurve::NoStyle style and a symbol 
           painting a point.
        */
pixhawk's avatar
pixhawk committed
96 97
        Dots,

Bryant's avatar
Bryant committed
98 99 100 101 102
        /*!
           Styles >= QwtPlotCurve::UserCurve are reserved for derived
           classes of QwtPlotCurve that overload drawCurve() with
           additional application specific curve types.
        */
pixhawk's avatar
pixhawk committed
103 104 105
        UserCurve = 100
    };

106
    /*!
Bryant's avatar
Bryant committed
107 108
      Attribute for drawing the curve
      \sa setCurveAttribute(), testCurveAttribute(), curveFitter()
pixhawk's avatar
pixhawk committed
109
    */
Bryant's avatar
Bryant committed
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128
    enum CurveAttribute
    {
        /*!
           For QwtPlotCurve::Steps only. 
           Draws a step function from the right to the left.
         */
        Inverted = 0x01,

        /*!
          Only in combination with QwtPlotCurve::Lines
          A QwtCurveFitter tries to
          interpolate/smooth the curve, before it is painted.

          \note Curve fitting requires temporary memory
          for calculating coefficients and additional points.
          If painting in QwtPlotCurve::Fitted mode is slow it might be better
          to fit the points, before they are passed to QwtPlotCurve.
         */
        Fitted = 0x02
pixhawk's avatar
pixhawk committed
129 130
    };

Bryant's avatar
Bryant committed
131 132 133
    //! Curve attributes
    typedef QFlags<CurveAttribute> CurveAttributes;

134
    /*!
Bryant's avatar
Bryant committed
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
        Attributes how to represent the curve on the legend

        \sa setLegendAttribute(), testLegendAttribute(),
            QwtPlotItem::legendData(), legendIcon()
     */

    enum LegendAttribute
    {
        /*!
          QwtPlotCurve tries to find a color representing the curve 
          and paints a rectangle with it.
         */
        LegendNoAttribute = 0x00,

        /*!
          If the style() is not QwtPlotCurve::NoCurve a line 
          is painted with the curve pen().
         */
        LegendShowLine = 0x01,

        /*!
          If the curve has a valid symbol it is painted.
         */
        LegendShowSymbol = 0x02,

        /*!
          If the curve has a brush a rectangle filled with the
          curve brush() is painted.
         */
        LegendShowBrush = 0x04
    };

    //! Legend attributes
    typedef QFlags<LegendAttribute> LegendAttributes;

    /*!
        Attributes to modify the drawing algorithm.
        The default setting enables ClipPolygons | FilterPoints

        \sa setPaintAttribute(), testPaintAttribute()
pixhawk's avatar
pixhawk committed
175
    */
Bryant's avatar
Bryant committed
176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
    enum PaintAttribute
    {
        /*!
          Clip polygons before painting them. In situations, where points
          are far outside the visible area (f.e when zooming deep) this
          might be a substantial improvement for the painting performance
         */
        ClipPolygons = 0x01,

        /*!
          Tries to reduce the data that has to be painted, by sorting out
          duplicates, or paintings outside the visible area. Might have a
          notable impact on curves with many close points.
          Only a couple of very basic filtering algorithms are implemented.
         */
        FilterPoints = 0x02,

        /*!
          Minimize memory usage that is temporarily needed for the 
          translated points, before they get painted.
          This might slow down the performance of painting 
         */
        MinimizeMemory = 0x04,

        /*!
          Render the points to a temporary image and paint the image.
          This is a very special optimization for Dots style, when
          having a huge amount of points. 
          With a reasonable number of points QPainter::drawPoints()
          will be faster.
         */
        ImageBuffer = 0x08
pixhawk's avatar
pixhawk committed
208 209
    };

Bryant's avatar
Bryant committed
210 211 212 213 214
    //! Paint attributes
    typedef QFlags<PaintAttribute> PaintAttributes;

    explicit QwtPlotCurve( const QString &title = QString::null );
    explicit QwtPlotCurve( const QwtText &title );
pixhawk's avatar
pixhawk committed
215 216 217 218 219

    virtual ~QwtPlotCurve();

    virtual int rtti() const;

Bryant's avatar
Bryant committed
220 221
    void setPaintAttribute( PaintAttribute, bool on = true );
    bool testPaintAttribute( PaintAttribute ) const;
pixhawk's avatar
pixhawk committed
222

Bryant's avatar
Bryant committed
223 224
    void setLegendAttribute( LegendAttribute, bool on = true );
    bool testLegendAttribute( LegendAttribute ) const;
pixhawk's avatar
pixhawk committed
225

Bryant's avatar
Bryant committed
226 227 228 229
#ifndef QWT_NO_COMPAT
    void setRawSamples( const double *xData, const double *yData, int size );
    void setSamples( const double *xData, const double *yData, int size );
    void setSamples( const QVector<double> &xData, const QVector<double> &yData );
pixhawk's avatar
pixhawk committed
230
#endif
Bryant's avatar
Bryant committed
231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
    void setSamples( const QVector<QPointF> & );
    void setSamples( QwtSeriesData<QPointF> * );

    int closestPoint( const QPoint &pos, double *dist = NULL ) const;

    double minXValue() const;
    double maxXValue() const;
    double minYValue() const;
    double maxYValue() const;

    void setCurveAttribute( CurveAttribute, bool on = true );
    bool testCurveAttribute( CurveAttribute ) const;

    void setPen( const QColor &, qreal width = 0.0, Qt::PenStyle = Qt::SolidLine );
    void setPen( const QPen & );
pixhawk's avatar
pixhawk committed
246 247
    const QPen &pen() const;

Bryant's avatar
Bryant committed
248
    void setBrush( const QBrush & );
pixhawk's avatar
pixhawk committed
249 250
    const QBrush &brush() const;

Bryant's avatar
Bryant committed
251
    void setBaseline( double );
pixhawk's avatar
pixhawk committed
252 253
    double baseline() const;

Bryant's avatar
Bryant committed
254
    void setStyle( CurveStyle style );
pixhawk's avatar
pixhawk committed
255 256
    CurveStyle style() const;

Bryant's avatar
Bryant committed
257 258
    void setSymbol( QwtSymbol * );
    const QwtSymbol *symbol() const;
pixhawk's avatar
pixhawk committed
259

Bryant's avatar
Bryant committed
260
    void setCurveFitter( QwtCurveFitter * );
pixhawk's avatar
pixhawk committed
261 262
    QwtCurveFitter *curveFitter() const;

Bryant's avatar
Bryant committed
263 264 265
    virtual void drawSeries( QPainter *,
        const QwtScaleMap &xMap, const QwtScaleMap &yMap,
        const QRectF &canvasRect, int from, int to ) const;
pixhawk's avatar
pixhawk committed
266

Bryant's avatar
Bryant committed
267
    virtual QwtGraphic legendIcon( int index, const QSizeF & ) const;
pixhawk's avatar
pixhawk committed
268 269 270 271 272

protected:

    void init();

Bryant's avatar
Bryant committed
273 274 275
    virtual void drawCurve( QPainter *p, int style,
        const QwtScaleMap &xMap, const QwtScaleMap &yMap,
        const QRectF &canvasRect, int from, int to ) const;
pixhawk's avatar
pixhawk committed
276

Bryant's avatar
Bryant committed
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
    virtual void drawSymbols( QPainter *p, const QwtSymbol &,
        const QwtScaleMap &xMap, const QwtScaleMap &yMap,
        const QRectF &canvasRect, int from, int to ) const;

    virtual void drawLines( QPainter *p,
        const QwtScaleMap &xMap, const QwtScaleMap &yMap,
        const QRectF &canvasRect, int from, int to ) const;

    virtual void drawSticks( QPainter *p,
        const QwtScaleMap &xMap, const QwtScaleMap &yMap,
        const QRectF &canvasRect, int from, int to ) const;

    virtual void drawDots( QPainter *p,
        const QwtScaleMap &xMap, const QwtScaleMap &yMap,
        const QRectF &canvasRect, int from, int to ) const;
pixhawk's avatar
pixhawk committed
292

Bryant's avatar
Bryant committed
293 294 295 296 297 298 299 300 301 302 303 304
    virtual void drawSteps( QPainter *p,
        const QwtScaleMap &xMap, const QwtScaleMap &yMap,
        const QRectF &canvasRect, int from, int to ) const;

    virtual void fillCurve( QPainter *,
        const QwtScaleMap &, const QwtScaleMap &, 
        const QRectF &canvasRect, QPolygonF & ) const;

    void closePolyline( QPainter *,
        const QwtScaleMap &, const QwtScaleMap &, QPolygonF & ) const;

private:
pixhawk's avatar
pixhawk committed
305 306 307 308
    class PrivateData;
    PrivateData *d_data;
};

Bryant's avatar
Bryant committed
309 310
//! boundingRect().left()
inline double QwtPlotCurve::minXValue() const
pixhawk's avatar
pixhawk committed
311
{
Bryant's avatar
Bryant committed
312
    return boundingRect().left();
pixhawk's avatar
pixhawk committed
313 314
}

Bryant's avatar
Bryant committed
315 316
//! boundingRect().right()
inline double QwtPlotCurve::maxXValue() const
pixhawk's avatar
pixhawk committed
317
{
Bryant's avatar
Bryant committed
318
    return boundingRect().right();
pixhawk's avatar
pixhawk committed
319 320
}

Bryant's avatar
Bryant committed
321 322
//! boundingRect().top()
inline double QwtPlotCurve::minYValue() const
323
{
Bryant's avatar
Bryant committed
324
    return boundingRect().top();
pixhawk's avatar
pixhawk committed
325 326
}

Bryant's avatar
Bryant committed
327 328
//! boundingRect().bottom()
inline double QwtPlotCurve::maxYValue() const
329
{
Bryant's avatar
Bryant committed
330
    return boundingRect().bottom();
pixhawk's avatar
pixhawk committed
331 332
}

Bryant's avatar
Bryant committed
333 334 335 336
Q_DECLARE_OPERATORS_FOR_FLAGS( QwtPlotCurve::PaintAttributes )
Q_DECLARE_OPERATORS_FOR_FLAGS( QwtPlotCurve::LegendAttributes )
Q_DECLARE_OPERATORS_FOR_FLAGS( QwtPlotCurve::CurveAttributes )

pixhawk's avatar
pixhawk committed
337
#endif