LinechartWidget.h 7.29 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.
 *
 ****************************************************************************/
pixhawk's avatar
pixhawk committed
9 10 11 12 13 14 15


/**
 * @file
 *   @brief Definition of Line chart plot widget
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
16
 *   @author Thomas Gubler <thomasgubler@student.ethz.ch>
pixhawk's avatar
pixhawk committed
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
 */
#ifndef LINECHARTWIDGET_H
#define LINECHARTWIDGET_H

#include <QGridLayout>
#include <QWidget>
#include <QFrame>
#include <QComboBox>
#include <QVBoxLayout>
#include <QCheckBox>
#include <QScrollBar>
#include <QSpinBox>
#include <QMap>
#include <QString>
#include <QAction>
#include <QIcon>
#include <QLabel>
#include <QReadWriteLock>
#include <QToolButton>
36
#include <QTimer>
pixhawk's avatar
pixhawk committed
37 38 39 40 41 42 43 44 45 46 47 48
#include <qwt_plot_curve.h>

#include "LinechartPlot.h"
#include "UASInterface.h"
#include "ui_Linechart.h"

#include "LogCompressor.h"

/**
 * @brief The linechart widget allows to visualize different timeseries as lineplot.
 * The display interval, the timeseries and the scaling can be changed interactively
 **/
49 50
class LinechartWidget : public QWidget
{
pixhawk's avatar
pixhawk committed
51 52 53
    Q_OBJECT

public:
54
    LinechartWidget(int systemid, QWidget *parent = 0);
pixhawk's avatar
pixhawk committed
55 56 57 58 59 60
    ~LinechartWidget();

    static const int MIN_TIME_SCROLLBAR_VALUE = 0; ///< The minimum scrollbar value
    static const int MAX_TIME_SCROLLBAR_VALUE = 16383; ///< The maximum scrollbar value

public slots:
61
    void addCurve(const QString& curve, const QString& unit);
62
    void removeCurve(QString curve);
63 64 65 66
    /** @brief Recolor all curves */
    void recolor();
    /** @brief Set short names for curves */
    void setShortNames(bool enable);
67 68
    /** @brief Append data to the given curve. */
    void appendData(int uasId, const QString& curve, const QString& unit, const QVariant& value, quint64 usec);
69 70
    /** @brief Hide curves which do not match the filter pattern */
    void filterCurves(const QString &filter);
71

72
    void toggleLogarithmicScaling(bool toggled);
pixhawk's avatar
pixhawk committed
73 74 75 76
    void takeButtonClick(bool checked);
    void setPlotWindowPosition(int scrollBarValue);
    void setPlotWindowPosition(quint64 position);
    void setPlotInterval(quint64 interval);
77 78 79 80
    /** @brief Start automatic updates once visible */
    void showEvent(QShowEvent* event);
    /** @brief Stop automatic updates once hidden */
    void hideEvent(QHideEvent* event);
81
    void setActive(bool active);
82 83
    /** @brief Select one MAV for curve display */
    void selectActiveSystem(int mav);
pixhawk's avatar
pixhawk committed
84 85 86 87 88 89
    /** @brief Set the number of values to average over */
    void setAverageWindow(int windowSize);
    /** @brief Start logging to file */
    void startLogging();
    /** @brief Stop logging to file */
    void stopLogging();
90 91
    /** @brief Refresh the view */
    void refresh();
92 93 94 95
    /** @brief Write the current configuration to disk */
    void writeSettings();
    /** @brief Read the current configuration from disk */
    void readSettings();
96 97
    /** @brief Select all curves */
    void selectAllCurves(bool all);
98 99
    /** @brief Sets the focus to the LineEdit for plot-filtering */
    void setPlotFilterLineEditFocus();
pixhawk's avatar
pixhawk committed
100

101 102 103
private slots:
    /** Called when the user changes the time scale combobox. */
    void timeScaleChanged(int index);
104
    /** @brief Toggles visibility of curve based on bool match if corresponding checkbox is not checked */
105
    void filterCurve(const QString &key, bool match);
106

pixhawk's avatar
pixhawk committed
107 108 109
protected:
    void addCurveToList(QString curve);
    void removeCurveFromList(QString curve);
110
    QToolButton* createButton(QWidget* parent);
111
    void createCurveItem(QString curve);
112
    void createLayout();
113 114
    /** @brief Get the name for a curve key */
    QString getCurveName(const QString& key, bool shortEnabled);
pixhawk's avatar
pixhawk committed
115

116 117 118 119
    int sysid;                            ///< ID of the unmanned system this plot belongs to
    LinechartPlot* activePlot;            ///< Plot for this system
    QReadWriteLock* curvesLock;           ///< A lock (mutex) for the concurrent access on the curves
    QReadWriteLock plotWindowLock;        ///< A lock (mutex) for the concurrent access on the window position
pixhawk's avatar
pixhawk committed
120 121

    int curveListIndex;
122 123
    int curveListCounter;                 ///< Counter of curves in curve list
    QMap<QString, QLabel*>* curveLabels;  ///< References to the curve labels
124 125
    QMap<QString, QLabel*> curveNameLabels;  ///< References to the curve labels
    QMap<QString, QString> curveNames;    ///< Full curve names
126 127
    QMap<QString, QLabel*>* curveMeans;   ///< References to the curve means
    QMap<QString, QLabel*>* curveMedians; ///< References to the curve medians
128
    QMap<QString, QWidget*> curveUnits;    ///< References to the curve units
129
    QMap<QString, QLabel*>* curveVariances; ///< References to the curve variances
130
    QMap<QString, int> intData;           ///< Current values for integer-valued curves
131
    QMap<QString, QWidget*> colorIcons;    ///< Reference to color icons
132
    QMap<QString, QCheckBox*> checkBoxes;    ///< Reference to checkboxes
133 134

    QWidget* curvesWidget;                ///< The QWidget containing the curve selection button
135
    QGridLayout* curvesWidgetLayout;      ///< The layout for the curvesWidget QWidget
136 137
    QScrollBar* scrollbar;                ///< The plot window scroll bar
    QSpinBox* averageSpinBox;             ///< Spin box to setup average window filter size
pixhawk's avatar
pixhawk committed
138

139
    QAction* addNewCurve;                 ///< Add curve candidate to the active curves
pixhawk's avatar
pixhawk committed
140

141
    QComboBox *timeScaleCmb;
pixhawk's avatar
pixhawk committed
142 143 144

    QToolButton* scalingLogButton;
    QToolButton* logButton;
145
    QPointer<QCheckBox> timeButton;
pixhawk's avatar
pixhawk committed
146 147 148 149

    QFile* logFile;
    unsigned int logindex;
    bool logging;
150
    quint64 logStartTime;
151
    QTimer* updateTimer;
pixhawk's avatar
pixhawk committed
152
    LogCompressor* compressor;
153
    QCheckBox* selectAllCheckBox;
lm's avatar
lm committed
154
    int selectedMAV; ///< The MAV for which plot items are accepted, -1 for all systems
155 156 157
    quint64 lastTimestamp;
    bool userGroundTimeSet;
    bool autoGroundTimeSet;
LM's avatar
LM committed
158
    static const int updateInterval = 1000; ///< Time between number updates, in milliseconds
pixhawk's avatar
pixhawk committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193

    static const int MAX_CURVE_MENUITEM_NUMBER = 8;
    static const int PAGESTEP_TIME_SCROLLBAR_VALUE = (MAX_TIME_SCROLLBAR_VALUE - MIN_TIME_SCROLLBAR_VALUE) / 10;

private:
    Ui::linechart ui;
    void createActions();

signals:
    /**
         * @brief This signal is emitted if a curve is removed from the list
         *
         * @param curve The removed plot curve
         **/
    void curveRemoved(QString curve);

    /**
         * @brief This signal is emitted if a curve has been moved or added
         *
         * @param curve The moved or added curve
         * @param position The x-position of the curve (The centerline)
         **/
    void curveSet(QString curve, int position);

    /**
         * @brief This signal is emitted to change the visibility of a curve
         *
         * @param curve The changed curve
         * @pram visible The visibility
         **/
    void curveVisible(QString curve, bool visible);

    void plotWindowPositionUpdated(quint64 position);
    void plotWindowPositionUpdated(int position);

194 195 196
    /** @brief This signal is emitted once a logfile has been finished writing */
    void logfileWritten(QString fileName);

197 198 199 200 201 202
private slots:
    void _filterTimeout(void);
    void _restartFilterTimeout(void);

private:
    QTimer _filterTimer;
pixhawk's avatar
pixhawk committed
203 204 205
};

#endif // LINECHARTWIDGET_H