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