Initial import of version 0.3
[gpsdata] / src / satelliteview.cpp
1 /*
2  *  GPSData for Maemo.
3  *  Copyright (C) 2011 Roman Moravcik
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include <QtGui>
21
22 #include "satelliteview.h"
23
24 #ifndef M_PI
25 #define M_PI 3.14159265358979323846
26 #endif
27
28 void SatelliteView::updateWidget(const QList<QGeoSatelliteInfo> &satellites, bool inUseList)
29 {
30     if (inUseList)
31         m_satellitesInUse = satellites;
32     else
33         m_satellitesInView = satellites;
34
35     update();
36 }
37
38 void SatelliteView::paintEvent(QPaintEvent * /* event */)
39 {
40     QPainter painter(this);
41     painter.setRenderHint(QPainter::Antialiasing);
42
43     double widget_width = 0;
44     double widget_height = 0;
45     double widget_x = 0;
46     double widget_y = 0;
47
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;
54     } else {
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;
60     }
61     QRectF widgetArea(widget_x, widget_y, widget_width, widget_height);
62
63     /* Draw N/E/S/W labels */
64     paintLabels(painter, widgetArea);
65
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);
73
74     /* Draw grid */
75     paintGrid(painter, gridArea);
76
77     /* Draw satellites in view */
78     for (int index = 1; index <= m_numOfSatellites; ++index) {
79         paintSatellite(painter, gridArea, index);
80     }
81 }
82
83 void SatelliteView::paintLabels(QPainter &painter, const QRectF &area)
84 {
85     int label_height = painter.fontMetrics().height();
86     int label_width = label_height;
87
88     painter.setPen(QApplication::palette().color(QPalette::Text));
89
90     /* "North" label */
91     QRect northLabel(area.x(), area.y(), area.width(), label_height);
92     painter.drawText(northLabel, Qt::AlignCenter, tr("N"));
93
94     /* "South" label */
95     QRect southLabel(area.x(), area.y() + area.height() - label_height, area.width(), label_height);
96     painter.drawText(southLabel, Qt::AlignCenter, tr("S"));
97
98     /* "West" label */
99     QRect westLabel(area.x(), area.y(), label_width, area.height());
100     painter.drawText(westLabel, Qt::AlignCenter, tr("W"));
101
102     /* "East" label */
103     QRect eastLabel(area.x() + area.width() - label_width, area.y(), label_width, area.height());
104     painter.drawText(eastLabel, Qt::AlignCenter, tr("E"));
105 }
106
107 void SatelliteView::paintGrid(QPainter &painter, const QRectF &area)
108 {
109     painter.setPen(m_graphGridColor);
110     for (int i = 0; i < 3; i++) {
111         double grid_width =  area.width() * (3.0 - i) / 3.0;
112         double grid_height = area.height() * (3.0 - i) / 3.0;
113         double grid_x = area.x() + (area.width() * i / 6.0);
114         double grid_y = area.y() + (area.height() * i / 6.0);
115
116         QRectF gridArea(grid_x, grid_y, grid_width, grid_height);
117         painter.drawArc(gridArea, 0, 5760);
118     }
119
120     /* Grid */
121     painter.drawLine(area.x(), area.y() + area.height() / 2.0, area.x() + area.width(), area.y() + area.height() / 2.0);
122     painter.drawLine(area.x() + area.width() / 2.0, area.y(), area.x() + area.width() / 2.0, area.y() + area.height());
123
124 }
125
126 void SatelliteView::paintSatellite(QPainter &painter, const QRectF &area, int index)
127 {
128     double center_x = area.x() + (double) area.width() / 2.0;
129     double center_y = area.y() + (double) area.height() / 2.0;
130
131     bool inUse = false;
132     int satelliteElevation = -1;
133     int satelliteAzimuth = 0;
134     for (int iter = 0; iter < m_satellitesInView.count(); iter++) {
135         if (m_satellitesInView.at(iter).prnNumber() == index) {
136             if (m_satellitesInView.at(iter).hasAttribute(QGeoSatelliteInfo::Elevation))
137                 satelliteElevation = m_satellitesInView.at(iter).attribute(QGeoSatelliteInfo::Elevation);
138
139             if (m_satellitesInView.at(iter).hasAttribute(QGeoSatelliteInfo::Azimuth))
140                 satelliteAzimuth = m_satellitesInView.at(iter).attribute(QGeoSatelliteInfo::Azimuth);
141             break;
142         }
143     }
144     for (int iter = 0; iter < m_satellitesInUse.count(); iter++) {
145         if (m_satellitesInUse.at(iter).prnNumber() == index) {
146             inUse = true;
147             break;
148         }
149     }
150
151     /* Display only visible satellites */
152     if (satelliteElevation >= 0) {
153         int satellite_width = 30;
154         int satellite_height = 30;
155         double temp = (180 - satelliteElevation) * (double) area.width() / 360.0;
156         double satellite_x = temp * qCos((satelliteAzimuth - 90) * M_PI / 180.0) +
157                              center_x - (double) (satellite_width / 2.0);
158         double satellite_y = temp * qSin((satelliteAzimuth - 90) * M_PI / 180.0) +
159                              center_y - (double) (satellite_height / 2.0);
160         QRectF satelliteArea(satellite_x, satellite_y, satellite_width, satellite_height);
161
162         /* Draw a small circle with PRN number inside */
163         painter.setPen(m_satelliteBorderColor);
164         if (inUse)
165             painter.setBrush(m_satelliteInUseColor);
166         else
167             painter.setBrush(m_satellitehInViewColor);
168         painter.drawChord(satelliteArea, 0, 5760);
169
170         int panel_width = 10;
171         painter.setBrush(m_satelliteSolarPanelColor);
172         QRectF solarPanelLArea(satellite_x - panel_width, satellite_y,
173                                panel_width, satellite_height);
174         painter.drawRect(solarPanelLArea);
175
176         QRectF solarPanelRArea(satellite_x + satellite_width, satellite_y,
177                                panel_width, satellite_height);
178         painter.drawRect(solarPanelRArea);
179
180         /* Satellite's PRN must be painter over the satellite circle */
181         QFont font = QApplication::font();
182         font.setPixelSize(12);
183         painter.setFont(font);
184         painter.setPen(m_satelliteBorderColor);
185         painter.drawText(satelliteArea, Qt::AlignCenter, QString::number(index));
186     }
187 }
188
189 SatelliteView::SatelliteView(QWidget *parent) : QWidget(parent)
190 {
191     m_graphGridColor = QColor(74, 69, 66);
192     m_satelliteBorderColor = QColor(0, 0, 0);
193     m_satelliteSolarPanelColor = QColor(0, 75, 255);
194     m_satellitehInViewColor = QColor(153, 153, 153);
195     m_satelliteInUseColor = QColor(51, 191, 51);
196 }