-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotfinder.cpp
325 lines (261 loc) · 7.18 KB
/
spotfinder.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
#include "spotfinder.h"
#include <qplatformdefs.h> // MEEGO_EDITION_HARMATTAN
#include <QDebug>
SpotFinder::SpotFinder(QObject *parent) :
QObject(parent)
{
m_source = NULL;
m_azimuth = 0;
m_distance = 0;
m_positionFound = false;
m_spotFound = false;
m_locationEnabled = false;
m_targetCoordinate = NULL;
m_currentCoordinate = NULL;
m_selectedSpot = NULL;
m_model = NULL;
m_temperature = new Temperature();
connect(m_temperature, SIGNAL(ready()), this, SLOT(temperatureDataUpdated()));
emit initializationComplete();
}
SpotFinder::~SpotFinder()
{
if (m_source) delete m_source;
}
void SpotFinder::setLocationEnabled(bool enabled)
{
// enable location updates based on m_locationEnabled
m_locationEnabled = enabled;
// just stop updating location when disabled
if(!enabled) {
if(m_source) m_source->stopUpdates();
emit locationEnabledChanged();
return;
}
// create a new position source if not existing yet
if(!m_source)
{
m_source = QGeoPositionInfoSource::createDefaultSource(this);
if (m_source) {
connect(m_source, SIGNAL(positionUpdated(QGeoPositionInfo)),
this, SLOT(positionUpdated(QGeoPositionInfo)));
m_source->setUpdateInterval(2000);
}
}
m_source->startUpdates();
emit locationEnabledChanged();
return;
}
bool SpotFinder::locationEnabled() const
{
return m_locationEnabled;
}
void SpotFinder::positionUpdated(const QGeoPositionInfo &info)
{
//qDebug() << "position update";
// save current location
if(m_currentCoordinate) delete m_currentCoordinate;
m_currentCoordinate = new QGeoCoordinate(info.coordinate());
emit currentLatitudeChanged();
emit currentLongitudeChanged();
// by default, select nearest Spot if none is selected (at startup)
if (!m_spotFound && m_model)
{
m_model->sortByLocation(m_currentCoordinate);
selectSpot(0);
m_spotFound = true;
emit spotFoundChanged();
}
// calculate azimuth and distance to the selected Spot
if (m_targetCoordinate)
{
//m_azimuth = m_targetCoordinate->azimuthTo(info.coordinate());
m_azimuth = m_currentCoordinate->azimuthTo(*m_targetCoordinate);
m_distance = m_currentCoordinate->distanceTo(*m_targetCoordinate);
emit azimuthChanged();
emit distanceChanged();
}
if (!m_positionFound)
{
//qDebug() << "position found";
m_positionFound = true;
emit positionFoundChanged();
}
}
void SpotFinder::selectSpot(int index)
{
if (m_model == NULL)
{
qDebug() << "SpotFinder::selectSpot no model!";
m_spotFound = false;
return;
}
if (index < 0)
{
qDebug() << "SpotFinder::selectSpot index < 0";
m_spotFound = false;
}
m_selectedSpot = NULL;
m_selectedSpot = m_model->spotAt(index);
if (m_selectedSpot == NULL)
{
qDebug() << "SpotFinder::selectSpot NULL";
m_spotFound = false;
return;
}
if (m_targetCoordinate) delete m_targetCoordinate;
m_targetCoordinate = new QGeoCoordinate(m_selectedSpot->latitude(), m_selectedSpot->longitude());
m_temperature->setCoordinate(m_selectedSpot->latitude(), m_selectedSpot->longitude());
m_spotFound = true;
emit nameChanged();
emit addressChanged();
emit additionalInfoChanged();
emit spotFoundChanged();
emit targetChanged();
emit latitudeChanged();
emit longitudeChanged();
if (m_temperature)
{
emit waterTemperatureChanged();
emit airTemperatureChanged();
}
return;
}
void SpotFinder::sortByLocation()
{
if(!m_model || !m_currentCoordinate) return;
m_model->sortByLocation(m_currentCoordinate);
emit modelChanged();
return;
}
void SpotFinder::sortByName()
{
if(!m_model) return;
m_model->sortByName();
emit modelChanged();
}
qreal SpotFinder::azimuth() const
{
return m_azimuth;
}
qreal SpotFinder::distance() const
{
if (!m_selectedSpot) return 0;
//int for easier UI creation...
int dist = m_selectedSpot->distance();
return dist;
}
QGeoCoordinate* SpotFinder::target() const
{
if (!m_targetCoordinate) return new QGeoCoordinate();
else return m_targetCoordinate;
}
qreal SpotFinder::latitude() const
{
if (!m_targetCoordinate) return 0;
else return m_targetCoordinate->latitude();
}
qreal SpotFinder::longitude() const
{
if (!m_targetCoordinate) return 0;
else return m_targetCoordinate->longitude();
}
qreal SpotFinder::currentLatitude() const
{
if (!m_currentCoordinate) return 0;
else return m_currentCoordinate->latitude();
}
qreal SpotFinder::currentLongitude() const
{
if (!m_currentCoordinate) return 0;
else return m_currentCoordinate->longitude();
}
QString SpotFinder::name() const
{
if (!m_selectedSpot) return "";
return m_selectedSpot->name();
}
QString SpotFinder::address() const
{
//qDebug() << "address";
if (!m_selectedSpot) return "";
if(m_selectedSpot->address() == "www.liikuntapaikat.fi"
|| m_selectedSpot->address() == "Kansanterveyslaitos, SYKE")
{
return "";
}
else return m_selectedSpot->address();
}
QString SpotFinder::additionalInfo() const
{
if (!m_selectedSpot) return "";
return m_selectedSpot->additionalInfo();
}
bool SpotFinder::positionFound() const
{
//qDebug() << "posfound";
return m_positionFound;
}
bool SpotFinder::spotFound() const
{
//qDebug() << "spotfound";
return m_spotFound;
}
SpotModel* SpotFinder::model()
{
//qDebug() << "model";
return m_model;
}
void SpotFinder::setModel(QObject *model)
{
m_model = qobject_cast<SpotModel *>(model);
emit modelChanged();
return;
}
void SpotFinder::launchMaps() const
{
if (!m_selectedSpot) return;
#if defined(MEEGO_EDITION_HARMATTAN)
QDesktopServices::openUrl(QUrl("geo:" + QString::number(m_selectedSpot->latitude()) + "," + QString::number(m_selectedSpot->longitude()) + "?action=showOnMap&zoomLevel=13"));
#else
QDesktopServices::openUrl(QUrl("http://m.ovi.me/?c=" + QString::number(m_selectedSpot->latitude()) + "," + QString::number(m_selectedSpot->longitude())));
#endif
}
QString SpotFinder::waterTemperature() const
{
if(m_temperature) return m_temperature->waterTemperature();
else return "N/A";
}
QString SpotFinder::airTemperature() const
{
if(m_temperature) return QString::number(m_temperature->airTemperature());
else return 0;
}
QString SpotFinder::measurementLocation() const
{
if(m_temperature) return m_temperature->measurementLocation();
else return "";
}
bool SpotFinder::temperatureDataAvailable()
{
return m_temperature->isValid();
}
void SpotFinder::temperatureDataUpdated()
{
emit temperatureDataAvailableChanged();
emit waterTemperatureChanged();
}
double SpotFinder::latitudeAtIndex(int index)
{
if (!m_model) return 0;
Spot *temp = m_model->spotAt(index);
if (temp) return temp->latitude();
else return 0;
}
double SpotFinder::longitudeAtIndex(int index)
{
if (!m_model) return 0;
Spot *temp = m_model->spotAt(index);
if (temp) return temp->longitude();
else return 0;
}