Incorporated changes from bus project.
[ptas] / src / gpscontroller_p.cpp
1 #include "gpscontroller_p.h"
2
3 #include "location.h"
4 #include "locations.h"
5
6 #include <QObject>
7 #include <QGeoPositionInfo>
8 #include <QGeoPositionInfoSource>
9 #include <QDebug>
10
11 QTM_USE_NAMESPACE
12
13 GpsControllerPrivate::GpsControllerPrivate() :
14     m_gps(0),
15     m_liveLocation(new Location("livegps")),
16     m_fakeLocationLabel(),
17     m_useFakeLocation(false)
18 {
19 }
20
21 GpsControllerPrivate::~GpsControllerPrivate()
22 {
23     delete m_gps;
24     m_gps = 0;
25     delete m_liveLocation;
26     m_liveLocation = 0;
27 }
28
29 void GpsControllerPrivate::init()
30 {
31     m_gps = QGeoPositionInfoSource::createDefaultSource(this);
32     connect(
33                 m_gps, SIGNAL(positionUpdated(QGeoPositionInfo)),
34                 this, SLOT(updateLocation(QGeoPositionInfo))
35                 );
36 }
37
38 void GpsControllerPrivate::startGps()
39 {
40     m_gps->startUpdates();
41 }
42
43 void GpsControllerPrivate::stopGps()
44 {
45     m_gps->stopUpdates();
46 }
47
48 QGeoPositionInfoSource *GpsControllerPrivate::gps()
49 {
50     return m_gps;
51 }
52
53 void GpsControllerPrivate::setGps(QGeoPositionInfoSource *gps)
54 {
55     m_gps = gps;
56 }
57
58 Location *GpsControllerPrivate::liveLocation()
59 {
60     m_mostRecentlyReportedLocation = m_liveLocation;
61     return m_liveLocation;
62 }
63
64 Location *GpsControllerPrivate::fakeLocation()
65 {
66     Locations locations;
67     Location  *location = locations.location(fakeLocationLabel());
68     m_mostRecentlyReportedLocation = location;
69     return location;
70 }
71
72 QString GpsControllerPrivate::fakeLocationLabel()
73 {
74     return m_fakeLocationLabel;
75 }
76
77 void GpsControllerPrivate::setFakeLocationLabel(const QString &label)
78 {
79     m_fakeLocationLabel = label;
80 }
81
82 bool GpsControllerPrivate::useFakeLocation()
83 {
84     return m_useFakeLocation;
85 }
86
87 void GpsControllerPrivate::setUseFakeLocation(bool useFake)
88 {
89     m_useFakeLocation = useFake;
90 }
91
92 void GpsControllerPrivate::updateLocation(QGeoPositionInfo positionInfo)
93 {
94     qDebug() << Q_FUNC_INFO;
95     bool wasInvalid = !m_liveLocation->isValid();
96     qDebug() << "wasInvalid/" << wasInvalid;
97
98     m_liveLocation->setLocation(positionInfo);
99     if (wasInvalid) {
100         emit locationChanged(m_liveLocation);
101     }
102 }
103
104 Location *GpsControllerPrivate::mostRecentlyReportedLocation()
105 {
106     return m_mostRecentlyReportedLocation;
107 }