IncrementalPlot.h 4.31 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 36
#ifndef INCREMENTALPLOT_H
#define INCREMENTALPLOT_H

#include <QTimer>
#include <qwt_array.h>
#include <qwt_plot.h>
37
#include <qwt_legend.h>
38
#include <qwt_plot_grid.h>
39 40 41 42 43
#include <QMap>
#include "ScrollZoomer.h"

class QwtPlotCurve;

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

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

54
    /** @brief The number of datasets held in the data structure */
55
    int count() const;
56
    /** @brief The reserved size of the data structure in units */
57 58 59 60 61 62 63 64 65 66 67 68
    int size() const;
    const double *x() const;
    const double *y() const;

private:
    int d_count;
    QwtArray<double> d_x;
    QwtArray<double> d_y;
    QTimer *d_timer;
    int d_timerCount;
};

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

    /** @brief Get color map of this plot */
lm's avatar
lm committed
85
    QList<QColor> getColorMap();
86

87
    /** @brief Get next color of color map */
lm's avatar
lm committed
88
    QColor getNextColor();
89

90
    /** @brief Get color for curve id */
lm's avatar
lm committed
91
    QColor getColorForCurve(QString id);
92

93 94 95 96 97 98 99 100 101 102 103
    /** @brief Get the state of the grid */
    bool gridEnabled();

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

    float symbolWidth;
    float curveWidth;
    float gridWidth;
    float scaleWidth;

104
public slots:
105
    /** @brief Append one data point */
106 107
    void appendData(QString key, double x, double y);

108 109 110 111
    /** @brief Append multiple data points */
    void appendData(QString key, double* x, double* y, int size);

    /** @brief Reset the plot scaling to the default value */
112
    void resetScaling();
113 114 115 116 117

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

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

120 121
    /** @brief Show the plot legend */
    void showLegend(bool show);
122 123 124 125

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

126 127 128
    /** @brief Set new plot style */
    void setStyleText(QString style);

129 130 131 132 133 134 135
    /** @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);

136
protected:
137 138 139 140 141 142 143 144 145 146 147
    bool symmetric;        ///< Enable symmetric plotting
    QList<QColor> colors;  ///< Colormap for curves
    int nextColor;         ///< Next index in color map
    ScrollZoomer* zoomer;  ///< Zoomer class for widget
    QwtLegend* legend;     ///< Plot legend
    QwtPlotGrid* grid;     ///< Plot grid
    double xmin;           ///< Minimum x value seen
    double xmax;           ///< Maximum x value seen
    double ymin;           ///< Minimum y value seen
    double ymax;           ///< Maximum y value seen

148 149

private:
150 151
    QMap<QString, CurveData* > d_data;      ///< Data points
    QMap<QString, QwtPlotCurve* > d_curve;  ///< Plot curves
152 153 154
};

#endif /* INCREMENTALPLOT_H */