IncrementalPlot.h 3.25 KB
Newer Older
1 2 3 4 5 6 7 8
/****************************************************************************
 *
 *   (c) 2009-2016 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 *
 * QGroundControl is licensed according to the terms in the file
 * COPYING.md in the root of the source code directory.
 *
 ****************************************************************************/
9 10 11 12 13 14 15 16 17


/**
 * @file
 *   @brief Defition of class IncrementalPlot
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

18 19 20 21 22
#ifndef INCREMENTALPLOT_H
#define INCREMENTALPLOT_H

#include <QTimer>
#include <qwt_plot.h>
23
#include <qwt_legend.h>
24
#include <QMap>
25
#include "ChartPlot.h"
26 27 28

class QwtPlotCurve;

29 30 31
/**
 * @brief Plot data container for growing data
 */
32 33 34 35 36 37 38
class CurveData
{
public:
    CurveData();

    void append(double *x, double *y, int count);

39
    /** @brief The number of datasets held in the data structure */
40
    int count() const;
41
    /** @brief The reserved size of the data structure in units */
42 43 44 45 46 47
    int size() const;
    const double *x() const;
    const double *y() const;

private:
    int d_count;
48 49
    QVector<double> d_x;
    QVector<double> d_y;
50 51
};

52 53 54 55 56 57 58
/**
 * @brief Incremental plotting widget
 *
 * This widget plots data incrementally when new data arrives.
 * It will only repaint the minimum screen content necessary to avoid
 * a too high CPU consumption. It auto-scales the plot to new data.
 */
59
class IncrementalPlot : public ChartPlot
60 61 62
{
    Q_OBJECT
public:
63
    /** @brief Create a new, empty incremental plot */
64 65 66
    IncrementalPlot(QWidget *parent = NULL);
    virtual ~IncrementalPlot();

67
    /** @brief Get the state of the grid */
68
    bool gridEnabled() const;
69 70

    /** @brief Read out data from a curve */
71
    int data(const QString &key, double* r_x, double* r_y, int maxSize);
72

73
public slots:
74
    /** @brief Append one data point */
75
    void appendData(const QString &key, double x, double y);
76

77
    /** @brief Append multiple data points */
78
    void appendData(const QString &key, double* x, double* y, int size);
79 80

    /** @brief Reset the plot scaling to the default value */
81
    void resetScaling();
82 83 84 85 86

    /** @brief Update the plot scale based on current data/symmetric mode */
    void updateScale();

    /** @brief Remove all data from the plot and repaint */
87
    void removeData();
88

89 90
    /** @brief Show the plot legend */
    void showLegend(bool show);
91 92 93 94

    /** @brief Show the plot grid */
    void showGrid(bool show);

95
    /** @brief Set new plot style */
96
    void setStyleText(const QString &style);
97

98 99 100 101 102 103 104
    /** @brief Set symmetric axis scaling mode */
    void setSymmetric(bool symmetric);

protected slots:
    /** @brief Handle the click on a legend item */
    void handleLegendClick(QwtPlotItem* item, bool on);

105
protected:
106 107 108 109 110 111
    bool symmetric;        ///< Enable symmetric plotting
    QwtLegend* legend;     ///< Plot legend
    double xmin;           ///< Minimum x value seen
    double xmax;           ///< Maximum x value seen
    double ymin;           ///< Minimum y value seen
    double ymax;           ///< Maximum y value seen
112
    QString styleText;     ///< Curve style set by setStyleText
113 114

private:
115
    QMap<QString, CurveData* > d_data;      ///< Data points
116 117
    /** Helper function to apply styleText style to the given curve */
    void updateStyle(QwtPlotCurve *curve);
118 119 120
};

#endif /* INCREMENTALPLOT_H */