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 "routedialog.h"
11 #include "ui_routedialog.h"
12 #include "usersettings.h"
16 #include <QMessageBox>
18 #include <QFileDialog>
24 * In starting Qt 4.6 there is QVector3D.
25 * Later (updating Qt version) this class can be removed.
29 qreal x, y, z; // Location
32 Vector() { x=0.; y=0. ; z=0.; };
33 Vector( qreal initX, qreal initY, qreal initZ) { x = initX, y = initY; z = initZ; };
34 void setX( qreal newX) { x = newX; };
35 void setY( qreal newY) { y = newY; };
36 void setZ( qreal newZ) { z = newZ; };
37 void setV( qreal newV) { v = newV; };
38 qreal getX() { return x; };
39 qreal getY() { return y; };
40 qreal getZ() { return z; };
41 qreal getV() { return v; };
42 qreal length() { return sqrt(x*x+y*y+z*z); };
43 Vector operator+(Vector v)
45 x = x + v.x; y = y + v.y; z = z + v.z;
48 Vector operator-(Vector v)
50 x = x - v.x; y = y - v.y; z = z - v.z;
53 Vector operator/(qreal c)
55 x = x/c; y = y/c; z = z/c;
58 Vector crossProduct( Vector a, Vector b)
60 x = a.y*b.z - a.z*b.y;
61 y = a.z*b.x - a.x*b.z;
62 z = a.x*b.y - a.y*b.x;
70 Vector atPoint, fromPoint, up, a1, a2, a3;
71 qreal offsx, offsy, offsz;
75 qreal getOffsx() { return offsx; };
76 qreal getOffsy() { return offsy; };
77 qreal getOffsz() { return offsz; };
78 qreal getDval() { return dval; };
79 void setAngle( qreal newA) { angle = newA; };
80 void setUp( qreal newUpX, qreal newUpY, qreal newUpZ)
82 up.setX(newUpX); up.setY(newUpY); up.setZ(newUpZ);
84 void setAtPoint( qreal newX, qreal newY, qreal newZ)
86 atPoint.setX(newX); atPoint.setY(newY); atPoint.setZ(newZ);
88 void setFromPoint(qreal newX, qreal newY, qreal newZ)
90 fromPoint.setX(newX); fromPoint.setY(newY); fromPoint.setZ(newZ);
94 double amarkmag, tempmag;
97 dval = cos(angle/2.0)/sin(angle/2.0);
98 dist = atPoint-fromPoint;
99 amarkmag = dist.length();
102 temp.crossProduct( dist, up);
103 tempmag = temp.length();
106 temp.crossProduct( a1, a3);
107 tempmag = temp.length();
110 offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
111 offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
112 offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
113 //QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
114 //QMessageBox::about(0,"offs x y z", jono2);
116 Vector getAtPoint() { return atPoint; };
117 Vector getFromPoint() { return fromPoint; };
118 Vector getA1() { return a1; };
119 Vector getA2() { return a2; };
120 Vector getA3() { return a3; };
124 qreal xmax, xmin, ymin, ymax; // Limits in world coordinates
126 QList<Vector> vertexList; // Vertecies of route
128 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax; // data ranges
130 #define maxof(val1,val2) ((val1>val2)?val1:val2)
131 #define toradians( degrees) (degrees*0.017453293)
133 #define WIDTH 1.8 // For 3d viewing only
134 qreal a, b,c,d; // Used for 3d viewing to calculate screen coordinates
136 Viewing view3d; // Viewing settings for 3d
138 // Function prototypes
139 void dataMinMax( void);
140 void setAtPoint( Viewing *v);
141 void setFromPoint( Viewing *v);
142 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 );
144 #define R 6378.140 // The radius of the earth by kilometers
147 * count distance of two points (defined by longitude & latitude)
148 * on the surface of the earth.
150 qreal countDistance(Vector *p1, Vector *p2)
152 qreal dLon, dLat; // delta of longitude & latitude
155 dLon = p2->getX() - p1->getX(); // longitude difference
156 dLat = p2->getY() - p1->getY(); // latitude difference
157 if (dLon <0) dLon = -dLon;
158 if (dLat <0) dLat = -dLat;
160 dLon = dLon*3.14/180;
161 dLat = dLat*3.14/180;
162 a = (sin(dLat/2.))*(sin(dLat/2.)) +
163 (cos(p1->getY())*3.14/180)*(cos(p2->getY())*3.14/180)*(sin(dLon/2.))*(sin(dLon/2.));
164 c = 2.*atan(sqrt(a)/sqrt(1-a)); // c is angle between points p1 & p2 with circel by radius 1.
166 return R*c; // Return distance in kilometers
170 * Constructor of this class.
172 RouteDialog::RouteDialog(QWidget *parent) :
173 QDialog(parent), ui(new Ui::RouteDialog)
175 qDebug() << "__RouteDialog";
178 helpRoutingDialog = NULL;
180 this->setWindowTitle("Route");
181 left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates
184 ui->sendPushButton->setAutoFillBackground(true);
185 ui->sendPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
186 ui->newPushButton->setAutoFillBackground(true);
187 ui->newPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
190 ui->labelInfoToUser->setText("");
191 ui->speedValueLabel->setText("");
198 * Destructor of this class.
200 RouteDialog::~RouteDialog()
202 qDebug() << "__~RouteDialog";
210 void RouteDialog::changeEvent(QEvent *e)
212 QDialog::changeEvent(e);
214 case QEvent::LanguageChange:
215 ui->retranslateUi(this);
225 int RouteDialog::getLeft()
233 int RouteDialog::getRight()
241 int RouteDialog::getTop()
249 int RouteDialog::getBottom()
257 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y, QString startFinish)
261 pg.setPoint(0,x, y-25);
262 pg.setPoint(1,x+10,y-20);
263 pg.setPoint(2,x, y-15);
264 pg.setPoint(3,x,y-20);*/
265 if (y> (rD->getTop() + 25))
268 p->drawLine(x,y,x,y-15);
269 if (x <= (rD->getRight()-20))
272 p->drawLine( x, y-25, x+10, y-20);
273 p->drawLine( x+10, y-20, x, y-15);
274 p->drawLine( x, y-15, x, y-25);
276 // Draw start or finish
277 p->drawText(x+10, y, startFinish);
282 p->drawLine( x, y-25, x-10, y-20);
283 p->drawLine( x-10, y-20, x, y-15);
284 p->drawLine( x, y-15, x, y-25);
286 // Draw start or finish
287 p->drawText(x+10, y, startFinish);
290 else if (y <= (rD->getTop() + 25))
293 p->drawLine(x,y,x,y+15);
294 if (x <= (rD->getRight()-20))
297 p->drawLine( x, y+25, x+10, y+20);
298 p->drawLine( x+10, y+20, x, y+15);
299 p->drawLine( x, y+15, x, y+25);
301 // Draw start or finish
302 p->drawText(x+10, y+15, startFinish);
307 p->drawLine( x, y+25, x-10, y+20);
308 p->drawLine( x-10, y+20, x, y+15);
309 p->drawLine( x, y+15, x, y+25);
311 // Draw start or finish
312 p->drawText(x+10, y+15, startFinish);
316 // p->drawPolygon( pg,Qt::OddEvenFill);
317 //p->drawPolyline( &pg);
318 //p->drawPoints( pg);
322 * Draws route to the route dialog.
323 * Type 0 is 2d viewing and type 1 is for 3d viewing
327 void RouteDialog::paintEvent(QPaintEvent *)
332 int type = 0; // 0 for 2d, 1 for 3d
333 int startx, starty; // Starting point of the route
335 qreal x1, y1, x2, y2;
336 int x1Screen, y1Screen, x2Screen, y2Screen;
338 QPainter painter(this);
341 painter.setRenderHint(QPainter::Antialiasing, true);
342 painter.setPen(QPen((Qt::white),2));
343 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
345 // Draw route window frame
346 /*painter.drawLine(left,top,right,top);
347 painter.drawLine(right,top,right,bottom);
348 painter.drawLine(left,top,left,bottom);
349 painter.drawLine(left,bottom,right,bottom);*/
351 maxi = vertexList.size();
353 for (i=0; i<maxi-1; i++)
355 v1 = vertexList.at(i);
356 v2 = vertexList.at(i+1);
360 x1 = v1.getX(); y1 = v1.getY();
361 x2 = v2.getX(); y2 = v2.getY();
362 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
363 //QMessageBox::about(0,"Tark",jono);
365 x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
366 y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
367 x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
368 y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
372 transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
375 // Show with circle if starting point
379 startx = x1Screen; starty = y1Screen;
380 // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
381 drawFlag( this, &painter, x1Screen, y1Screen, "Start" );
383 painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
385 // Show the endig point if different than the starting point
386 if (x2Screen != startx || y2Screen != starty)
388 //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
389 drawFlag( this, &painter, x2Screen, y2Screen, "Finish" );
393 qreal maxvx, maxvy; // max speed point coordinates
394 qreal maxv; // max speed
398 for (i=0; i<maxi-1; i++)
400 v = vertexList.at(i);
408 // Translate world coordinates to screen coordinates
409 x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
410 y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
412 // Show max velocity point by yellow circle
413 painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
414 painter.drawEllipse( 650, 225, 10, 10);
417 //jono = QString("%1 km/h").arg(maxv);
418 jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
419 ui->speedValueLabel->setText(jono);
426 bool RouteDialog::readRouteFromFile( QString &routeFile)
428 QString rFile = routeFile; //Not used
433 //QString fileName = QFileDialog::getOpenFileName(this,
434 // tr("Read Route"), "./", tr("Route Files (*.txt)"));
436 //file.setFileName( fileName);
437 file.setFileName( "routetemp.xml");
438 if (!file.open(QIODevice::ReadOnly))
440 QMessageBox::about(0, "Error", "File not found");
450 QString astr1, astr2, astr3, astr4;
451 QString str1, str2, str3, str4;
452 rivi = file.readLine();
458 astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
459 astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
460 astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
461 astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
465 str1 = astr1.section('"',1,1);
466 str2 = astr2.section('"',1,1);
467 str3 = astr3.section('"',1,1);
468 str4 = astr4.section('"',1,1);
469 //QString str = QString("%1 %2 %3 %4").arg(str1).arg(str2).arg(str3).arg(str4);
470 //QMessageBox::about(0, "LUKEE", str);
473 if (str1.length() > 0)
475 x = str2.toDouble();// latitude y-value
476 y = str1.toDouble();// longitude x-value
477 z = str3.toDouble();// altitude z-value
478 v = str4.toDouble();// speed km/h
479 // QString str = QString("%1 %2 %3 %4").arg(x).arg(y).arg(z).arg(v);
480 // QMessageBox::about(0, "LUKEE", str);
481 temp.setX( x); // Longitude
482 temp.setY( y); // Latitude
483 temp.setZ( z); // altitude
486 vertexList.append(temp);
497 str1 = rivi.section(" ", 0, 0);
498 if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
500 str1 = rivi.section(" ", 2, 2); // latitude y-value
501 str2 = rivi.section(" ", 4, 4); // longitude x-value
502 str3 = rivi.section(" ", 6, 6); // altitude z-value
503 str4 = rivi.section(" ", 8, 8); // speed km/h
504 //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
505 //QMessageBox::about(0, "LUKEE", str);
507 if (str1.length() > 0)
514 temp.setX( x); // Longitude
515 temp.setY( y); // Latitude
516 temp.setZ( z); // altitude
519 vertexList.append(temp);
527 /******** in 3d use only */
532 //angle = toradians(60);
534 view3d.setUp( 1.0, 0.0, 0.0);
535 view3d.setAngle(toradians(60));
536 setAtPoint( &view3d);
537 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
538 setFromPoint( &view3d);
540 /****** end of 3d *****/
543 //Testing distance counting
546 //a1.setX( xmin); a1.setY( ymin);
547 //a2.setX( xmax); a2.setY( ymax);
548 a1.setX( 25.483); a1.setY( 65.017); // Oulu
549 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
550 dist = countDistance( &a1, &a2);
551 QString str = QString("Min & Max datan välimatka %1").arg(dist);
552 QMessageBox::about( 0, "Testi", str);
559 * Find out data range for x-, y- and z-coordinates
561 void dataMinMax( void)
567 temp = vertexList.at(0);
568 objxmax = objxmin = temp.getX();
569 objymax = objymin = temp.getY();
570 objzmax = objzmin = temp.getZ();
572 maxi = vertexList.size();
573 for (i=1; i<maxi; i++)
575 temp = vertexList.at(i);
584 else if (x > objxmax)
593 else if (y > objymax)
602 else if (z > objzmax)
607 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
608 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
609 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
610 //QMessageBox::about(0,"Tark", jono);
614 * Setting the point where the viewed object is. In the middle of datapoints.
616 void setAtPoint( Viewing *v)
622 x = (objxmax+objxmin)/2.0;
623 y= (objymax+objymin)/2.0;
624 z= (objzmax+objzmin)/2.0;
626 v->setAtPoint( x, y, z);
627 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
628 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
631 test = v->getAtPoint();
632 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
633 QMessageBox::about(0,"At point", jono);
638 * Setting the point where the object is viewed by eye.
640 void setFromPoint( Viewing *v)
644 point = v->getAtPoint();
646 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
648 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
651 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
654 v->setFromPoint(x,y,z);
655 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
656 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
658 test = v->getFromPoint();
659 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
660 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
666 #define LEFTEDGE 0x01
667 #define RIGHTEDGE 0x02
668 #define BOTTOMEDGE 0x04
672 * Returns a code specifying which edge in the viewing pyramid was crossed.
673 * There may be more than one.
675 int code( qreal x, qreal y, qreal z)
680 if (x<-z) c |= LEFTEDGE;
681 if (x>z) c |= RIGHTEDGE;
682 if (y<-z) c |= BOTTOMEDGE;
683 if (y>z) c |= TOPEDGE;
689 * Converts clipped world coordinates to screen coordinates.
691 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
693 *xScreen = (int) (a*xWorld+b);
694 *yScreen = (int) (c*yWorld+d);
698 * Clips the line segment in three-dimensional coordinates to the
701 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
709 while (c1!= NOEDGE || c2 != NOEDGE)
711 if ((c1 & c2 ) != NOEDGE) return;
713 if (c == NOEDGE) c = c2;
714 if ((c&LEFTEDGE) == LEFTEDGE)
717 t = (z1+x1)/((x1-x2)-(z2-z1));
722 else if ((c&RIGHTEDGE) == RIGHTEDGE)
724 // Crosses right edge
725 t = (z1-x1)/((x2-x1)-(z2-z1));
730 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
732 // Crosses bottom edge
733 t = (z1+y1)/((y1-y2)-(z2-z1));
738 else if ((c&TOPEDGE) == TOPEDGE)
741 t = (z1-y1)/((y2-y1)-(z2-z1));
761 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
762 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
766 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
767 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
769 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
773 * Transform the segment connecting the two vectors into the viewing plane.
774 * clip3d() clips the line if needed.
776 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
779 qreal x1, y1, z1, x2, y2, z2;
786 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
787 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
788 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
790 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
791 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
792 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
794 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
798 * This slot function is called when ever new push button clicked.
800 void RouteDialog::on_newPushButton_clicked()
802 close(); // go back to previous dialog
806 * This slot function is called when ever send push button clicked.
808 void RouteDialog::on_sendPushButton_clicked()
810 ui->sendPushButton->setEnabled(false);
815 * This function is set info text to user.
817 void RouteDialog::setLabelInfoToUser(QString infoText)
819 this->ui->labelInfoToUser->setText(infoText);
823 * This function enable send server button.
825 void RouteDialog::setSendServerButtonEnabled()
827 ui->sendPushButton->setEnabled(true);
831 * This function check login and set send route to server button disabled/enabled.
833 void RouteDialog::checkLogin()
837 ui->sendPushButton->setEnabled(true);
838 ui->labelInfoToUser->setText("");
842 ui->sendPushButton->setEnabled(false);
843 ui->labelInfoToUser->setText("You're not logged! Please register or log in.");
848 * This slot function called when ever info button clicked.
850 void RouteDialog::on_pushButtonInfo_clicked()
852 if(!helpRoutingDialog)
854 helpRoutingDialog = new HelpRoutingDialog;
856 connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
857 helpRoutingDialog->show();
861 * This slot function called when ever dialog rejected.
863 void RouteDialog::killHelpDialog()
865 if(helpRoutingDialog)
867 qDebug() << "__Route kill: helpRoutingDialog";
868 delete helpRoutingDialog;
869 helpRoutingDialog = NULL;