IncrementalPlot.h 3.85 KB
Newer Older
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
/*=====================================================================

QGroundControl Open Source Ground Control Station

(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>

This file is part of the QGROUNDCONTROL project

    QGROUNDCONTROL is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    QGROUNDCONTROL is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.

======================================================================*/

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

31 32 33 34 35
#ifndef INCREMENTALPLOT_H
#define INCREMENTALPLOT_H

#include <QTimer>
#include <qwt_plot.h>
36
#include <qwt_legend.h>
37
#include <QMap>
38
#include "ChartPlot.h"
39 40 41

class QwtPlotCurve;

42 43 44
/**
 * @brief Plot data container for growing data
 */
45 46 47 48 49 50 51
class CurveData
{
public:
    CurveData();

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

52
    /** @brief The number of datasets held in the data structure */
53
    int count() const;
54
    /** @brief The reserved size of the data structure in units */
55 56 57 58 59 60
    int size() const;
    const double *x() const;
    const double *y() const;

private:
    int d_count;
61 62
    QVector<double> d_x;
    QVector<double> d_y;
63 64
};

65 66 67 68 69 70 71
/**
 * @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.
 */
72
class IncrementalPlot : public ChartPlot
73 74 75
{
    Q_OBJECT
public:
76
    /** @brief Create a new, empty incremental plot */
77 78 79
    IncrementalPlot(QWidget *parent = NULL);
    virtual ~IncrementalPlot();

80
    /** @brief Get the state of the grid */
81
    bool gridEnabled() const;
82 83

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

86
public slots:
87
    /** @brief Append one data point */
88
    void appendData(const QString &key, double x, double y);
89

90
    /** @brief Append multiple data points */
91
    void appendData(const QString &key, double* x, double* y, int size);
92 93

    /** @brief Reset the plot scaling to the default value */
94
    void resetScaling();
95 96 97 98 99

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

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

102 103
    /** @brief Show the plot legend */
    void showLegend(bool show);
104 105 106 107

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

108
    /** @brief Set new plot style */
109
    void setStyleText(const QString &style);
110

111 112 113 114 115 116 117
    /** @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);

118
protected:
119 120 121 122 123 124
    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
125
    QString styleText;     ///< Curve style set by setStyleText
126 127

private:
128
    QMap<QString, CurveData* > d_data;      ///< Data points
129 130
    /** Helper function to apply styleText style to the given curve */
    void updateStyle(QwtPlotCurve *curve);
131 132 133
};

#endif /* INCREMENTALPLOT_H */