4 * @author Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
5 * @author Toni Jussila <toni.jussila@fudeco.com>
6 * @copyright (c) 2010 Speed Freak team
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
10 #include "routesavedialog.h"
11 #include "routedialog.h"
12 #include "ui_routedialog.h"
13 #include "usersettings.h"
17 #include <QMessageBox>
19 #include <QFileDialog>
25 * In starting Qt 4.6 there is QVector3D.
26 * Later (updating Qt version) this class can be removed.
30 qreal x, y, z; // Location
33 Vector() { x=0.; y=0. ; z=0.; };
34 Vector( qreal initX, qreal initY, qreal initZ) { x = initX, y = initY; z = initZ; };
35 void setX( qreal newX) { x = newX; };
36 void setY( qreal newY) { y = newY; };
37 void setZ( qreal newZ) { z = newZ; };
38 void setV( qreal newV) { v = newV; };
39 qreal getX() { return x; };
40 qreal getY() { return y; };
41 qreal getZ() { return z; };
42 qreal getV() { return v; };
43 qreal length() { return sqrt(x*x+y*y+z*z); };
44 Vector operator+(Vector v)
46 x = x + v.x; y = y + v.y; z = z + v.z;
49 Vector operator-(Vector v)
51 x = x - v.x; y = y - v.y; z = z - v.z;
54 Vector operator/(qreal c)
56 x = x/c; y = y/c; z = z/c;
59 Vector crossProduct( Vector a, Vector b)
61 x = a.y*b.z - a.z*b.y;
62 y = a.z*b.x - a.x*b.z;
63 z = a.x*b.y - a.y*b.x;
71 Vector atPoint, fromPoint, up, a1, a2, a3;
72 qreal offsx, offsy, offsz;
76 qreal getOffsx() { return offsx; };
77 qreal getOffsy() { return offsy; };
78 qreal getOffsz() { return offsz; };
79 qreal getDval() { return dval; };
80 void setAngle( qreal newA) { angle = newA; };
81 void setUp( qreal newUpX, qreal newUpY, qreal newUpZ)
83 up.setX(newUpX); up.setY(newUpY); up.setZ(newUpZ);
85 void setAtPoint( qreal newX, qreal newY, qreal newZ)
87 atPoint.setX(newX); atPoint.setY(newY); atPoint.setZ(newZ);
89 void setFromPoint(qreal newX, qreal newY, qreal newZ)
91 fromPoint.setX(newX); fromPoint.setY(newY); fromPoint.setZ(newZ);
95 double amarkmag, tempmag;
99 dval = cos(angle/2.0)/sin(angle/2.0);
100 dist = atPoint-fromPoint;
101 amarkmag = dist.length();
104 temp.crossProduct( dist, up);
105 tempmag = temp.length();
108 temp.crossProduct( a1, a3);
109 tempmag = temp.length();
112 offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
113 offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
114 offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
115 //QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
116 //QMessageBox::about(0,"offs x y z", jono2);
118 Vector getAtPoint() { return atPoint; };
119 Vector getFromPoint() { return fromPoint; };
120 Vector getA1() { return a1; };
121 Vector getA2() { return a2; };
122 Vector getA3() { return a3; };
126 qreal xmax, xmin, ymin, ymax; // Limits in world coordinates
128 QList<Vector> vertexList; // Vertecies of route
130 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax; // data ranges
132 #define maxof(val1,val2) ((val1>val2)?val1:val2)
133 #define toradians( degrees) (degrees*0.017453293)
135 #define WIDTH 1.8 // For 3d viewing only
136 qreal a, b,c,d; // Used for 3d viewing to calculate screen coordinates
138 Viewing view3d; // Viewing settings for 3d
140 // Function prototypes
141 void dataMinMax( void);
142 void setAtPoint( Viewing *v);
143 void setFromPoint( Viewing *v);
144 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 );
146 #define R 6378.140 // The radius of the earth by kilometers
149 * count distance of two points (defined by longitude & latitude)
150 * on the surface of the earth.
152 qreal countDistance(Vector *p1, Vector *p2)
154 qreal dLon, dLat; // delta of longitude & latitude
157 dLon = p2->getX() - p1->getX(); // longitude difference
158 dLat = p2->getY() - p1->getY(); // latitude difference
159 if (dLon <0) dLon = -dLon;
160 if (dLat <0) dLat = -dLat;
162 dLon = dLon*3.14/180;
163 dLat = dLat*3.14/180;
164 a = (sin(dLat/2.))*(sin(dLat/2.)) +
165 (cos(p1->getY())*3.14/180)*(cos(p2->getY())*3.14/180)*(sin(dLon/2.))*(sin(dLon/2.));
166 c = 2.*atan(sqrt(a)/sqrt(1-a)); // c is angle between points p1 & p2 with circel by radius 1.
168 return R*c; // Return distance in kilometers
172 * Constructor of this class.
174 RouteDialog::RouteDialog(RouteSaveDialog *parent) :
175 QDialog(parent), ui(new Ui::RouteDialog)
177 qDebug() << "__RouteDialog";
180 this->setWindowTitle("Route");
181 left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates
183 helpRoutingDialog = NULL;
186 ui->sendPushButton->setAutoFillBackground(true);
187 ui->sendPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
190 ui->labelInfoToUser->setText("");
191 ui->speedValueLabel->setText("");
192 ui->avgSpeedValueLabel->setText("");
199 ui->avgSpeedValueLabel->setText(average.sprintf("%.1f", parent->getAverageSpeed()) + " km/h");
200 ui->distanceValueLabel->setText(parent->getDistanceTraveled() + " km");
204 * Destructor of this class.
206 RouteDialog::~RouteDialog()
208 qDebug() << "__~RouteDialog";
216 void RouteDialog::changeEvent(QEvent *e)
218 QDialog::changeEvent(e);
220 case QEvent::LanguageChange:
221 ui->retranslateUi(this);
231 int RouteDialog::getLeft()
239 int RouteDialog::getRight()
247 int RouteDialog::getTop()
255 int RouteDialog::getBottom()
263 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y, QString startFinish)
267 pg.setPoint(0,x, y-25);
268 pg.setPoint(1,x+10,y-20);
269 pg.setPoint(2,x, y-15);
270 pg.setPoint(3,x,y-20);*/
271 if (y> (rD->getTop() + 25))
274 p->drawLine(x,y,x,y-15);
275 if (x <= (rD->getRight()-20))
278 p->drawLine( x, y-25, x+10, y-20);
279 p->drawLine( x+10, y-20, x, y-15);
280 p->drawLine( x, y-15, x, y-25);
282 // Draw start or finish
283 p->drawText(x+10, y, startFinish);
288 p->drawLine( x, y-25, x-10, y-20);
289 p->drawLine( x-10, y-20, x, y-15);
290 p->drawLine( x, y-15, x, y-25);
292 // Draw start or finish
293 p->drawText(x+10, y, startFinish);
296 else if (y <= (rD->getTop() + 25))
299 p->drawLine(x,y,x,y+15);
300 if (x <= (rD->getRight()-20))
303 p->drawLine( x, y+25, x+10, y+20);
304 p->drawLine( x+10, y+20, x, y+15);
305 p->drawLine( x, y+15, x, y+25);
307 // Draw start or finish
308 p->drawText(x+10, y+15, startFinish);
313 p->drawLine( x, y+25, x-10, y+20);
314 p->drawLine( x-10, y+20, x, y+15);
315 p->drawLine( x, y+15, x, y+25);
317 // Draw start or finish
318 p->drawText(x+10, y+15, startFinish);
322 // p->drawPolygon( pg,Qt::OddEvenFill);
323 //p->drawPolyline( &pg);
324 //p->drawPoints( pg);
328 * Draws route to the route dialog.
329 * Type 0 is 2d viewing and type 1 is for 3d viewing
333 void RouteDialog::paintEvent(QPaintEvent *)
338 int type = 0; // 0 for 2d, 1 for 3d
339 int startx = 0; // Starting x point of the route
340 int starty = 0; // Starting y point of the route
343 qreal x1, y1, x2, y2;
349 QPainter painter(this);
351 painter.setRenderHint(QPainter::Antialiasing, true);
352 painter.setPen(QPen((Qt::white),2));
353 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
355 // Draw route window frame
356 /*painter.drawLine(left,top,right,top);
357 painter.drawLine(right,top,right,bottom);
358 painter.drawLine(left,top,left,bottom);
359 painter.drawLine(left,bottom,right,bottom);*/
361 maxi = vertexList.size();
363 for (i=0; i<maxi-1; i++)
365 v1 = vertexList.at(i);
366 v2 = vertexList.at(i+1);
370 x1 = v1.getX(); y1 = v1.getY();
371 x2 = v2.getX(); y2 = v2.getY();
372 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
373 //QMessageBox::about(0,"Tark",jono);
375 x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
376 y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
377 x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
378 y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
382 transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
385 // Show with circle if starting point
389 startx = x1Screen; starty = y1Screen;
390 // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
391 drawFlag( this, &painter, x1Screen, y1Screen, "Start" );
393 painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
395 // Show the endig point if different than the starting point
396 if (x2Screen != startx || y2Screen != starty)
398 //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
399 drawFlag( this, &painter, x2Screen, y2Screen, "Finish" );
403 qreal maxvx = 0; // max speed x point
404 qreal maxvy = 0; // max speed y point
405 qreal maxv = 0; // max speed
409 for (i=0; i<maxi-1; i++)
411 v = vertexList.at(i);
419 // Translate world coordinates to screen coordinates
420 x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
421 y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
423 // Show max velocity point by yellow circle
424 painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
425 painter.drawEllipse( ui->maxSpeedLabel->geometry().x()-15, ui->maxSpeedLabel->geometry().y()+15, 10, 10);
428 //jono = QString("%1 km/h").arg(maxv);
429 jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
430 ui->speedValueLabel->setText(jono);
436 * @param QString route file
438 bool RouteDialog::readRouteFromFile( QString &routeFile )
440 QString rFile = routeFile;
441 fileName = routeFile;
445 int progressbarInt = 5;
448 file.setFileName( rFile);
449 if (!file.open(QIODevice::ReadOnly))
451 QMessageBox::about(0, "Error", "Empty file");
454 emit progressbar(progressbarInt);
460 QString astr1, astr2, astr3, astr4;
461 QString str1, str2, str3, str4;
462 rivi = file.readLine();
467 astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
468 astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
469 astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
470 astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
474 str1 = astr1.section('"',1,1);
475 str2 = astr2.section('"',1,1);
476 str3 = astr3.section('"',1,1);
477 str4 = astr4.section('"',1,1);
479 if (str1.length() > 0)
481 x = str2.toDouble();// latitude y-value
482 y = str1.toDouble();// longitude x-value
483 z = str3.toDouble();// altitude z-value
484 v = str4.toDouble();// speed km/h
485 temp.setX( x); // Longitude
486 temp.setY( y); // Latitude
487 temp.setZ( z); // altitude
490 vertexList.append(temp);
498 if(progressbarInt < 50)
500 emit progressbar(progressbarInt);
505 if(count == 1) // Check is file empty.
508 emit progressbar(progressbarInt);
509 qDebug() << "count: " + QString::number(count);
510 /******** in 3d use only */
515 emit progressbar(60);
516 //angle = toradians(60);
517 view3d.setUp( 1.0, 0.0, 0.0);
518 emit progressbar(70);
519 view3d.setAngle(toradians(60));
520 emit progressbar(80);
521 setAtPoint( &view3d); // If file is empty software crash here.
522 emit progressbar(90);
523 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
524 emit progressbar(95);
525 setFromPoint( &view3d);
526 emit progressbar(98);
528 emit progressbar(99);
529 /****** end of 3d *****/
531 //Testing distance counting
534 //a1.setX( xmin); a1.setY( ymin);
535 //a2.setX( xmax); a2.setY( ymax);
536 a1.setX( 25.483); a1.setY( 65.017); // Oulu
537 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
538 dist = countDistance( &a1, &a2);
539 QString str = QString("Min & Max datan välimatka %1").arg(dist);
540 QMessageBox::about( 0, "Testi", str);
542 emit progressbar(100);
545 else // Probably file is empty.
547 QMessageBox::about(0, "Error", "Empty file");
553 * Find out data range for x-, y- and z-coordinates
555 void dataMinMax( void)
561 temp = vertexList.at(0);
562 objxmax = objxmin = temp.getX();
563 objymax = objymin = temp.getY();
564 objzmax = objzmin = temp.getZ();
566 maxi = vertexList.size();
567 for (i=1; i<maxi; i++)
569 temp = vertexList.at(i);
578 else if (x > objxmax)
587 else if (y > objymax)
596 else if (z > objzmax)
601 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
602 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
603 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
604 //QMessageBox::about(0,"Tark", jono);
608 * Setting the point where the viewed object is. In the middle of datapoints.
610 void setAtPoint( Viewing *v)
616 x = (objxmax+objxmin)/2.0;
617 y= (objymax+objymin)/2.0;
618 z= (objzmax+objzmin)/2.0;
620 v->setAtPoint( x, y, z);
621 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
622 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
625 test = v->getAtPoint();
626 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
627 QMessageBox::about(0,"At point", jono);
632 * Setting the point where the object is viewed by eye.
634 void setFromPoint( Viewing *v)
638 point = v->getAtPoint();
640 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
642 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
645 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
648 v->setFromPoint(x,y,z);
649 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
650 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
652 test = v->getFromPoint();
653 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
654 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
660 #define LEFTEDGE 0x01
661 #define RIGHTEDGE 0x02
662 #define BOTTOMEDGE 0x04
666 * Returns a code specifying which edge in the viewing pyramid was crossed.
667 * There may be more than one.
669 int code( qreal x, qreal y, qreal z)
674 if (x<-z) c |= LEFTEDGE;
675 if (x>z) c |= RIGHTEDGE;
676 if (y<-z) c |= BOTTOMEDGE;
677 if (y>z) c |= TOPEDGE;
683 * Converts clipped world coordinates to screen coordinates.
685 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
687 *xScreen = (int) (a*xWorld+b);
688 *yScreen = (int) (c*yWorld+d);
692 * Clips the line segment in three-dimensional coordinates to the
695 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
708 while (c1!= NOEDGE || c2 != NOEDGE)
710 if ((c1 & c2 ) != NOEDGE) return;
712 if (c == NOEDGE) c = c2;
713 if ((c&LEFTEDGE) == LEFTEDGE)
716 t = (z1+x1)/((x1-x2)-(z2-z1));
721 else if ((c&RIGHTEDGE) == RIGHTEDGE)
723 // Crosses right edge
724 t = (z1-x1)/((x2-x1)-(z2-z1));
729 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
731 // Crosses bottom edge
732 t = (z1+y1)/((y1-y2)-(z2-z1));
737 else if ((c&TOPEDGE) == TOPEDGE)
740 t = (z1-y1)/((y2-y1)-(z2-z1));
760 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
761 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
765 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
766 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
768 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
772 * Transform the segment connecting the two vectors into the viewing plane.
773 * clip3d() clips the line if needed.
775 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
778 qreal x1, y1, z1, x2, y2, z2;
785 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
786 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
787 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
789 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
790 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
791 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
793 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
797 * This slot function is called when ever send push button clicked.
799 void RouteDialog::on_sendPushButton_clicked()
801 ui->sendPushButton->setEnabled(false);
803 // Check default file dir.
804 if(!QDir("/home/user/MyDocs/speedfreak/route").exists())
806 QDir().mkdir("/home/user/MyDocs/speedfreak/route");
809 // If file name is empty set it to default.
811 fileName = "/home/user/MyDocs/speedfreak/route/";
813 if(fileName == "/home/user/MyDocs/speedfreak/route/routetemp.xml")
814 fileName = "/home/user/MyDocs/speedfreak/route/route.xml";
816 qDebug() << "__FileName: " + fileName;
817 QString newFileName = QFileDialog::getSaveFileName(this, tr("Save Route"), fileName);
818 int server = QMessageBox::question(this, "Save route to server?", "", 4,3);
820 if(server == 3) // Yes button
822 qDebug() << "__Save to server: yes";
823 emit sendroute(newFileName,1); // Save route to server and phone.
825 else if(server == 4) // No button
827 qDebug() << "__Save to server: no";
830 emit sendroute(newFileName,0); // Save route to phone.
835 * This function is set info text to user.
837 void RouteDialog::setLabelInfoToUser(QString infoText)
839 this->ui->labelInfoToUser->setText(infoText);
843 * This function enable send server button.
845 void RouteDialog::setSendServerButtonEnabled()
847 ui->sendPushButton->setEnabled(true);
851 * This function check login and set send route to server button disabled/enabled.
853 void RouteDialog::checkLogin()
857 ui->sendPushButton->setEnabled(true);
858 ui->labelInfoToUser->setText("");
862 ui->sendPushButton->setEnabled(false);
863 ui->labelInfoToUser->setText("You're not logged! Please register or log in.");
868 * This slot function called when ever info button clicked.
870 void RouteDialog::on_pushButtonInfo_clicked()
872 if(!helpRoutingDialog)
874 helpRoutingDialog = new HelpRoutingDialog;
876 connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
877 helpRoutingDialog->show();
881 * This slot function called when ever dialog rejected.
883 void RouteDialog::killHelpDialog()
885 if(helpRoutingDialog)
887 qDebug() << "__Route kill: helpRoutingDialog";
888 delete helpRoutingDialog;
889 helpRoutingDialog = NULL;