3 * Copyright (C) 2011 Roman Moravcik
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "satelliteview.h"
25 # define M_PI 3.14159265358979323846
28 void SatelliteView::updateWidget(const QList<QGeoSatelliteInfo> &satellites, bool inUseList)
31 m_satellitesInUse = satellites;
33 m_satellitesInView = satellites;
38 void SatelliteView::paintEvent(QPaintEvent * /* event */)
40 QPainter painter(this);
41 painter.setRenderHint(QPainter::Antialiasing);
43 double widget_width = 0;
44 double widget_height = 0;
48 if (rect().width() < rect().height()) {
49 /* Portrait orientation */
50 widget_width = rect().width() - (2 * m_margin);
51 widget_height = widget_width;
52 widget_x = rect().x() + m_margin;
53 widget_y = rect().y() + (rect().height() - widget_height) / 2.0;
55 /* Landscape orientation */
56 widget_height = rect().height() - (2 * m_margin);
57 widget_width = widget_height;
58 widget_x = rect().x() + (rect().width() - widget_width) / 2.0;
59 widget_y = rect().y() + m_margin;
61 QRectF widgetArea(widget_x, widget_y, widget_width, widget_height);
63 /* Draw N/E/S/W labels */
64 paintLabels(painter, widgetArea);
66 int label_height = painter.fontMetrics().height();
67 int label_width = label_height;
68 double grid_x = widget_x + m_margin + label_width;
69 double grid_y = widget_y + m_margin + label_height;
70 double grid_width = widget_width - (2 * m_margin) - (2 * label_width);
71 double grid_height = widget_height - (2 * m_margin) - (2 * label_height);
72 QRectF gridArea(grid_x, grid_y, grid_width, grid_height);
75 paintGrid(painter, gridArea);
77 /* Draw satellites in view */
78 for (int index = 1; index <= m_numOfSatellites; ++index) {
79 paintSatellite(painter, gridArea, index);
83 void SatelliteView::paintLabels(QPainter &painter, const QRectF &area)
85 int label_height = painter.fontMetrics().height();
86 int label_width = label_height;
88 painter.setPen(QApplication::palette().color(QPalette::Text));
91 QRect northLabel(area.x(), area.y(), area.width(), label_height);
92 painter.drawText(northLabel, Qt::AlignCenter, tr("N"));
95 QRect southLabel(area.x(), area.y() + area.height() - label_height, area.width(), label_height);
96 painter.drawText(southLabel, Qt::AlignCenter, tr("S"));
99 QRect westLabel(area.x(), area.y(), label_width, area.height());
100 painter.drawText(westLabel, Qt::AlignCenter, tr("W"));
103 QRect eastLabel(area.x() + area.width() - label_width, area.y(), label_width, area.height());
104 painter.drawText(eastLabel, Qt::AlignCenter, tr("E"));
107 void SatelliteView::paintGrid(QPainter &painter, const QRectF &area)
109 double center_x = area.x() + area.width() / 2.0;
110 double center_y = area.y() + area.height() / 2.0;
112 painter.setPen(QPen(m_graphGridColor, 2, Qt::DotLine));
113 for (int i = 0; i < 3; i++) {
114 double grid_width = area.width() * (3.0 - i) / 3.0;
115 double grid_height = area.height() * (3.0 - i) / 3.0;
116 double grid_x = area.x() + (area.width() * i / 6.0);
117 double grid_y = area.y() + (area.height() * i / 6.0);
119 QRectF gridArea(grid_x, grid_y, grid_width, grid_height);
120 painter.drawArc(gridArea, 0, 5760);
123 for (int i = 0; i < 360; i += 30) {
124 painter.drawLine(center_x, center_y,
125 center_x + ((area.width() / 2.0) * qCos(i * M_PI / 180.0)),
126 center_y + ((area.height() / 2.0) * qSin(i * M_PI / 180.0)));
130 void SatelliteView::paintSatellite(QPainter &painter, const QRectF &area, int index)
132 double center_x = area.x() + area.width() / 2.0;
133 double center_y = area.y() + area.height() / 2.0;
136 int satelliteElevation = -1;
137 int satelliteAzimuth = 0;
138 for (int iter = 0; iter < m_satellitesInView.count(); iter++) {
139 if (m_satellitesInView.at(iter).prnNumber() == index) {
140 if (m_satellitesInView.at(iter).hasAttribute(QGeoSatelliteInfo::Elevation))
141 satelliteElevation = m_satellitesInView.at(iter).attribute(QGeoSatelliteInfo::Elevation);
143 if (m_satellitesInView.at(iter).hasAttribute(QGeoSatelliteInfo::Azimuth))
144 satelliteAzimuth = m_satellitesInView.at(iter).attribute(QGeoSatelliteInfo::Azimuth);
148 for (int iter = 0; iter < m_satellitesInUse.count(); iter++) {
149 if (m_satellitesInUse.at(iter).prnNumber() == index) {
155 /* Display only visible satellites */
156 if (satelliteElevation >= 0) {
157 int satellite_width = 30;
158 int satellite_height = 30;
159 double temp = (180 - satelliteElevation) * (double) area.width() / 360.0;
160 double satellite_x = temp * qCos((satelliteAzimuth - 90) * M_PI / 180.0) +
161 center_x - (double) (satellite_width / 2.0);
162 double satellite_y = temp * qSin((satelliteAzimuth - 90) * M_PI / 180.0) +
163 center_y - (double) (satellite_height / 2.0);
164 QRectF satelliteArea(satellite_x, satellite_y, satellite_width, satellite_height);
166 /* Draw a small circle with PRN number inside */
167 painter.setPen(m_satelliteBorderColor);
169 painter.setBrush(m_satelliteInUseColor);
171 painter.setBrush(m_satellitehInViewColor);
172 painter.drawChord(satelliteArea, 0, 5760);
174 int panel_width = 10;
175 painter.setBrush(m_satelliteSolarPanelColor);
176 QRectF solarPanelLArea(satellite_x - panel_width, satellite_y,
177 panel_width, satellite_height);
178 painter.drawRect(solarPanelLArea);
180 QRectF solarPanelRArea(satellite_x + satellite_width, satellite_y,
181 panel_width, satellite_height);
182 painter.drawRect(solarPanelRArea);
184 /* Satellite's PRN must be painter over the satellite circle */
185 QFont font = QApplication::font();
186 font.setPixelSize(12);
187 painter.setFont(font);
188 painter.setPen(m_satelliteBorderColor);
189 painter.drawText(satelliteArea, Qt::AlignCenter, QString::number(index));
193 SatelliteView::SatelliteView(QWidget *parent) : QWidget(parent)
195 m_graphGridColor = QColor(74, 69, 66);
196 m_satelliteBorderColor = QColor(0, 0, 0);
197 m_satelliteSolarPanelColor = QColor(0, 75, 255);
198 m_satellitehInViewColor = QColor(153, 153, 153);
199 m_satelliteInUseColor = QColor(51, 191, 51);