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)");
189 ui->labelInfoToUser->setText("");
190 ui->speedValueLabel->setText("");
191 ui->avgSpeedValueLabel->setText("");
198 ui->avgSpeedValueLabel->setText(average.sprintf("%.1f", parent->getAverageSpeed()) + " km/h");
199 ui->distanceValueLabel->setText(parent->getDistanceTraveled() + " km");
203 * Destructor of this class.
205 RouteDialog::~RouteDialog()
207 qDebug() << "__~RouteDialog";
215 void RouteDialog::changeEvent(QEvent *e)
217 QDialog::changeEvent(e);
219 case QEvent::LanguageChange:
220 ui->retranslateUi(this);
230 int RouteDialog::getLeft()
238 int RouteDialog::getRight()
246 int RouteDialog::getTop()
254 int RouteDialog::getBottom()
262 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y, QString startFinish)
266 pg.setPoint(0,x, y-25);
267 pg.setPoint(1,x+10,y-20);
268 pg.setPoint(2,x, y-15);
269 pg.setPoint(3,x,y-20);*/
270 if (y> (rD->getTop() + 25))
273 p->drawLine(x,y,x,y-15);
274 if (x <= (rD->getRight()-20))
277 p->drawLine( x, y-25, x+10, y-20);
278 p->drawLine( x+10, y-20, x, y-15);
279 p->drawLine( x, y-15, x, y-25);
281 // Draw start or finish
282 p->drawText(x+10, y, startFinish);
287 p->drawLine( x, y-25, x-10, y-20);
288 p->drawLine( x-10, y-20, x, y-15);
289 p->drawLine( x, y-15, x, y-25);
291 // Draw start or finish
292 p->drawText(x+10, y, startFinish);
295 else if (y <= (rD->getTop() + 25))
298 p->drawLine(x,y,x,y+15);
299 if (x <= (rD->getRight()-20))
302 p->drawLine( x, y+25, x+10, y+20);
303 p->drawLine( x+10, y+20, x, y+15);
304 p->drawLine( x, y+15, x, y+25);
306 // Draw start or finish
307 p->drawText(x+10, y+15, startFinish);
312 p->drawLine( x, y+25, x-10, y+20);
313 p->drawLine( x-10, y+20, x, y+15);
314 p->drawLine( x, y+15, x, y+25);
316 // Draw start or finish
317 p->drawText(x+10, y+15, startFinish);
321 // p->drawPolygon( pg,Qt::OddEvenFill);
322 //p->drawPolyline( &pg);
323 //p->drawPoints( pg);
327 * Draws route to the route dialog.
328 * Type 0 is 2d viewing and type 1 is for 3d viewing
332 void RouteDialog::paintEvent(QPaintEvent *)
337 int type = 0; // 0 for 2d, 1 for 3d
338 int startx, starty; // Starting point of the route
340 qreal x1, y1, x2, y2;
341 int x1Screen, y1Screen, x2Screen, y2Screen;
343 QPainter painter(this);
346 painter.setRenderHint(QPainter::Antialiasing, true);
347 painter.setPen(QPen((Qt::white),2));
348 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
350 // Draw route window frame
351 /*painter.drawLine(left,top,right,top);
352 painter.drawLine(right,top,right,bottom);
353 painter.drawLine(left,top,left,bottom);
354 painter.drawLine(left,bottom,right,bottom);*/
356 maxi = vertexList.size();
358 for (i=0; i<maxi-1; i++)
360 v1 = vertexList.at(i);
361 v2 = vertexList.at(i+1);
365 x1 = v1.getX(); y1 = v1.getY();
366 x2 = v2.getX(); y2 = v2.getY();
367 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
368 //QMessageBox::about(0,"Tark",jono);
370 x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
371 y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
372 x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
373 y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
377 transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
380 // Show with circle if starting point
384 startx = x1Screen; starty = y1Screen;
385 // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
386 drawFlag( this, &painter, x1Screen, y1Screen, "Start" );
388 painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
390 // Show the endig point if different than the starting point
391 if (x2Screen != startx || y2Screen != starty)
393 //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
394 drawFlag( this, &painter, x2Screen, y2Screen, "Finish" );
398 qreal maxvx, maxvy; // max speed point coordinates
399 qreal maxv; // max speed
403 for (i=0; i<maxi-1; i++)
405 v = vertexList.at(i);
413 // Translate world coordinates to screen coordinates
414 x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
415 y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
417 // Show max velocity point by yellow circle
418 painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
419 painter.drawEllipse( ui->maxSpeedLabel->geometry().x()-15, ui->maxSpeedLabel->geometry().y()+15, 10, 10);
422 //jono = QString("%1 km/h").arg(maxv);
423 jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
424 ui->speedValueLabel->setText(jono);
431 bool RouteDialog::readRouteFromFile( QString &routeFile )
433 QString rFile = routeFile; //Not used
438 file.setFileName( "routetemp.xml");
439 if (!file.open(QIODevice::ReadOnly))
441 QMessageBox::about(0, "Error", "File not found");
446 emit progressbar(50);
449 //qDebug() << "__jee ";
453 QString astr1, astr2, astr3, astr4;
454 QString str1, str2, str3, str4;
455 rivi = file.readLine();
460 astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
461 astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
462 astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
463 astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
467 str1 = astr1.section('"',1,1);
468 str2 = astr2.section('"',1,1);
469 str3 = astr3.section('"',1,1);
470 str4 = astr4.section('"',1,1);
472 if (str1.length() > 0)
474 x = str2.toDouble();// latitude y-value
475 y = str1.toDouble();// longitude x-value
476 z = str3.toDouble();// altitude z-value
477 v = str4.toDouble();// speed km/h
478 temp.setX( x); // Longitude
479 temp.setY( y); // Latitude
480 temp.setZ( z); // altitude
483 vertexList.append(temp);
496 /******** in 3d use only */
501 //angle = toradians(60);
503 view3d.setUp( 1.0, 0.0, 0.0);
504 view3d.setAngle(toradians(60));
505 setAtPoint( &view3d);
506 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
507 setFromPoint( &view3d);
509 /****** end of 3d *****/
512 //Testing distance counting
515 //a1.setX( xmin); a1.setY( ymin);
516 //a2.setX( xmax); a2.setY( ymax);
517 a1.setX( 25.483); a1.setY( 65.017); // Oulu
518 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
519 dist = countDistance( &a1, &a2);
520 QString str = QString("Min & Max datan välimatka %1").arg(dist);
521 QMessageBox::about( 0, "Testi", str);
523 emit progressbar(100);
528 * Find out data range for x-, y- and z-coordinates
530 void dataMinMax( void)
536 temp = vertexList.at(0);
537 objxmax = objxmin = temp.getX();
538 objymax = objymin = temp.getY();
539 objzmax = objzmin = temp.getZ();
541 maxi = vertexList.size();
542 for (i=1; i<maxi; i++)
544 temp = vertexList.at(i);
553 else if (x > objxmax)
562 else if (y > objymax)
571 else if (z > objzmax)
576 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
577 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
578 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
579 //QMessageBox::about(0,"Tark", jono);
583 * Setting the point where the viewed object is. In the middle of datapoints.
585 void setAtPoint( Viewing *v)
591 x = (objxmax+objxmin)/2.0;
592 y= (objymax+objymin)/2.0;
593 z= (objzmax+objzmin)/2.0;
595 v->setAtPoint( x, y, z);
596 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
597 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
600 test = v->getAtPoint();
601 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
602 QMessageBox::about(0,"At point", jono);
607 * Setting the point where the object is viewed by eye.
609 void setFromPoint( Viewing *v)
613 point = v->getAtPoint();
615 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
617 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
620 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
623 v->setFromPoint(x,y,z);
624 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
625 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
627 test = v->getFromPoint();
628 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
629 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
635 #define LEFTEDGE 0x01
636 #define RIGHTEDGE 0x02
637 #define BOTTOMEDGE 0x04
641 * Returns a code specifying which edge in the viewing pyramid was crossed.
642 * There may be more than one.
644 int code( qreal x, qreal y, qreal z)
649 if (x<-z) c |= LEFTEDGE;
650 if (x>z) c |= RIGHTEDGE;
651 if (y<-z) c |= BOTTOMEDGE;
652 if (y>z) c |= TOPEDGE;
658 * Converts clipped world coordinates to screen coordinates.
660 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
662 *xScreen = (int) (a*xWorld+b);
663 *yScreen = (int) (c*yWorld+d);
667 * Clips the line segment in three-dimensional coordinates to the
670 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
678 while (c1!= NOEDGE || c2 != NOEDGE)
680 if ((c1 & c2 ) != NOEDGE) return;
682 if (c == NOEDGE) c = c2;
683 if ((c&LEFTEDGE) == LEFTEDGE)
686 t = (z1+x1)/((x1-x2)-(z2-z1));
691 else if ((c&RIGHTEDGE) == RIGHTEDGE)
693 // Crosses right edge
694 t = (z1-x1)/((x2-x1)-(z2-z1));
699 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
701 // Crosses bottom edge
702 t = (z1+y1)/((y1-y2)-(z2-z1));
707 else if ((c&TOPEDGE) == TOPEDGE)
710 t = (z1-y1)/((y2-y1)-(z2-z1));
730 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
731 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
735 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
736 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
738 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
742 * Transform the segment connecting the two vectors into the viewing plane.
743 * clip3d() clips the line if needed.
745 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
748 qreal x1, y1, z1, x2, y2, z2;
755 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
756 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
757 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
759 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
760 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
761 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
763 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
767 * This slot function is called when ever send push button clicked.
769 void RouteDialog::on_sendPushButton_clicked()
771 ui->sendPushButton->setEnabled(false);
776 * This function is set info text to user.
778 void RouteDialog::setLabelInfoToUser(QString infoText)
780 this->ui->labelInfoToUser->setText(infoText);
784 * This function enable send server button.
786 void RouteDialog::setSendServerButtonEnabled()
788 ui->sendPushButton->setEnabled(true);
792 * This function check login and set send route to server button disabled/enabled.
794 void RouteDialog::checkLogin()
798 ui->sendPushButton->setEnabled(true);
799 ui->labelInfoToUser->setText("");
803 ui->sendPushButton->setEnabled(false);
804 ui->labelInfoToUser->setText("You're not logged! Please register or log in.");
809 * This slot function called when ever info button clicked.
811 void RouteDialog::on_pushButtonInfo_clicked()
813 if(!helpRoutingDialog)
815 helpRoutingDialog = new HelpRoutingDialog;
817 connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
818 helpRoutingDialog->show();
822 * This slot function called when ever dialog rejected.
824 void RouteDialog::killHelpDialog()
826 if(helpRoutingDialog)
828 qDebug() << "__Route kill: helpRoutingDialog";
829 delete helpRoutingDialog;
830 helpRoutingDialog = NULL;