d24bcb2f4c5bf65449d5c21c76aab239ac8cf8fa
[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
107     delete timerSatellitePicture;
108     delete timerRoutePicture;
109     delete pixmapRouteStop;
110     delete pixmapRouteStart;
111     delete iconRouteStop;
112     delete iconRouteStart;
113 }
114
115 /**
116   *
117   */
118 void RouteSaveDialog::changeEvent(QEvent *e)
119 {
120     QDialog::changeEvent(e);
121     switch (e->type()) {
122     case QEvent::LanguageChange:
123         ui->retranslateUi(this);
124         break;
125     default:
126         break;
127     }
128 }
129
130 /**
131   * Draws compass to the UI
132   * @param QPaintEvent
133  */
134 void RouteSaveDialog::paintEvent(QPaintEvent *)
135 {
136     QPainter painter(this);
137
138     painter.setRenderHint(QPainter::Antialiasing, true);
139     painter.setPen(QPen((Qt::gray),2));
140
141     painter.drawEllipse(arrowStartEast.x() - 30, arrowStartEast.y() - 50, 100, 100);
142
143     QFont font;
144     font.setPixelSize(26);
145     painter.setFont(font);
146     painter.drawText(arrowStartNorth.x() - 10, arrowEndNorth.y() - 45, "N");
147     painter.drawText(arrowStartEast.x() - 65, arrowStartEast.y() + 5, "W");
148     painter.drawText(arrowStartNorth.x()-10, arrowStartNorth.y()+55, "S");
149     painter.drawText(arrowEndEast.x() + 40, arrowEndEast.y() + 5, "E");
150
151     font.setPixelSize(12);
152     painter.setFont(font);
153     painter.drawText(arrowStartNorth.x() + 40, arrowEndNorth.y() - 25, "NE");
154     painter.drawText(arrowStartNorth.x() + 40, arrowStartEast.y() + 45, "SE");
155     painter.drawText(arrowStartEast.x() - 45, arrowStartEast.y() + 45, "SW");
156     painter.drawText(arrowStartEast.x() - 45, arrowEndNorth.y() - 25, "NW");
157
158     if (direction >= 67.5 && direction <= 112.5)
159     {
160         // East arrow
161         painter.drawLine(arrowStartEast, arrowEndEast);
162         painter.drawLine(arrowEndEast.x(), arrowEndEast.y(), arrowEndEast.x()-10, arrowEndEast.y()-5);
163         painter.drawLine(arrowEndEast.x(), arrowEndEast.y(), arrowEndEast.x()-10, arrowEndEast.y()+5);
164     }
165
166     else if (direction <= 292.5 && direction >= 247.5)
167     {
168         // West arrow
169         painter.drawLine(arrowStartEast, arrowEndEast);
170         painter.drawLine(arrowStartEast.x(), arrowStartEast.y(), arrowStartEast.x()+10, arrowEndEast.y()-5);
171         painter.drawLine(arrowStartEast.x(), arrowStartEast.y(), arrowStartEast.x()+10, arrowStartEast.y()+5);
172     }
173
174     else if (direction <= 202.5 && direction >= 157.5)
175     {
176         // South arrow
177         painter.drawLine(arrowStartNorth, arrowEndNorth);
178         painter.drawLine(arrowStartNorth.x(), arrowStartNorth.y(), arrowStartNorth.x()-5, arrowStartNorth.y()-10);
179         painter.drawLine(arrowStartNorth.x(), arrowStartNorth.y(), arrowStartNorth.x()+5, arrowStartNorth.y()-10);
180     }
181
182     else if (direction > 22.5 && direction < 67.5)
183     {
184         // Northeast arrow
185         painter.drawLine(arrowStartNortheast, arrowEndNortheast);
186         painter.drawLine(arrowEndNortheast.x(), arrowEndNortheast.y(), arrowEndNortheast.x()-10, arrowEndNortheast.y());
187         painter.drawLine(arrowEndNortheast.x(), arrowEndNortheast.y(), arrowEndNortheast.x(), arrowEndNortheast.y()+10);
188     }
189
190     else if (direction > 202.5 && direction < 247.5)
191     {
192         // Southwest arrow
193         painter.drawLine(arrowStartNortheast, arrowEndNortheast);
194         painter.drawLine(arrowStartNortheast.x(), arrowStartNortheast.y(), arrowStartNortheast.x(), arrowStartNortheast.y() - 10);
195         painter.drawLine(arrowStartNortheast.x(), arrowStartNortheast.y(), arrowStartNortheast.x() + 10, arrowStartNortheast.y());
196     }
197
198     else if (direction > 292.5 && direction < 336.5)
199     {
200         // Northwest arrow
201         painter.drawLine(arrowStartNorthwest, arrowEndNorthwest);
202         painter.drawLine(arrowEndNorthwest.x(), arrowEndNorthwest.y(), arrowEndNorthwest.x(), arrowEndNorthwest.y()+10);
203         painter.drawLine(arrowEndNorthwest.x(), arrowEndNorthwest.y(), arrowEndNorthwest.x()+10, arrowEndNorthwest.y());
204     }
205
206     else if (direction > 112.5 && direction < 157.5)
207     {
208         // Southeast arrow
209         painter.drawLine(arrowStartNorthwest, arrowEndNorthwest);
210         painter.drawLine(arrowStartNorthwest.x(), arrowStartNorthwest.y(), arrowStartNorthwest.x(), arrowStartNorthwest.y()-10);
211         painter.drawLine(arrowStartNorthwest.x(), arrowStartNorthwest.y(), arrowStartNorthwest.x()-10, arrowStartNorthwest.y());
212     }
213
214     else
215     {
216         // North arrow
217         painter.drawLine(arrowStartNorth, arrowEndNorth);
218         painter.drawLine(arrowEndNorth.x(), arrowEndNorth.y(), arrowEndNorth.x()-5, arrowEndNorth.y()+10);
219         painter.drawLine(arrowEndNorth.x(), arrowEndNorth.y(), arrowEndNorth.x()+5, arrowEndNorth.y()+10);
220     }
221 }
222
223 /**
224   *This slot function is called when route start/stop button clicked.
225   */
226 void RouteSaveDialog::on_buttonRouteStartStop_clicked()
227 {
228     if ( buttonStatus == true )//If start button clicked
229     {
230         qDebug() << "__start button clicked";
231
232         //Clear variables
233         averageSpeed = 0.0;
234         speed = 0.0;
235         allSpeeds = 0.0;
236         speedCount = 1;
237
238         buttonStatus = false;
239         ui->buttonRouteStartStop->setIcon(*iconRouteStop);
240         location->startPollingGPS();
241         gpsStatus();
242     }
243     else //If stop button clicked
244     {
245         qDebug() << "__stop button clicked";
246         buttonStatus = true;
247         ui->buttonRouteStartStop->setIcon(*iconRouteStart);
248
249         //Satellite picture and label
250         ui->labelRouteSatelliteStatus->setText("Searching satellite");
251         ui->labelRouteSatelliteStatus->setVisible(0);
252         ui->labelRouteSatellitePicture->setVisible(0);
253         timerSatellitePicture->stop();
254
255         //Route picture and label
256         ui->labelRouteStatus->setVisible(0);
257         ui->labelRoutePicture->setVisible(0);
258         timerRoutePicture->stop();
259         location->stopPollingGPS();
260
261         // Progres bar
262         if(!calibrateDialog)
263             calibrateDialog = new CalibrateDialog();
264
265         calibrateDialog->show();
266         calibrateDialog->resetProgressValue();
267         int points = 100;
268         int iteration = 0;
269         calibrateDialog->setMaxValue( points + 1 );
270
271         /*do {
272             calibrateDialog->setProgressValue(iteration);
273             iteration++;
274
275         } while(iteration != points);*/
276
277         QString routeFile = QString("routetemp.xml");
278         if (routeDialog->readRouteFromFile( routeFile , calibrateDialog) == true)
279         {
280             if(!routeDialog)
281                 routeDialog = new RouteDialog(this);
282             connect(routeDialog, SIGNAL(sendroute()), this, SLOT(sendRoute()));
283
284             calibrateDialog->close();
285             routeDialog->show();
286         }
287         else
288         {
289             calibrateDialog->close();
290         }
291
292         //Set GPS speed labels in visible
293         ui->labelGpsSpeed->setVisible(0);
294         ui->labelGpsAvgSpeed->setVisible(0);
295
296         //GPS distance label
297         ui->labelDistance->setVisible(0);
298
299         //Stop route recording
300         gpsData->stopRouteRecording();
301
302         //User info label
303         ui->labelUserInfo->setText("Push start button");
304     }
305 }
306
307 /**
308   * This slot function is called when satellite picture timer timeout(400ms).
309   */
310 void RouteSaveDialog::timerSatellitePictureTimeout()
311 {
312     //If satellite picture visible.
313     if (ui->labelRouteSatellitePicture->isVisible() == 1)
314     {
315         ui->labelRouteSatelliteStatus->setVisible(0);
316         ui->labelRouteSatellitePicture->setVisible(0);
317     }
318     else
319     {
320         ui->labelRouteSatelliteStatus->setVisible(1);
321         ui->labelRouteSatellitePicture->setVisible(1);
322     }
323     timerSatellitePicture->start();
324 }
325
326 /**
327   * This slot function is called when route picture timer timeout(400ms).
328   */
329 void RouteSaveDialog::timerRoutePictureTimeout()
330 {
331     //If route picture visible.
332     if (ui->labelRoutePicture->isVisible() == 1)
333     {
334         ui->labelRouteStatus->setVisible(0);
335         ui->labelRoutePicture->setVisible(0);
336     }
337     else
338     {
339         ui->labelRouteStatus->setVisible(1);
340         ui->labelRoutePicture->setVisible(1);
341     }
342     timerRoutePicture->start();
343 }
344
345 /**
346   * This slot function is called when GPS update location.
347   */
348 void RouteSaveDialog::gpsStatus()
349 {
350     //IF GPS start button clicked
351     if (buttonStatus == false)
352     {
353         //ui->labelSignalStrength->setText(QString::number(location->getSignalStrength()));    //Returns average signal strength of satellites which are in use.
354
355         //If GPS find 4 or more satellite and signal stregth is 30 or more.
356         if (location->getSatellitesInUse() >= 4 && location->getSignalStrength() >= 30)
357         {
358             //Satellite picture and label
359             ui->labelRouteSatelliteStatus->setText("GPS Ready");
360             ui->labelRouteSatelliteStatus->setVisible(1);
361             ui->labelRouteSatellitePicture->setVisible(1);
362             timerSatellitePicture->stop();
363
364             //Route picture and label
365             ui->labelRouteStatus->setText("Recorded " + QString::number(gpsData->roundCounter) + " route point");
366             ui->labelUserInfo->setText("Recorded " + QString::number(gpsData->roundCounter) + " route point");
367             ui->labelRouteStatus->setVisible(1);
368             ui->labelRoutePicture->setVisible(1);
369             timerRoutePicture->start();
370
371             //Get GPS speed
372             speed = location->getSpeed();
373
374             //Get GPS track means direction
375             direction = gpsData->getDirection();
376             repaint();
377
378             //Set GPS speed
379             gpsSpeed.sprintf("%.0f", speed);
380             ui->labelGpsSpeed->setText(gpsSpeed + " km/h");
381             ui->labelGpsSpeed->setVisible(1);
382
383             //Set GPS average speed
384             allSpeeds += speed;
385             averageSpeed = allSpeeds/speedCount;
386             gpsSpeed.sprintf("%.0f",averageSpeed);
387             ui->labelGpsAvgSpeed->setText("Avg: " + gpsSpeed + " km/h");
388             ui->labelGpsAvgSpeed->setVisible(1);
389             speedCount++;
390
391             //Set distance traveled.
392             distanceString.sprintf("%.3f", gpsData->getDistanceTraveled());
393             ui->labelDistance->setText(distanceString + " km");
394             ui->labelDistance->setVisible(1);
395
396             //Start route recording
397             gpsData->startRouteRecording();
398         }
399         else //If GPS find less than 4 satellite or signal strength is poor.
400         {
401             //Satellite picture and label
402             ui->labelRouteSatelliteStatus->setText("Searching satellite");
403             ui->labelUserInfo->setText("Searching satellite");
404             ui->labelRouteSatelliteStatus->setVisible(1);
405             ui->labelRouteSatellitePicture->setVisible(1);
406             timerSatellitePicture->start();
407
408             //Route picture and label
409             ui->labelRouteStatus->setVisible(0);
410             ui->labelRoutePicture->setVisible(0);
411             timerRoutePicture->stop();
412
413             //Set GPS speed labels in visible
414             ui->labelGpsSpeed->setVisible(0);
415             ui->labelGpsAvgSpeed->setVisible(0);
416
417             //GPS distance label
418             ui->labelDistance->setVisible(0);
419         }
420     }
421     else //If stop button clicked
422     {
423         //Satellite picture and label
424         ui->labelRouteSatelliteStatus->setText("Searching satellite");
425         ui->labelUserInfo->setText("Push start button");
426         ui->labelRouteSatelliteStatus->setVisible(0);
427         ui->labelRouteSatellitePicture->setVisible(0);
428         timerSatellitePicture->stop();
429
430         //Route picture and label
431         ui->labelRouteStatus->setVisible(0);
432         ui->labelRoutePicture->setVisible(0);
433         timerRoutePicture->stop();
434
435         //Set GPS speed labels in visible
436         ui->labelGpsSpeed->setVisible(0);
437         ui->labelGpsAvgSpeed->setVisible(0);
438
439         //GPS distance label
440         ui->labelDistance->setVisible(0);
441     }
442 }
443
444 /**
445   * This slot function is called when routeDialog emit sendroute (sendPushButton).
446   */
447 void RouteSaveDialog::sendRoute()
448 {
449     emit sendroute(); //Emit mainwindow clientSendRoute
450 }
451
452 /**
453   * This slot function called when ever info button clicked.
454   */
455 void RouteSaveDialog::on_pushButtonInfo_clicked()
456 {
457     if(!helpRoutingDialog)
458     {
459         helpRoutingDialog = new HelpRoutingDialog;
460     }
461     connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
462     helpRoutingDialog->show();
463 }
464
465 /**
466   * This slot function called when ever dialog rejected.
467   */
468 void RouteSaveDialog::killHelpDialog()
469 {
470     if(helpRoutingDialog)
471     {
472         qDebug() << "__Route save kill: helpRoutingDialog";
473         delete helpRoutingDialog;
474         helpRoutingDialog = NULL;
475     }
476 }
477
478 /**
479   * This function return speed average.
480   * @return double average speed
481   */
482 double RouteSaveDialog::getAverageSpeed()
483 {
484     return averageSpeed;
485 }
486
487 /**
488   * This function return distance traveled in QString.
489   * @return QString distance traveled
490   */
491 QString RouteSaveDialog::getDistanceTraveled()
492 {
493     return distanceString;
494 }