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";
213 delete calibrateDialog;
219 void RouteDialog::changeEvent(QEvent *e)
221 QDialog::changeEvent(e);
223 case QEvent::LanguageChange:
224 ui->retranslateUi(this);
234 int RouteDialog::getLeft()
242 int RouteDialog::getRight()
250 int RouteDialog::getTop()
258 int RouteDialog::getBottom()
266 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y, QString startFinish)
270 pg.setPoint(0,x, y-25);
271 pg.setPoint(1,x+10,y-20);
272 pg.setPoint(2,x, y-15);
273 pg.setPoint(3,x,y-20);*/
274 if (y> (rD->getTop() + 25))
277 p->drawLine(x,y,x,y-15);
278 if (x <= (rD->getRight()-20))
281 p->drawLine( x, y-25, x+10, y-20);
282 p->drawLine( x+10, y-20, x, y-15);
283 p->drawLine( x, y-15, x, y-25);
285 // Draw start or finish
286 p->drawText(x+10, y, startFinish);
291 p->drawLine( x, y-25, x-10, y-20);
292 p->drawLine( x-10, y-20, x, y-15);
293 p->drawLine( x, y-15, x, y-25);
295 // Draw start or finish
296 p->drawText(x+10, y, startFinish);
299 else if (y <= (rD->getTop() + 25))
302 p->drawLine(x,y,x,y+15);
303 if (x <= (rD->getRight()-20))
306 p->drawLine( x, y+25, x+10, y+20);
307 p->drawLine( x+10, y+20, x, y+15);
308 p->drawLine( x, y+15, x, y+25);
310 // Draw start or finish
311 p->drawText(x+10, y+15, startFinish);
316 p->drawLine( x, y+25, x-10, y+20);
317 p->drawLine( x-10, y+20, x, y+15);
318 p->drawLine( x, y+15, x, y+25);
320 // Draw start or finish
321 p->drawText(x+10, y+15, startFinish);
325 // p->drawPolygon( pg,Qt::OddEvenFill);
326 //p->drawPolyline( &pg);
327 //p->drawPoints( pg);
331 * Draws route to the route dialog.
332 * Type 0 is 2d viewing and type 1 is for 3d viewing
336 void RouteDialog::paintEvent(QPaintEvent *)
341 int type = 0; // 0 for 2d, 1 for 3d
342 int startx, starty; // Starting point of the route
344 qreal x1, y1, x2, y2;
345 int x1Screen, y1Screen, x2Screen, y2Screen;
347 QPainter painter(this);
350 painter.setRenderHint(QPainter::Antialiasing, true);
351 painter.setPen(QPen((Qt::white),2));
352 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
354 // Draw route window frame
355 /*painter.drawLine(left,top,right,top);
356 painter.drawLine(right,top,right,bottom);
357 painter.drawLine(left,top,left,bottom);
358 painter.drawLine(left,bottom,right,bottom);*/
360 maxi = vertexList.size();
362 for (i=0; i<maxi-1; i++)
364 v1 = vertexList.at(i);
365 v2 = vertexList.at(i+1);
369 x1 = v1.getX(); y1 = v1.getY();
370 x2 = v2.getX(); y2 = v2.getY();
371 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
372 //QMessageBox::about(0,"Tark",jono);
374 x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
375 y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
376 x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
377 y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
381 transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
384 // Show with circle if starting point
388 startx = x1Screen; starty = y1Screen;
389 // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
390 drawFlag( this, &painter, x1Screen, y1Screen, "Start" );
392 painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
394 // Show the endig point if different than the starting point
395 if (x2Screen != startx || y2Screen != starty)
397 //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
398 drawFlag( this, &painter, x2Screen, y2Screen, "Finish" );
402 qreal maxvx, maxvy; // max speed point coordinates
403 qreal maxv; // max speed
407 for (i=0; i<maxi-1; i++)
409 v = vertexList.at(i);
417 // Translate world coordinates to screen coordinates
418 x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
419 y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
421 // Show max velocity point by yellow circle
422 painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
423 painter.drawEllipse( ui->maxSpeedLabel->geometry().x()-15, ui->maxSpeedLabel->geometry().y()+15, 10, 10);
426 //jono = QString("%1 km/h").arg(maxv);
427 jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
428 ui->speedValueLabel->setText(jono);
435 bool RouteDialog::readRouteFromFile( QString &routeFile , CalibrateDialog *calibrateDialog)
437 QString rFile = routeFile; //Not used
442 progresbar = calibrateDialog;
443 int progresbarValue = 0;
444 progresbar->setProgressValue(++progresbarValue);
446 //QString fileName = QFileDialog::getOpenFileName(this,
447 // tr("Read Route"), "./", tr("Route Files (*.txt)"));
449 //file.setFileName( fileName);
450 file.setFileName( "routetemp.xml");
451 if (!file.open(QIODevice::ReadOnly))
453 QMessageBox::about(0, "Error", "File not found");
454 progresbar->setProgressValue(100);
464 QString astr1, astr2, astr3, astr4;
465 QString str1, str2, str3, str4;
466 rivi = file.readLine();
472 astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
473 astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
474 astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
475 astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
479 str1 = astr1.section('"',1,1);
480 str2 = astr2.section('"',1,1);
481 str3 = astr3.section('"',1,1);
482 str4 = astr4.section('"',1,1);
483 //QString str = QString("%1 %2 %3 %4").arg(str1).arg(str2).arg(str3).arg(str4);
484 //QMessageBox::about(0, "LUKEE", str);
487 if (str1.length() > 0)
489 x = str2.toDouble();// latitude y-value
490 y = str1.toDouble();// longitude x-value
491 z = str3.toDouble();// altitude z-value
492 v = str4.toDouble();// speed km/h
493 // QString str = QString("%1 %2 %3 %4").arg(x).arg(y).arg(z).arg(v);
494 // QMessageBox::about(0, "LUKEE", str);
495 temp.setX( x); // Longitude
496 temp.setY( y); // Latitude
497 temp.setZ( z); // altitude
500 vertexList.append(temp);
511 str1 = rivi.section(" ", 0, 0);
512 if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
514 str1 = rivi.section(" ", 2, 2); // latitude y-value
515 str2 = rivi.section(" ", 4, 4); // longitude x-value
516 str3 = rivi.section(" ", 6, 6); // altitude z-value
517 str4 = rivi.section(" ", 8, 8); // speed km/h
518 //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
519 //QMessageBox::about(0, "LUKEE", str);
521 if (str1.length() > 0)
528 temp.setX( x); // Longitude
529 temp.setY( y); // Latitude
530 temp.setZ( z); // altitude
533 vertexList.append(temp);
541 /******** in 3d use only */
546 //angle = toradians(60);
548 view3d.setUp( 1.0, 0.0, 0.0);
549 view3d.setAngle(toradians(60));
550 setAtPoint( &view3d);
551 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
552 setFromPoint( &view3d);
554 /****** end of 3d *****/
557 //Testing distance counting
560 //a1.setX( xmin); a1.setY( ymin);
561 //a2.setX( xmax); a2.setY( ymax);
562 a1.setX( 25.483); a1.setY( 65.017); // Oulu
563 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
564 dist = countDistance( &a1, &a2);
565 QString str = QString("Min & Max datan välimatka %1").arg(dist);
566 QMessageBox::about( 0, "Testi", str);
573 * Find out data range for x-, y- and z-coordinates
575 void dataMinMax( void)
581 temp = vertexList.at(0);
582 objxmax = objxmin = temp.getX();
583 objymax = objymin = temp.getY();
584 objzmax = objzmin = temp.getZ();
586 maxi = vertexList.size();
587 for (i=1; i<maxi; i++)
589 temp = vertexList.at(i);
598 else if (x > objxmax)
607 else if (y > objymax)
616 else if (z > objzmax)
621 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
622 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
623 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
624 //QMessageBox::about(0,"Tark", jono);
628 * Setting the point where the viewed object is. In the middle of datapoints.
630 void setAtPoint( Viewing *v)
636 x = (objxmax+objxmin)/2.0;
637 y= (objymax+objymin)/2.0;
638 z= (objzmax+objzmin)/2.0;
640 v->setAtPoint( x, y, z);
641 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
642 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
645 test = v->getAtPoint();
646 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
647 QMessageBox::about(0,"At point", jono);
652 * Setting the point where the object is viewed by eye.
654 void setFromPoint( Viewing *v)
658 point = v->getAtPoint();
660 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
662 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
665 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
668 v->setFromPoint(x,y,z);
669 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
670 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
672 test = v->getFromPoint();
673 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
674 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
680 #define LEFTEDGE 0x01
681 #define RIGHTEDGE 0x02
682 #define BOTTOMEDGE 0x04
686 * Returns a code specifying which edge in the viewing pyramid was crossed.
687 * There may be more than one.
689 int code( qreal x, qreal y, qreal z)
694 if (x<-z) c |= LEFTEDGE;
695 if (x>z) c |= RIGHTEDGE;
696 if (y<-z) c |= BOTTOMEDGE;
697 if (y>z) c |= TOPEDGE;
703 * Converts clipped world coordinates to screen coordinates.
705 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
707 *xScreen = (int) (a*xWorld+b);
708 *yScreen = (int) (c*yWorld+d);
712 * Clips the line segment in three-dimensional coordinates to the
715 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
723 while (c1!= NOEDGE || c2 != NOEDGE)
725 if ((c1 & c2 ) != NOEDGE) return;
727 if (c == NOEDGE) c = c2;
728 if ((c&LEFTEDGE) == LEFTEDGE)
731 t = (z1+x1)/((x1-x2)-(z2-z1));
736 else if ((c&RIGHTEDGE) == RIGHTEDGE)
738 // Crosses right edge
739 t = (z1-x1)/((x2-x1)-(z2-z1));
744 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
746 // Crosses bottom edge
747 t = (z1+y1)/((y1-y2)-(z2-z1));
752 else if ((c&TOPEDGE) == TOPEDGE)
755 t = (z1-y1)/((y2-y1)-(z2-z1));
775 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
776 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
780 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
781 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
783 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
787 * Transform the segment connecting the two vectors into the viewing plane.
788 * clip3d() clips the line if needed.
790 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
793 qreal x1, y1, z1, x2, y2, z2;
800 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
801 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
802 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
804 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
805 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
806 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
808 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
812 * This slot function is called when ever new push button clicked.
814 void RouteDialog::on_newPushButton_clicked()
816 close(); // go back to previous dialog
820 * This slot function is called when ever send push button clicked.
822 void RouteDialog::on_sendPushButton_clicked()
824 ui->sendPushButton->setEnabled(false);
829 * This function is set info text to user.
831 void RouteDialog::setLabelInfoToUser(QString infoText)
833 this->ui->labelInfoToUser->setText(infoText);
837 * This function enable send server button.
839 void RouteDialog::setSendServerButtonEnabled()
841 ui->sendPushButton->setEnabled(true);
845 * This function check login and set send route to server button disabled/enabled.
847 void RouteDialog::checkLogin()
851 ui->sendPushButton->setEnabled(true);
852 ui->labelInfoToUser->setText("");
856 ui->sendPushButton->setEnabled(false);
857 ui->labelInfoToUser->setText("You're not logged! Please register or log in.");
862 * This slot function called when ever info button clicked.
864 void RouteDialog::on_pushButtonInfo_clicked()
866 if(!helpRoutingDialog)
868 helpRoutingDialog = new HelpRoutingDialog;
870 connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
871 helpRoutingDialog->show();
875 * This slot function called when ever dialog rejected.
877 void RouteDialog::killHelpDialog()
879 if(helpRoutingDialog)
881 qDebug() << "__Route kill: helpRoutingDialog";
882 delete helpRoutingDialog;
883 helpRoutingDialog = NULL;