Added initial harmattan packaging.
[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     double center_x = area.x() + area.width() / 2.0;
110     double center_y = area.y() + area.height() / 2.0;
111
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);
118
119         QRectF gridArea(grid_x, grid_y, grid_width, grid_height);
120         painter.drawArc(gridArea, 0, 5760);
121     }
122
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)));
127     }
128 }
129
130 void SatelliteView::paintSatellite(QPainter &painter, const QRectF &area, int index)
131 {
132     double center_x = area.x() + area.width() / 2.0;
133     double center_y = area.y() + area.height() / 2.0;
134
135     bool inUse = false;
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);
142
143             if (m_satellitesInView.at(iter).hasAttribute(QGeoSatelliteInfo::Azimuth))
144                 satelliteAzimuth = m_satellitesInView.at(iter).attribute(QGeoSatelliteInfo::Azimuth);
145             break;
146         }
147     }
148     for (int iter = 0; iter < m_satellitesInUse.count(); iter++) {
149         if (m_satellitesInUse.at(iter).prnNumber() == index) {
150             inUse = true;
151             break;
152         }
153     }
154
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);
165
166         /* Draw a small circle with PRN number inside */
167         painter.setPen(m_satelliteBorderColor);
168         if (inUse)
169             painter.setBrush(m_satelliteInUseColor);
170         else
171             painter.setBrush(m_satellitehInViewColor);
172         painter.drawChord(satelliteArea, 0, 5760);
173
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);
179
180         QRectF solarPanelRArea(satellite_x + satellite_width, satellite_y,
181                                panel_width, satellite_height);
182         painter.drawRect(solarPanelRArea);
183
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));
190     }
191 }
192
193 SatelliteView::SatelliteView(QWidget *parent) : QWidget(parent)
194 {
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);
200 }