dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
gpssnrwidget.cpp
Go to the documentation of this file.
1 #include "gpssnrwidget.h"
2 
3 GpsSnrWidget::GpsSnrWidget(QWidget *parent)
4  : QGraphicsView(parent)
5 {
6 
7  scene = new QGraphicsScene(this);
8  setScene(scene);
9 
10  // Now create 'max Shown Satellites' satellite icons which we will move around on the map:
11  for (int i = 0; i < MAX_SATELLITES; i++) {
12  satellites[i][0] = 0;
13  satellites[i][1] = 0;
14  satellites[i][2] = 0;
15  satellites[i][3] = 0;
16  }
17 
18  for (int i = 0; i < MAX_SHOWN_SATELLITES; i++) {
19  boxes[i] = new QGraphicsRectItem();
20  boxes[i]->setBrush(QColor("Green"));
21  scene->addItem(boxes[i]);
22  boxes[i]->hide();
23 
24  satTexts[i] = new QGraphicsSimpleTextItem("##", boxes[i]);
25  satTexts[i]->setBrush(QColor("White"));
26  satTexts[i]->setFont(QFont("Courier", -1, QFont::ExtraBold));
27 
28  satSNRs[i] = new QGraphicsSimpleTextItem("##", boxes[i]);
29  satSNRs[i]->setBrush(QColor("Black"));
30  satSNRs[i]->setFont(QFont("Courier", -1, QFont::ExtraBold));
31  }
32 }
33 
35 {
36  delete scene;
37  scene = nullptr;
38 }
39 
40 void GpsSnrWidget::showEvent(QShowEvent *event)
41 {
42  Q_UNUSED(event)
43 
45 }
46 
47 void GpsSnrWidget::resizeEvent(QResizeEvent *event)
48 {
49  Q_UNUSED(event);
50 
52 }
53 
55 {
56  scene->setSceneRect(0, 0, this->viewport()->width(), this->viewport()->height());
57 
58  int drawIndex = 0;
59 
60  /* Display ones with a positive snr first; UBX driver orders this way but
61  * NMEA does not
62  */
63  for (int index = 0; index < MAX_SATELLITES; index++) {
64  if (satellites[index][3] > 0) {
65  drawSat(drawIndex++, index);
66  }
67  }
68 
69  for (int index = 0; index < MAX_SATELLITES; index++) {
70  if (satellites[index][3] <= 0) {
71  drawSat(drawIndex++, index);
72  }
73  }
74 }
75 
76 void GpsSnrWidget::updateSat(int index, int prn, int elevation, int azimuth, int snr)
77 {
78  if (index >= MAX_SATELLITES) {
79  // A bit of error checking never hurts.
80  return;
81  }
82 
83  // TODO: add range checking
84  satellites[index][0] = prn;
85  satellites[index][1] = elevation;
86  satellites[index][2] = azimuth;
87  satellites[index][3] = snr;
88 }
89 
90 void GpsSnrWidget::drawSat(int drawIndex, int index)
91 {
92  if (index >= MAX_SATELLITES) {
93  // A bit of error checking never hurts.
94  return;
95  }
96 
97  if (drawIndex >= MAX_SHOWN_SATELLITES) {
98  return;
99  }
100 
101  const int prn = satellites[index][0];
102  const int snr = satellites[index][3];
103  if (prn && snr) {
104  boxes[drawIndex]->show();
105 
106  // When using integer values, width and height are the
107  // box width and height, but the left and bottom borders are drawn on the box,
108  // and the top and right borders are drawn just next to the box.
109  // So the box seems one pixel wider and higher with a border.
110  // I'm sure there's a good explanation for that :)
111 
112  // Casting to int rounds down, which is what I want.
113  // Minus 2 to allow a pixel of white left and right.
114  int availableWidth = (int)((scene->width() - 2) / MAX_SHOWN_SATELLITES);
115 
116  // 2 pixels, one on each side.
117  qreal width = availableWidth - 2;
118  // SNR = 1-99 (0 is special)..
119  qreal height = int((scene->height() / 99) * snr + 0.5);
120  // 1 for showing a pixel of white to the left.
121  qreal x = availableWidth * drawIndex + 1;
122  // Rember, 0 is at the top.
123  qreal y = scene->height() - height;
124  // Compensate for the extra pixel for the border.
125  boxes[drawIndex]->setRect(0, 0, width - 1, height - 1);
126  boxes[drawIndex]->setPos(x, y);
127 
128  QRectF boxRect = boxes[drawIndex]->boundingRect();
129  QString prnString = QString().number(prn);
130  if (prnString.length() == 1) {
131  prnString = "0" + prnString;
132  }
133  satTexts[drawIndex]->setText(prnString);
134  QRectF textRect = satTexts[drawIndex]->boundingRect();
135 
136  QTransform matrix;
137  qreal scale = 0.85 * (boxRect.width() / textRect.width());
138  matrix.translate(boxRect.width() / 2, boxRect.height());
139  matrix.scale(scale, scale);
140  matrix.translate(-textRect.width() / 2, -textRect.height());
141  satTexts[drawIndex]->setTransform(matrix, false);
142 
143  QString snrString = QString().number(snr);
144  if (snrString.length() == 1) { // Will probably never happen!
145  snrString = "0" + snrString;
146  }
147  satSNRs[drawIndex]->setText(snrString);
148  textRect = satSNRs[drawIndex]->boundingRect();
149 
150  matrix.reset();
151  scale = 0.85 * (boxRect.width() / textRect.width());
152  matrix.translate(boxRect.width() / 2, 0);
153  matrix.scale(scale, scale);
154  matrix.translate(-textRect.width() / 2, -textRect.height());
155  satSNRs[drawIndex]->setTransform(matrix, false);
156 
157  } else {
158  boxes[drawIndex]->hide();
159  }
160 }
for i
Definition: OPPlots.m:140
GpsSnrWidget(QWidget *parent=nullptr)
Definition: gpssnrwidget.cpp:3
void resizeEvent(QResizeEvent *event)
void satellitesDone()
x
Definition: OPPlots.m:100
void showEvent(QShowEvent *event)
void updateSat(int index, int prn, int elevation, int azimuth, int snr)
y
Definition: OPPlots.m:101