1 #include "gpssnrwidget.h"
3 GpsSnrWidget::GpsSnrWidget(QWidget
*parent
) :
6 scene
= new QGraphicsScene(this);
9 // Now create 'maxSatellites' satellite icons which we will move around on the map:
10 for (int i
= 0; i
< MAX_SATTELITES
; i
++) {
16 boxes
[i
] = new QGraphicsRectItem();
17 boxes
[i
]->setBrush(QColor("Green"));
18 scene
->addItem(boxes
[i
]);
21 satTexts
[i
] = new QGraphicsSimpleTextItem("##", boxes
[i
]);
22 satTexts
[i
]->setBrush(QColor("Black"));
23 satTexts
[i
]->setFont(QFont("Courier"));
25 satSNRs
[i
] = new QGraphicsSimpleTextItem("##", boxes
[i
]);
26 satSNRs
[i
]->setBrush(QColor("Black"));
27 satSNRs
[i
]->setFont(QFont("Courier"));
31 GpsSnrWidget::~GpsSnrWidget()
37 void GpsSnrWidget::showEvent(QShowEvent
*event
)
40 scene
->setSceneRect(0, 0, this->viewport()->width(), this->viewport()->height());
41 for (int index
= 0; index
< MAX_SATTELITES
; index
++) {
46 void GpsSnrWidget::resizeEvent(QResizeEvent
*event
)
49 scene
->setSceneRect(0, 0, this->viewport()->width(), this->viewport()->height());
50 for (int index
= 0; index
< MAX_SATTELITES
; index
++) {
55 void GpsSnrWidget::updateSat(int index
, int prn
, int elevation
, int azimuth
, int snr
)
57 if (index
>= MAX_SATTELITES
) {
58 // A bit of error checking never hurts.
62 // TODO: add range checking
63 satellites
[index
][0] = prn
;
64 satellites
[index
][1] = elevation
;
65 satellites
[index
][2] = azimuth
;
66 satellites
[index
][3] = snr
;
71 void GpsSnrWidget::drawSat(int index
)
73 if (index
>= MAX_SATTELITES
) {
74 // A bit of error checking never hurts.
78 const int prn
= satellites
[index
][0];
79 const int snr
= satellites
[index
][3];
83 // When using integer values, width and height are the
84 // box width and height, but the left and bottom borders are drawn on the box,
85 // and the top and right borders are drawn just next to the box.
86 // So the box seems one pixel wider and higher with a border.
87 // I'm sure there's a good explanation for that :)
89 // Casting to int rounds down, which is what I want.
90 // Minus 2 to allow a pixel of white left and right.
91 int availableWidth
= (int)((scene
->width() - 2) / MAX_SATTELITES
);
93 // 2 pixels, one on each side.
94 qreal width
= availableWidth
- 2;
95 // SNR = 1-99 (0 is special)..
96 qreal height
= int((scene
->height() / 99) * snr
+ 0.5);
97 // 1 for showing a pixel of white to the left.
98 qreal x
= availableWidth
* index
+ 1;
99 // Rember, 0 is at the top.
100 qreal y
= scene
->height() - height
;
101 // Compensate for the extra pixel for the border.
102 boxes
[index
]->setRect(0, 0, width
- 1, height
- 1);
103 boxes
[index
]->setPos(x
, y
);
105 QRectF boxRect
= boxes
[index
]->boundingRect();
107 // Change color for SBAS & QZSS 120-158, 193-197 range
108 // GLONASS range 65-96 or 255, BeiDou 33-64 or 159-163
109 if ((prn
> 119 && prn
< 159) || (prn
> 192 && prn
< 198)) {
110 boxes
[index
]->setBrush(QColor("#fd700b"));
111 } else if ((prn
> 64 && prn
< 97) || 255 == prn
) {
112 boxes
[index
]->setBrush(QColor("Cyan"));
113 } else if ((prn
> 32 && prn
< 65) || (prn
> 158 && prn
< 164)) {
114 boxes
[index
]->setBrush(QColor("Red"));
116 boxes
[index
]->setBrush(QColor("Green"));
118 QString prnString
= QString().number(prn
);
119 if (prnString
.length() == 1) {
120 prnString
= "0" + prnString
;
122 satTexts
[index
]->setText(prnString
);
123 QRectF textRect
= satTexts
[index
]->boundingRect();
126 qreal scale
= 0.85 * (boxRect
.width() / textRect
.width());
127 matrix
.translate(boxRect
.width() / 2, boxRect
.height());
128 matrix
.scale(scale
, scale
);
129 matrix
.translate(-textRect
.width() / 2, -textRect
.height());
130 satTexts
[index
]->setTransform(matrix
, false);
132 QString snrString
= QString().number(snr
);
133 if (snrString
.length() == 1) { // Will probably never happen!
134 snrString
= "0" + snrString
;
136 satSNRs
[index
]->setText(snrString
);
137 textRect
= satSNRs
[index
]->boundingRect();
140 scale
= 0.85 * (boxRect
.width() / textRect
.width());
141 matrix
.translate(boxRect
.width() / 2, 0);
142 matrix
.scale(scale
, scale
);
143 matrix
.translate(-textRect
.width() / 2, -textRect
.height());
144 satSNRs
[index
]->setTransform(matrix
, false);
146 boxes
[index
]->hide();