LinechartWidget.h 8.4 KB
Newer Older
pixhawk's avatar
pixhawk committed
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
/*=====================================================================

PIXHAWK Micro Air Vehicle Flying Robotics Toolkit

(c) 2009 PIXHAWK PROJECT  <http://pixhawk.ethz.ch>

This file is part of the PIXHAWK project

    PIXHAWK 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.

    PIXHAWK 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 PIXHAWK. If not, see <http://www.gnu.org/licenses/>.

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

/**
 * @file
 *   @brief Definition of Line chart plot widget
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */
#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>
49
#include <QTimer>
pixhawk's avatar
pixhawk committed
50 51 52 53 54 55 56 57 58 59 60 61
#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
 **/
62 63
class LinechartWidget : public QWidget
{
pixhawk's avatar
pixhawk committed
64 65 66
    Q_OBJECT

public:
67
    LinechartWidget(int systemid, QWidget *parent = 0);
pixhawk's avatar
pixhawk committed
68 69 70 71 72 73
    ~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:
74
    void addCurve(const QString& curve, const QString& unit);
75
    void removeCurve(QString curve);
76 77 78 79
    /** @brief Recolor all curves */
    void recolor();
    /** @brief Set short names for curves */
    void setShortNames(bool enable);
80 81 82 83 84 85 86 87 88 89 90 91 92
    /** @brief Append int8 data to the given curve. */
    void appendData(int uasId, const QString& curve, const QString& unit, qint8 value, quint64 usec);
    /** @brief Append uint8 data to the given curve. */
    void appendData(int uasId, const QString& curve, const QString& unit, quint8 value, quint64 usec);
    /** @brief Append int16 data to the given curve. */
    void appendData(int uasId, const QString& curve, const QString& unit, qint16 value, quint64 usec);
    /** @brief Append uint16 data to the given curve. */
    void appendData(int uasId, const QString& curve, const QString& unit, quint16 value, quint64 usec);
    /** @brief Append int32 data to the given curve. */
    void appendData(int uasId, const QString& curve, const QString& unit, qint32 value, quint64 usec);
    /** @brief Append uint32 data to the given curve. */
    void appendData(int uasId, const QString& curve, const QString& unit, quint32 value, quint64 usec);
    /** @brief Append int64 data to the given curve. */
lm's avatar
lm committed
93
    void appendData(int uasId, const QString& curve, const QString& unit, qint64 value, quint64 usec);
94
    /** @brief Append uint64 data to the given curve. */
lm's avatar
lm committed
95
    void appendData(int uasId, const QString& curve, const QString& unit, quint64 value, quint64 usec);
96 97 98
    /** @brief Append double data to the given curve. */
    void appendData(int uasId, const QString& curve, const QString& unit, double value, quint64 usec);
	
pixhawk's avatar
pixhawk committed
99 100 101 102
    void takeButtonClick(bool checked);
    void setPlotWindowPosition(int scrollBarValue);
    void setPlotWindowPosition(quint64 position);
    void setPlotInterval(quint64 interval);
103 104 105 106
    /** @brief Start automatic updates once visible */
    void showEvent(QShowEvent* event);
    /** @brief Stop automatic updates once hidden */
    void hideEvent(QHideEvent* event);
107
    void setActive(bool active);
108 109
    /** @brief Select one MAV for curve display */
    void selectActiveSystem(int mav);
pixhawk's avatar
pixhawk committed
110 111 112 113 114 115
    /** @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();
116 117
    /** @brief Refresh the view */
    void refresh();
118 119 120 121
    /** @brief Write the current configuration to disk */
    void writeSettings();
    /** @brief Read the current configuration from disk */
    void readSettings();
122 123
    /** @brief Select all curves */
    void selectAllCurves(bool all);
pixhawk's avatar
pixhawk committed
124 125 126 127

protected:
    void addCurveToList(QString curve);
    void removeCurveFromList(QString curve);
128
    QToolButton* createButton(QWidget* parent);
129
    void createCurveItem(QString curve);
130
    void createLayout();
131 132
    /** @brief Get the name for a curve key */
    QString getCurveName(const QString& key, bool shortEnabled);
pixhawk's avatar
pixhawk committed
133

134 135 136 137
    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
138 139

    int curveListIndex;
140 141 142
    int curveListCounter;                 ///< Counter of curves in curve list
    QList<QString>* listedCurves;         ///< Curves listed
    QMap<QString, QLabel*>* curveLabels;  ///< References to the curve labels
143 144
    QMap<QString, QLabel*> curveNameLabels;  ///< References to the curve labels
    QMap<QString, QString> curveNames;    ///< Full curve names
145 146
    QMap<QString, QLabel*>* curveMeans;   ///< References to the curve means
    QMap<QString, QLabel*>* curveMedians; ///< References to the curve medians
147
    QMap<QString, QLabel*>* curveVariances; ///< References to the curve variances
148
    QMap<QString, int> intData;           ///< Current values for integer-valued curves
149
    QMap<QString, QWidget*> colorIcons;    ///< Reference to color icons
150 151

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

156 157 158
    QAction* setScalingLogarithmic;       ///< Set logarithmic scaling
    QAction* setScalingLinear;            ///< Set linear scaling
    QAction* addNewCurve;                 ///< Add curve candidate to the active curves
pixhawk's avatar
pixhawk committed
159 160 161 162 163 164 165

    QMenu* curveMenu;
    QGridLayout* mainLayout;

    QToolButton* scalingLinearButton;
    QToolButton* scalingLogButton;
    QToolButton* logButton;
166
    QPointer<QCheckBox> timeButton;
pixhawk's avatar
pixhawk committed
167 168 169 170

    QFile* logFile;
    unsigned int logindex;
    bool logging;
171
    quint64 logStartTime;
172
    QTimer* updateTimer;
pixhawk's avatar
pixhawk committed
173
    LogCompressor* compressor;
174
    QCheckBox* selectAllCheckBox;
lm's avatar
lm committed
175
    int selectedMAV; ///< The MAV for which plot items are accepted, -1 for all systems
LM's avatar
LM committed
176
    static const int updateInterval = 1000; ///< Time between number updates, in milliseconds
pixhawk's avatar
pixhawk committed
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

    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);

212 213 214
    /** @brief This signal is emitted once a logfile has been finished writing */
    void logfileWritten(QString fileName);

pixhawk's avatar
pixhawk committed
215 216 217
};

#endif // LINECHARTWIDGET_H