IncrementalPlot.h 3.18 KB
Newer Older
1 2
/****************************************************************************
 *
3
 *   (c) 2009-2018 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
4 5 6 7 8
 *
 * 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
#pragma once
19 20 21

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

class QwtPlotCurve;

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

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

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

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

51 52 53 54 55 56 57
/**
 * @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.
 */
58
class IncrementalPlot : public ChartPlot
59 60 61
{
    Q_OBJECT
public:
62
    /** @brief Create a new, empty incremental plot */
63 64 65
    IncrementalPlot(QWidget *parent = NULL);
    virtual ~IncrementalPlot();

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

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

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

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

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

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

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

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

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

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

97 98 99 100 101 102 103
    /** @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);

104
protected:
105 106 107 108 109 110
    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
111
    QString styleText;     ///< Curve style set by setStyleText
112 113

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