HDDisplay.h 11 KB
Newer Older
pixhawk's avatar
pixhawk committed
1 2
/*=====================================================================

3
QGroundControl Open Source Ground Control Station
pixhawk's avatar
pixhawk committed
4

5
(c) 2009, 2010 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
pixhawk's avatar
pixhawk committed
6

7
This file is part of the QGROUNDCONTROL project
pixhawk's avatar
pixhawk committed
8

9
    QGROUNDCONTROL is free software: you can redistribute it and/or modify
pixhawk's avatar
pixhawk committed
10 11 12 13
    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.

14
    QGROUNDCONTROL is distributed in the hope that it will be useful,
pixhawk's avatar
pixhawk committed
15 16 17 18 19
    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
20
    along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
pixhawk's avatar
pixhawk committed
21 22 23 24 25 26 27 28 29 30 31 32 33 34

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

/**
 * @file
 *   @brief Definition of Head Down Display (HDD)
 *
 *   @author Lorenz Meier <mavteam@student.ethz.ch>
 *
 */

#ifndef HDDISPLAY_H
#define HDDISPLAY_H

35
#include <QtGui/QGraphicsView>
pixhawk's avatar
pixhawk committed
36 37 38
#include <QColor>
#include <QTimer>
#include <QFontDatabase>
pixhawk's avatar
pixhawk committed
39
#include <QMap>
40
#include <QContextMenuEvent>
pixhawk's avatar
pixhawk committed
41
#include <QPair>
pixhawk's avatar
pixhawk committed
42 43 44 45
#include <cmath>

#include "UASInterface.h"

46 47 48
namespace Ui
{
class HDDisplay;
pixhawk's avatar
pixhawk committed
49 50
}

51 52 53 54 55 56 57 58 59 60
/**
 * @brief Head Down Display Widget
 *
 * This widget is used for any head down display as base widget. It handles the basic widget setup
 * each head down instrument has a virtual screen size in millimeters as base coordinate system
 * this virtual screen size is then scaled to pixels on the screen.
 * When the pixel per millimeter ratio is known, a 1:1 representation is possible on the screen
 */
class HDDisplay : public QGraphicsView
{
pixhawk's avatar
pixhawk committed
61 62
    Q_OBJECT
public:
63
    HDDisplay(QStringList* plotList, QString title="", QWidget *parent = 0);
pixhawk's avatar
pixhawk committed
64 65 66
    ~HDDisplay();

public slots:
67 68 69 70 71 72 73 74 75 76 77 78 79
    /** @brief Update the HDD with new int8 data */
    void updateValue(const int uasId, const QString& name, const QString& unit, const qint8 value, const quint64 msec);
    /** @brief Update the HDD with new uint8 data */
    void updateValue(const int uasId, const QString& name, const QString& unit, const quint8 value, const quint64 msec);
    /** @brief Update the HDD with new int16 data */
    void updateValue(const int uasId, const QString& name, const QString& unit, const qint16 value, const quint64 msec);
    /** @brief Update the HDD with new uint16 data */
    void updateValue(const int uasId, const QString& name, const QString& unit, const quint16 value, const quint64 msec);
    /** @brief Update the HDD with new int32 data */
    void updateValue(const int uasId, const QString& name, const QString& unit, const qint32 value, const quint64 msec);
    /** @brief Update the HDD with new uint32 data */
    void updateValue(const int uasId, const QString& name, const QString& unit, const quint32 value, const quint64 msec);
    /** @brief Update the HDD with new int64 data */
80
    void updateValue(const int uasId, const QString& name, const QString& unit, const qint64 value, const quint64 msec);
81
    /** @brief Update the HDD with new uint64 data */
82
    void updateValue(const int uasId, const QString& name, const QString& unit, const quint64 value, const quint64 msec);
83 84 85
    /** @brief Update the HDD with new double data */
    void updateValue(const int uasId, const QString& name, const QString& unit, const double value, const quint64 msec);
	
86
    virtual void setActiveUAS(UASInterface* uas);
87 88
	
	/** @brief Connects a source to the updateValue() signals */
89
    void addSource(QObject* obj);
90 91
	/** @brief Disconnects a source to the updateValue() signals */
    void removeSource(QObject* obj);
pixhawk's avatar
pixhawk committed
92

93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
    /** @brief Removes a plot item by the action data */
    void removeItemByAction();
    /** @brief Bring up the menu to add a gauge */
    void addGauge();
    /** @brief Add a gauge using this spec string */
    void addGauge(const QString& gauge);
    /** @brief Set the title of this widget and any existing parent dock widget */
    void setTitle();
    /** @brief Set the number of colums via popup */
    void setColumns();
    /** @brief Set the number of colums */
    void setColumns(int cols);
    /** @brief Save the current layout and state to disk */
    void saveState();
    /** @brief Restore the last layout and state from disk */
    void restoreState();

110
protected slots:
111 112 113
    void enableGLRendering(bool enable);
    //void render(QPainter* painter, const QRectF& target = QRectF(), const QRect& source = QRect(), Qt::AspectRatioMode aspectRatioMode = Qt::KeepAspectRatio);
    void renderOverlay();
114
    void triggerUpdate();
115 116
    /** @brief Adjust the size hint for the current gauge layout */
    void adjustGaugeAspectRatio();
pixhawk's avatar
pixhawk committed
117 118

protected:
119
    QSize sizeHint() const;
120 121
    void changeEvent(QEvent* e);
    void paintEvent(QPaintEvent* event);
122
    void showEvent(QShowEvent* event);
123
    void hideEvent(QHideEvent* event);
124 125 126
    void contextMenuEvent(QContextMenuEvent* event);
    QList<QAction*> getItemRemoveActions();
    void createActions();
pixhawk's avatar
pixhawk committed
127 128 129
    float refLineWidthToPen(float line);
    float refToScreenX(float x);
    float refToScreenY(float y);
130 131
    float screenToRefX(float x);
    float screenToRefY(float y);
pixhawk's avatar
pixhawk committed
132 133 134
    void rotatePolygonClockWiseRad(QPolygonF& p, float angle, QPointF origin);
    void drawPolygon(QPolygonF refPolygon, QPainter* painter);
    void drawLine(float refX1, float refY1, float refX2, float refY2, float width, const QColor& color, QPainter* painter);
135 136
    void drawEllipse(float refX, float refY, float radiusX, float radiusY, float lineWidth, const QColor& color, QPainter* painter);
    void drawCircle(float refX, float refY, float radius, float lineWidth, const QColor& color, QPainter* painter);
pixhawk's avatar
pixhawk committed
137 138 139

    void drawChangeRateStrip(float xRef, float yRef, float height, float minRate, float maxRate, float value, QPainter* painter);
    void drawChangeIndicatorGauge(float xRef, float yRef, float radius, float expectedMaxChange, float value, const QColor& color, QPainter* painter, bool solid=true);
140
    void drawGauge(float xRef, float yRef, float radius, float min, float max, const QString name, float value, const QColor& color, QPainter* painter, bool symmetric, QPair<float, float> goodRange, QPair<float, float> criticalRange, bool solid=true);
pixhawk's avatar
pixhawk committed
141 142 143
    void drawSystemIndicator(float xRef, float yRef, int maxNum, float maxWidth, float maxHeight, QPainter* painter);
    void paintText(QString text, QColor color, float fontSize, float refX, float refY, QPainter* painter);

144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
//    //Holds the current centerpoint for the view, used for panning and zooming
//     QPointF currentCenterPoint;
//
//     //From panning the view
//     QPoint lastPanPoint;
//
//     //Set the current centerpoint in the
//     void setCenter(const QPointF& centerPoint);
//     QPointF getCenter() { return currentCenterPoint; }
//
//     //Take over the interaction
//     virtual void mousePressEvent(QMouseEvent* event);
//     virtual void mouseReleaseEvent(QMouseEvent* event);
//     virtual void mouseMoveEvent(QMouseEvent* event);
//     virtual void wheelEvent(QWheelEvent* event);
//     virtual void resizeEvent(QResizeEvent* event);

161
    UASInterface* uas;                 ///< The uas currently monitored
162
    QMap<QString, double> values;       ///< The variables this HUD displays
163
    QMap<QString, QString> units;      ///< The units
164 165 166
    QMap<QString, float> valuesDot;    ///< First derivative of the variable
    QMap<QString, float> valuesMean;   ///< Mean since system startup for this variable
    QMap<QString, int> valuesCount;    ///< Number of values received so far
pixhawk's avatar
pixhawk committed
167
    QMap<QString, quint64> lastUpdate; ///< The last update time for this variable
168 169
    QMap<QString, float> minValues;    ///< The minimum value this variable is assumed to have
    QMap<QString, float> maxValues;    ///< The maximum value this variable is assumed to have
170 171
    QMap<QString, bool> symmetric;     ///< Draw the gauge / dial symmetric bool = yes
    QMap<QString, bool> intValues;     ///< Is the gauge value an integer?
172
    QMap<QString, QString> customNames; ///< Custom names for the data names
pixhawk's avatar
pixhawk committed
173 174
    QMap<QString, QPair<float, float> > goodRanges; ///< The range of good values
    QMap<QString, QPair<float, float> > critRanges; ///< The range of critical values
175
    double scalingFactor;      ///< Factor used to scale all absolute values to screen coordinates
pixhawk's avatar
pixhawk committed
176
    float xCenterOffset, yCenterOffset; ///< Offset from center of window in mm coordinates
177 178
    float vwidth;              ///< Virtual width of this window, 200 mm per default. This allows to hardcode positions and aspect ratios. This virtual image plane is then scaled to the window size.
    float vheight;             ///< Virtual height of this window, 150 mm per default
pixhawk's avatar
pixhawk committed
179

180 181
    int xCenter;               ///< Center of the HUD instrument in pixel coordinates. Allows to off-center the whole instrument in its OpenGL window, e.g. to fit another instrument
    int yCenter;               ///< Center of the HUD instrument in pixel coordinates. Allows to off-center the whole instrument in its OpenGL window, e.g. to fit another instrument
pixhawk's avatar
pixhawk committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195

    // HUD colors
    QColor backgroundColor;    ///< Background color
    QColor defaultColor;       ///< Color for most HUD elements, e.g. pitch lines, center cross, change rate gauges
    QColor setPointColor;      ///< Color for the current control set point, e.g. yaw desired
    QColor warningColor;       ///< Color for warning messages
    QColor criticalColor;      ///< Color for caution messages
    QColor infoColor;          ///< Color for normal/default messages
    QColor fuelColor;          ///< Current color for the fuel message, can be info, warning or critical color

    // Blink rates
    int warningBlinkRate;      ///< Blink rate of warning messages, will be rounded to the refresh rate

    QTimer* refreshTimer;      ///< The main timer, controls the update rate
196
    static const int updateInterval = 300; ///< Update interval in milliseconds
pixhawk's avatar
pixhawk committed
197 198 199 200 201 202 203 204 205
    QPainter* hudPainter;
    QFont font;                ///< The HUD font, per default the free Bitstream Vera SANS, which is very close to actual HUD fonts
    QFontDatabase fontDatabase;///< Font database, only used to load the TrueType font file (the HUD font is directly loaded from file rather than from the system)
    bool hardwareAcceleration; ///< Enable hardware acceleration

    float strongStrokeWidth;   ///< Strong line stroke width, used throughout the HUD
    float normalStrokeWidth;   ///< Normal line stroke width, used throughout the HUD
    float fineStrokeWidth;     ///< Fine line stroke width, used throughout the HUD

lm's avatar
lm committed
206 207
    QStringList* acceptList;       ///< Variable names to plot
    QStringList* acceptUnitList;   ///< Unit names to plot
pixhawk's avatar
pixhawk committed
208

209
    quint64 lastPaintTime;     ///< Last time this widget was refreshed
210 211 212 213 214
    int columns;               ///< Number of instrument columns

    QAction* addGaugeAction;   ///< Action adding a gauge
    QAction* setTitleAction;   ///< Action setting the title
    QAction* setColumnsAction; ///< Action setting the number of columns
215
    bool valuesChanged;
216

pixhawk's avatar
pixhawk committed
217 218 219 220 221
private:
    Ui::HDDisplay *m_ui;
};

#endif // HDDISPLAY_H