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