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;
73 Vector atPoint, fromPoint, up, a1, a2, a3;
74 qreal offsx, offsy, offsz;
78 qreal getOffsx() { return offsx; };
79 qreal getOffsy() { return offsy; };
80 qreal getOffsz() { return offsz; };
81 qreal getDval() { return dval; };
82 void setAngle( qreal newA) { angle = newA; };
83 void setUp( qreal newUpX, qreal newUpY, qreal newUpZ)
85 up.setX(newUpX); up.setY(newUpY); up.setZ(newUpZ);
87 void setAtPoint( qreal newX, qreal newY, qreal newZ)
89 atPoint.setX(newX); atPoint.setY(newY); atPoint.setZ(newZ);
91 void setFromPoint(qreal newX, qreal newY, qreal newZ)
93 fromPoint.setX(newX); fromPoint.setY(newY); fromPoint.setZ(newZ);
97 double amarkmag, tempmag;
101 dval = cos(angle/2.0)/sin(angle/2.0);
102 dist = atPoint-fromPoint;
103 amarkmag = dist.length();
106 temp.crossProduct( dist, up);
107 tempmag = temp.length();
110 temp.crossProduct( a1, a3);
111 tempmag = temp.length();
114 offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
115 offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
116 offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
117 //QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
118 //QMessageBox::about(0,"offs x y z", jono2);
120 Vector getAtPoint() { return atPoint; };
121 Vector getFromPoint() { return fromPoint; };
122 Vector getA1() { return a1; };
123 Vector getA2() { return a2; };
124 Vector getA3() { return a3; };
128 qreal xmax, xmin, ymin, ymax; // Limits in world coordinates
130 QList<Vector> vertexList; // Vertecies of route
132 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax; // data ranges
134 #define maxof(val1,val2) ((val1>val2)?val1:val2)
135 #define toradians( degrees) (degrees*0.017453293)
137 #define WIDTH 1.8 // For 3d viewing only
138 qreal a, b,c,d; // Used for 3d viewing to calculate screen coordinates
140 Viewing view3d; // Viewing settings for 3d
142 // Function prototypes
143 void dataMinMax( void);
144 void setAtPoint( Viewing *v);
145 void setFromPoint( Viewing *v);
146 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 );
148 #define R 6378.140 // The radius of the earth by kilometers
151 * count distance of two points (defined by longitude & latitude)
152 * on the surface of the earth.
154 qreal countDistance(Vector *p1, Vector *p2)
156 qreal dLon, dLat; // delta of longitude & latitude
159 dLon = p2->getX() - p1->getX(); // longitude difference
160 dLat = p2->getY() - p1->getY(); // latitude difference
161 if (dLon <0) dLon = -dLon;
162 if (dLat <0) dLat = -dLat;
164 dLon = dLon*3.14/180;
165 dLat = dLat*3.14/180;
166 a = (sin(dLat/2.))*(sin(dLat/2.)) +
167 (cos(p1->getY())*3.14/180)*(cos(p2->getY())*3.14/180)*(sin(dLon/2.))*(sin(dLon/2.));
168 c = 2.*atan(sqrt(a)/sqrt(1-a)); // c is angle between points p1 & p2 with circel by radius 1.
170 return R*c; // Return distance in kilometers
174 * Constructor of this class.
176 RouteDialog::RouteDialog(RouteSaveDialog *parent) :
177 QDialog(parent), ui(new Ui::RouteDialog)
179 qDebug() << "__RouteDialog";
182 this->setWindowTitle("Route");
183 left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates
185 helpRoutingDialog = NULL;
188 ui->sendPushButton->setAutoFillBackground(true);
189 ui->sendPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
192 ui->labelInfoToUser->setText("");
193 ui->speedValueLabel->setText("");
194 ui->avgSpeedValueLabel->setText("");
201 ui->avgSpeedValueLabel->setText(average.sprintf("%.1f", parent->getAverageSpeed()) + " km/h");
202 ui->distanceValueLabel->setText(parent->getDistanceTraveled() + " km");
206 * Destructor of this class.
208 RouteDialog::~RouteDialog()
210 qDebug() << "__~RouteDialog";
218 void RouteDialog::changeEvent(QEvent *e)
220 QDialog::changeEvent(e);
222 case QEvent::LanguageChange:
223 ui->retranslateUi(this);
235 int RouteDialog::getLeft()
245 int RouteDialog::getRight()
255 int RouteDialog::getTop()
265 int RouteDialog::getBottom()
273 * @param RouteDialog*
277 * @param QString startFinish
279 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y, QString startFinish)
281 if (y> (rD->getTop() + 25))
284 p->drawLine(x,y,x,y-15);
285 if (x <= (rD->getRight()-20))
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);
298 p->drawLine( x, y-25, x-10, y-20);
299 p->drawLine( x-10, y-20, x, y-15);
300 p->drawLine( x, y-15, x, y-25);
302 // Draw start or finish
303 p->drawText(x+10, y, startFinish);
306 else if (y <= (rD->getTop() + 25))
309 p->drawLine(x,y,x,y+15);
310 if (x <= (rD->getRight()-20))
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);
323 p->drawLine( x, y+25, x-10, y+20);
324 p->drawLine( x-10, y+20, x, y+15);
325 p->drawLine( x, y+15, x, y+25);
327 // Draw start or finish
328 p->drawText(x+10, y+15, startFinish);
334 * Draws route to the route dialog.
335 * Type 0 is 2d viewing and type 1 is for 3d viewing
337 * @param QPaintEvent*
340 void RouteDialog::paintEvent(QPaintEvent *)
345 int type = 0; // 0 for 2d, 1 for 3d
346 int startx = 0; // Starting x point of the route
347 int starty = 0; // Starting y point of the route
350 qreal x1, y1, x2, y2;
356 QPainter painter(this);
358 painter.setRenderHint(QPainter::Antialiasing, true);
359 painter.setPen(QPen((Qt::white),2));
360 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
362 maxi = vertexList.size();
364 for (i=0; i<maxi-1; i++)
366 v1 = vertexList.at(i);
367 v2 = vertexList.at(i+1);
371 x1 = v1.getX(); y1 = v1.getY();
372 x2 = v2.getX(); y2 = v2.getY();
373 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
374 //QMessageBox::about(0,"Tark",jono);
376 x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
377 y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
378 x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
379 y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
383 transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
386 // Show with circle if starting point
390 startx = x1Screen; starty = y1Screen;
391 // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
392 drawFlag( this, &painter, x1Screen, y1Screen, "Start" );
394 painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
396 // Show the endig point if different than the starting point
397 if (x2Screen != startx || y2Screen != starty)
399 //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
400 drawFlag( this, &painter, x2Screen, y2Screen, "Finish" );
404 qreal maxvx = 0; // max speed x point
405 qreal maxvy = 0; // max speed y point
406 qreal maxv = 0; // max speed
410 for (i=0; i<maxi-1; i++)
412 v = vertexList.at(i);
420 // Translate world coordinates to screen coordinates
421 x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
422 y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
424 // Show max velocity point by yellow circle
425 painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
426 painter.drawEllipse( ui->maxSpeedLabel->geometry().x()-15, ui->maxSpeedLabel->geometry().y()+15, 10, 10);
429 //jono = QString("%1 km/h").arg(maxv);
430 jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
431 ui->speedValueLabel->setText(jono);
436 * read route from file
438 * @param QString route file
440 bool RouteDialog::readRouteFromFile( QString &routeFile )
442 QString rFile = routeFile;
443 fileName = routeFile;
447 int progressbarInt = 5;
450 file.setFileName( rFile);
451 if (!file.open(QIODevice::ReadOnly))
453 QMessageBox::about(0, "Error", "Empty file");
456 emit progressbar(progressbarInt);
462 QString astr1, astr2, astr3, astr4;
463 QString str1, str2, str3, str4;
464 rivi = file.readLine();
469 astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
470 astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
471 astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
472 astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
476 str1 = astr1.section('"',1,1);
477 str2 = astr2.section('"',1,1);
478 str3 = astr3.section('"',1,1);
479 str4 = astr4.section('"',1,1);
481 if (str1.length() > 0)
483 x = str2.toDouble();// latitude y-value
484 y = str1.toDouble();// longitude x-value
485 z = str3.toDouble();// altitude z-value
486 v = str4.toDouble();// speed km/h
487 temp.setX( x); // Longitude
488 temp.setY( y); // Latitude
489 temp.setZ( z); // altitude
492 vertexList.append(temp);
500 if(progressbarInt < 50)
502 emit progressbar(progressbarInt);
507 if(count == 1) // Check is file empty.
510 emit progressbar(progressbarInt);
511 qDebug() << "count: " + QString::number(count);
512 /******** in 3d use only */
517 emit progressbar(60);
518 //angle = toradians(60);
519 view3d.setUp( 1.0, 0.0, 0.0);
520 emit progressbar(70);
521 view3d.setAngle(toradians(60));
522 emit progressbar(80);
523 setAtPoint( &view3d); // If file is empty software crash here.
524 emit progressbar(90);
525 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
526 emit progressbar(95);
527 setFromPoint( &view3d);
528 emit progressbar(98);
530 emit progressbar(99);
531 /****** end of 3d *****/
533 //Testing distance counting
536 //a1.setX( xmin); a1.setY( ymin);
537 //a2.setX( xmax); a2.setY( ymax);
538 a1.setX( 25.483); a1.setY( 65.017); // Oulu
539 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
540 dist = countDistance( &a1, &a2);
541 QString str = QString("Min & Max datan välimatka %1").arg(dist);
542 QMessageBox::about( 0, "Testi", str);
544 emit progressbar(100);
547 else // Probably file is empty.
549 QMessageBox::about(0, "Error", "Empty file");
555 * Find out data range for x-, y- and z-coordinates
557 void dataMinMax( void)
563 temp = vertexList.at(0);
564 objxmax = objxmin = temp.getX();
565 objymax = objymin = temp.getY();
566 objzmax = objzmin = temp.getZ();
568 maxi = vertexList.size();
569 for (i=1; i<maxi; i++)
571 temp = vertexList.at(i);
580 else if (x > objxmax)
589 else if (y > objymax)
598 else if (z > objzmax)
603 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
604 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
605 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
606 //QMessageBox::about(0,"Tark", jono);
610 * Setting the point where the viewed object is. In the middle of datapoints.
612 void setAtPoint( Viewing *v)
618 x = (objxmax+objxmin)/2.0;
619 y= (objymax+objymin)/2.0;
620 z= (objzmax+objzmin)/2.0;
622 v->setAtPoint( x, y, z);
623 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
624 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
627 test = v->getAtPoint();
628 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
629 QMessageBox::about(0,"At point", jono);
634 * Setting the point where the object is viewed by eye.
636 void setFromPoint( Viewing *v)
640 point = v->getAtPoint();
642 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
644 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
647 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
650 v->setFromPoint(x,y,z);
651 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
652 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
654 test = v->getFromPoint();
655 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
656 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
661 #define LEFTEDGE 0x01
662 #define RIGHTEDGE 0x02
663 #define BOTTOMEDGE 0x04
667 * Returns a code specifying which edge in the viewing pyramid was crossed.
668 * There may be more than one.
675 int code( qreal x, qreal y, qreal z)
679 if (x<-z) c |= LEFTEDGE;
680 if (x>z) c |= RIGHTEDGE;
681 if (y<-z) c |= BOTTOMEDGE;
682 if (y>z) c |= TOPEDGE;
687 * Converts clipped world coordinates to screen coordinates.
689 * @param qreal xWorld
690 * @param qreal yWorld
691 * @param int *xScreen
692 * @param int *yScreen
694 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
696 *xScreen = (int) (a*xWorld+b);
697 *yScreen = (int) (c*yWorld+d);
701 * Clips the line segment in three-dimensional coordinates to the
710 * @param int *xscreen1
711 * @param int *yscreen1
712 * @param int *xscreen2
713 * @param int *yscreen2
715 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
728 while (c1!= NOEDGE || c2 != NOEDGE)
730 if ((c1 & c2 ) != NOEDGE) return;
732 if (c == NOEDGE) c = c2;
733 if ((c&LEFTEDGE) == LEFTEDGE)
736 t = (z1+x1)/((x1-x2)-(z2-z1));
741 else if ((c&RIGHTEDGE) == RIGHTEDGE)
743 // Crosses right edge
744 t = (z1-x1)/((x2-x1)-(z2-z1));
749 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
751 // Crosses bottom edge
752 t = (z1+y1)/((y1-y2)-(z2-z1));
757 else if ((c&TOPEDGE) == TOPEDGE)
760 t = (z1-y1)/((y2-y1)-(z2-z1));
780 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
781 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
785 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
786 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
788 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
792 * Transform the segment connecting the two vectors into the viewing plane.
793 * clip3d() clips the line if needed.
798 * @param int *xscreen1
799 * @param int *yscreen1
800 * @param int *xscreen2
801 * @param int *yscreen2
803 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
806 qreal x1, y1, z1, x2, y2, z2;
813 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
814 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
815 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
817 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
818 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
819 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
821 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
825 * This slot function is called when ever send push button clicked.
827 void RouteDialog::on_sendPushButton_clicked()
829 ui->sendPushButton->setEnabled(false);
831 // Check default file dir.
832 if(!QDir("/home/user/MyDocs/speedfreak/route").exists())
834 QDir().mkdir("/home/user/MyDocs/speedfreak/route");
837 // If file name is empty set it to default.
839 fileName = "/home/user/MyDocs/speedfreak/route/";
841 if(fileName == "/home/user/MyDocs/speedfreak/route/routetemp.xml")
842 fileName = "/home/user/MyDocs/speedfreak/route/route.xml";
844 qDebug() << "__FileName: " + fileName;
845 QString newFileName = QFileDialog::getSaveFileName(this, tr("Save Route"), fileName);
846 int server = QMessageBox::question(this, "Save route to server?", "", 4,3);
848 if(server == 3) // Yes button
850 qDebug() << "__Save to server: yes";
851 emit sendroute(newFileName,1); // Save route to server and phone.
853 else if(server == 4) // No button
855 qDebug() << "__Save to server: no";
858 emit sendroute(newFileName,0); // Save route to phone.
863 * This function is set info text to user.
865 * @param QString info text
867 void RouteDialog::setLabelInfoToUser(QString infoText)
869 this->ui->labelInfoToUser->setText(infoText);
873 * This function enable send server button.
875 void RouteDialog::setSendServerButtonEnabled()
877 ui->sendPushButton->setEnabled(true);
881 * This function check login and set send route to server button disabled/enabled.
883 void RouteDialog::checkLogin()
887 ui->sendPushButton->setEnabled(true);
888 ui->labelInfoToUser->setText("");
892 ui->sendPushButton->setEnabled(false);
893 ui->labelInfoToUser->setText("You're not logged! Please register or log in.");
898 * This slot function called when ever info button clicked.
900 void RouteDialog::on_pushButtonInfo_clicked()
902 if(!helpRoutingDialog)
904 helpRoutingDialog = new HelpRoutingDialog;
906 connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
907 helpRoutingDialog->show();
911 * This slot function called when ever dialog rejected.
913 void RouteDialog::killHelpDialog()
915 if(helpRoutingDialog)
917 qDebug() << "__Route kill: helpRoutingDialog";
918 delete helpRoutingDialog;
919 helpRoutingDialog = NULL;