QGroundControl
Ground Control Station for MAVLink Drones
Loading...
Searching...
No Matches
MAVLinkMessageField.cc
Go to the documentation of this file.
3#include "MAVLinkMessage.h"
4#include "QGCApplication.h"
6
7#include <QtCharts/QLineSeries>
8#include <QtCharts/QAbstractSeries>
9
10QGC_LOGGING_CATEGORY(MAVLinkMessageFieldLog, "AnalyzeView.MAVLinkMessageField")
11
12QGCMAVLinkMessageField::QGCMAVLinkMessageField(const QString &name, const QString &type, QGCMAVLinkMessage *parent)
13 : QObject(parent)
14 , _type(type)
15 , _name(name)
16 , _msg(parent)
17{
18 // qCDebug(MAVLinkMessageFieldLog) << Q_FUNC_INFO << this;
19
20 qCDebug(MAVLinkMessageFieldLog) << "Field:" << name << type;
21}
22
23QGCMAVLinkMessageField::~QGCMAVLinkMessageField()
24{
25 // qCDebug(MAVLinkMessageFieldLog) << Q_FUNC_INFO << this;
26}
27
28void QGCMAVLinkMessageField::addSeries(MAVLinkChartController *chartController, QAbstractSeries *series)
29{
30 if (_pSeries) {
31 return;
32 }
33
34 _chartController = chartController;
35 _pSeries = series;
36 emit seriesChanged();
37
38 _dataIndex = 0;
39 _msg->updateFieldSelection();
40}
41
42void QGCMAVLinkMessageField::delSeries()
43{
44 if (!_pSeries) {
45 return;
46 }
47
48 _values.clear();
49 QLineSeries *const lineSeries = static_cast<QLineSeries*>(_pSeries);
50 lineSeries->replace(_values);
51 _pSeries = nullptr;
52 _chartController = nullptr;
53 emit seriesChanged();
54 _msg->updateFieldSelection();
55}
56
57QString QGCMAVLinkMessageField::label() const
58{
59 return (_msg->name() + ": " + _name);
60}
61
62void QGCMAVLinkMessageField::setSelectable(bool sel)
63{
64 if (_selectable != sel) {
65 _selectable = sel;
66 emit selectableChanged();
67 }
68}
69
70int QGCMAVLinkMessageField::chartIndex() const
71{
72 if (_chartController) {
73 return _chartController->chartIndex();
74 }
75
76 return 0;
77}
78
79void QGCMAVLinkMessageField::updateValue(const QString &newValue, qreal v)
80{
81 if (_value != newValue) {
82 _value = newValue;
83 emit valueChanged();
84 }
85
86 if (!_pSeries || !_chartController) {
87 return;
88 }
89
90 const int count = _values.count();
91 if (count < (50 * 60)) {
92 const QPointF p(qgcApp()->msecsSinceBoot(), v);
93 _values.append(p);
94 } else {
95 if (_dataIndex >= count) {
96 _dataIndex = 0;
97 }
98 _values[_dataIndex].setX(qgcApp()->msecsSinceBoot());
99 _values[_dataIndex].setY(v);
100 _dataIndex++;
101 }
102
103 if (_chartController->rangeYIndex() != 0) {
104 return;
105 }
106
107 qreal vmin = std::numeric_limits<qreal>::max();
108 qreal vmax = std::numeric_limits<qreal>::min();
109 for (const QPointF &point : _values) {
110 const qreal value = point.y();
111 if (vmax < value) {
112 vmax = value;
113 }
114 if (vmin > value) {
115 vmin = value;
116 }
117 }
118
119 bool changed = false;
120 if (std::abs(_rangeMin - vmin) > 0.000001) {
121 _rangeMin = vmin;
122 changed = true;
123 }
124
125 if (std::abs(_rangeMax - vmax) > 0.000001) {
126 _rangeMax = vmax;
127 changed = true;
128 }
129
130 if (changed) {
131 _chartController->updateYRange();
132 }
133}
134
135void QGCMAVLinkMessageField::updateSeries()
136{
137 const int count = _values.count();
138 if (count <= 1) {
139 return;
140 }
141
142 QList<QPointF> s;
143 s.reserve(count);
144 int idx = _dataIndex;
145 for (int i = 0; i < count; i++, idx++) {
146 if (idx >= count) {
147 idx = 0;
148 }
149
150 const QPointF p(_values[idx]);
151 s.append(p);
152 }
153
154 QLineSeries *const lineSeries = static_cast<QLineSeries*>(_pSeries);
155 lineSeries->replace(s);
156}
#define qgcApp()
#define QGC_LOGGING_CATEGORY(name, categoryStr)