2 * Route save dialog class
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
9 #include "routesavedialog.h"
10 #include "ui_routesavedialog.h"
13 #include <QFileDialog>
16 const QPoint arrowStartEast(100, 100);
17 const QPoint arrowEndEast(140, 100);
19 const QPoint arrowStartNorth(120, 120);
20 const QPoint arrowEndNorth(120, 80);
22 const QPoint arrowStartNortheast(100, 120);
23 const QPoint arrowEndNortheast(140, 80);
25 const QPoint arrowStartNorthwest(140, 120);
26 const QPoint arrowEndNorthwest(100, 80);
29 * Constructor of this class.
30 * @param QWidget pointer to parent object. By default the value is NULL.
32 RouteSaveDialog::RouteSaveDialog(QWidget *parent) :
33 QDialog(parent), ui(new Ui::RouteSaveDialog){
35 qDebug() << "__RouteSaveDialog";
37 this->setWindowTitle("Tracking");
42 helpRoutingDialog = NULL;
43 calibrateDialog = NULL;
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)");
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
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
84 timerRoutePicture = new QTimer();
85 timerRoutePicture->setInterval(400);
86 connect(timerRoutePicture, SIGNAL(timeout()),this, SLOT(timerRoutePictureTimeout()));
89 location = new Maemo5Location(this);
90 gpsData = new GPSData(location);
91 connect(location,SIGNAL(agnss()),this,SLOT(gpsStatus()));
94 QString folder = "speedfreak_route";
95 if(!QDir(folder).exists())
102 * Destructor of this class. Deletes all dynamic objects and sets them to NULL.
104 RouteSaveDialog::~RouteSaveDialog()
106 qDebug() << "__~RouteSaveDialog";
117 delete calibrateDialog;
118 calibrateDialog = NULL;
121 delete timerSatellitePicture;
122 delete timerRoutePicture;
123 delete pixmapRouteStop;
124 delete pixmapRouteStart;
125 delete iconRouteStop;
126 delete iconRouteStart;
132 void RouteSaveDialog::changeEvent(QEvent *e)
134 QDialog::changeEvent(e);
136 case QEvent::LanguageChange:
137 ui->retranslateUi(this);
145 * Draws compass to the UI
148 void RouteSaveDialog::paintEvent(QPaintEvent *)
150 QPainter painter(this);
152 painter.setRenderHint(QPainter::Antialiasing, true);
153 painter.setPen(QPen((Qt::gray),2));
155 painter.drawEllipse(arrowStartEast.x() - 30, arrowStartEast.y() - 50, 100, 100);
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");
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");
172 if (direction >= 67.5 && direction <= 112.5)
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);
180 else if (direction <= 292.5 && direction >= 247.5)
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);
188 else if (direction <= 202.5 && direction >= 157.5)
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);
196 else if (direction > 22.5 && direction < 67.5)
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);
204 else if (direction > 202.5 && direction < 247.5)
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());
212 else if (direction > 292.5 && direction < 336.5)
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());
220 else if (direction > 112.5 && direction < 157.5)
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());
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);
238 *This slot function is called when route start/stop button clicked.
240 void RouteSaveDialog::on_buttonRouteStartStop_clicked()
242 if ( buttonStatus == true )//If start button clicked
244 qDebug() << "__start button clicked";
252 buttonStatus = false;
253 ui->buttonRouteStartStop->setIcon(*iconRouteStop);
254 location->startPollingGPS();
257 else //If stop button clicked
259 qDebug() << "__stop button clicked";
261 ui->buttonRouteStartStop->setIcon(*iconRouteStart);
263 //Satellite picture and label
264 ui->labelRouteSatelliteStatus->setText("Searching satellite");
265 ui->labelRouteSatelliteStatus->setVisible(0);
266 ui->labelRouteSatellitePicture->setVisible(0);
267 timerSatellitePicture->stop();
269 //Route picture and label
270 ui->labelRouteStatus->setVisible(0);
271 ui->labelRoutePicture->setVisible(0);
272 timerRoutePicture->stop();
273 location->stopPollingGPS();
278 calibrateDialog = new CalibrateDialog();
281 progressbarPoints = 100;
282 progressbarIteration = 0;
283 calibrateDialog->resetProgressValue();
284 calibrateDialog->setMaxValue( progressbarPoints );
285 calibrateDialog->setTitle("Calculating route...");
286 calibrateDialog->show();
291 routeDialog = new RouteDialog(this);
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()));
299 QString routeFile = QString(".//speedfreak_route/routetemp.xml");
300 if (routeDialog->readRouteFromFile( routeFile ) == true)
302 //calibrateDialog->close();
307 //calibrateDialog->close();
309 calibrateDialog->close();*/
311 //Set GPS speed labels in visible
312 ui->labelGpsSpeed->setVisible(0);
313 ui->labelGpsAvgSpeed->setVisible(0);
316 ui->labelDistance->setVisible(0);
318 //Stop route recording
319 gpsData->stopRouteRecording();
322 ui->labelUserInfo->setText("Push start button");
324 openRouteDialog("routetemp.xml");
329 * This slot function is called when satellite picture timer timeout(400ms).
331 void RouteSaveDialog::timerSatellitePictureTimeout()
333 //If satellite picture visible.
334 if (ui->labelRouteSatellitePicture->isVisible() == 1)
336 ui->labelRouteSatelliteStatus->setVisible(0);
337 ui->labelRouteSatellitePicture->setVisible(0);
341 ui->labelRouteSatelliteStatus->setVisible(1);
342 ui->labelRouteSatellitePicture->setVisible(1);
344 timerSatellitePicture->start();
348 * This slot function is called when route picture timer timeout(400ms).
350 void RouteSaveDialog::timerRoutePictureTimeout()
352 //If route picture visible.
353 if (ui->labelRoutePicture->isVisible() == 1)
355 ui->labelRouteStatus->setVisible(0);
356 ui->labelRoutePicture->setVisible(0);
360 ui->labelRouteStatus->setVisible(1);
361 ui->labelRoutePicture->setVisible(1);
363 timerRoutePicture->start();
367 * This slot function is called when GPS update location.
369 void RouteSaveDialog::gpsStatus()
371 //IF GPS start button clicked
372 if (buttonStatus == false)
374 //ui->labelSignalStrength->setText(QString::number(location->getSignalStrength())); //Returns average signal strength of satellites which are in use.
376 //If GPS find 4 or more satellite and signal stregth is 30 or more.
377 if (location->getSatellitesInUse() >= 4 && location->getSignalStrength() >= 30)
379 //Satellite picture and label
380 ui->labelRouteSatelliteStatus->setText("GPS Ready");
381 ui->labelRouteSatelliteStatus->setVisible(1);
382 ui->labelRouteSatellitePicture->setVisible(1);
383 timerSatellitePicture->stop();
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();
393 speed = location->getSpeed();
395 //Get GPS track means direction
396 direction = gpsData->getDirection();
400 gpsSpeed.sprintf("%.0f", speed);
401 ui->labelGpsSpeed->setText(gpsSpeed + " km/h");
402 ui->labelGpsSpeed->setVisible(1);
404 //Set GPS average speed
406 averageSpeed = allSpeeds/speedCount;
407 gpsSpeed.sprintf("%.0f",averageSpeed);
408 ui->labelGpsAvgSpeed->setText("Avg: " + gpsSpeed + " km/h");
409 ui->labelGpsAvgSpeed->setVisible(1);
412 //Set distance traveled.
413 distanceString.sprintf("%.3f", gpsData->getDistanceTraveled());
414 ui->labelDistance->setText(distanceString + " km");
415 ui->labelDistance->setVisible(1);
417 //Start route recording
418 gpsData->startRouteRecording();
420 else //If GPS find less than 4 satellite or signal strength is poor.
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();
429 //Route picture and label
430 ui->labelRouteStatus->setVisible(0);
431 ui->labelRoutePicture->setVisible(0);
432 timerRoutePicture->stop();
434 //Set GPS speed labels in visible
435 ui->labelGpsSpeed->setVisible(0);
436 ui->labelGpsAvgSpeed->setVisible(0);
439 ui->labelDistance->setVisible(0);
442 else //If stop button clicked
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();
451 //Route picture and label
452 ui->labelRouteStatus->setVisible(0);
453 ui->labelRoutePicture->setVisible(0);
454 timerRoutePicture->stop();
456 //Set GPS speed labels in visible
457 ui->labelGpsSpeed->setVisible(0);
458 ui->labelGpsAvgSpeed->setVisible(0);
461 ui->labelDistance->setVisible(0);
466 * This slot function is called when routeDialog emit sendroute (sendPushButton).
468 void RouteSaveDialog::sendRoute(QString s,int i)
470 emit sendroute(s,i); //Emit mainwindow clientSendRoute
474 * This slot function called when ever info button clicked.
476 void RouteSaveDialog::on_pushButtonInfo_clicked()
478 if(!helpRoutingDialog)
480 helpRoutingDialog = new HelpRoutingDialog;
482 connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
483 helpRoutingDialog->show();
487 * This slot function called when ever dialog rejected.
489 void RouteSaveDialog::killHelpDialog()
491 if(helpRoutingDialog)
493 qDebug() << "__Route save kill: helpRoutingDialog";
494 delete helpRoutingDialog;
495 helpRoutingDialog = NULL;
500 * This slot function called when ever route dialog rejected.
502 void RouteSaveDialog::killRouteDialog()
506 qDebug() << "__Route save kill: routeDialog";
512 qDebug() << "__Route save kill: calibrateDialog";
513 delete calibrateDialog;
514 calibrateDialog = NULL;
519 * This function return speed average.
520 * @return double average speed
522 double RouteSaveDialog::getAverageSpeed()
528 * This function return distance traveled in QString.
529 * @return QString distance traveled
531 QString RouteSaveDialog::getDistanceTraveled()
533 return distanceString;
539 void RouteSaveDialog::setProgressbar(int i)
541 qDebug() << "__setProgressbar " ;
543 calibrateDialog->setProgressValue(i);
544 progressbarIteration++;
548 * This slot function called when ever load route button clicked.
550 void RouteSaveDialog::on_buttonLoadRoute_clicked()
552 QString fileName = QFileDialog::getOpenFileName(this, tr("Open route"), QDir::currentPath());
553 qDebug() << "__Opening: " + fileName;
554 openRouteDialog(fileName);
558 * This function open route.
559 * @param QString file name
561 void RouteSaveDialog::openRouteDialog(QString fileName)
566 calibrateDialog = new CalibrateDialog();
569 progressbarPoints = 100;
570 progressbarIteration = 0;
571 calibrateDialog->resetProgressValue();
572 calibrateDialog->setMaxValue( progressbarPoints );
573 calibrateDialog->setTitle("Calculating route...");
574 calibrateDialog->show();
578 routeDialog = new RouteDialog(this);
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()));
585 if (routeDialog->readRouteFromFile( fileName ) == true)
587 calibrateDialog->close();
592 calibrateDialog->close();