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)");
65 ui->buttonLoadRoute->setAutoFillBackground(true);
66 ui->buttonLoadRoute->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
68 //Satellite picture and label
69 ui->labelRouteSatelliteStatus->setVisible(0);
70 ui->labelRouteSatellitePicture->setVisible(0);
71 //ui->labelRouteSatellitePicture->setPixmap(QPixmap("Graphics/satellite_vista.png"));
72 timerSatellitePicture = new QTimer();
73 timerSatellitePicture->setInterval(400);
74 connect(timerSatellitePicture, SIGNAL(timeout()),this, SLOT(timerSatellitePictureTimeout()));
75 ui->labelUserInfo->setText("Push start button"); //User info label
77 //Invisible or clear labels
78 ui->labelRouteStatus->setVisible(0);
79 ui->labelRoutePicture->setVisible(0);
80 ui->labelGpsSpeed->setVisible(0); //GPS speed label
81 ui->labelGpsAvgSpeed->setVisible(0); //GPS average speed label
82 ui->labelDistance->setVisible(0); //GPS distance label
83 ui->labelSignalStrength->setText(""); //GPS signal strength label
86 timerRoutePicture = new QTimer();
87 timerRoutePicture->setInterval(400);
88 connect(timerRoutePicture, SIGNAL(timeout()),this, SLOT(timerRoutePictureTimeout()));
91 location = new Maemo5Location(this);
92 gpsData = new GPSData(location);
93 connect(location,SIGNAL(agnss()),this,SLOT(gpsStatus()));
96 QString folder = "/home/user/MyDocs/speedfreak";
97 if(!QDir(folder).exists())
101 if(!QDir(folder + "/route").exists())
103 QDir().mkdir(folder + "/route");
108 * Destructor of this class. Deletes all dynamic objects and sets them to NULL.
110 RouteSaveDialog::~RouteSaveDialog()
112 qDebug() << "__~RouteSaveDialog";
123 delete calibrateDialog;
124 calibrateDialog = NULL;
127 delete timerSatellitePicture;
128 delete timerRoutePicture;
129 delete pixmapRouteStop;
130 delete pixmapRouteStart;
131 delete iconRouteStop;
132 delete iconRouteStart;
138 void RouteSaveDialog::changeEvent(QEvent *e)
140 QDialog::changeEvent(e);
142 case QEvent::LanguageChange:
143 ui->retranslateUi(this);
151 * Draws compass to the UI
154 void RouteSaveDialog::paintEvent(QPaintEvent *)
156 QPainter painter(this);
158 painter.setRenderHint(QPainter::Antialiasing, true);
159 painter.setPen(QPen((Qt::gray),2));
161 painter.drawEllipse(arrowStartEast.x() - 30, arrowStartEast.y() - 50, 100, 100);
164 font.setPixelSize(26);
165 painter.setFont(font);
166 painter.drawText(arrowStartNorth.x() - 10, arrowEndNorth.y() - 45, "N");
167 painter.drawText(arrowStartEast.x() - 65, arrowStartEast.y() + 5, "W");
168 painter.drawText(arrowStartNorth.x()-10, arrowStartNorth.y()+55, "S");
169 painter.drawText(arrowEndEast.x() + 40, arrowEndEast.y() + 5, "E");
171 font.setPixelSize(12);
172 painter.setFont(font);
173 painter.drawText(arrowStartNorth.x() + 40, arrowEndNorth.y() - 25, "NE");
174 painter.drawText(arrowStartNorth.x() + 40, arrowStartEast.y() + 45, "SE");
175 painter.drawText(arrowStartEast.x() - 45, arrowStartEast.y() + 45, "SW");
176 painter.drawText(arrowStartEast.x() - 45, arrowEndNorth.y() - 25, "NW");
178 if (direction >= 67.5 && direction <= 112.5)
181 painter.drawLine(arrowStartEast, arrowEndEast);
182 painter.drawLine(arrowEndEast.x(), arrowEndEast.y(), arrowEndEast.x()-10, arrowEndEast.y()-5);
183 painter.drawLine(arrowEndEast.x(), arrowEndEast.y(), arrowEndEast.x()-10, arrowEndEast.y()+5);
186 else if (direction <= 292.5 && direction >= 247.5)
189 painter.drawLine(arrowStartEast, arrowEndEast);
190 painter.drawLine(arrowStartEast.x(), arrowStartEast.y(), arrowStartEast.x()+10, arrowEndEast.y()-5);
191 painter.drawLine(arrowStartEast.x(), arrowStartEast.y(), arrowStartEast.x()+10, arrowStartEast.y()+5);
194 else if (direction <= 202.5 && direction >= 157.5)
197 painter.drawLine(arrowStartNorth, arrowEndNorth);
198 painter.drawLine(arrowStartNorth.x(), arrowStartNorth.y(), arrowStartNorth.x()-5, arrowStartNorth.y()-10);
199 painter.drawLine(arrowStartNorth.x(), arrowStartNorth.y(), arrowStartNorth.x()+5, arrowStartNorth.y()-10);
202 else if (direction > 22.5 && direction < 67.5)
205 painter.drawLine(arrowStartNortheast, arrowEndNortheast);
206 painter.drawLine(arrowEndNortheast.x(), arrowEndNortheast.y(), arrowEndNortheast.x()-10, arrowEndNortheast.y());
207 painter.drawLine(arrowEndNortheast.x(), arrowEndNortheast.y(), arrowEndNortheast.x(), arrowEndNortheast.y()+10);
210 else if (direction > 202.5 && direction < 247.5)
213 painter.drawLine(arrowStartNortheast, arrowEndNortheast);
214 painter.drawLine(arrowStartNortheast.x(), arrowStartNortheast.y(), arrowStartNortheast.x(), arrowStartNortheast.y() - 10);
215 painter.drawLine(arrowStartNortheast.x(), arrowStartNortheast.y(), arrowStartNortheast.x() + 10, arrowStartNortheast.y());
218 else if (direction > 292.5 && direction < 336.5)
221 painter.drawLine(arrowStartNorthwest, arrowEndNorthwest);
222 painter.drawLine(arrowEndNorthwest.x(), arrowEndNorthwest.y(), arrowEndNorthwest.x(), arrowEndNorthwest.y()+10);
223 painter.drawLine(arrowEndNorthwest.x(), arrowEndNorthwest.y(), arrowEndNorthwest.x()+10, arrowEndNorthwest.y());
226 else if (direction > 112.5 && direction < 157.5)
229 painter.drawLine(arrowStartNorthwest, arrowEndNorthwest);
230 painter.drawLine(arrowStartNorthwest.x(), arrowStartNorthwest.y(), arrowStartNorthwest.x(), arrowStartNorthwest.y()-10);
231 painter.drawLine(arrowStartNorthwest.x(), arrowStartNorthwest.y(), arrowStartNorthwest.x()-10, arrowStartNorthwest.y());
237 painter.drawLine(arrowStartNorth, arrowEndNorth);
238 painter.drawLine(arrowEndNorth.x(), arrowEndNorth.y(), arrowEndNorth.x()-5, arrowEndNorth.y()+10);
239 painter.drawLine(arrowEndNorth.x(), arrowEndNorth.y(), arrowEndNorth.x()+5, arrowEndNorth.y()+10);
244 *This slot function is called when route start/stop button clicked.
246 void RouteSaveDialog::on_buttonRouteStartStop_clicked()
248 if ( buttonStatus == true )//If start button clicked
250 qDebug() << "__start button clicked";
258 buttonStatus = false;
259 ui->buttonRouteStartStop->setIcon(*iconRouteStop);
260 location->startPollingGPS();
263 else //If stop button clicked
265 qDebug() << "__stop button clicked";
267 ui->buttonRouteStartStop->setIcon(*iconRouteStart);
269 //Satellite picture and label
270 ui->labelRouteSatelliteStatus->setText("Searching satellite");
271 ui->labelRouteSatelliteStatus->setVisible(0);
272 ui->labelRouteSatellitePicture->setVisible(0);
273 timerSatellitePicture->stop();
275 //Route picture and label
276 ui->labelRouteStatus->setVisible(0);
277 ui->labelRoutePicture->setVisible(0);
278 timerRoutePicture->stop();
279 location->stopPollingGPS();
281 //Set GPS speed labels in visible
282 ui->labelGpsSpeed->setVisible(0);
283 ui->labelGpsAvgSpeed->setVisible(0);
286 ui->labelDistance->setVisible(0);
288 //Stop route recording
289 gpsData->stopRouteRecording();
292 ui->labelUserInfo->setText("Push start button");
294 fileName = "/home/user/MyDocs/speedfreak/route/routetemp.xml";
300 * This slot function is called when satellite picture timer timeout(400ms).
302 void RouteSaveDialog::timerSatellitePictureTimeout()
304 //If satellite picture visible.
305 if (ui->labelRouteSatellitePicture->isVisible() == 1)
307 ui->labelRouteSatelliteStatus->setVisible(0);
308 ui->labelRouteSatellitePicture->setVisible(0);
312 ui->labelRouteSatelliteStatus->setVisible(1);
313 ui->labelRouteSatellitePicture->setVisible(1);
315 timerSatellitePicture->start();
319 * This slot function is called when route picture timer timeout(400ms).
321 void RouteSaveDialog::timerRoutePictureTimeout()
323 //If route picture visible.
324 if (ui->labelRoutePicture->isVisible() == 1)
326 ui->labelRouteStatus->setVisible(0);
327 ui->labelRoutePicture->setVisible(0);
331 ui->labelRouteStatus->setVisible(1);
332 ui->labelRoutePicture->setVisible(1);
334 timerRoutePicture->start();
338 * This slot function is called when GPS update location.
340 void RouteSaveDialog::gpsStatus()
342 //IF GPS start button clicked
343 if (buttonStatus == false)
345 //ui->labelSignalStrength->setText(QString::number(location->getSignalStrength())); //Returns average signal strength of satellites which are in use.
347 //If GPS find 4 or more satellite and signal stregth is 30 or more.
348 if (location->getSatellitesInUse() >= 4 && location->getSignalStrength() >= 30)
350 //Satellite picture and label
351 ui->labelRouteSatelliteStatus->setText("GPS Ready");
352 ui->labelRouteSatelliteStatus->setVisible(1);
353 ui->labelRouteSatellitePicture->setVisible(1);
354 timerSatellitePicture->stop();
356 //Route picture and label
357 ui->labelRouteStatus->setText("Recorded " + QString::number(gpsData->roundCounter) + " route point");
358 ui->labelUserInfo->setText("Recorded " + QString::number(gpsData->roundCounter) + " route point");
359 ui->labelRouteStatus->setVisible(1);
360 ui->labelRoutePicture->setVisible(1);
361 timerRoutePicture->start();
364 speed = location->getSpeed();
366 //Get GPS track means direction
367 direction = gpsData->getDirection();
371 gpsSpeed.sprintf("%.0f", speed);
372 ui->labelGpsSpeed->setText(gpsSpeed + " km/h");
373 ui->labelGpsSpeed->setVisible(1);
375 //Set GPS average speed
377 averageSpeed = allSpeeds/speedCount;
378 gpsSpeed.sprintf("%.0f",averageSpeed);
379 ui->labelGpsAvgSpeed->setText("Avg: " + gpsSpeed + " km/h");
380 ui->labelGpsAvgSpeed->setVisible(1);
383 //Set distance traveled.
384 distanceString.sprintf("%.3f", gpsData->getDistanceTraveled());
385 ui->labelDistance->setText(distanceString + " km");
386 ui->labelDistance->setVisible(1);
388 //Start route recording
389 gpsData->startRouteRecording();
391 else //If GPS find less than 4 satellite or signal strength is poor.
393 //Satellite picture and label
394 ui->labelRouteSatelliteStatus->setText("Searching satellite");
395 ui->labelUserInfo->setText("Searching satellite");
396 ui->labelRouteSatelliteStatus->setVisible(1);
397 ui->labelRouteSatellitePicture->setVisible(1);
398 timerSatellitePicture->start();
400 //Route picture and label
401 ui->labelRouteStatus->setVisible(0);
402 ui->labelRoutePicture->setVisible(0);
403 timerRoutePicture->stop();
405 //Set GPS speed labels in visible
406 ui->labelGpsSpeed->setVisible(0);
407 ui->labelGpsAvgSpeed->setVisible(0);
410 ui->labelDistance->setVisible(0);
413 else //If stop button clicked
415 //Satellite picture and label
416 ui->labelRouteSatelliteStatus->setText("Searching satellite");
417 ui->labelUserInfo->setText("Push start button");
418 ui->labelRouteSatelliteStatus->setVisible(0);
419 ui->labelRouteSatellitePicture->setVisible(0);
420 timerSatellitePicture->stop();
422 //Route picture and label
423 ui->labelRouteStatus->setVisible(0);
424 ui->labelRoutePicture->setVisible(0);
425 timerRoutePicture->stop();
427 //Set GPS speed labels in visible
428 ui->labelGpsSpeed->setVisible(0);
429 ui->labelGpsAvgSpeed->setVisible(0);
432 ui->labelDistance->setVisible(0);
437 * This slot function is called when routeDialog emit sendroute (sendPushButton).
439 void RouteSaveDialog::sendRoute(QString newName, int i)
441 emit sendroute(fileName, newName, i); //Emit mainwindow clientSendRoute
445 * This slot function called when ever info button clicked.
447 void RouteSaveDialog::on_pushButtonInfo_clicked()
449 if(!helpRoutingDialog)
451 helpRoutingDialog = new HelpRoutingDialog;
453 connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
454 helpRoutingDialog->show();
458 * This slot function called when ever dialog rejected.
460 void RouteSaveDialog::killHelpDialog()
462 if(helpRoutingDialog)
464 qDebug() << "__Route save kill: helpRoutingDialog";
465 delete helpRoutingDialog;
466 helpRoutingDialog = NULL;
471 * This slot function called when ever route dialog rejected.
473 void RouteSaveDialog::killRouteDialog()
477 qDebug() << "__Route save kill: routeDialog";
483 qDebug() << "__Route save kill: calibrateDialog";
484 delete calibrateDialog;
485 calibrateDialog = NULL;
490 * This function return speed average.
491 * @return double average speed
493 double RouteSaveDialog::getAverageSpeed()
499 * This function return distance traveled in QString.
500 * @return QString distance traveled
502 QString RouteSaveDialog::getDistanceTraveled()
504 return distanceString;
510 void RouteSaveDialog::setProgressbar(int i)
512 qDebug() << "__setProgressbar: " + QString::number(i);
514 calibrateDialog->setProgressValue(i);
515 progressbarIteration++;
519 * This slot function called when ever load route button clicked.
521 void RouteSaveDialog::on_buttonLoadRoute_clicked()
523 fileName = QFileDialog::getOpenFileName(this, tr("Open route"), "/home/user/MyDocs/speedfreak/route", "XML (*.xml)");
524 qDebug() << "__Opening: " + fileName;
525 openRouteDialog();//fileName);
529 * This function open and show route.
530 * @param QString file name
532 void RouteSaveDialog::openRouteDialog()
539 calibrateDialog = new CalibrateDialog();
542 progressbarPoints = 100;
543 progressbarIteration = 0;
544 calibrateDialog->resetProgressValue();
545 calibrateDialog->setMaxValue( progressbarPoints );
546 calibrateDialog->setTitle("Calculating route...");
547 calibrateDialog->show();
551 routeDialog = new RouteDialog(this);
554 connect(routeDialog, SIGNAL(sendroute(QString, int)), this, SLOT(sendRoute(QString, int)));
555 connect(routeDialog, SIGNAL(progressbar(int)), this, SLOT(setProgressbar(int)));
556 connect(routeDialog, SIGNAL(rejected()), this, SLOT(killRouteDialog()));
558 if (routeDialog->readRouteFromFile( fileName ) == true)
560 calibrateDialog->close();
565 calibrateDialog->close();