Maemo package folder added.
[speedfreak] / Client / routedialog.cpp
1 /*
2  * RouteDialog class
3  *
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
8  */
9
10 #include "routesavedialog.h"
11 #include "routedialog.h"
12 #include "ui_routedialog.h"
13 #include "usersettings.h"
14 #include <cmath>
15 #include <QPainter>
16 #include <QList>
17 #include <QMessageBox>
18 #include <QFile>
19 #include <QFileDialog>
20 #include <QPolygon>
21 #include <QDebug>
22
23 /**
24   * Vector class.
25   * In starting Qt 4.6 there is QVector3D.
26   * Later (updating Qt version) this class can be removed.
27   */
28 class Vector
29 {
30     qreal x, y, z;      // Location
31     qreal v;            // Velocity
32 public:
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)
45     {
46         x = x + v.x; y = y + v.y; z = z + v.z;
47         return *this;
48     };
49     Vector operator-(Vector v)
50     {
51         x = x - v.x; y = y - v.y; z = z - v.z;
52         return *this;
53     };
54     Vector operator/(qreal c)
55     {
56         x = x/c; y = y/c; z = z/c;
57         return *this;
58     };
59     Vector crossProduct( Vector a, Vector b)
60     {
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;
64         return *this;
65     };
66 };
67
68 /**
69   * Viewing class.
70   */
71 class Viewing
72 {
73     Vector atPoint, fromPoint, up, a1, a2, a3;
74     qreal offsx, offsy, offsz;
75     qreal dval;
76     qreal angle;
77 public:
78     qreal getOffsx() { return offsx; };
79     qreal getOffsy() { return offsy; };
80     qreal getOffsz() { return offsz; };
81     qreal getDval() { return dval; };
82     void setAngle( qreal newA) { angle = newA; };
83     void setUp( qreal newUpX, qreal newUpY, qreal newUpZ)
84     {
85         up.setX(newUpX); up.setY(newUpY); up.setZ(newUpZ);
86     };
87     void setAtPoint( qreal newX, qreal newY, qreal newZ)
88     {
89         atPoint.setX(newX); atPoint.setY(newY); atPoint.setZ(newZ);
90     };
91     void setFromPoint(qreal newX, qreal newY, qreal newZ)
92     {
93         fromPoint.setX(newX); fromPoint.setY(newY); fromPoint.setZ(newZ);
94     }
95     void setEye()
96     {
97         double amarkmag, tempmag;
98         Vector temp;
99         Vector dist;
100
101         dval = cos(angle/2.0)/sin(angle/2.0);
102         dist = atPoint-fromPoint;
103         amarkmag = dist.length();
104         a3 = dist/amarkmag;
105
106         temp.crossProduct( dist, up);
107         tempmag = temp.length();
108         a1 = temp/tempmag;
109
110         temp.crossProduct( a1, a3);
111         tempmag = temp.length();
112         a2 = temp/tempmag;
113
114         offsx = -a1.getX()*fromPoint.getX() - a1.getY()*fromPoint.getY() - a1.getZ()*fromPoint.getZ();
115         offsy = -a2.getX()*fromPoint.getX() - a2.getY()*fromPoint.getY() - a2.getZ()*fromPoint.getZ();
116         offsz = -a3.getX()*fromPoint.getX() - a3.getY()*fromPoint.getY() - a3.getZ()*fromPoint.getZ();
117         //QString jono2 = QString("offsx %1 offsy %2 offsz %3").arg(offsx).arg(offsy).arg(offsz);
118         //QMessageBox::about(0,"offs x y z", jono2);
119     } ;
120     Vector getAtPoint() { return atPoint; };
121     Vector getFromPoint() { return fromPoint; };
122     Vector getA1() { return a1; };
123     Vector getA2() { return a2; };
124     Vector getA3() { return a3; };
125     Viewing () {};
126 };
127
128 qreal xmax, xmin, ymin, ymax;       // Limits in world coordinates
129
130 QList<Vector> vertexList;           // Vertecies of route
131
132 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax; // data ranges
133
134 #define maxof(val1,val2)  ((val1>val2)?val1:val2)
135 #define toradians( degrees) (degrees*0.017453293)
136
137 #define WIDTH 1.8   // For 3d viewing only
138 qreal a, b,c,d; // Used for 3d viewing to calculate screen coordinates
139
140 Viewing view3d;   // Viewing settings for 3d
141
142 // Function prototypes
143 void dataMinMax( void);
144 void setAtPoint( Viewing *v);
145 void setFromPoint( Viewing *v);
146 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 );
147
148 #define R 6378.140 // The radius of the earth by kilometers
149
150 /**
151   * count distance of two points (defined by longitude & latitude)
152   * on the surface of the earth.
153   */
154 qreal countDistance(Vector *p1, Vector *p2)
155 {
156     qreal dLon, dLat;   // delta of longitude & latitude
157     qreal a, c;
158
159     dLon = p2->getX() - p1->getX();     // longitude difference
160     dLat = p2->getY() - p1->getY();     // latitude difference
161     if (dLon <0) dLon = -dLon;
162     if (dLat <0) dLat = -dLat;
163
164     dLon = dLon*3.14/180;
165     dLat = dLat*3.14/180;
166     a = (sin(dLat/2.))*(sin(dLat/2.)) +
167         (cos(p1->getY())*3.14/180)*(cos(p2->getY())*3.14/180)*(sin(dLon/2.))*(sin(dLon/2.));
168     c = 2.*atan(sqrt(a)/sqrt(1-a));     // c is angle between points p1 & p2 with circel by radius 1.
169
170     return R*c;   // Return distance in kilometers
171 }
172
173 /**
174   * Constructor of this class.
175   */
176 RouteDialog::RouteDialog(RouteSaveDialog *parent) :
177     QDialog(parent), ui(new Ui::RouteDialog)
178 {
179     qDebug() << "__RouteDialog";
180     ui->setupUi(this);
181
182     this->setWindowTitle("Route");
183     left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates
184
185     helpRoutingDialog = NULL;
186
187     // Button settings
188     ui->sendPushButton->setAutoFillBackground(true);
189     ui->sendPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
190
191     // Clear labels
192     ui->labelInfoToUser->setText("");
193     ui->speedValueLabel->setText("");
194     ui->avgSpeedValueLabel->setText("");
195
196     // Check login
197     checkLogin();
198
199     // Set average speed
200     QString average;
201     ui->avgSpeedValueLabel->setText(average.sprintf("%.1f", parent->getAverageSpeed()) + " km/h");
202     ui->distanceValueLabel->setText(parent->getDistanceTraveled() + " km");
203 }
204
205 /**
206   * Destructor of this class.
207   */
208 RouteDialog::~RouteDialog()
209 {
210     qDebug() << "__~RouteDialog";
211     if(ui)
212         delete ui;
213 }
214
215 /**
216   *
217   */
218 void RouteDialog::changeEvent(QEvent *e)
219 {
220     QDialog::changeEvent(e);
221     switch (e->type()) {
222     case QEvent::LanguageChange:
223         ui->retranslateUi(this);
224         break;
225     default:
226         break;
227     }
228 }
229
230 /**
231   * Get left.
232   *
233   * @return int left
234   */
235 int RouteDialog::getLeft()
236 {
237     return left;
238 }
239
240 /**
241   * Get right.
242   *
243   * @return int right
244   */
245 int RouteDialog::getRight()
246 {
247     return right;
248 }
249
250 /**
251   * Get top.
252   *
253   * @return int top
254   */
255 int RouteDialog::getTop()
256 {
257     return top;
258 }
259
260 /**
261   * Get bottom.
262   *
263   * @return int bottom
264   */
265 int RouteDialog::getBottom()
266 {
267     return bottom;
268 }
269
270 /**
271   * draw flag
272   *
273   * @param RouteDialog*
274   * @param QPainter*
275   * @param int x
276   * @param int y
277   * @param QString startFinish
278   */
279 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y, QString startFinish)
280 {
281     if (y> (rD->getTop() + 25))
282     {
283         // Upside
284         p->drawLine(x,y,x,y-15);
285         if (x <= (rD->getRight()-20))
286         {
287             // flag right
288             p->drawLine( x,    y-25, x+10, y-20);
289             p->drawLine( x+10, y-20, x,    y-15);
290             p->drawLine( x,    y-15, x,    y-25);
291
292             // Draw start or finish
293             p->drawText(x+10, y, startFinish);
294         }
295         else
296         {
297             // Flag left
298             p->drawLine( x,    y-25, x-10, y-20);
299             p->drawLine( x-10, y-20, x,    y-15);
300             p->drawLine( x,    y-15, x,    y-25);
301
302             // Draw start or finish
303             p->drawText(x+10, y, startFinish);
304         }    
305     }
306     else if (y <= (rD->getTop() + 25))
307     {
308         // downside
309         p->drawLine(x,y,x,y+15);
310         if (x <= (rD->getRight()-20))
311         {
312             // flag right
313             p->drawLine( x,    y+25, x+10, y+20);
314             p->drawLine( x+10, y+20, x,    y+15);
315             p->drawLine( x,    y+15, x,    y+25);
316
317             // Draw start or finish
318             p->drawText(x+10, y+15, startFinish);
319         }
320         else
321         {
322             // Flag left
323             p->drawLine( x,    y+25, x-10, y+20);
324             p->drawLine( x-10, y+20, x,    y+15);
325             p->drawLine( x,    y+15, x,    y+25);
326
327             // Draw start or finish
328             p->drawText(x+10, y+15, startFinish);
329         }
330     }
331 }
332
333 /**
334   * Draws route to the route dialog.
335   * Type 0 is 2d viewing and type 1 is for 3d viewing
336   *
337   * @param QPaintEvent*
338  */
339 /* */
340 void RouteDialog::paintEvent(QPaintEvent *)
341 {
342     // Check login
343     checkLogin();
344
345     int type = 0; //  0 for 2d, 1 for 3d
346     int startx = 0; // Starting x point of the route
347     int starty = 0; // Starting y point of the route
348     int i = 0;
349     int maxi = 0;
350     qreal x1, y1, x2, y2;
351     int x1Screen = 0;
352     int y1Screen = 0;
353     int x2Screen = 0;
354     int y2Screen = 0;
355     Vector v1, v2;
356     QPainter painter(this);
357
358     painter.setRenderHint(QPainter::Antialiasing, true);
359     painter.setPen(QPen((Qt::white),2));
360     painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
361
362     maxi = vertexList.size();
363
364     for (i=0; i<maxi-1; i++)
365     {
366        v1 = vertexList.at(i);
367        v2 = vertexList.at(i+1);
368
369        if (type == 0)
370        {    // 2d
371             x1 = v1.getX(); y1 = v1.getY();
372             x2 = v2.getX(); y2 = v2.getY();
373             //QString jono = QString("x: %1 y: %2").arg(x1).arg(y1);
374             //QMessageBox::about(0,"Tark",jono);
375
376             x1Screen = left + (x1-xmin)/(xmax-xmin)*(right-left);
377             y1Screen = top + (ymax-y1)/(ymax-ymin)*(bottom-top);
378             x2Screen = left + (x2-xmin)/(xmax-xmin)*(right-left);
379             y2Screen = top + (ymax-y2)/(ymax-ymin)*(bottom-top);
380         }
381         else if (type == 1)
382         {   // 3d
383             transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
384         }
385
386         // Show with circle if starting point
387         if (i==0)
388         {
389             // Starting point
390             startx = x1Screen; starty = y1Screen;
391            // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
392            drawFlag( this, &painter,  x1Screen,  y1Screen, "Start" );
393         }
394         painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
395     }
396     // Show the endig point if different than the starting point
397     if (x2Screen != startx || y2Screen != starty)
398     {
399         //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
400         drawFlag( this, &painter, x2Screen, y2Screen, "Finish" );
401     }
402
403     {
404         qreal maxvx = 0; // max speed x point
405         qreal maxvy = 0; // max speed y point
406         qreal maxv = 0;  // max speed
407         Vector v;
408
409         maxv = 0.0;
410         for (i=0; i<maxi-1; i++)
411         {
412             v = vertexList.at(i);
413             if (v.getV() > maxv)
414             {
415                 maxv = v.getV();
416                 maxvx = v.getX();
417                 maxvy = v.getY();
418             }
419         }
420         // Translate world coordinates to screen coordinates
421         x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
422         y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
423
424         // Show max velocity point by yellow circle
425         painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
426         painter.drawEllipse( ui->maxSpeedLabel->geometry().x()-15, ui->maxSpeedLabel->geometry().y()+15, 10, 10);
427
428         QString jono;
429         //jono = QString("%1 km/h").arg(maxv);
430         jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
431         ui->speedValueLabel->setText(jono);
432     }
433 }
434
435 /**
436   * read route from file
437   *
438   * @param QString route file
439   */
440 bool RouteDialog::readRouteFromFile( QString &routeFile )
441 {
442     QString rFile = routeFile;
443     fileName = routeFile;
444     Vector temp;
445     QString rivi;
446     QFile file;
447     int progressbarInt = 5;
448     int count;
449
450     file.setFileName( rFile);
451     if (!file.open(QIODevice::ReadOnly))
452     {
453         QMessageBox::about(0, "Error", "Empty file");
454         return false;
455     }
456     emit progressbar(progressbarInt);
457     vertexList.clear();
458
459     while(!file.atEnd())
460     {
461         bool allRead;
462         QString astr1, astr2, astr3, astr4;
463         QString str1, str2, str3, str4;
464         rivi = file.readLine();
465         allRead = false;
466         count = 0;
467         while( !allRead)
468         {
469             astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
470             astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
471             astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
472             astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
473
474             {
475                 double x, y, z, v;
476                 str1 = astr1.section('"',1,1);
477                 str2 = astr2.section('"',1,1);
478                 str3 = astr3.section('"',1,1);
479                 str4 = astr4.section('"',1,1);
480
481                 if (str1.length() > 0)
482                 {
483                     x = str2.toDouble();// latitude y-value
484                     y = str1.toDouble();// longitude x-value
485                     z = str3.toDouble();// altitude z-value
486                     v = str4.toDouble();// speed km/h
487                     temp.setX( x); // Longitude
488                     temp.setY( y); // Latitude
489                     temp.setZ( z); // altitude
490                     temp.setV( v);
491
492                     vertexList.append(temp);
493                     count++;
494                 }
495                 else
496                 {
497                     allRead = true;
498                 }
499             }
500             if(progressbarInt < 50)
501                 progressbarInt++;
502             emit progressbar(progressbarInt);
503         }
504     }
505     file.close();
506
507     if(count == 1) // Check is file empty.
508     {
509         progressbarInt = 50;
510         emit progressbar(progressbarInt);
511         qDebug() << "count: " + QString::number(count);
512          /********  in 3d use only */
513          a = 400/2.;
514          b = 1 - a*(-1);
515          c = -300/2.;
516          d = 300 - c*(-1);
517          emit progressbar(60);
518          //angle = toradians(60);
519          view3d.setUp( 1.0, 0.0, 0.0);
520          emit progressbar(70);
521          view3d.setAngle(toradians(60));
522          emit progressbar(80);
523          setAtPoint( &view3d); // If file is empty software crash here.
524          emit progressbar(90);
525          xmin = objxmin; xmax = objxmax; ymin = objymin; ymax = objymax; // 2d viewing needs this !!!!
526          emit progressbar(95);
527          setFromPoint( &view3d);
528          emit progressbar(98);
529          view3d.setEye();
530          emit progressbar(99);
531          /****** end of 3d *****/
532          /*
533          //Testing distance counting
534          Vector a1, a2;
535          qreal dist;
536          //a1.setX( xmin); a1.setY( ymin);
537          //a2.setX( xmax); a2.setY( ymax);
538          a1.setX( 25.483); a1.setY( 65.017); // Oulu
539          a2.setX( 27.767); a2.setY( 64.283); // Kajaani
540          dist = countDistance( &a1, &a2);
541          QString str = QString("Min & Max datan välimatka %1").arg(dist);
542          QMessageBox::about( 0, "Testi", str);
543          */
544         emit progressbar(100);
545         return true;
546     }
547     else // Probably file is empty.
548     {
549         QMessageBox::about(0, "Error", "Empty file");
550         return false;
551     }
552 }
553
554 /**
555   * Find out data range for x-, y- and z-coordinates
556   */
557 void dataMinMax( void)
558 {
559     int i, maxi;
560     qreal x,y,z;
561     Vector temp;
562
563     temp = vertexList.at(0);
564     objxmax = objxmin = temp.getX();
565     objymax = objymin = temp.getY();
566     objzmax = objzmin = temp.getZ();
567
568     maxi = vertexList.size();
569     for (i=1; i<maxi; i++)
570     {
571         temp = vertexList.at(i);
572         x = temp.getX();
573         y = temp.getY();
574         z = temp.getZ();
575
576         if (x < objxmin)
577         {
578                 objxmin = x;
579         }
580         else if (x > objxmax)
581         {
582                objxmax = x;
583         }
584
585         if (y < objymin)
586         {
587                 objymin = y;
588         }
589         else if (y > objymax)
590         {
591                objymax = y;
592         }
593
594         if (z < objzmin)
595         {
596                 objzmin = z;
597         }
598         else if (z > objzmax)
599         {
600                objzmax = z;
601         }
602     }
603     //QString jono = QString("ojxmin %1 objxmax %2").arg(objxmin).arg(objxmax);
604     //QString jono = QString("ojymin %1 objymax %2").arg(objymin).arg(objymax);
605     //QString jono = QString("ojzmin %1 objzmax %2").arg(objzmin).arg(objzmax);
606     //QMessageBox::about(0,"Tark", jono);
607 }
608
609 /**
610   * Setting the point where the viewed object is. In the middle of datapoints.
611   */
612 void setAtPoint( Viewing *v)
613 {
614     qreal x, y, z;
615     dataMinMax();
616     //Vector test;
617
618     x = (objxmax+objxmin)/2.0;
619     y= (objymax+objymin)/2.0;
620     z= (objzmax+objzmin)/2.0;
621
622     v->setAtPoint( x, y, z);
623     //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x()).arg(atPoint.y()).arg(atPoint.z());
624     //QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(atPoint.x).arg(atPoint.y).arg(atPoint.z);
625
626     /* *
627     test = v->getAtPoint();
628     QString jono = QString("AtX %1 Aty %2 AtZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
629     QMessageBox::about(0,"At point", jono);
630     * */
631 }
632
633 /**
634   * Setting the point where the object is viewed by eye.
635   */
636 void setFromPoint( Viewing *v)
637 {
638     qreal x, y, z;
639     Vector point;
640     point = v->getAtPoint();
641     //Vector test;
642     //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
643     //x = 3.0;
644     //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
645     x = point.getX();
646     //y = point.getY();
647     y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
648     z = point.getZ();
649
650     v->setFromPoint(x,y,z);
651     //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x()).arg(fromPoint.y()).arg(fromPoint.z());
652     //QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(fromPoint.x).arg(fromPoint.y).arg(fromPoint.z);
653     /* *
654     test = v->getFromPoint();
655     QString jono = QString("FromX %1 FromY %2 FromZ %3").arg(test.getX()).arg(test.getY()).arg(test.getZ());
656     QMessageBox::about(0,"From point", jono); // (1.9, 0.5, 0.5)
657     * */
658 }
659
660 #define NOEDGE     0x00
661 #define LEFTEDGE   0x01
662 #define RIGHTEDGE  0x02
663 #define BOTTOMEDGE 0x04
664 #define TOPEDGE    0x08
665
666 /**
667   * Returns a code specifying which edge in the viewing pyramid was crossed.
668   * There may be more than one.
669   *
670   * @param qreal x
671   * @param qreal y
672   * @param qreal z
673   * @return int
674   */
675 int code( qreal x, qreal y, qreal z)
676 {
677     int c;
678     c = NOEDGE;
679     if (x<-z) c |= LEFTEDGE;
680     if (x>z) c |= RIGHTEDGE;
681     if (y<-z) c |= BOTTOMEDGE;
682     if (y>z) c |= TOPEDGE;
683     return c;
684 }
685
686 /**
687   * Converts clipped world coordinates to screen coordinates.
688   *
689   * @param qreal xWorld
690   * @param qreal yWorld
691   * @param int *xScreen
692   * @param int *yScreen
693   */
694 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
695 {
696    *xScreen = (int) (a*xWorld+b);
697    *yScreen = (int) (c*yWorld+d);
698 }
699
700 /**
701   * Clips the line segment in three-dimensional coordinates to the
702   * viewing pyramid.
703   *
704   * @param qreal x1
705   * @param qreal y1
706   * @param qreal z1
707   * @param qreal x2
708   * @param qreal y2
709   * @param qreal z2
710   * @param int *xscreen1
711   * @param int *yscreen1
712   * @param int *xscreen2
713   * @param int *yscreen2
714   */
715 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
716 {
717     int c = 0;
718     int c1 = 0;
719     int c2 = 0;
720     qreal x = 0;
721     qreal y = 0;
722     qreal z = 0;
723     qreal t = 0;
724
725     c1 = code(x1,y1,z1);
726     c2 = code(x2,y2,z2);
727
728     while (c1!= NOEDGE || c2 != NOEDGE)
729     {
730         if ((c1 & c2 ) != NOEDGE) return;
731         c = c1;
732         if (c == NOEDGE) c = c2;
733         if ((c&LEFTEDGE) == LEFTEDGE)
734         {
735                 // Crosses left edge
736                 t = (z1+x1)/((x1-x2)-(z2-z1));
737                 z = t*(z2-z1)+z1;
738                 x = -z;
739                 y = t*(y2-y1)+y1;
740         }
741         else if ((c&RIGHTEDGE) == RIGHTEDGE)
742         {
743                 // Crosses right edge
744                 t = (z1-x1)/((x2-x1)-(z2-z1));
745                 z = t*(z2-z1)+z1;
746                 x = z;
747                 y = t*(y2-y1)+y1;
748         }
749         else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
750         {
751                 // Crosses bottom edge
752                 t = (z1+y1)/((y1-y2)-(z2-z1));
753                 z = t*(z2-z1)+z1;
754                 x = t*(x2-x1)+x1;
755                 y = -z;
756         }
757         else if ((c&TOPEDGE) == TOPEDGE)
758         {
759                 // Crosses top edge
760                 t = (z1-y1)/((y2-y1)-(z2-z1));
761                 z = t*(z2-z1)+z1;
762                 x = t*(x2-x1)+x1;
763                 y = z;
764         }
765
766         if (c == c1)
767         {
768             x1=x; y1=y; z1=z;
769             c1 = code(x,y,z);
770         }
771         else
772         {
773             x2=x; y2=y; z2=z;
774             c2 = code(x,y,z);
775         }
776     }
777
778     if (z1 != 0)
779     {
780         WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
781         WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
782     }
783     else
784     {
785         WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
786         WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
787     }
788     //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
789 }
790
791 /**
792   * Transform the segment connecting the two vectors into the viewing plane.
793   * clip3d() clips the line if needed.
794   *
795   * @param Viewing *v
796   * @param Vector *v1
797   * @param Vector *v2
798   * @param int *xscreen1
799   * @param int *yscreen1
800   * @param int *xscreen2
801   * @param int *yscreen2
802   */
803 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
804
805 {
806     qreal x1, y1, z1, x2, y2, z2;
807     Vector a1, a2, a3;
808
809     a1 = v->getA1();
810     a2 = v->getA2();
811     a3 = v->getA3();
812
813     x1 = (a1.getX()*v1->getX() + a1.getY()*v1->getY() + a1.getZ()*v1->getZ() + v->getOffsx())*v->getDval();
814     y1 = (a2.getX()*v1->getX() + a2.getY()*v1->getY() + a2.getZ()*v1->getZ() + v->getOffsy())*v->getDval();
815     z1 = a3.getX()*v1->getX() + a3.getY()*v1->getY() + a3.getZ()*v1->getZ() + v->getOffsz();
816
817     x2 = (a1.getX()*v2->getX() + a1.getY()*v2->getY() + a1.getZ()*v2->getZ() + v->getOffsx())*v->getDval();
818     y2 = (a2.getX()*v2->getX() + a2.getY()*v2->getY() + a2.getZ()*v2->getZ() + v->getOffsy())*v->getDval();
819     z2 = a3.getX()*v2->getX() + a3.getY()*v2->getY() + a3.getZ()*v2->getZ() + v->getOffsz();
820
821     clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
822 }
823
824 /**
825   * This slot function is called when ever send push button clicked.
826   */
827 void RouteDialog::on_sendPushButton_clicked()
828 {
829     ui->sendPushButton->setEnabled(false);
830
831     // Check default file dir.
832     if(!QDir("/home/user/MyDocs/speedfreak/route").exists())
833     {
834         QDir().mkdir("/home/user/MyDocs/speedfreak/route");
835     }
836
837     // If file name is empty set it to default.
838     if(fileName == "")
839         fileName = "/home/user/MyDocs/speedfreak/route/";
840
841     if(fileName == "/home/user/MyDocs/speedfreak/route/routetemp.xml")
842         fileName = "/home/user/MyDocs/speedfreak/route/route.xml";
843
844     qDebug() << "__FileName: " + fileName;
845     QString newFileName = QFileDialog::getSaveFileName(this, tr("Save Route"), fileName);
846     int server = QMessageBox::question(this, "Save route to server?", "", 4,3);
847
848     if(server == 3) // Yes button
849     {
850         qDebug() << "__Save to server: yes";
851         emit sendroute(newFileName,1); // Save route to server and phone.
852     }
853     else if(server == 4) // No button
854     {
855         qDebug() << "__Save to server: no";
856
857         if(fileName != "")
858             emit sendroute(newFileName,0); // Save route to phone.
859     }
860 }
861
862 /**
863   * This function is set info text to user.
864   *
865   * @param QString info text
866   */
867 void RouteDialog::setLabelInfoToUser(QString infoText)
868 {
869     this->ui->labelInfoToUser->setText(infoText);
870 }
871
872 /**
873   * This function enable send server button.
874   */
875 void RouteDialog::setSendServerButtonEnabled()
876 {
877     ui->sendPushButton->setEnabled(true);
878 }
879
880 /**
881   * This function check login and set send route to server button disabled/enabled.
882   */
883 void RouteDialog::checkLogin()
884 {
885     if (loginSaved())
886     {
887         ui->sendPushButton->setEnabled(true);
888         ui->labelInfoToUser->setText("");
889     }
890     else
891     {
892         ui->sendPushButton->setEnabled(false);
893         ui->labelInfoToUser->setText("You're not logged! Please register or log in.");
894     }
895 }
896
897 /**
898   * This slot function called when ever info button clicked.
899   */
900 void RouteDialog::on_pushButtonInfo_clicked()
901 {    
902     if(!helpRoutingDialog)
903     {
904         helpRoutingDialog = new HelpRoutingDialog;
905     }
906     connect(helpRoutingDialog, SIGNAL(rejected()), this, SLOT(killHelpDialog()));
907     helpRoutingDialog->show();
908 }
909
910 /**
911   * This slot function called when ever dialog rejected.
912   */
913 void RouteDialog::killHelpDialog()
914 {
915     if(helpRoutingDialog)
916     {
917         qDebug() << "__Route kill: helpRoutingDialog";
918         delete helpRoutingDialog;
919         helpRoutingDialog = NULL;
920     }
921 }