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;
98 dval = cos(angle/2.0)/sin(angle/2.0);
99 dist = atPoint-fromPoint;
100 amarkmag = dist.length();
103 temp.crossProduct( dist, up);
104 tempmag = temp.length();
107 temp.crossProduct( a1, a3);
108 tempmag = temp.length();
111 offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
112 offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
113 offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
114 //QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
115 //QMessageBox::about(0,"offs x y z", jono2);
117 Vector getAtPoint() { return atPoint; };
118 Vector getFromPoint() { return fromPoint; };
119 Vector getA1() { return a1; };
120 Vector getA2() { return a2; };
121 Vector getA3() { return a3; };
125 qreal xmax, xmin, ymin, ymax; // Limits in world coordinates
127 QList<Vector> vertexList; // Vertecies of route
129 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax; // data ranges
131 #define maxof(val1,val2) ((val1>val2)?val1:val2)
132 #define toradians( degrees) (degrees*0.017453293)
134 #define WIDTH 1.8 // For 3d viewing only
135 qreal a, b,c,d; // Used for 3d viewing to calculate screen coordinates
137 Viewing view3d; // Viewing settings for 3d
139 // Function prototypes
140 void dataMinMax( void);
141 void setAtPoint( Viewing *v);
142 void setFromPoint( Viewing *v);
143 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 );
145 #define R 6378.140 // The radius of the earth by kilometers
148 * count distance of two points (defined by longitude & latitude)
149 * on the surface of the earth.
151 qreal countDistance(Vector *p1, Vector *p2)
153 qreal dLon, dLat; // delta of longitude & latitude
156 dLon = p2->getX() - p1->getX(); // longitude difference
157 dLat = p2->getY() - p1->getY(); // latitude difference
158 if (dLon <0) dLon = -dLon;
159 if (dLat <0) dLat = -dLat;
161 dLon = dLon*3.14/180;
162 dLat = dLat*3.14/180;
163 a = (sin(dLat/2.))*(sin(dLat/2.)) +
164 (cos(p1->getY())*3.14/180)*(cos(p2->getY())*3.14/180)*(sin(dLon/2.))*(sin(dLon/2.));
165 c = 2.*atan(sqrt(a)/sqrt(1-a)); // c is angle between points p1 & p2 with circel by radius 1.
167 return R*c; // Return distance in kilometers
171 * Constructor of this class.
173 RouteDialog::RouteDialog(RouteSaveDialog *parent) :
174 QDialog(parent), ui(new Ui::RouteDialog)
176 qDebug() << "__RouteDialog";
179 this->setWindowTitle("Route");
180 left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates
182 helpRoutingDialog = NULL;
185 ui->sendPushButton->setAutoFillBackground(true);
186 ui->sendPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
187 ui->newPushButton->setAutoFillBackground(true);
188 ui->newPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
191 ui->labelInfoToUser->setText("");
192 ui->speedValueLabel->setText("");
193 ui->avgSpeedValueLabel->setText("");
200 ui->avgSpeedValueLabel->setText(average.sprintf("%.1f", parent->getAverageSpeed()) + " km/h");
201 ui->distanceValueLabel->setText(parent->getDistanceTraveled() + " km");
205 * Destructor of this class.
207 RouteDialog::~RouteDialog()
209 qDebug() << "__~RouteDialog";
217 void RouteDialog::changeEvent(QEvent *e)
219 QDialog::changeEvent(e);
221 case QEvent::LanguageChange:
222 ui->retranslateUi(this);
232 int RouteDialog::getLeft()
240 int RouteDialog::getRight()
248 int RouteDialog::getTop()
256 int RouteDialog::getBottom()
264 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y, QString startFinish)
268 pg.setPoint(0,x, y-25);
269 pg.setPoint(1,x+10,y-20);
270 pg.setPoint(2,x, y-15);
271 pg.setPoint(3,x,y-20);*/
272 if (y> (rD->getTop() + 25))
275 p->drawLine(x,y,x,y-15);
276 if (x <= (rD->getRight()-20))
279 p->drawLine( x, y-25, x+10, y-20);
280 p->drawLine( x+10, y-20, x, y-15);
281 p->drawLine( x, y-15, x, y-25);
283 // Draw start or finish
284 p->drawText(x+10, y, startFinish);
289 p->drawLine( x, y-25, x-10, y-20);
290 p->drawLine( x-10, y-20, x, y-15);
291 p->drawLine( x, y-15, x, y-25);
293 // Draw start or finish
294 p->drawText(x+10, y, startFinish);
297 else if (y <= (rD->getTop() + 25))
300 p->drawLine(x,y,x,y+15);
301 if (x <= (rD->getRight()-20))
304 p->drawLine( x, y+25, x+10, y+20);
305 p->drawLine( x+10, y+20, x, y+15);
306 p->drawLine( x, y+15, x, y+25);
308 // Draw start or finish
309 p->drawText(x+10, y+15, startFinish);
314 p->drawLine( x, y+25, x-10, y+20);
315 p->drawLine( x-10, y+20, x, y+15);
316 p->drawLine( x, y+15, x, y+25);
318 // Draw start or finish
319 p->drawText(x+10, y+15, startFinish);
323 // p->drawPolygon( pg,Qt::OddEvenFill);
324 //p->drawPolyline( &pg);
325 //p->drawPoints( pg);
329 * Draws route to the route dialog.
330 * Type 0 is 2d viewing and type 1 is for 3d viewing
334 void RouteDialog::paintEvent(QPaintEvent *)
339 int type = 0; // 0 for 2d, 1 for 3d
340 int startx, starty; // Starting point of the route
342 qreal x1, y1, x2, y2;
343 int x1Screen, y1Screen, x2Screen, y2Screen;
345 QPainter painter(this);
348 painter.setRenderHint(QPainter::Antialiasing, true);
349 painter.setPen(QPen((Qt::white),2));
350 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
352 // Draw route window frame
353 /*painter.drawLine(left,top,right,top);
354 painter.drawLine(right,top,right,bottom);
355 painter.drawLine(left,top,left,bottom);
356 painter.drawLine(left,bottom,right,bottom);*/
358 maxi = vertexList.size();
360 for (i=0; i<maxi-1; i++)
362 v1 = vertexList.at(i);
363 v2 = vertexList.at(i+1);
367 x1 = v1.getX(); y1 = v1.getY();
368 x2 = v2.getX(); y2 = v2.getY();
369 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
370 //QMessageBox::about(0,"Tark",jono);
372 x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
373 y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
374 x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
375 y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
379 transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
382 // Show with circle if starting point
386 startx = x1Screen; starty = y1Screen;
387 // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
388 drawFlag( this, &painter, x1Screen, y1Screen, "Start" );
390 painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
392 // Show the endig point if different than the starting point
393 if (x2Screen != startx || y2Screen != starty)
395 //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
396 drawFlag( this, &painter, x2Screen, y2Screen, "Finish" );
400 qreal maxvx, maxvy; // max speed point coordinates
401 qreal maxv; // max speed
405 for (i=0; i<maxi-1; i++)
407 v = vertexList.at(i);
415 // Translate world coordinates to screen coordinates
416 x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
417 y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
419 // Show max velocity point by yellow circle
420 painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
421 painter.drawEllipse( ui->maxSpeedLabel->geometry().x()-15, ui->maxSpeedLabel->geometry().y()+15, 10, 10);
424 //jono = QString("%1 km/h").arg(maxv);
425 jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
426 ui->speedValueLabel->setText(jono);
433 bool RouteDialog::readRouteFromFile( QString &routeFile , CalibrateDialog *calibrateDialog)
435 QString rFile = routeFile; //Not used
440 progresbar = calibrateDialog;
441 int progresbarValue = 0;
442 progresbar->setProgressValue(++progresbarValue);
444 //QString fileName = QFileDialog::getOpenFileName(this,
445 // tr("Read Route"), "./", tr("Route Files (*.txt)"));
447 //file.setFileName( fileName);
448 file.setFileName( "routetemp.xml");
449 if (!file.open(QIODevice::ReadOnly))
451 QMessageBox::about(0, "Error", "File not found");
452 progresbar->setProgressValue(100);
462 QString astr1, astr2, astr3, astr4;
463 QString str1, str2, str3, str4;
464 rivi = file.readLine();
470 astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
471 astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
472 astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
473 astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
477 str1 = astr1.section('"',1,1);
478 str2 = astr2.section('"',1,1);
479 str3 = astr3.section('"',1,1);
480 str4 = astr4.section('"',1,1);
481 //QString str = QString("%1 %2 %3 %4").arg(str1).arg(str2).arg(str3).arg(str4);
482 //QMessageBox::about(0, "LUKEE", str);
485 if (str1.length() > 0)
487 x = str2.toDouble();// latitude y-value
488 y = str1.toDouble();// longitude x-value
489 z = str3.toDouble();// altitude z-value
490 v = str4.toDouble();// speed km/h
491 // QString str = QString("%1 %2 %3 %4").arg(x).arg(y).arg(z).arg(v);
492 // QMessageBox::about(0, "LUKEE", str);
493 temp.setX( x); // Longitude
494 temp.setY( y); // Latitude
495 temp.setZ( z); // altitude
498 vertexList.append(temp);
509 str1 = rivi.section(" ", 0, 0);
510 if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
512 str1 = rivi.section(" ", 2, 2); // latitude y-value
513 str2 = rivi.section(" ", 4, 4); // longitude x-value
514 str3 = rivi.section(" ", 6, 6); // altitude z-value
515 str4 = rivi.section(" ", 8, 8); // speed km/h
516 //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
517 //QMessageBox::about(0, "LUKEE", str);
519 if (str1.length() > 0)
526 temp.setX( x); // Longitude
527 temp.setY( y); // Latitude
528 temp.setZ( z); // altitude
531 vertexList.append(temp);
539 /******** in 3d use only */
544 //angle = toradians(60);
546 view3d.setUp( 1.0, 0.0, 0.0);
547 view3d.setAngle(toradians(60));
548 setAtPoint( &view3d);
549 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
550 setFromPoint( &view3d);
552 /****** end of 3d *****/
555 //Testing distance counting
558 //a1.setX( xmin); a1.setY( ymin);
559 //a2.setX( xmax); a2.setY( ymax);
560 a1.setX( 25.483); a1.setY( 65.017); // Oulu
561 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
562 dist = countDistance( &a1, &a2);
563 QString str = QString("Min & Max datan välimatka %1").arg(dist);
564 QMessageBox::about( 0, "Testi", str);
571 * Find out data range for x-, y- and z-coordinates
573 void dataMinMax( void)
579 temp = vertexList.at(0);
580 objxmax = objxmin = temp.getX();
581 objymax = objymin = temp.getY();
582 objzmax = objzmin = temp.getZ();
584 maxi = vertexList.size();
585 for (i=1; i<maxi; i++)
587 temp = vertexList.at(i);
596 else if (x > objxmax)
605 else if (y > objymax)
614 else if (z > objzmax)
619 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
620 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
621 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
622 //QMessageBox::about(0,"Tark", jono);
626 * Setting the point where the viewed object is. In the middle of datapoints.
628 void setAtPoint( Viewing *v)
634 x = (objxmax+objxmin)/2.0;
635 y= (objymax+objymin)/2.0;
636 z= (objzmax+objzmin)/2.0;
638 v->setAtPoint( x, y, z);
639 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
640 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
643 test = v->getAtPoint();
644 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
645 QMessageBox::about(0,"At point", jono);
650 * Setting the point where the object is viewed by eye.
652 void setFromPoint( Viewing *v)
656 point = v->getAtPoint();
658 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
660 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
663 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
666 v->setFromPoint(x,y,z);
667 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
668 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
670 test = v->getFromPoint();
671 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
672 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
678 #define LEFTEDGE 0x01
679 #define RIGHTEDGE 0x02
680 #define BOTTOMEDGE 0x04
684 * Returns a code specifying which edge in the viewing pyramid was crossed.
685 * There may be more than one.
687 int code( qreal x, qreal y, qreal z)
692 if (x<-z) c |= LEFTEDGE;
693 if (x>z) c |= RIGHTEDGE;
694 if (y<-z) c |= BOTTOMEDGE;
695 if (y>z) c |= TOPEDGE;
701 * Converts clipped world coordinates to screen coordinates.
703 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
705 *xScreen = (int) (a*xWorld+b);
706 *yScreen = (int) (c*yWorld+d);
710 * Clips the line segment in three-dimensional coordinates to the
713 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
721 while (c1!= NOEDGE || c2 != NOEDGE)
723 if ((c1 & c2 ) != NOEDGE) return;
725 if (c == NOEDGE) c = c2;
726 if ((c&LEFTEDGE) == LEFTEDGE)
729 t = (z1+x1)/((x1-x2)-(z2-z1));
734 else if ((c&RIGHTEDGE) == RIGHTEDGE)
736 // Crosses right edge
737 t = (z1-x1)/((x2-x1)-(z2-z1));
742 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
744 // Crosses bottom edge
745 t = (z1+y1)/((y1-y2)-(z2-z1));
750 else if ((c&TOPEDGE) == TOPEDGE)
753 t = (z1-y1)/((y2-y1)-(z2-z1));
773 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
774 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
778 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
779 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
781 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
785 * Transform the segment connecting the two vectors into the viewing plane.
786 * clip3d() clips the line if needed.
788 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
791 qreal x1, y1, z1, x2, y2, z2;
798 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
799 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
800 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
802 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
803 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
804 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
806 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
810 * This slot function is called when ever new push button clicked.
812 void RouteDialog::on_newPushButton_clicked()
814 close(); // go back to previous dialog
818 * This slot function is called when ever send push button clicked.
820 void RouteDialog::on_sendPushButton_clicked()
822 ui->sendPushButton->setEnabled(false);
827 * This function is set info text to user.
829 void RouteDialog::setLabelInfoToUser(QString infoText)
831 this->ui->labelInfoToUser->setText(infoText);
835 * This function enable send server button.
837 void RouteDialog::setSendServerButtonEnabled()
839 ui->sendPushButton->setEnabled(true);
843 * This function check login and set send route to server button disabled/enabled.
845 void RouteDialog::checkLogin()
849 ui->sendPushButton->setEnabled(true);
850 ui->labelInfoToUser->setText("");
854 ui->sendPushButton->setEnabled(false);
855 ui->labelInfoToUser->setText("You're not logged! Please register or log in.");
860 * This slot function called when ever info button clicked.
862 void RouteDialog::on_pushButtonInfo_clicked()
864 if(!helpRoutingDialog)
866 helpRoutingDialog = new HelpRoutingDialog;
868 connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
869 helpRoutingDialog->show();
873 * This slot function called when ever dialog rejected.
875 void RouteDialog::killHelpDialog()
877 if(helpRoutingDialog)
879 qDebug() << "__Route kill: helpRoutingDialog";
880 delete helpRoutingDialog;
881 helpRoutingDialog = NULL;