ed66bee19d2f4e0486f0adc48dba7d9aa9c54d76
[speedfreak] / Client / routesavedialog.cpp
1 /*
2  * Route save dialog class
3  *
4  * @author     Toni Jussila <toni.jussila@fudeco.com>
5  * @copyright  (c) 2010 Speed Freak team
6  * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
7  */
8
9 #include "routesavedialog.h"
10 #include "ui_routesavedialog.h"
11 #include <QDebug>
12 #include <QPainter>
13
14 const QPoint arrowStartEast(100, 100);
15 const QPoint arrowEndEast(140, 100);
16
17 const QPoint arrowStartNorth(120, 120);
18 const QPoint arrowEndNorth(120, 80);
19
20 const QPoint arrowStartNortheast(100, 120);
21 const QPoint arrowEndNortheast(140, 80);
22
23 const QPoint arrowStartNorthwest(140, 120);
24 const QPoint arrowEndNorthwest(100, 80);
25
26 /**
27   * Constructor of this class.
28   * @param QWidget pointer to parent object. By default the value is NULL.
29   */
30 RouteSaveDialog::RouteSaveDialog(QWidget *parent) :
31     QDialog(parent), ui(new Ui::RouteSaveDialog){
32
33     qDebug() << "__RouteSaveDialog";
34     ui->setupUi(this);
35     this->setWindowTitle("Tracking");
36
37     routeDialog = NULL;
38     location = NULL;
39     gpsData = NULL;
40     helpRoutingDialog = NULL;
41     calibrateDialog = NULL;
42
43     //Clear variables
44     averageSpeed = 0.0;
45     speed = 0.0;
46     allSpeeds = 0.0;
47     speedCount = 0;
48     direction = 0.0;
49
50     //Button settings
51     buttonStatus = true;
52     pixmapRouteStop = new QPixmap("Graphics/route_stop.png");
53     pixmapRouteStart = new QPixmap("Graphics/route_start.png");
54     iconRouteStop = new QIcon(*pixmapRouteStop);
55     iconRouteStart = new QIcon(*pixmapRouteStart);
56     QSize iconSize(125, 125);
57     ui->buttonRouteStartStop->setIconSize(iconSize);
58     ui->buttonRouteStartStop->setIcon(*iconRouteStart);
59     ui->buttonRouteStartStop->setAutoFillBackground(true);
60     ui->buttonRouteStartStop->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
61     ui->pushButtonInfo->setAutoFillBackground(true);
62     ui->pushButtonInfo->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
63
64     //Satellite picture and label
65     ui->labelRouteSatelliteStatus->setVisible(0);
66     ui->labelRouteSatellitePicture->setVisible(0);
67     //ui->labelRouteSatellitePicture->setPixmap(QPixmap("Graphics/satellite_vista.png"));
68     timerSatellitePicture = new QTimer();
69     timerSatellitePicture->setInterval(400);
70     connect(timerSatellitePicture, SIGNAL(timeout()),this, SLOT(timerSatellitePictureTimeout()));
71     ui->labelUserInfo->setText("Push start button");  //User info label
72
73     //Invisible or clear labels
74     ui->labelRouteStatus->setVisible(0);
75     ui->labelRoutePicture->setVisible(0);
76     ui->labelGpsSpeed->setVisible(0); //GPS speed label
77     ui->labelGpsAvgSpeed->setVisible(0); //GPS average speed label
78     ui->labelDistance->setVisible(0); //GPS distance label
79     ui->labelSignalStrength->setText(""); //GPS signal strength label
80
81     // Timer
82     timerRoutePicture = new QTimer();
83     timerRoutePicture->setInterval(400);
84     connect(timerRoutePicture, SIGNAL(timeout()),this, SLOT(timerRoutePictureTimeout()));
85
86     //GPS
87     location = new Maemo5Location(this);
88     gpsData = new GPSData(location);
89     connect(location,SIGNAL(agnss()),this,SLOT(gpsStatus()));
90 }
91
92 /**
93   * Destructor of this class. Deletes all dynamic objects and sets them to NULL.
94   */
95 RouteSaveDialog::~RouteSaveDialog()
96 {
97     qDebug() << "__~RouteSaveDialog";
98     if(ui)
99         delete ui;
100     if(gpsData)
101         delete gpsData;
102     if(location)
103         delete location;
104     if(routeDialog)
105         delete routeDialog;
106     if(calibrateDialog)
107     {
108         delete calibrateDialog;
109         calibrateDialog = NULL;
110     }
111
112     delete timerSatellitePicture;
113     delete timerRoutePicture;
114     delete pixmapRouteStop;
115     delete pixmapRouteStart;
116     delete iconRouteStop;
117     delete iconRouteStart;
118 }
119
120 /**
121   *
122   */
123 void RouteSaveDialog::changeEvent(QEvent *e)
124 {
125     QDialog::changeEvent(e);
126     switch (e->type()) {
127     case QEvent::LanguageChange:
128         ui->retranslateUi(this);
129         break;
130     default:
131         break;
132     }
133 }
134
135 /**
136   * Draws compass to the UI
137   * @param QPaintEvent
138  */
139 void RouteSaveDialog::paintEvent(QPaintEvent *)
140 {
141     QPainter painter(this);
142
143     painter.setRenderHint(QPainter::Antialiasing, true);
144     painter.setPen(QPen((Qt::gray),2));
145
146     painter.drawEllipse(arrowStartEast.x() - 30, arrowStartEast.y() - 50, 100, 100);
147
148     QFont font;
149     font.setPixelSize(26);
150     painter.setFont(font);
151     painter.drawText(arrowStartNorth.x() - 10, arrowEndNorth.y() - 45, "N");
152     painter.drawText(arrowStartEast.x() - 65, arrowStartEast.y() + 5, "W");
153     painter.drawText(arrowStartNorth.x()-10, arrowStartNorth.y()+55, "S");
154     painter.drawText(arrowEndEast.x() + 40, arrowEndEast.y() + 5, "E");
155
156     font.setPixelSize(12);
157     painter.setFont(font);
158     painter.drawText(arrowStartNorth.x() + 40, arrowEndNorth.y() - 25, "NE");
159     painter.drawText(arrowStartNorth.x() + 40, arrowStartEast.y() + 45, "SE");
160     painter.drawText(arrowStartEast.x() - 45, arrowStartEast.y() + 45, "SW");
161     painter.drawText(arrowStartEast.x() - 45, arrowEndNorth.y() - 25, "NW");
162
163     if (direction >= 67.5 && direction <= 112.5)
164     {
165         // East arrow
166         painter.drawLine(arrowStartEast, arrowEndEast);
167         painter.drawLine(arrowEndEast.x(), arrowEndEast.y(), arrowEndEast.x()-10, arrowEndEast.y()-5);
168         painter.drawLine(arrowEndEast.x(), arrowEndEast.y(), arrowEndEast.x()-10, arrowEndEast.y()+5);
169     }
170
171     else if (direction <= 292.5 && direction >= 247.5)
172     {
173         // West arrow
174         painter.drawLine(arrowStartEast, arrowEndEast);
175         painter.drawLine(arrowStartEast.x(), arrowStartEast.y(), arrowStartEast.x()+10, arrowEndEast.y()-5);
176         painter.drawLine(arrowStartEast.x(), arrowStartEast.y(), arrowStartEast.x()+10, arrowStartEast.y()+5);
177     }
178
179     else if (direction <= 202.5 && direction >= 157.5)
180     {
181         // South arrow
182         painter.drawLine(arrowStartNorth, arrowEndNorth);
183         painter.drawLine(arrowStartNorth.x(), arrowStartNorth.y(), arrowStartNorth.x()-5, arrowStartNorth.y()-10);
184         painter.drawLine(arrowStartNorth.x(), arrowStartNorth.y(), arrowStartNorth.x()+5, arrowStartNorth.y()-10);
185     }
186
187     else if (direction > 22.5 && direction < 67.5)
188     {
189         // Northeast arrow
190         painter.drawLine(arrowStartNortheast, arrowEndNortheast);
191         painter.drawLine(arrowEndNortheast.x(), arrowEndNortheast.y(), arrowEndNortheast.x()-10, arrowEndNortheast.y());
192         painter.drawLine(arrowEndNortheast.x(), arrowEndNortheast.y(), arrowEndNortheast.x(), arrowEndNortheast.y()+10);
193     }
194
195     else if (direction > 202.5 && direction < 247.5)
196     {
197         // Southwest arrow
198         painter.drawLine(arrowStartNortheast, arrowEndNortheast);
199         painter.drawLine(arrowStartNortheast.x(), arrowStartNortheast.y(), arrowStartNortheast.x(), arrowStartNortheast.y() - 10);
200         painter.drawLine(arrowStartNortheast.x(), arrowStartNortheast.y(), arrowStartNortheast.x() + 10, arrowStartNortheast.y());
201     }
202
203     else if (direction > 292.5 && direction < 336.5)
204     {
205         // Northwest arrow
206         painter.drawLine(arrowStartNorthwest, arrowEndNorthwest);
207         painter.drawLine(arrowEndNorthwest.x(), arrowEndNorthwest.y(), arrowEndNorthwest.x(), arrowEndNorthwest.y()+10);
208         painter.drawLine(arrowEndNorthwest.x(), arrowEndNorthwest.y(), arrowEndNorthwest.x()+10, arrowEndNorthwest.y());
209     }
210
211     else if (direction > 112.5 && direction < 157.5)
212     {
213         // Southeast arrow
214         painter.drawLine(arrowStartNorthwest, arrowEndNorthwest);
215         painter.drawLine(arrowStartNorthwest.x(), arrowStartNorthwest.y(), arrowStartNorthwest.x(), arrowStartNorthwest.y()-10);
216         painter.drawLine(arrowStartNorthwest.x(), arrowStartNorthwest.y(), arrowStartNorthwest.x()-10, arrowStartNorthwest.y());
217     }
218
219     else
220     {
221         // North arrow
222         painter.drawLine(arrowStartNorth, arrowEndNorth);
223         painter.drawLine(arrowEndNorth.x(), arrowEndNorth.y(), arrowEndNorth.x()-5, arrowEndNorth.y()+10);
224         painter.drawLine(arrowEndNorth.x(), arrowEndNorth.y(), arrowEndNorth.x()+5, arrowEndNorth.y()+10);
225     }
226 }
227
228 /**
229   *This slot function is called when route start/stop button clicked.
230   */
231 void RouteSaveDialog::on_buttonRouteStartStop_clicked()
232 {
233     if ( buttonStatus == true )//If start button clicked
234     {
235         qDebug() << "__start button clicked";
236
237         //Clear variables
238         averageSpeed = 0.0;
239         speed = 0.0;
240         allSpeeds = 0.0;
241         speedCount = 1;
242
243         buttonStatus = false;
244         ui->buttonRouteStartStop->setIcon(*iconRouteStop);
245         location->startPollingGPS();
246         gpsStatus();
247     }
248     else //If stop button clicked
249     {
250         qDebug() << "__stop button clicked";
251         buttonStatus = true;
252         ui->buttonRouteStartStop->setIcon(*iconRouteStart);
253
254         //Satellite picture and label
255         ui->labelRouteSatelliteStatus->setText("Searching satellite");
256         ui->labelRouteSatelliteStatus->setVisible(0);
257         ui->labelRouteSatellitePicture->setVisible(0);
258         timerSatellitePicture->stop();
259
260         //Route picture and label
261         ui->labelRouteStatus->setVisible(0);
262         ui->labelRoutePicture->setVisible(0);
263         timerRoutePicture->stop();
264         location->stopPollingGPS();
265
266         // Progress bar
267         if(!calibrateDialog)
268         {
269             calibrateDialog = new CalibrateDialog();
270         }
271
272         progressbarPoints = 100;
273         progressbarIteration = 0;
274         calibrateDialog->resetProgressValue();
275         calibrateDialog->setMaxValue( progressbarPoints );
276         calibrateDialog->setTitle("Calculating route...");
277         calibrateDialog->show();
278
279
280         if(!routeDialog)
281         {
282             routeDialog = new RouteDialog(this);
283         }
284
285         connect(routeDialog, SIGNAL(sendroute()),      this, SLOT(sendRoute()));
286         connect(routeDialog, SIGNAL(progressbar(int)), this, SLOT(setProgressbar(int)));
287         connect(routeDialog, SIGNAL(rejected()),       this, SLOT(killRouteDialog()));
288         //connect(routeDialog, SIGNAL(killRoute()),      this, SLOT(killRouteDialog()));
289
290         QString routeFile = QString("routetemp.xml");
291         if (routeDialog->readRouteFromFile( routeFile ) == true)
292         {
293             //calibrateDialog->close();
294             routeDialog->show();
295         }
296         else
297         {
298             //calibrateDialog->close();
299         }
300 calibrateDialog->close();
301         //Set GPS speed labels in visible
302         ui->labelGpsSpeed->setVisible(0);
303         ui->labelGpsAvgSpeed->setVisible(0);
304
305         //GPS distance label
306         ui->labelDistance->setVisible(0);
307
308         //Stop route recording
309         gpsData->stopRouteRecording();
310
311         //User info label
312         ui->labelUserInfo->setText("Push start button");
313     }
314 }
315
316 /**
317   * This slot function is called when satellite picture timer timeout(400ms).
318   */
319 void RouteSaveDialog::timerSatellitePictureTimeout()
320 {
321     //If satellite picture visible.
322     if (ui->labelRouteSatellitePicture->isVisible() == 1)
323     {
324         ui->labelRouteSatelliteStatus->setVisible(0);
325         ui->labelRouteSatellitePicture->setVisible(0);
326     }
327     else
328     {
329         ui->labelRouteSatelliteStatus->setVisible(1);
330         ui->labelRouteSatellitePicture->setVisible(1);
331     }
332     timerSatellitePicture->start();
333 }
334
335 /**
336   * This slot function is called when route picture timer timeout(400ms).
337   */
338 void RouteSaveDialog::timerRoutePictureTimeout()
339 {
340     //If route picture visible.
341     if (ui->labelRoutePicture->isVisible() == 1)
342     {
343         ui->labelRouteStatus->setVisible(0);
344         ui->labelRoutePicture->setVisible(0);
345     }
346     else
347     {
348         ui->labelRouteStatus->setVisible(1);
349         ui->labelRoutePicture->setVisible(1);
350     }
351     timerRoutePicture->start();
352 }
353
354 /**
355   * This slot function is called when GPS update location.
356   */
357 void RouteSaveDialog::gpsStatus()
358 {
359     //IF GPS start button clicked
360     if (buttonStatus == false)
361     {
362         //ui->labelSignalStrength->setText(QString::number(location->getSignalStrength()));    //Returns average signal strength of satellites which are in use.
363
364         //If GPS find 4 or more satellite and signal stregth is 30 or more.
365         if (location->getSatellitesInUse() >= 4 && location->getSignalStrength() >= 30)
366         {
367             //Satellite picture and label
368             ui->labelRouteSatelliteStatus->setText("GPS Ready");
369             ui->labelRouteSatelliteStatus->setVisible(1);
370             ui->labelRouteSatellitePicture->setVisible(1);
371             timerSatellitePicture->stop();
372
373             //Route picture and label
374             ui->labelRouteStatus->setText("Recorded " + QString::number(gpsData->roundCounter) + " route point");
375             ui->labelUserInfo->setText("Recorded " + QString::number(gpsData->roundCounter) + " route point");
376             ui->labelRouteStatus->setVisible(1);
377             ui->labelRoutePicture->setVisible(1);
378             timerRoutePicture->start();
379
380             //Get GPS speed
381             speed = location->getSpeed();
382
383             //Get GPS track means direction
384             direction = gpsData->getDirection();
385             repaint();
386
387             //Set GPS speed
388             gpsSpeed.sprintf("%.0f", speed);
389             ui->labelGpsSpeed->setText(gpsSpeed + " km/h");
390             ui->labelGpsSpeed->setVisible(1);
391
392             //Set GPS average speed
393             allSpeeds += speed;
394             averageSpeed = allSpeeds/speedCount;
395             gpsSpeed.sprintf("%.0f",averageSpeed);
396             ui->labelGpsAvgSpeed->setText("Avg: " + gpsSpeed + " km/h");
397             ui->labelGpsAvgSpeed->setVisible(1);
398             speedCount++;
399
400             //Set distance traveled.
401             distanceString.sprintf("%.3f", gpsData->getDistanceTraveled());
402             ui->labelDistance->setText(distanceString + " km");
403             ui->labelDistance->setVisible(1);
404
405             //Start route recording
406             gpsData->startRouteRecording();
407         }
408         else //If GPS find less than 4 satellite or signal strength is poor.
409         {
410             //Satellite picture and label
411             ui->labelRouteSatelliteStatus->setText("Searching satellite");
412             ui->labelUserInfo->setText("Searching satellite");
413             ui->labelRouteSatelliteStatus->setVisible(1);
414             ui->labelRouteSatellitePicture->setVisible(1);
415             timerSatellitePicture->start();
416
417             //Route picture and label
418             ui->labelRouteStatus->setVisible(0);
419             ui->labelRoutePicture->setVisible(0);
420             timerRoutePicture->stop();
421
422             //Set GPS speed labels in visible
423             ui->labelGpsSpeed->setVisible(0);
424             ui->labelGpsAvgSpeed->setVisible(0);
425
426             //GPS distance label
427             ui->labelDistance->setVisible(0);
428         }
429     }
430     else //If stop button clicked
431     {
432         //Satellite picture and label
433         ui->labelRouteSatelliteStatus->setText("Searching satellite");
434         ui->labelUserInfo->setText("Push start button");
435         ui->labelRouteSatelliteStatus->setVisible(0);
436         ui->labelRouteSatellitePicture->setVisible(0);
437         timerSatellitePicture->stop();
438
439         //Route picture and label
440         ui->labelRouteStatus->setVisible(0);
441         ui->labelRoutePicture->setVisible(0);
442         timerRoutePicture->stop();
443
444         //Set GPS speed labels in visible
445         ui->labelGpsSpeed->setVisible(0);
446         ui->labelGpsAvgSpeed->setVisible(0);
447
448         //GPS distance label
449         ui->labelDistance->setVisible(0);
450     }
451 }
452
453 /**
454   * This slot function is called when routeDialog emit sendroute (sendPushButton).
455   */
456 void RouteSaveDialog::sendRoute()
457 {
458     emit sendroute(); //Emit mainwindow clientSendRoute
459 }
460
461 /**
462   * This slot function called when ever info button clicked.
463   */
464 void RouteSaveDialog::on_pushButtonInfo_clicked()
465 {
466     if(!helpRoutingDialog)
467     {
468         helpRoutingDialog = new HelpRoutingDialog;
469     }
470     connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
471     helpRoutingDialog->show();
472 }
473
474 /**
475   * This slot function called when ever dialog rejected.
476   */
477 void RouteSaveDialog::killHelpDialog()
478 {
479     if(helpRoutingDialog)
480     {
481         qDebug() << "__Route save kill: helpRoutingDialog";
482         delete helpRoutingDialog;
483         helpRoutingDialog = NULL;
484     }
485 }
486
487 /**
488   * This slot function called when ever dialog rejected.
489   */
490 void RouteSaveDialog::killRouteDialog()
491 {
492     if(routeDialog)
493     {
494         qDebug() << "__Route save kill: routeDialog";
495         delete routeDialog;
496         routeDialog = NULL;
497     }
498     if(calibrateDialog)
499     {
500         qDebug() << "__Route save kill: calibrateDialog";
501         delete calibrateDialog;
502         calibrateDialog = NULL;
503     }
504 }
505
506 /**
507   * This function return speed average.
508   * @return double average speed
509   */
510 double RouteSaveDialog::getAverageSpeed()
511 {
512     return averageSpeed;
513 }
514
515 /**
516   * This function return distance traveled in QString.
517   * @return QString distance traveled
518   */
519 QString RouteSaveDialog::getDistanceTraveled()
520 {
521     return distanceString;
522 }
523
524 /**
525   * This function
526   */
527 void RouteSaveDialog::setProgressbar(int i)
528 {
529     qDebug() << "__setProgressbar " ;//+ i;
530     qDebug() << i;
531     calibrateDialog->setProgressValue(i);//progressbarIteration);
532     progressbarIteration++;
533 }