~ubuntu-branches/ubuntu/saucy/merkaartor/saucy

« back to all changes in this revision

Viewing changes to GPS/qgpssatellitetracker.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Bernd Zeimetz
  • Date: 2009-09-13 00:52:12 UTC
  • mto: (1.2.7 upstream) (0.1.3 upstream) (3.1.7 sid)
  • mto: This revision was merged to the branch mainline in revision 10.
  • Revision ID: james.westby@ubuntu.com-20090913005212-pjecal8zxm07x0fj
ImportĀ upstreamĀ versionĀ 0.14+svnfixes~20090912

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/***************************************************************************
2
 
 *   Copyright (C) 2005 by Robin Gingras                                   *
3
 
 *   neozenkai@cox.net                                                     *
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                         *
17
 
 *   Free Software Foundation, Inc.,                                       *
18
 
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19
 
 ***************************************************************************/
20
 
 
21
 
#include <stdio.h>
22
 
#include <stdlib.h>
23
 
#include <math.h>
24
 
#ifndef M_PI
25
 
#define M_PI        3.14159265358979323846
26
 
#endif
27
 
 
28
 
#include <QPainter>
29
 
#include <QPen>
30
 
#include <QBrush>
31
 
 
32
 
#include "qgpssatellitetracker.h"
33
 
 
34
 
QGPSSatelliteTracker::QGPSSatelliteTracker(QWidget *parent) 
35
 
: QWidget(parent), Heading(0)
36
 
{
37
 
}
38
 
 
39
 
void QGPSSatelliteTracker::setSatellites(const std::vector<Satellite>& aList)
40
 
{
41
 
        List = aList;
42
 
        update();
43
 
}
44
 
 
45
 
void QGPSSatelliteTracker::setHeading(int x)
46
 
{
47
 
        Heading = x;
48
 
        update();
49
 
}
50
 
 
51
 
/**
52
 
 * private void QGPSSatelliteTracker::paintEvent()
53
 
 *
54
 
 * Does the actual drawing of the widget. Reads the satArray array to
55
 
 * get satellite positions and PRNs. Reads the satActive array to get
56
 
 * which satellites are being used.
57
 
 *
58
 
 * @param QPaintEvent * Ignored by this function
59
 
 */
60
 
 
61
 
void QGPSSatelliteTracker::paintEvent(QPaintEvent *)
62
 
{
63
 
    QPainter painter(this);
64
 
    painter.setRenderHint(QPainter::Antialiasing);
65
 
    painter.translate(width()/2,height()/2);
66
 
 
67
 
    int rad = width();
68
 
    if (height() < rad)
69
 
            rad = height();
70
 
    rad /= 2;
71
 
    rad -= 2;
72
 
 
73
 
    painter.setPen(QPen(palette().mid(), 1, Qt::SolidLine));
74
 
    painter.setBrush(QBrush(Qt::black, Qt::NoBrush));
75
 
 
76
 
    // first paint the two reference circles, one at 0 degrees, and
77
 
    // the other at 45 degrees
78
 
    painter.drawEllipse(-rad,-rad, rad*2,rad*2);
79
 
    painter.drawEllipse(-rad/2,-rad/2,rad,rad);
80
 
 
81
 
    // now the reference lines, one vertical and the other horizontal
82
 
    painter.drawLine(-rad,0,rad,0);
83
 
    painter.drawLine(0,-rad,0,rad);
84
 
 
85
 
   // plot heading
86
 
   if (List.size())
87
 
    {
88
 
        painter.setPen(QPen(QColor(240,32,32),3,Qt::SolidLine,Qt::RoundCap));
89
 
        float Alfa = Heading-90;
90
 
        Alfa = Alfa*3.141592/180;
91
 
        float fx = cos(Alfa)*rad*3/4;
92
 
        float fy = sin(Alfa)*rad*3/4;
93
 
    painter.drawLine(0,0,int(fx),int(fy));
94
 
    painter.drawLine(int(fx),int(fy),
95
 
        int(fx+cos(Alfa+3.1415*5/6)*8),int(fy+sin(Alfa+3.1415*5/6)*8));
96
 
    painter.drawLine(int(fx),int(fy),
97
 
        int(fx+cos(Alfa-3.1415*5/6)*8),int(fy+sin(Alfa-3.1415*5/6)*8));
98
 
    }
99
 
 
100
 
 
101
 
    // now plot the satellites
102
 
    painter.setPen(QPen(palette().foreground(), 2, Qt::SolidLine));
103
 
    painter.setBrush(QBrush(palette().foreground()));
104
 
 
105
 
    int x,y;
106
 
    for (unsigned int i=0; i<List.size(); ++i)
107
 
    {
108
 
        if(List[i].SignalStrength > 0)
109
 
        {
110
 
            getCoordsFromPos(rad,List[i].Elevation,List[i].Azimuth, x, y);
111
 
 
112
 
            painter.drawEllipse(x - 2, y - 2, 4, 4);
113
 
            painter.drawText(x + 5, y - 1, QString("%1").arg(List[i].Id));
114
 
        }
115
 
    }
116
 
}
117
 
 
118
 
/**
119
 
 * private void QGPSSatelliteTracker::getCoordsFromPos()
120
 
 *
121
 
 * Inputs and elevation and azimuth, and translates them to coordinates
122
 
 * that QPainter can use. This one took some thought, but I think it's
123
 
 * correct. Satellites jump around, maybe a problem?
124
 
 *
125
 
 * @param int elevation Elevation of the satellite in degrees, 0 - 90
126
 
 * @param int azimuth   Azimuth of the satellite in degrees, 0 - 360
127
 
 * @param int &x        Pointer to x-coordinate
128
 
 * @param int &y        Pointer to y-coordinate
129
 
 */
130
 
 
131
 
void QGPSSatelliteTracker::getCoordsFromPos(int rad, int elevation, int azimuth, int &x, int &y)
132
 
{
133
 
    int theta = azimuth-90;
134
 
 
135
 
    // since the "origin" of the sky is 90 (directly overhead), reverse
136
 
    // elevation to make the origin 0 (an elevation of 90 becomes 0, etc)
137
 
    elevation = 90 - elevation;
138
 
 
139
 
    // you should know this (slept too much in trig)
140
 
        x = int(cos(theta*M_PI/180) * elevation * rad / 90);
141
 
        y = int(sin(theta*M_PI/180) * elevation * rad / 90);
142
 
}