Version bump (0.3-3)
[gpsdata] / src / satellitesignalstrength.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 "satellitesignalstrength.h"
23
24 void SatelliteSignalStrength::updateWidget(const QList<QGeoSatelliteInfo> &satellites, bool inUseList)
25 {
26     if (inUseList)
27         m_satellitesInUse = satellites;
28     else
29         m_satellitesInView = satellites;
30
31     update();
32 }
33
34 void SatelliteSignalStrength::showTitle(bool show)
35 {
36     m_showTitle = show;
37 }
38
39 void SatelliteSignalStrength::paintEvent(QPaintEvent * /* event */)
40 {
41     QPainter painter(this);
42     painter.setRenderHint(QPainter::Antialiasing);
43
44     int title_height = 0;
45     if (m_showTitle) {
46         int title_x = rect().x() + m_margin;
47         int title_y = rect().y() + m_margin;
48         int title_width = rect().width() - (2 * m_margin);
49         title_height = painter.fontMetrics().height();
50         QRectF titleArea(title_x, title_y, title_width, title_height);
51
52         /* Draw title */
53         paintTitle(painter, titleArea);
54      }
55
56     /* Signal strength graph */
57     int graph_x = rect().x();
58     int graph_y = rect().y() + title_height + m_spacing;
59     int graph_width = rect().width();
60     int graph_height = rect().height() - title_height - m_spacing;
61     QRectF graphArea(graph_x, graph_y, graph_width, graph_height);
62
63     /* Signal strength bar graphs */
64     for (int index = 1; index <= m_numOfSatellites; ++index) {
65         paintBarGraph(painter, graphArea, index);
66     }
67 }
68
69 void SatelliteSignalStrength::paintTitle(QPainter &painter, const QRectF &area)
70 {
71     painter.setPen(QApplication::palette().color(QPalette::Text));
72     painter.drawText(area, Qt::AlignLeft, tr("Signal strength:"));
73 }
74
75 void SatelliteSignalStrength::paintBarGraph(QPainter &painter, const QRectF &area, int index)
76 {
77     /* Bar graph backround */
78     double barGraphBackground_x;
79     double barGraphBackground_y;
80     double barGraphBackground_width = (double) (area.width() - (2.0 * m_margin) -
81                                       (m_spacing * (m_numOfSatellitesInRow - 1))) / (double) m_numOfSatellitesInRow;
82     double barGraphBackground_height = (double) (area.height() - (2.0 * m_margin) -
83                                        (2.0 * m_spacing)) / (double) m_numOfRows;
84
85     if (index <= m_numOfSatellitesInRow) {
86         barGraphBackground_x = area.x() + m_margin +
87                                ((index - 1) * (barGraphBackground_width + m_spacing));
88         barGraphBackground_y = area.y() + m_margin;
89     } else {
90         barGraphBackground_x = area.x() + m_margin +
91                                (((index - 1) - m_numOfSatellitesInRow) * (barGraphBackground_width + m_spacing));
92         barGraphBackground_y = area.y() + m_margin + (2.0 * m_spacing) + barGraphBackground_height;
93     }
94     QRectF barGraphBackgroundArea(barGraphBackground_x, barGraphBackground_y,
95                                   barGraphBackground_width, barGraphBackground_height);
96
97     painter.setPen(m_barGraphBorderColor);
98     painter.setBrush(m_barGraphBackgroundColor);
99     painter.drawRect(barGraphBackgroundArea);
100
101     bool inUse = false;
102     int signalStrength = 0;
103
104     for (int iter = 0; iter < m_satellitesInView.count(); iter++) {
105         if (m_satellitesInView.at(iter).prnNumber() == index) {
106             signalStrength = m_satellitesInView.at(iter).signalStrength();
107             break;
108         }
109     }
110     for (int iter = 0; iter < m_satellitesInUse.count(); iter++) {
111         if (m_satellitesInUse.at(iter).prnNumber() == index) {
112             signalStrength = m_satellitesInUse.at(iter).signalStrength();
113             inUse = true;
114             break;
115         }
116     }
117
118     if (signalStrength > 0) {
119         if (inUse) {
120             painter.setPen(m_barGraphInUseColor);
121             painter.setBrush(m_barGraphInUseColor);
122         } else {
123             painter.setPen(m_barGraphInViewColor);
124             painter.setBrush(m_barGraphInViewColor);
125         }
126
127         double level_width = barGraphBackground_width - 2.0;
128         double level_height = (double) signalStrength * (barGraphBackground_height - 2.0) / 100.0;
129         double level_x = barGraphBackground_x + 1.0;
130         double level_y = barGraphBackground_y + barGraphBackground_height - level_height - 1.0;
131
132         QRectF barGraphLevelArea(level_x, level_y, level_width, level_height);
133         painter.drawRect(barGraphLevelArea);
134     }
135
136     /* Satellite's PRN must be painter over the level bar */
137     QFont font = QApplication::font();
138     font.setPixelSize(12);
139     painter.setFont(font);
140     painter.setPen(QApplication::palette().color(QPalette::Text));
141     painter.drawText(barGraphBackgroundArea, Qt::AlignCenter, QString::number(index));
142 }
143
144 SatelliteSignalStrength::SatelliteSignalStrength(QWidget *parent) : QWidget(parent)
145 {
146     m_barGraphBorderColor = QColor(0, 0, 0);
147     m_barGraphBackgroundColor = QColor(74, 69, 66);
148     m_barGraphInViewColor = QColor(153, 153, 153);
149     m_barGraphInUseColor = QColor(51, 191, 51);
150 }