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