dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
telemetrymonitorwidget.cpp
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 #include "telemetrymonitorwidget.h"
28 
29 #include <QObject>
30 #include <QtGui>
31 #include <QFont>
32 #include <QDebug>
33 
35  : QGraphicsView(parent)
36 {
37  setMinimumSize(200, parent->height());
38  setMaximumSize(200, parent->height());
39  setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
40  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
41  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
42  setAlignment(Qt::AlignCenter);
43  setFrameStyle(QFrame::NoFrame);
44  setStyleSheet("QGraphicsView{background:transparent;}");
45 
46  setAttribute(Qt::WA_TranslucentBackground);
47  setWindowFlags(Qt::FramelessWindowHint);
48 
49  // This comes from the size of the image
50  QGraphicsScene *scene = new QGraphicsScene(0, 0, 1240, 144, this);
51 
52  renderer = new QSvgRenderer();
53  if (renderer->load(QString(":/core/images/tx-rx.svg"))) {
54  graph = new QGraphicsSvgItem();
55  graph->setSharedRenderer(renderer);
56  graph->setElementId("txrxBackground");
57 
58  QString name;
59  QGraphicsSvgItem *pt;
60 
61  QRectF orig;
62  QMatrix Matrix;
63  QTransform trans;
64 
65  for (int i = 0; i < NODE_NUMELEM; i++) {
66  name = QString("tx%0").arg(i);
67  if (renderer->elementExists(name)) {
68  trans.reset();
69  pt = new QGraphicsSvgItem();
70  pt->setSharedRenderer(renderer);
71  pt->setElementId(name);
72  pt->setParentItem(graph);
73  orig = renderer->boundsOnElement(name);
74  Matrix = renderer->matrixForElement(name);
75  orig = Matrix.mapRect(orig);
76  trans.translate(orig.x(), orig.y());
77  pt->setTransform(trans, false);
78  txNodes.append(pt);
79  }
80 
81  name = QString("rx%0").arg(i);
82  if (renderer->elementExists(name)) {
83  trans.reset();
84  pt = new QGraphicsSvgItem();
85  pt->setSharedRenderer(renderer);
86  pt->setElementId(name);
87  pt->setParentItem(graph);
88  orig = renderer->boundsOnElement(name);
89  Matrix = renderer->matrixForElement(name);
90  orig = Matrix.mapRect(orig);
91  trans.translate(orig.x(), orig.y());
92  pt->setTransform(trans, false);
93  rxNodes.append(pt);
94  }
95  }
96  scene->addItem(graph);
97  txSpeed = new QGraphicsTextItem();
98  txSpeed->setDefaultTextColor(Qt::white);
99  txSpeed->setFont(QFont("Helvetica", 30, 2));
100  txSpeed->setParentItem(graph);
101  orig = renderer->boundsOnElement("txPH");
102  Matrix = renderer->matrixForElement("txPH");
103  orig = Matrix.mapRect(orig);
104  trans.reset();
105  trans.translate(orig.x(), orig.y() - 8);
106  txSpeed->setTransform(trans, false);
107  rxSpeed = new QGraphicsTextItem();
108  rxSpeed->setDefaultTextColor(Qt::white);
109  rxSpeed->setFont(QFont("Helvetica", 30, 2));
110  rxSpeed->setParentItem(graph);
111  trans.reset();
112  orig = renderer->boundsOnElement("rxPH");
113  Matrix = renderer->matrixForElement("rxPH");
114  orig = Matrix.mapRect(orig);
115  trans.translate(orig.x(), orig.y() - 8);
116  rxSpeed->setTransform(trans, false);
117 
118  scene->setSceneRect(graph->boundingRect());
119 
120  setScene(scene);
121  }
122 
123  m_connected = false;
124  txValue = 0.0;
125  rxValue = 0.0;
126 
127  setMin(0.0);
128  setMax(1200.0);
129 
130  showTelemetry();
131 }
132 
134 {
135  while (!txNodes.isEmpty())
136  delete txNodes.takeFirst();
137 
138  while (!rxNodes.isEmpty())
139  delete rxNodes.takeFirst();
140 }
141 
143 {
144  m_connected = true;
145 
146  // flash the lights
147  updateTelemetry(maxValue, maxValue);
148 }
149 
151 {
152  // flash the lights
153  updateTelemetry(maxValue, maxValue);
154 
155  m_connected = false;
156  updateTelemetry(0.0, 0.0);
157 }
158 
163 void TelemetryMonitorWidget::updateTelemetry(double txRate, double rxRate)
164 {
165  txValue = txRate;
166  rxValue = rxRate;
167 
168  showTelemetry();
169 }
170 
175 {
176  txIndex = (txValue - minValue) / (maxValue - minValue) * NODE_NUMELEM;
177  rxIndex = (rxValue - minValue) / (maxValue - minValue) * NODE_NUMELEM;
178 
179  if (m_connected)
180  this->setToolTip(QString("Tx: %0 bytes/sec\nRx: %1 bytes/sec")
181  .arg(txValue, 0, 'f', 0)
182  .arg(rxValue, 0, 'f', 0));
183  else
184  this->setToolTip(QString("Disconnected"));
185 
186  int i;
187  QGraphicsItem *node;
188 
189  for (i = 0; i < txNodes.count(); i++) {
190  node = txNodes.at(i);
191  node->setVisible(m_connected && i < txIndex);
192  node->update();
193  }
194 
195  for (i = 0; i < rxNodes.count(); i++) {
196  node = rxNodes.at(i);
197  node->setVisible(m_connected && i < rxIndex);
198  node->update();
199  }
200 
201  txSpeed->setPlainText(QString::number(txValue, 'f', 0));
202  txSpeed->setVisible(m_connected);
203 
204  rxSpeed->setPlainText(QString::number(rxValue, 'f', 0));
205  rxSpeed->setVisible(m_connected);
206 
207  update();
208 }
209 
210 void TelemetryMonitorWidget::showEvent(QShowEvent *event)
211 {
212  Q_UNUSED(event);
213 
214  fitInView(graph, Qt::KeepAspectRatio);
215 }
216 
217 void TelemetryMonitorWidget::resizeEvent(QResizeEvent *event)
218 {
219  Q_UNUSED(event);
220 
221  fitInView(graph, Qt::KeepAspectRatio);
222 }
TelemetryMonitorWidget(QWidget *parent=nullptr)
for i
Definition: OPPlots.m:140
void resizeEvent(QResizeEvent *event)
void showEvent(QShowEvent *event)
void updateTelemetry(double txRate, double rxRate)
Called by the UAVObject which got updated Updates the numeric value and/or the icon if the dial wants...