|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include <windows.h> |
|
43 #include <gpsapi.h> // including requires <windows.h> |
|
44 |
|
45 #include "qgeopositioninfosource_wince_p.h" |
|
46 #include "qgeoinfothread_wince_p.h" |
|
47 |
|
48 QTM_BEGIN_NAMESPACE |
|
49 |
|
50 // ========== QGeoPositionInfoValidator ========== |
|
51 |
|
52 QGeoPositionInfoValidator::QGeoPositionInfoValidator() : QGeoInfoValidator() {} |
|
53 |
|
54 QGeoPositionInfoValidator::~QGeoPositionInfoValidator() {} |
|
55 |
|
56 // Returns true if data contains at least the minimal amount of data we need to produce the |
|
57 // QGeoPositionInfoSource positionUpdated signal, otherwise returns false. |
|
58 bool QGeoPositionInfoValidator::valid(const GPS_POSITION &data) const |
|
59 { |
|
60 if (((data.dwValidFields & GPS_VALID_LATITUDE) == 0) |
|
61 || ((data.dwValidFields & GPS_VALID_LONGITUDE) == 0) |
|
62 || ((data.dwValidFields & GPS_VALID_UTC_TIME) == 0)) { |
|
63 return false; |
|
64 } |
|
65 return true; |
|
66 } |
|
67 |
|
68 // ========== QGeoPositionInfoSourceWinCE ========== |
|
69 |
|
70 QGeoPositionInfoSourceWinCE::QGeoPositionInfoSourceWinCE(QObject *parent) |
|
71 : QGeoPositionInfoSource(parent) |
|
72 { |
|
73 QGeoInfoValidator *validator = new QGeoPositionInfoValidator(); |
|
74 |
|
75 // The QGeoInfoThreadWinCE instance takes ownership of the validator. |
|
76 infoThread = new QGeoInfoThreadWinCE(validator, true, this); |
|
77 infoThread->start(); |
|
78 // QGeoInfoThreadWinCE takes care of registering GPS_POSITION as a metatype. |
|
79 connect(infoThread, SIGNAL(dataUpdated(GPS_POSITION)), this, SLOT(dataUpdated(GPS_POSITION))); |
|
80 connect(infoThread, SIGNAL(updateTimeout()), this, SIGNAL(updateTimeout())); |
|
81 } |
|
82 |
|
83 QGeoPositionInfoSourceWinCE::~QGeoPositionInfoSourceWinCE() |
|
84 { |
|
85 delete infoThread; |
|
86 } |
|
87 |
|
88 QGeoPositionInfoSource::PositioningMethods QGeoPositionInfoSourceWinCE::supportedPositioningMethods() const |
|
89 { |
|
90 return QGeoPositionInfoSource::SatellitePositioningMethods; |
|
91 } |
|
92 |
|
93 QGeoPositionInfo QGeoPositionInfoSourceWinCE::lastKnownPosition(bool) const |
|
94 { |
|
95 return lastPosition; |
|
96 } |
|
97 |
|
98 void QGeoPositionInfoSourceWinCE::setUpdateInterval(int msec) |
|
99 { |
|
100 // If msec is 0 we send updates as data becomes available, otherwise we force msec to be equal |
|
101 // to or larger than the minimum update interval. |
|
102 if (msec != 0 && msec < MinimumUpdateInterval) |
|
103 msec = MinimumUpdateInterval; |
|
104 |
|
105 infoThread->setUpdateInterval(msec); |
|
106 QGeoPositionInfoSource::setUpdateInterval(msec); |
|
107 } |
|
108 |
|
109 int QGeoPositionInfoSourceWinCE::minimumUpdateInterval() const |
|
110 { |
|
111 return MinimumUpdateInterval; |
|
112 } |
|
113 |
|
114 void QGeoPositionInfoSourceWinCE::startUpdates() |
|
115 { |
|
116 infoThread->startUpdates(); |
|
117 } |
|
118 |
|
119 void QGeoPositionInfoSourceWinCE::stopUpdates() |
|
120 { |
|
121 infoThread->stopUpdates(); |
|
122 } |
|
123 |
|
124 void QGeoPositionInfoSourceWinCE::requestUpdate(int timeout) |
|
125 { |
|
126 // A timeout of 0 means to use the default timeout, which is handled by the QGeoInfoThreadWinCE |
|
127 // instance, otherwise if timeout is less than the minimum update interval we emit a |
|
128 // updateTimeout signal |
|
129 if (timeout < minimumUpdateInterval() && timeout != 0) |
|
130 emit updateTimeout(); |
|
131 else |
|
132 infoThread->requestUpdate(timeout); |
|
133 } |
|
134 |
|
135 /* |
|
136 This is _only_ called when QGeoPositionInfoValidator::valid() returns true for the position. |
|
137 This means that it is implied that: |
|
138 - (data.dwValidFields & GPS_VALID_LATITUDE) != 0 |
|
139 - (data.dwValidFields & GPS_VALID_LONGITUDE) != 0 |
|
140 - (data.dwValidFields & GPS_VALID_UTC_TIME) != 0 |
|
141 |
|
142 This guarantees that the newly created position will be valid. |
|
143 If the code is changed such that this is no longer guaranteed then this method will need to be |
|
144 updated to test for those conditions. |
|
145 */ |
|
146 void QGeoPositionInfoSourceWinCE::dataUpdated(GPS_POSITION data) |
|
147 { |
|
148 QGeoCoordinate coordinate(data.dblLatitude, data.dblLongitude); |
|
149 |
|
150 // The altitude is optional in QGeoCoordinate, so we do not strictly require that the |
|
151 // GPS_POSITION structure has valid altitude data in order to trigger an update. |
|
152 if ((data.dwValidFields & GPS_VALID_ALTITUDE_WRT_SEA_LEVEL) != 0) |
|
153 coordinate.setAltitude(data.flAltitudeWRTSeaLevel); |
|
154 |
|
155 QDate date(data.stUTCTime.wYear, data.stUTCTime.wMonth, data.stUTCTime.wDay); |
|
156 QTime time(data.stUTCTime.wHour, data.stUTCTime.wMinute, data.stUTCTime.wSecond, |
|
157 data.stUTCTime.wMilliseconds); |
|
158 |
|
159 QDateTime dateTime(date, time, Qt::UTC); |
|
160 |
|
161 QGeoPositionInfo pos(coordinate, dateTime); |
|
162 |
|
163 // The following properties are optional, and so are set if the data is present and valid in |
|
164 // the GPS_POSITION structure. |
|
165 if ((data.dwValidFields & GPS_VALID_SPEED) != 0) |
|
166 pos.setAttribute(QGeoPositionInfo::GroundSpeed, data.flSpeed); |
|
167 |
|
168 if ((data.dwValidFields & GPS_VALID_HEADING) != 0) |
|
169 pos.setAttribute(QGeoPositionInfo::Direction, data.flHeading); |
|
170 |
|
171 if ((data.dwValidFields & GPS_VALID_MAGNETIC_VARIATION) != 0) |
|
172 pos.setAttribute(QGeoPositionInfo::MagneticVariation, data.dblMagneticVariation); |
|
173 |
|
174 lastPosition = pos; |
|
175 emit positionUpdated(pos); |
|
176 } |
|
177 |
|
178 #include "moc_qgeopositioninfosource_wince_p.cpp" |
|
179 QTM_END_NAMESPACE |