Root/
1 | /* |
2 | * Copyright 2010-2011 Niels Kummerfeldt <niels.kummerfeldt@tu-harburg.de> |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by |
6 | * the Free Software Foundation; either version 2 of the License, or |
7 | * (at your option) any later version. |
8 | * |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
12 | * GNU General Public License for more details. |
13 | * |
14 | * You should have received a copy of the GNU General Public License |
15 | * along with this program; if not, write to the Free Software |
16 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, |
17 | * Boston, MA 02110-1301 USA |
18 | */ |
19 | |
20 | #include "gpslayer.h" |
21 | |
22 | #include "gpsclient.h" |
23 | #include "mapwidget.h" |
24 | |
25 | #include <QtCore/QPoint> |
26 | |
27 | GpsLayer::GpsLayer(MapWidget *map) : |
28 | AbstractLayer(map), |
29 | m_gps(new GpsClient(this)), |
30 | m_pos(QPointF(0, 0)), |
31 | m_alt(0), |
32 | m_track(0), |
33 | m_speed(0), |
34 | m_fix(false) |
35 | { |
36 | setVisible(false); |
37 | connect(m_gps, SIGNAL(position(QPointF)), this, SLOT(position(QPointF))); |
38 | connect(m_gps, SIGNAL(altitude(qreal)), this, SLOT(altitude(qreal))); |
39 | connect(m_gps, SIGNAL(direction(qreal)), this, SLOT(direction(qreal))); |
40 | connect(m_gps, SIGNAL(speed(qreal)), this, SLOT(speed(qreal))); |
41 | connect(m_gps, SIGNAL(connected()), this, SLOT(connected())); |
42 | connect(m_gps, SIGNAL(disconnected()), this, SLOT(disconnected())); |
43 | connect(m_gps, SIGNAL(fixed(bool)), this, SLOT(fixed(bool))); |
44 | m_gps->connectGps(); |
45 | } |
46 | |
47 | void GpsLayer::keyPressed(QKeyEvent *event) |
48 | { |
49 | if (event->modifiers() == Qt::NoModifier && |
50 | event->key() == Qt::Key_G) { |
51 | if (isVisible()) { |
52 | map()->centerOnGeoPos(m_pos.x(), m_pos.y()); |
53 | } |
54 | } |
55 | } |
56 | |
57 | void GpsLayer::paint(QPainter *painter) |
58 | { |
59 | QPoint pos = map()->geo2screen(m_pos.x(), m_pos.y()); |
60 | painter->setRenderHint(QPainter::Antialiasing, true); |
61 | painter->setPen(QPen(QColor(0, 0, 255, 110), 4)); |
62 | painter->drawEllipse(pos, 8, 8); |
63 | |
64 | if (m_fix) { |
65 | painter->setPen(QPen(QColor(0, 0, 255, 210), 2, Qt::SolidLine, Qt::RoundCap, Qt::MiterJoin)); |
66 | painter->translate(pos); |
67 | painter->rotate(m_track); |
68 | painter->drawLine(QPoint(4, 3), QPoint(0, -7)); |
69 | painter->drawLine(QPoint(-4, 3), QPoint(0, -7)); |
70 | painter->drawLine(QPoint(4, 3), QPoint(0, 0)); |
71 | painter->drawLine(QPoint(-4, 3), QPoint(0, 0)); |
72 | } else { |
73 | painter->drawText(pos.x()-8, pos.y()-8, 16, 16, Qt::AlignCenter, "?"); |
74 | } |
75 | } |
76 | |
77 | void GpsLayer::position(const QPointF &pos) |
78 | { |
79 | m_pos = pos; |
80 | if (isVisible()) { |
81 | map()->update(); |
82 | } |
83 | } |
84 | |
85 | void GpsLayer::altitude(qreal alt) |
86 | { |
87 | m_alt = alt; |
88 | if (isVisible()) { |
89 | map()->update(); |
90 | } |
91 | } |
92 | |
93 | void GpsLayer::direction(qreal track) |
94 | { |
95 | m_track = track; |
96 | if (isVisible()) { |
97 | map()->update(); |
98 | } |
99 | } |
100 | |
101 | void GpsLayer::speed(qreal speed) |
102 | { |
103 | m_speed = speed; |
104 | if (isVisible()) { |
105 | map()->update(); |
106 | } |
107 | } |
108 | |
109 | void GpsLayer::connected() |
110 | { |
111 | setVisible(true); |
112 | map()->update(); |
113 | } |
114 | |
115 | void GpsLayer::disconnected() |
116 | { |
117 | setVisible(false); |
118 | map()->update(); |
119 | } |
120 | |
121 | void GpsLayer::fixed(bool fix) |
122 | { |
123 | m_fix = fix; |
124 | if (isVisible()) { |
125 | map()->update(); |
126 | } |
127 | } |
128 | |
129 |
Branches:
master