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";
177 this->setWindowTitle("Route");
178 left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates
181 ui->sendPushButton->setAutoFillBackground(true);
182 ui->sendPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
183 ui->newPushButton->setAutoFillBackground(true);
184 ui->newPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
187 ui->labelInfoToUser->setText("");
188 ui->speedValueLabel->setText("");
195 * Destructor of this class.
197 RouteDialog::~RouteDialog()
199 qDebug() << "__~RouteDialog";
207 void RouteDialog::changeEvent(QEvent *e)
209 QDialog::changeEvent(e);
211 case QEvent::LanguageChange:
212 ui->retranslateUi(this);
222 int RouteDialog::getLeft()
230 int RouteDialog::getRight()
238 int RouteDialog::getTop()
246 int RouteDialog::getBottom()
254 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y)
258 pg.setPoint(0,x, y-25);
259 pg.setPoint(1,x+10,y-20);
260 pg.setPoint(2,x, y-15);
261 pg.setPoint(3,x,y-20);*/
262 if (y> (rD->getTop() + 25))
265 p->drawLine(x,y,x,y-15);
266 if (x <= (rD->getRight()-20))
269 p->drawLine( x, y-25, x+10, y-20);
270 p->drawLine( x+10, y-20, x, y-15);
271 p->drawLine( x, y-15, x, y-25);
276 p->drawLine( x, y-25, x-10, y-20);
277 p->drawLine( x-10, y-20, x, y-15);
278 p->drawLine( x, y-15, x, y-25);
282 else if (y <= (rD->getTop() + 25))
285 p->drawLine(x,y,x,y+15);
286 if (x <= (rD->getRight()-20))
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);
296 p->drawLine( x, y+25, x-10, y+20);
297 p->drawLine( x-10, y+20, x, y+15);
298 p->drawLine( x, y+15, x, y+25);
302 // p->drawPolygon( pg,Qt::OddEvenFill);
303 //p->drawPolyline( &pg);
304 //p->drawPoints( pg);
308 * Draws route to the route dialog.
309 * Type 0 is 2d viewing and type 1 is for 3d viewing
313 void RouteDialog::paintEvent(QPaintEvent *)
318 int type = 0; // 0 for 2d, 1 for 3d
319 int startx, starty; // Starting point of the route
321 qreal x1, y1, x2, y2;
322 int x1Screen, y1Screen, x2Screen, y2Screen;
324 QPainter painter(this);
326 painter.setRenderHint(QPainter::Antialiasing, true);
327 painter.setPen(QPen((Qt::white),2));
328 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
330 // Draw route window frame
331 /*painter.drawLine(left,top,right,top);
332 painter.drawLine(right,top,right,bottom);
333 painter.drawLine(left,top,left,bottom);
334 painter.drawLine(left,bottom,right,bottom);*/
336 maxi = vertexList.size();
338 for (i=0; i<maxi-1; i++)
340 v1 = vertexList.at(i);
341 v2 = vertexList.at(i+1);
345 x1 = v1.getX(); y1 = v1.getY();
346 x2 = v2.getX(); y2 = v2.getY();
347 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
348 //QMessageBox::about(0,"Tark",jono);
350 x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
351 y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
352 x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
353 y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
357 transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
360 // Show with circle if starting point
364 startx = x1Screen; starty = y1Screen;
365 // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
366 drawFlag( this, &painter, x1Screen , y1Screen);
369 painter.drawText(x1Screen+10, y1Screen, "Start");
371 painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
373 // Show the endig point if different than the starting point
374 if (x2Screen != startx || y2Screen != starty)
376 //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
377 drawFlag( this, &painter,x2Screen, y2Screen );
380 painter.drawText(x2Screen+10, y2Screen, "Finish");
384 qreal maxvx, maxvy; // max speed point coordinates
385 qreal maxv; // max speed
389 for (i=0; i<maxi-1; i++)
391 v = vertexList.at(i);
399 // Translate world coordinates to screen coordinates
400 x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
401 y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
403 // Show max velocity point by yellow circle
404 painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
405 painter.drawEllipse( 650, 225, 10, 10);
408 //jono = QString("%1 km/h").arg(maxv);
409 jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
410 ui->speedValueLabel->setText(jono);
417 bool RouteDialog::readRouteFromFile( QString &routeFile)
419 QString rFile = routeFile; //Not used
424 //QString fileName = QFileDialog::getOpenFileName(this,
425 // tr("Read Route"), "./", tr("Route Files (*.txt)"));
427 //file.setFileName( fileName);
428 file.setFileName( "routetemp.xml");
429 if (!file.open(QIODevice::ReadOnly))
431 QMessageBox::about(0, "Error", "File not found");
441 QString astr1, astr2, astr3, astr4;
442 QString str1, str2, str3, str4;
443 rivi = file.readLine();
449 astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
450 astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
451 astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
452 astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
456 str1 = astr1.section('"',1,1);
457 str2 = astr2.section('"',1,1);
458 str3 = astr3.section('"',1,1);
459 str4 = astr4.section('"',1,1);
460 //QString str = QString("%1 %2 %3 %4").arg(str1).arg(str2).arg(str3).arg(str4);
461 //QMessageBox::about(0, "LUKEE", str);
464 if (str1.length() > 0)
466 x = str2.toDouble();// latitude y-value
467 y = str1.toDouble();// longitude x-value
468 z = str3.toDouble();// altitude z-value
469 v = str4.toDouble();// speed km/h
470 // QString str = QString("%1 %2 %3 %4").arg(x).arg(y).arg(z).arg(v);
471 // QMessageBox::about(0, "LUKEE", str);
472 temp.setX( x); // Longitude
473 temp.setY( y); // Latitude
474 temp.setZ( z); // altitude
477 vertexList.append(temp);
488 str1 = rivi.section(" ", 0, 0);
489 if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
491 str1 = rivi.section(" ", 2, 2); // latitude y-value
492 str2 = rivi.section(" ", 4, 4); // longitude x-value
493 str3 = rivi.section(" ", 6, 6); // altitude z-value
494 str4 = rivi.section(" ", 8, 8); // speed km/h
495 //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
496 //QMessageBox::about(0, "LUKEE", str);
498 if (str1.length() > 0)
505 temp.setX( x); // Longitude
506 temp.setY( y); // Latitude
507 temp.setZ( z); // altitude
510 vertexList.append(temp);
518 /******** in 3d use only */
523 //angle = toradians(60);
525 view3d.setUp( 1.0, 0.0, 0.0);
526 view3d.setAngle(toradians(60));
527 setAtPoint( &view3d);
528 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
529 setFromPoint( &view3d);
531 /****** end of 3d *****/
534 //Testing distance counting
537 //a1.setX( xmin); a1.setY( ymin);
538 //a2.setX( xmax); a2.setY( ymax);
539 a1.setX( 25.483); a1.setY( 65.017); // Oulu
540 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
541 dist = countDistance( &a1, &a2);
542 QString str = QString("Min & Max datan välimatka %1").arg(dist);
543 QMessageBox::about( 0, "Testi", str);
550 * Find out data range for x-, y- and z-coordinates
552 void dataMinMax( void)
558 temp = vertexList.at(0);
559 objxmax = objxmin = temp.getX();
560 objymax = objymin = temp.getY();
561 objzmax = objzmin = temp.getZ();
563 maxi = vertexList.size();
564 for (i=1; i<maxi; i++)
566 temp = vertexList.at(i);
575 else if (x > objxmax)
584 else if (y > objymax)
593 else if (z > objzmax)
598 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
599 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
600 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
601 //QMessageBox::about(0,"Tark", jono);
605 * Setting the point where the viewed object is. In the middle of datapoints.
607 void setAtPoint( Viewing *v)
613 x = (objxmax+objxmin)/2.0;
614 y= (objymax+objymin)/2.0;
615 z= (objzmax+objzmin)/2.0;
617 v->setAtPoint( x, y, z);
618 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
619 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
622 test = v->getAtPoint();
623 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
624 QMessageBox::about(0,"At point", jono);
629 * Setting the point where the object is viewed by eye.
631 void setFromPoint( Viewing *v)
635 point = v->getAtPoint();
637 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
639 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
642 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
645 v->setFromPoint(x,y,z);
646 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
647 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
649 test = v->getFromPoint();
650 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
651 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
657 #define LEFTEDGE 0x01
658 #define RIGHTEDGE 0x02
659 #define BOTTOMEDGE 0x04
663 * Returns a code specifying which edge in the viewing pyramid was crossed.
664 * There may be more than one.
666 int code( qreal x, qreal y, qreal z)
671 if (x<-z) c |= LEFTEDGE;
672 if (x>z) c |= RIGHTEDGE;
673 if (y<-z) c |= BOTTOMEDGE;
674 if (y>z) c |= TOPEDGE;
680 * Converts clipped world coordinates to screen coordinates.
682 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
684 *xScreen = (int) (a*xWorld+b);
685 *yScreen = (int) (c*yWorld+d);
689 * Clips the line segment in three-dimensional coordinates to the
692 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
700 while (c1!= NOEDGE || c2 != NOEDGE)
702 if ((c1 & c2 ) != NOEDGE) return;
704 if (c == NOEDGE) c = c2;
705 if ((c&LEFTEDGE) == LEFTEDGE)
708 t = (z1+x1)/((x1-x2)-(z2-z1));
713 else if ((c&RIGHTEDGE) == RIGHTEDGE)
715 // Crosses right edge
716 t = (z1-x1)/((x2-x1)-(z2-z1));
721 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
723 // Crosses bottom edge
724 t = (z1+y1)/((y1-y2)-(z2-z1));
729 else if ((c&TOPEDGE) == TOPEDGE)
732 t = (z1-y1)/((y2-y1)-(z2-z1));
752 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
753 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
757 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
758 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
760 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
764 * Transform the segment connecting the two vectors into the viewing plane.
765 * clip3d() clips the line if needed.
767 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
770 qreal x1, y1, z1, x2, y2, z2;
777 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
778 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
779 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
781 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
782 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
783 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
785 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
789 * This slot function is called when ever new push button clicked.
791 void RouteDialog::on_newPushButton_clicked()
793 close(); // go back to previous dialog
797 * This slot function is called when ever send push button clicked.
799 void RouteDialog::on_sendPushButton_clicked()
801 ui->sendPushButton->setEnabled(false);
806 * This function is set info text to user.
808 void RouteDialog::setLabelInfoToUser(QString infoText)
810 this->ui->labelInfoToUser->setText(infoText);
814 * This function enable send server button.
816 void RouteDialog::setSendServerButtonEnabled()
818 ui->sendPushButton->setEnabled(true);
822 * This function check login and set send route to server button disabled/enabled.
824 void RouteDialog::checkLogin()
828 ui->sendPushButton->setEnabled(true);
829 ui->labelInfoToUser->setText("");
833 ui->sendPushButton->setEnabled(false);
834 ui->labelInfoToUser->setText("You're not logged! Please register or log in.");