qwt_plot_curve.h 6.59 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
/* -*- 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 <qpen.h>
#include <qstring.h>
#include "qwt_global.h"
#include "qwt_plot_item.h"
#include "qwt_text.h"
#include "qwt_polygon.h"
#include "qwt_data.h"

class QPainter;
class QwtScaleMap;
class QwtSymbol;
class QwtCurveFitter;

/*!
  \brief A class which draws curves

  This class can be used to display data as a curve in the  x-y plane.
  It supports different display styles, spline interpolation and symbols.

  \par Usage
  <dl><dt>A. Assign curve properties</dt>
  <dd>When a curve is created, it is configured to draw black solid lines
35
  with QwtPlotCurve::Lines and no symbols. You can change this by calling
pixhawk's avatar
pixhawk committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
  setPen(), setStyle() and setSymbol().</dd>
  <dt>B. Assign or change data.</dt>
  <dd>Data can be set in two ways:<ul>
  <li>setData() is overloaded to initialize the x and y data by
  copying from different data structures with different kind of copy semantics.
  <li>setRawData() only stores the pointers and size information
  and is provided for backwards compatibility.  This function is less safe (you
  must not delete the data while they are attached), but has been more
  efficient, and has been more convenient for dynamically changing data.
  Use of setData() in combination with a problem-specific subclass
  of QwtData is always preferrable.</ul></dd>
  <dt>C. Draw</dt>
  <dd>draw() maps the data into pixel coordinates and paints them.
  </dd></dl>

  \par Example:
  see examples/curvdemo

  \sa QwtData, QwtSymbol, QwtScaleMap
*/
class QWT_EXPORT QwtPlotCurve: public QwtPlotItem
{
public:
59
    enum CurveType {
pixhawk's avatar
pixhawk committed
60 61 62 63
        Yfx,
        Xfy
    };

64 65
    /*!
        Curve styles.
pixhawk's avatar
pixhawk committed
66 67
        \sa setStyle
    */
68
    enum CurveStyle {
pixhawk's avatar
pixhawk committed
69 70 71 72 73 74 75 76 77 78
        NoCurve,

        Lines,
        Sticks,
        Steps,
        Dots,

        UserCurve = 100
    };

79 80
    /*!
        Curve attributes.
pixhawk's avatar
pixhawk committed
81 82
        \sa setCurveAttribute, testCurveAttribute
    */
83
    enum CurveAttribute {
pixhawk's avatar
pixhawk committed
84 85 86 87
        Inverted = 1,
        Fitted = 2
    };

88 89
    /*!
        Paint attributes
pixhawk's avatar
pixhawk committed
90 91
        \sa setPaintAttribute, testPaintAttribute
    */
92
    enum PaintAttribute {
pixhawk's avatar
pixhawk committed
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
        PaintFiltered = 1,
        ClipPolygons = 2
    };

    explicit QwtPlotCurve();
    explicit QwtPlotCurve(const QwtText &title);
    explicit QwtPlotCurve(const QString &title);

    virtual ~QwtPlotCurve();

    virtual int rtti() const;

    void setCurveType(CurveType);
    CurveType curveType() const;

    void setPaintAttribute(PaintAttribute, bool on = true);
    bool testPaintAttribute(PaintAttribute) const;

    void setRawData(const double *x, const double *y, int size);
    void setData(const double *xData, const double *yData, int size);
    void setData(const QwtArray<double> &xData, const QwtArray<double> &yData);
#if QT_VERSION < 0x040000
    void setData(const QwtArray<QwtDoublePoint> &data);
#else
    void setData(const QPolygonF &data);
#endif
    void setData(const QwtData &data);
120

pixhawk's avatar
pixhawk committed
121 122 123 124 125 126 127 128 129 130 131 132
    int closestPoint(const QPoint &pos, double *dist = NULL) const;

    QwtData &data();
    const QwtData &data() const;

    int dataSize() const;
    inline double x(int i) const;
    inline double y(int i) const;

    virtual QwtDoubleRect boundingRect() const;

    //! boundingRect().left()
133 134 135
    inline double minXValue() const {
        return boundingRect().left();
    }
pixhawk's avatar
pixhawk committed
136
    //! boundingRect().right()
137 138 139
    inline double maxXValue() const {
        return boundingRect().right();
    }
pixhawk's avatar
pixhawk committed
140
    //! boundingRect().top()
141 142 143
    inline double minYValue() const {
        return boundingRect().top();
    }
pixhawk's avatar
pixhawk committed
144
    //! boundingRect().bottom()
145 146 147
    inline double maxYValue() const {
        return boundingRect().bottom();
    }
pixhawk's avatar
pixhawk committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169

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

    void setPen(const QPen &);
    const QPen &pen() const;

    void setBrush(const QBrush &);
    const QBrush &brush() const;

    void setBaseline(double ref);
    double baseline() const;

    void setStyle(CurveStyle style);
    CurveStyle style() const;

    void setSymbol(const QwtSymbol &s);
    const QwtSymbol& symbol() const;

    void setCurveFitter(QwtCurveFitter *);
    QwtCurveFitter *curveFitter() const;

170 171 172
    virtual void draw(QPainter *p,
                      const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                      const QRect &) const;
pixhawk's avatar
pixhawk committed
173

174 175 176
    virtual void draw(QPainter *p,
                      const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                      int from, int to) const;
pixhawk's avatar
pixhawk committed
177 178 179 180 181 182 183 184 185 186

    void draw(int from, int to) const;

    virtual void updateLegend(QwtLegend *) const;

protected:

    void init();

    virtual void drawCurve(QPainter *p, int style,
187 188
                           const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                           int from, int to) const;
pixhawk's avatar
pixhawk committed
189 190

    virtual void drawSymbols(QPainter *p, const QwtSymbol &,
191 192
                             const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                             int from, int to) const;
pixhawk's avatar
pixhawk committed
193 194

    void drawLines(QPainter *p,
195 196
                   const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                   int from, int to) const;
pixhawk's avatar
pixhawk committed
197
    void drawSticks(QPainter *p,
198 199
                    const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                    int from, int to) const;
pixhawk's avatar
pixhawk committed
200
    void drawDots(QPainter *p,
201 202
                  const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                  int from, int to) const;
pixhawk's avatar
pixhawk committed
203
    void drawSteps(QPainter *p,
204 205
                   const QwtScaleMap &xMap, const QwtScaleMap &yMap,
                   int from, int to) const;
pixhawk's avatar
pixhawk committed
206 207

    void fillCurve(QPainter *,
208 209
                   const QwtScaleMap &, const QwtScaleMap &,
                   QwtPolygon &) const;
pixhawk's avatar
pixhawk committed
210
    void closePolyline(const QwtScaleMap &, const QwtScaleMap &,
211
                       QwtPolygon &) const;
pixhawk's avatar
pixhawk committed
212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235

private:
    QwtData *d_xy;

    class PrivateData;
    PrivateData *d_data;
};

//! \return the the curve data
inline QwtData &QwtPlotCurve::data()
{
    return *d_xy;
}

//! \return the the curve data
inline const QwtData &QwtPlotCurve::data() const
{
    return *d_xy;
}

/*!
    \param i index
    \return x-value at position i
*/
236 237 238
inline double QwtPlotCurve::x(int i) const
{
    return d_xy->x(i);
pixhawk's avatar
pixhawk committed
239 240 241 242 243 244
}

/*!
    \param i index
    \return y-value at position i
*/
245 246 247
inline double QwtPlotCurve::y(int i) const
{
    return d_xy->y(i);
pixhawk's avatar
pixhawk committed
248 249 250
}

#endif