/
mainwindow.cpp
532 lines (410 loc) · 17.5 KB
/
mainwindow.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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QUrl>
#include <QtGui/QMainWindow>
#include <QtNetwork/QNetworkAccessManager>
#include <QtNetwork/QNetworkReply>
#include <QString>
#include <QtCore/QPointer>
#include <QStringBuilder>
#include <QXmlStreamReader>
#include <QXmlStreamAttributes>
#include <QtNetwork/QNetworkRequest>
#include <QtLocation/QGeoCoordinate>
#include <QtCore/QCoreApplication>
MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent), ui(new Ui::MainWindow)
{
//setup the gui and connect signals to slots
requestWeather = false;
ui->setupUi(this);
manager = new QNetworkAccessManager(this);
geoCodingSuccessfull = false;
connect(this, SIGNAL(programStarted()),this,SLOT(sendRequest()));
connect(manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
connect(ui->btn_go,SIGNAL(clicked()),this, SLOT(sendWeatherRequest()));
ui->statusBar->showMessage(QString("Looking for Geolocation.."));
startGPS();
}
MainWindow::~MainWindow()
{
delete ui;
}
//void MainWindow::programStarted1()
//{
// emit sendRequest();
//}
void MainWindow::sendRequest()
{
requestWeather = false;
geoCodingSuccessfull = false;
QString latlng = this->latlng;
if (!latlng.isNull()) {
QUrl url("http://maps.google.com/maps/api/geocode/xml");
url.addQueryItem("latlng", latlng);
url.addQueryItem("sensor", "true"); // Required by google
qDebug(url.toString().toAscii());
manager->get(QNetworkRequest(url));
ui->statusBar->showMessage(QString("Location request sended, waiting for response..."));
} else {
ui->statusBar->showMessage(QString("No Geolocation found."));
}
}
QString MainWindow::getPictureString(QString weatherCondition){
QString pictureString = "";
if(weatherCondition.compare("Cloudy")==0){
pictureString = ":/new/prefix1/img/cloudy.png";
}else if(weatherCondition.compare("Dust")==0){
pictureString = ":/new/prefix1/img/dust.png";
}else if(weatherCondition.compare("Chance of Storm")==0){
pictureString = ":/new/prefix1/img/chance_of_storm.png";
}else if(weatherCondition.compare("Chance of Tstorm")==0){
pictureString = ":/new/prefix1/img/chance_of_tstorm.png";
}else if(weatherCondition.compare("Thunderstorm")==0){
pictureString = ":/new/prefix1/img/thunderstorm.png";
}else if(weatherCondition.compare("Chance of Snow")==0){
pictureString = ":/new/prefix1/img/chance_of_snow.png";
}else if(weatherCondition.compare("Chance of Rain")==0){
pictureString = ":/new/prefix1/img/chance_of_rain.png";
}else if(weatherCondition.compare("Fog")==0){
pictureString = ":/new/prefix1/img/fog.png";
}else if(weatherCondition.compare("Flurries")==0){
pictureString = ":/new/prefix1/img/flurries.png";
}else if(weatherCondition.compare("Overcast")==0){
pictureString = ":/new/prefix1/img/cloudy.png";
}else if(weatherCondition.compare("Clear")==0){
pictureString = ":/new/prefix1/img/sunny.png";
}else if(weatherCondition.compare("Haze")==0){
pictureString = ":/new/prefix1/img/haze.png";
}else if(weatherCondition.compare("Icy")==0){
pictureString = ":/new/prefix1/img/icy.png";
}else if(weatherCondition.compare("Mist")==0){
pictureString = ":/new/prefix1/img/mist.png";
}else if(weatherCondition.compare("Mostly Cloudy")==0){
pictureString = ":/new/prefix1/img/mostly_cloudy.png";
}else if(weatherCondition.compare("Partly Sunny")==0){
pictureString = ":/new/prefix1/img/mostly_sunny.png";
}else if(weatherCondition.compare("Mostly Sunny")==0){
pictureString = ":/new/prefix1/img/mostly_sunny.png";
}else if(weatherCondition.compare("Partly Cloudy")==0){
pictureString = ":/new/prefix1/img/partly_cloudy.png";
}else if(weatherCondition.compare("Rain")==0){
pictureString = ":/new/prefix1/img/rain.png";
}else if(weatherCondition.compare("Light rain")==0){
pictureString = ":/new/prefix1/img/mist.png";
}else if(weatherCondition.compare("Sleet")==0){
pictureString = ":/new/prefix1/img/sleet.png";
}else if(weatherCondition.compare("Snow")==0){
pictureString = ":/new/prefix1/img/snow.png";
}else if(weatherCondition.compare("Smoke")==0){
pictureString = ":/new/prefix1/img/smoke.png";
}else if(weatherCondition.compare("Storm")==0){
pictureString = ":/new/prefix1/img/storm.png";
}else if(weatherCondition.compare("Sunny")==0){
pictureString = ":/new/prefix1/img/sunny.png";
}else if(weatherCondition.compare("Sleet")==0){
pictureString = ":/new/prefix1/img/sleet.png";
}
// }else{
// pictureString = ":/new/prefix1/img/unknown.png";
// }
return pictureString;
}
void MainWindow::sendWeatherRequest()
{
requestWeather = true;
QString weather = ui->le_pos->text();
if (!weather.isNull() && weather.compare("") != 0) {
QUrl url("http://maps.google.com/ig/api");
url.addQueryItem("weather", weather);
url.addQueryItem("hl", "en"); // Required by google
qDebug(url.toString().toAscii());
manager->get(QNetworkRequest(url));
ui->statusBar->showMessage(QString("Weather Request sended, waiting for response..."));
} else {
ui->statusBar->showMessage(QString("Failed to send a weather request."));
}
}
void MainWindow::replyFinished(QNetworkReply* response)
{
bool locationFound = false;
//set variable for request-type
if(!requestWeather){
QXmlStreamReader xml(response);
if (!xml.hasError()) {
QString address = "";
while (!xml.atEnd() && !locationFound) {
xml.readNextStartElement();
if (xml.name() == "formatted_address") {
geoCodingSuccessfull = true;
address = *(new QString(address.append((xml.readElementText()))));
const QString address2 = address;
ui->le_pos->setText(address2);
locationFound = true;
// ui->lb_main_date->setText(*address);
}
}
ui->statusBar->showMessage(QString("Successfully received XML location data."));
} else {
ui->statusBar->showMessage(QString("Failed to parse XML location data."));
}
}else{
QXmlStreamReader xml(response);
QXmlStreamAttributes attributes;
if (!xml.hasError()) {
//initializing variables
QString date = "";
QString condition = "";
QString cond1 = "";
QString cond2 = "";
QString cond3 = "";
QString cond4 = "";
QString temp = "";
QString humidity = "";
QString windCondition = "";
QString high = "";
QString low = "";
QString day = "";
QString degrees = " °F";
//already added °F?
bool added = false;
bool cityNotFound = false;
//setting counters to specify how often a variable was called
//because variabels appear more often in the xml(4 days are displayed)
// - for all others than condition: take the first, third and fourth day (second is the same day as first)
int condCounter = 0;
int dayCounter = 0;
int lowCounter = 0;
int highCounter = 0;
//parsing the xml
while (!xml.atEnd()) {
xml.readNextStartElement();
if (xml.name() == "problem_cause") {
cityNotFound = true;
}
//setting date
if (xml.name() == "forecast_date") {
attributes = xml.attributes();
date = *(new QString(date.append(attributes.value("data"))));
const QString date2 = date;
ui->lb_main_date->setText(date2);
}
//parsing weather condition data
if (xml.name() == "condition") {
attributes = xml.attributes();
condition = *(new QString(condition.append(attributes.value("data"))));
//setting cond variables for the different times condition was found
if(condCounter == 0){
cond1 = condition;
const QString condition2 = condition;
ui->lb_main_condition->setText(condition2);
ui->img_main->setPixmap(QPixmap(getPictureString(condition)));
}else if(condCounter == 4){
cond2 = condition;
// qDebug(getPictureString(condition));
ui->img_weather_fc_1->setPixmap(QPixmap(getPictureString(condition)));
}
else if(condCounter == 6){
cond3 = condition;
// qDebug(getPictureString(condition));
ui->img_weather_fc_2->setPixmap(QPixmap(getPictureString(condition)));
}
else if(condCounter == 8){
cond4 = condition;
// qDebug(getPictureString(condition));
ui->img_weather_fc_3->setPixmap(QPixmap(getPictureString(condition)));
}
condCounter++;
condition = "";
}
//parsing temp of today
if (xml.name() == "temp_f") {
attributes = xml.attributes();
temp = *(new QString(temp.append(attributes.value("data"))));
if(!added){
temp.append(degrees);
added = true;
}
const QString temp2 = temp;
ui->lb_main_temp->setText(temp2);
}
//parsing humidity data
if (xml.name() == "humidity") {
attributes = xml.attributes();
humidity = *(new QString(humidity.append(attributes.value("data"))));
const QString humidity2 = humidity;
ui->lb_main_humidity->setText(humidity2);
}
//parsing wind condition data
if (xml.name() == "wind_condition") {
attributes = xml.attributes();
windCondition = *(new QString(windCondition.append(attributes.value("data"))));
const QString wind2 = windCondition;
ui->lb_main_wind->setText(wind2);
}
//parsing high temp data of the xml
if (xml.name() == "high") {
attributes = xml.attributes();
high = *(new QString(high.append(attributes.value("data"))));
if(highCounter == 2){
const QString high2 = high;
ui->lb_temp_high_1->setText(high2);
}else if(highCounter == 4){
const QString high3 = high;
ui->lb_temp_high_2->setText(high3);;
}else if(highCounter == 6){
const QString high4 = high;
ui->lb_temp_high_3->setText(high4);
}
highCounter++;
high = "";
}
//parsing low temp data of the xml
if (xml.name() == "low") {
attributes = xml.attributes();
low = *(new QString(low.append(attributes.value("data"))));
if(lowCounter == 2){
const QString low2 = low;
ui->lb_temp_low_1->setText(low2);
}else if(lowCounter == 4){
const QString low3 = low;
ui->lb_temp_low_2->setText(low3);
}else if(lowCounter == 6){
const QString low4 = low;
ui->lb_temp_low_3->setText(low4);
}
low = "";
lowCounter++;
}
//parsing out day of week
if (xml.name() == "day_of_week") {
attributes = xml.attributes();
day = *(new QString(day.append(attributes.value("data"))));
if(dayCounter == 2){
const QString day2 = day;
ui->lb_date_fc_1->setText(day2);
}else if(dayCounter == 4){
const QString day3 = day;
ui->lb_date_fc_2->setText(day3);
}else if(dayCounter == 6){
const QString day4 = day;
ui->lb_date_fc_3->setText(day4);
}
day = "";
dayCounter++;
}//end-if
}//end while
//update the status bar
//check if city was found
if(cityNotFound){
ui->statusBar->showMessage(QString("Address was not found. Please try again!"));
}else{
ui->statusBar->showMessage(QString("Successfully received XML weather data."));
}
}else{
ui->statusBar->showMessage(QString("XML error while parsing."));
}
}//end if-request-weather
}
void MainWindow::positionUpdated(QGeoPositionInfo geoPositionInfo)
{
if (geoPositionInfo.isValid())
{
// Stop regular position updates, because a valid position has been
// obtained
locationDataSource->stopUpdates();
// Get the current location as latitude and longitude
QGeoCoordinate geoCoordinate = geoPositionInfo.coordinate();
qreal latitude = geoCoordinate.latitude();
qreal longitude = geoCoordinate.longitude();
QString string1;
QString string2;
// QChar c = 'g';
//build the string for latitude-longitude
string1.setNum(latitude);
this->latlng.append(string1);
string2.setNum(longitude);
this->latlng.append(",");
this->latlng.append(string2);
//send the location request
sendRequest();
}
}
void MainWindow::startGPS()
{
// Obtain the location data source if it is not obtained already
if (!locationDataSource)
{
locationDataSource =
QGeoPositionInfoSource::createDefaultSource(this);
if (locationDataSource)
{
// Whenever the location data source signals that the current
// position is updated, the positionUpdated function is called.
QObject::connect(locationDataSource,
SIGNAL(positionUpdated(QGeoPositionInfo)),
this,
SLOT(positionUpdated(QGeoPositionInfo)));
// Start listening for position updates
locationDataSource->startUpdates();
} else {
// Not able to obtain the location data source
// TODO: Error handling
}
} else {
// Start listening for position updates
locationDataSource->startUpdates();
}
}
//handle orientation
void MainWindow::setOrientation(ScreenOrientation orientation)
{
#if defined(Q_OS_SYMBIAN)
// If the version of Qt on the device is < 4.7.2, that attribute won't work
if (orientation != ScreenOrientationAuto) {
const QStringList v = QString::fromAscii(qVersion()).split(QLatin1Char('.'));
if (v.count() == 3 && (v.at(0).toInt() << 16 | v.at(1).toInt() << 8 | v.at(2).toInt()) < 0x040702) {
qWarning("Screen orientation locking only supported with Qt 4.7.2 and above");
return;
}
}
#endif // Q_OS_SYMBIAN
Qt::WidgetAttribute attribute;
switch (orientation) {
#if QT_VERSION < 0x040702
// Qt < 4.7.2 does not yet have the Qt::WA_*Orientation attributes
case ScreenOrientationLockPortrait:
attribute = static_cast<Qt::WidgetAttribute>(128);
break;
case ScreenOrientationLockLandscape:
attribute = static_cast<Qt::WidgetAttribute>(129);
break;
default:
case ScreenOrientationAuto:
attribute = static_cast<Qt::WidgetAttribute>(130);
break;
#else // QT_VERSION < 0x040702
case ScreenOrientationLockPortrait:
attribute = Qt::WA_LockPortraitOrientation;
break;
case ScreenOrientationLockLandscape:
attribute = Qt::WA_LockLandscapeOrientation;
break;
default:
case ScreenOrientationAuto:
attribute = Qt::WA_AutoOrientation;
break;
#endif // QT_VERSION < 0x040702
};
setAttribute(attribute, true);
}
void MainWindow::showExpanded()
{
#if defined(Q_OS_SYMBIAN) || defined(Q_WS_SIMULATOR)
showFullScreen();
#elif defined(Q_WS_MAEMO_5)
showMaximized();
#else
show();
#endif
}