dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
gpsconstellationwidget.cpp
Go to the documentation of this file.
1 
12 /*
13  * This program is free software; you can redistribute it and/or modify
14  * it under the terms of the GNU General Public License as published by
15  * the Free Software Foundation; either version 3 of the License, or
16  * (at your option) any later version.
17  *
18  * This program is distributed in the hope that it will be useful, but
19  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21  * for more details.
22  *
23  * You should have received a copy of the GNU General Public License along
24  * with this program; if not, see <http://www.gnu.org/licenses/>
25  */
26 
27 #include "gpsconstellationwidget.h"
28 
29 #include <QtGui>
30 #include <QDebug>
31 
32 /*
33  * Initialize the widget
34  */
36  : QGraphicsView(parent)
37 {
38 
39  // Create a layout, add a QGraphicsView and put the SVG inside.
40  // The constellation widget looks like this:
41  // |--------------------|
42  // | |
43  // | |
44  // | Constellation |
45  // | |
46  // | |
47  // | |
48  // |--------------------|
49 
50  setHorizontalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
51  setVerticalScrollBarPolicy(Qt::ScrollBarAlwaysOff);
52 
53  QSvgRenderer *renderer = new QSvgRenderer();
54  renderer->load(QString(":/gpsgadget/images/gpsSky.svg"));
55 
56  world = new QGraphicsSvgItem();
57  world->setSharedRenderer(renderer);
58  world->setElementId("map");
59 
60  scene = new QGraphicsScene(this);
61  scene->addItem(world);
62  scene->setSceneRect(world->boundingRect());
63  setScene(scene);
64 
65  // Now create 'maxSatellites' satellite icons which we will move around on the map:
66  for (int i = 0; i < MAX_SATELLITES; i++) {
67  satellites[i][0] = 0;
68  satellites[i][1] = 0;
69  satellites[i][2] = 0;
70  satellites[i][3] = 0;
71 
72  satIcons[i] = new QGraphicsSvgItem(world);
73  satIcons[i]->setSharedRenderer(renderer);
74  satIcons[i]->setElementId("gpssatellite");
75  satIcons[i]->hide();
76 
77  satTexts[i] = new QGraphicsSimpleTextItem("##", satIcons[i]);
78  satTexts[i]->setBrush(QColor("Black"));
79  satTexts[i]->setFont(QFont("Courier", -1, QFont::Bold));
80  }
81 }
82 
84 {
85  delete scene;
86  scene = nullptr;
87 
88  // delete renderer;
89  // renderer = 0;
90 }
91 
92 void GpsConstellationWidget::showEvent(QShowEvent *event)
93 {
94  Q_UNUSED(event)
95  // Thit fitInView method should only be called now, once the
96  // widget is shown, otherwise it cannot compute its values and
97  // the result is usually a ahrsbargraph that is way too small.
98  fitInView(world, Qt::KeepAspectRatio);
99  // Scale, can't use fitInView since that doesn't work until we're shown.
100  // qreal factor = height()/world->boundingRect().height();
101  // world->setScale(factor);
102  // setSceneRect(world->boundingRect());
103 }
104 
105 void GpsConstellationWidget::resizeEvent(QResizeEvent *event)
106 {
107  Q_UNUSED(event);
108  fitInView(world, Qt::KeepAspectRatio);
109 }
110 
111 void GpsConstellationWidget::updateSat(int index, int prn, int elevation, int azimuth, int snr)
112 {
113  if (index >= MAX_SATELLITES) {
114  // A bit of error checking never hurts.
115  return;
116  }
117 
118  // TODO: add range checking
119  satellites[index][0] = prn;
120  satellites[index][1] = elevation;
121  satellites[index][2] = azimuth;
122  satellites[index][3] = snr;
123 
124  if (prn && elevation >= 0) {
125  /* From ublox 8 receiver manual; summary of all SV numbering schemes */
126  if (prn <= 32) {
127  satIcons[index]->setElementId("gpssatellite");
128  } else if ((prn >= 120) && (prn <= 158)) {
129  satIcons[index]->setElementId("sbassatellite");
130  } else {
131  satIcons[index]->setElementId("othersat");
132  }
133 
134  QPointF opd = polarToCoord(elevation, azimuth);
135  opd += QPointF(-satIcons[index]->boundingRect().center().x(),
136  -satIcons[index]->boundingRect().center().y());
137  satIcons[index]->setTransform(QTransform::fromTranslate(opd.x(), opd.y()), false);
138 
139  if (snr) {
140  satIcons[index]->setOpacity(1.0);
141  satIcons[index]->setZValue(1.0);
142  } else {
143  satIcons[index]->setOpacity(0.53);
144  satIcons[index]->setZValue(0);
145  }
146  satIcons[index]->show();
147 
148  QRectF iconRect = satIcons[index]->boundingRect();
149  QString prnString = QString().number(prn);
150  if (prnString.length() == 1) {
151  prnString = "0" + prnString;
152  }
153  satTexts[index]->setText(prnString);
154  QRectF textRect = satTexts[index]->boundingRect();
155 
156  QTransform matrix;
157  qreal scale = 0.65 * (iconRect.width() / textRect.width());
158  matrix.translate(iconRect.width() / 2, iconRect.height() / 2);
159  matrix.scale(scale, scale);
160  matrix.translate(-textRect.width() / 2, -textRect.height() / 2);
161  satTexts[index]->setTransform(matrix, false);
162  } else {
163  satIcons[index]->hide();
164  }
165 }
166 
171 QPointF GpsConstellationWidget::polarToCoord(int elevation, int azimuth)
172 {
173  double x;
174  double y;
175  double rad_elevation;
176  double rad_azimuth;
177 
178  rad_elevation = M_PI * elevation / 180;
179  rad_azimuth = M_PI * azimuth / 180;
180 
181  x = cos(rad_elevation) * sin(rad_azimuth);
182  y = -cos(rad_elevation) * cos(rad_azimuth);
183 
184  x = world->boundingRect().width() / 2 * x;
185  y = world->boundingRect().height() / 2 * y;
186 
187  x = (world->boundingRect().width() / 2) + x;
188  y = (world->boundingRect().height() / 2) + y;
189 
190  return QPointF(x, y);
191 }
for i
Definition: OPPlots.m:140
void showEvent(QShowEvent *event)
x
Definition: OPPlots.m:100
void updateSat(int index, int prn, int elevation, int azimuth, int snr)
GpsConstellationWidget(QWidget *parent=nullptr)
void resizeEvent(QResizeEvent *event)
y
Definition: OPPlots.m:101