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, QString startFinish)
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);
273 // Draw start or finish
274 p->drawText(x+10, y, startFinish);
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);
287 else if (y <= (rD->getTop() + 25))
290 p->drawLine(x,y,x,y+15);
291 if (x <= (rD->getRight()-20))
294 p->drawLine( x, y+25, x+10, y+20);
295 p->drawLine( x+10, y+20, x, y+15);
296 p->drawLine( x, y+15, x, y+25);
298 // Draw start or finish
299 p->drawText(x+10, y+15, startFinish);
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);
313 // p->drawPolygon( pg,Qt::OddEvenFill);
314 //p->drawPolyline( &pg);
315 //p->drawPoints( pg);
319 * Draws route to the route dialog.
320 * Type 0 is 2d viewing and type 1 is for 3d viewing
324 void RouteDialog::paintEvent(QPaintEvent *)
329 int type = 0; // 0 for 2d, 1 for 3d
330 int startx, starty; // Starting point of the route
332 qreal x1, y1, x2, y2;
333 int x1Screen, y1Screen, x2Screen, y2Screen;
335 QPainter painter(this);
338 painter.setRenderHint(QPainter::Antialiasing, true);
339 painter.setPen(QPen((Qt::white),2));
340 painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
342 // Draw route window frame
343 /*painter.drawLine(left,top,right,top);
344 painter.drawLine(right,top,right,bottom);
345 painter.drawLine(left,top,left,bottom);
346 painter.drawLine(left,bottom,right,bottom);*/
348 maxi = vertexList.size();
350 for (i=0; i<maxi-1; i++)
352 v1 = vertexList.at(i);
353 v2 = vertexList.at(i+1);
357 x1 = v1.getX(); y1 = v1.getY();
358 x2 = v2.getX(); y2 = v2.getY();
359 //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
360 //QMessageBox::about(0,"Tark",jono);
362 x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
363 y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
364 x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
365 y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
369 transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
372 // Show with circle if starting point
376 startx = x1Screen; starty = y1Screen;
377 // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
378 drawFlag( this, &painter, x1Screen, y1Screen, "Start" );
380 painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
382 // Show the endig point if different than the starting point
383 if (x2Screen != startx || y2Screen != starty)
385 //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
386 drawFlag( this, &painter, x2Screen, y2Screen, "Finish" );
390 qreal maxvx, maxvy; // max speed point coordinates
391 qreal maxv; // max speed
395 for (i=0; i<maxi-1; i++)
397 v = vertexList.at(i);
405 // Translate world coordinates to screen coordinates
406 x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
407 y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
409 // Show max velocity point by yellow circle
410 painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
411 painter.drawEllipse( 650, 225, 10, 10);
414 //jono = QString("%1 km/h").arg(maxv);
415 jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
416 ui->speedValueLabel->setText(jono);
423 bool RouteDialog::readRouteFromFile( QString &routeFile)
425 QString rFile = routeFile; //Not used
430 //QString fileName = QFileDialog::getOpenFileName(this,
431 // tr("Read Route"), "./", tr("Route Files (*.txt)"));
433 //file.setFileName( fileName);
434 file.setFileName( "routetemp.xml");
435 if (!file.open(QIODevice::ReadOnly))
437 QMessageBox::about(0, "Error", "File not found");
447 QString astr1, astr2, astr3, astr4;
448 QString str1, str2, str3, str4;
449 rivi = file.readLine();
455 astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
456 astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
457 astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
458 astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
462 str1 = astr1.section('"',1,1);
463 str2 = astr2.section('"',1,1);
464 str3 = astr3.section('"',1,1);
465 str4 = astr4.section('"',1,1);
466 //QString str = QString("%1 %2 %3 %4").arg(str1).arg(str2).arg(str3).arg(str4);
467 //QMessageBox::about(0, "LUKEE", str);
470 if (str1.length() > 0)
472 x = str2.toDouble();// latitude y-value
473 y = str1.toDouble();// longitude x-value
474 z = str3.toDouble();// altitude z-value
475 v = str4.toDouble();// speed km/h
476 // QString str = QString("%1 %2 %3 %4").arg(x).arg(y).arg(z).arg(v);
477 // QMessageBox::about(0, "LUKEE", str);
478 temp.setX( x); // Longitude
479 temp.setY( y); // Latitude
480 temp.setZ( z); // altitude
483 vertexList.append(temp);
494 str1 = rivi.section(" ", 0, 0);
495 if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
497 str1 = rivi.section(" ", 2, 2); // latitude y-value
498 str2 = rivi.section(" ", 4, 4); // longitude x-value
499 str3 = rivi.section(" ", 6, 6); // altitude z-value
500 str4 = rivi.section(" ", 8, 8); // speed km/h
501 //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
502 //QMessageBox::about(0, "LUKEE", str);
504 if (str1.length() > 0)
511 temp.setX( x); // Longitude
512 temp.setY( y); // Latitude
513 temp.setZ( z); // altitude
516 vertexList.append(temp);
524 /******** in 3d use only */
529 //angle = toradians(60);
531 view3d.setUp( 1.0, 0.0, 0.0);
532 view3d.setAngle(toradians(60));
533 setAtPoint( &view3d);
534 xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
535 setFromPoint( &view3d);
537 /****** end of 3d *****/
540 //Testing distance counting
543 //a1.setX( xmin); a1.setY( ymin);
544 //a2.setX( xmax); a2.setY( ymax);
545 a1.setX( 25.483); a1.setY( 65.017); // Oulu
546 a2.setX( 27.767); a2.setY( 64.283); // Kajaani
547 dist = countDistance( &a1, &a2);
548 QString str = QString("Min & Max datan välimatka %1").arg(dist);
549 QMessageBox::about( 0, "Testi", str);
556 * Find out data range for x-, y- and z-coordinates
558 void dataMinMax( void)
564 temp = vertexList.at(0);
565 objxmax = objxmin = temp.getX();
566 objymax = objymin = temp.getY();
567 objzmax = objzmin = temp.getZ();
569 maxi = vertexList.size();
570 for (i=1; i<maxi; i++)
572 temp = vertexList.at(i);
581 else if (x > objxmax)
590 else if (y > objymax)
599 else if (z > objzmax)
604 //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
605 //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
606 //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
607 //QMessageBox::about(0,"Tark", jono);
611 * Setting the point where the viewed object is. In the middle of datapoints.
613 void setAtPoint( Viewing *v)
619 x = (objxmax+objxmin)/2.0;
620 y= (objymax+objymin)/2.0;
621 z= (objzmax+objzmin)/2.0;
623 v->setAtPoint( x, y, z);
624 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
625 //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
628 test = v->getAtPoint();
629 QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
630 QMessageBox::about(0,"At point", jono);
635 * Setting the point where the object is viewed by eye.
637 void setFromPoint( Viewing *v)
641 point = v->getAtPoint();
643 //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
645 //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
648 y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
651 v->setFromPoint(x,y,z);
652 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
653 //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
655 test = v->getFromPoint();
656 QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
657 QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
663 #define LEFTEDGE 0x01
664 #define RIGHTEDGE 0x02
665 #define BOTTOMEDGE 0x04
669 * Returns a code specifying which edge in the viewing pyramid was crossed.
670 * There may be more than one.
672 int code( qreal x, qreal y, qreal z)
677 if (x<-z) c |= LEFTEDGE;
678 if (x>z) c |= RIGHTEDGE;
679 if (y<-z) c |= BOTTOMEDGE;
680 if (y>z) c |= TOPEDGE;
686 * Converts clipped world coordinates to screen coordinates.
688 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
690 *xScreen = (int) (a*xWorld+b);
691 *yScreen = (int) (c*yWorld+d);
695 * Clips the line segment in three-dimensional coordinates to the
698 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
706 while (c1!= NOEDGE || c2 != NOEDGE)
708 if ((c1 & c2 ) != NOEDGE) return;
710 if (c == NOEDGE) c = c2;
711 if ((c&LEFTEDGE) == LEFTEDGE)
714 t = (z1+x1)/((x1-x2)-(z2-z1));
719 else if ((c&RIGHTEDGE) == RIGHTEDGE)
721 // Crosses right edge
722 t = (z1-x1)/((x2-x1)-(z2-z1));
727 else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
729 // Crosses bottom edge
730 t = (z1+y1)/((y1-y2)-(z2-z1));
735 else if ((c&TOPEDGE) == TOPEDGE)
738 t = (z1-y1)/((y2-y1)-(z2-z1));
758 WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
759 WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
763 WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
764 WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
766 //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
770 * Transform the segment connecting the two vectors into the viewing plane.
771 * clip3d() clips the line if needed.
773 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
776 qreal x1, y1, z1, x2, y2, z2;
783 x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
784 y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
785 z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
787 x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
788 y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
789 z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
791 clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
795 * This slot function is called when ever new push button clicked.
797 void RouteDialog::on_newPushButton_clicked()
799 close(); // go back to previous dialog
803 * This slot function is called when ever send push button clicked.
805 void RouteDialog::on_sendPushButton_clicked()
807 ui->sendPushButton->setEnabled(false);
812 * This function is set info text to user.
814 void RouteDialog::setLabelInfoToUser(QString infoText)
816 this->ui->labelInfoToUser->setText(infoText);
820 * This function enable send server button.
822 void RouteDialog::setSendServerButtonEnabled()
824 ui->sendPushButton->setEnabled(true);
828 * This function check login and set send route to server button disabled/enabled.
830 void RouteDialog::checkLogin()
834 ui->sendPushButton->setEnabled(true);
835 ui->labelInfoToUser->setText("");
839 ui->sendPushButton->setEnabled(false);
840 ui->labelInfoToUser->setText("You're not logged! Please register or log in.");