Bugfix: Route dialog draws start and finish texts out of the screen.
[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 "routedialog.h"
11 #include "ui_routedialog.h"
12 #include "usersettings.h"
13 #include <cmath>
14 #include <QPainter>
15 #include <QList>
16 #include <QMessageBox>
17 #include <QFile>
18 #include <QFileDialog>
19 #include <QPolygon>
20 #include <QDebug>
21
22 /**
23   * Vector class.
24   * In starting Qt 4.6 there is QVector3D.
25   * Later (updating Qt version) this class can be removed.
26   */
27 class Vector
28 {
29     qreal x, y, z;      // Location
30     qreal v;            // Velocity
31 public:
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)
44     {
45         x = x + v.x; y = y + v.y; z = z + v.z;
46         return *this;
47     };
48     Vector operator-(Vector v)
49     {
50         x = x - v.x; y = y - v.y; z = z - v.z;
51         return *this;
52     };
53     Vector operator/(qreal c)
54     {
55         x = x/c; y = y/c; z = z/c;
56         return *this;
57     };
58     Vector crossProduct( Vector a, Vector b)
59     {
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;
63         return *this;
64     };
65 };
66
67
68 class Viewing
69 {
70     Vector atPoint, fromPoint, up, a1, a2, a3;
71     qreal offsx, offsy, offsz;
72     qreal dval;
73     qreal angle;
74 public:
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)
81     {
82         up.setX(newUpX); up.setY(newUpY); up.setZ(newUpZ);
83     };
84     void setAtPoint( qreal newX, qreal newY, qreal newZ)
85     {
86         atPoint.setX(newX); atPoint.setY(newY); atPoint.setZ(newZ);
87     };
88     void setFromPoint(qreal newX, qreal newY, qreal newZ)
89     {
90         fromPoint.setX(newX); fromPoint.setY(newY); fromPoint.setZ(newZ);
91     }
92     void setEye()
93     {
94         double amarkmag, tempmag;
95         Vector temp, dist;
96
97         dval = cos(angle/2.0)/sin(angle/2.0);
98         dist = atPoint-fromPoint;
99         amarkmag = dist.length();
100         a3 = dist/amarkmag;
101
102         temp.crossProduct( dist, up);
103         tempmag = temp.length();
104         a1 = temp/tempmag;
105
106         temp.crossProduct( a1, a3);
107         tempmag = temp.length();
108         a2 = temp/tempmag;
109
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);
115     } ;
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; };
121     Viewing () {};
122 };
123
124 qreal xmax, xmin, ymin, ymax;       // Limits in world coordinates
125
126 QList<Vector> vertexList;           // Vertecies of route
127
128 qreal objxmin, objxmax, objymin, objymax, objzmin, objzmax; // data ranges
129
130 #define maxof(val1,val2)  ((val1>val2)?val1:val2)
131 #define toradians( degrees) (degrees*0.017453293)
132
133 #define WIDTH 1.8   // For 3d viewing only
134 qreal a, b,c,d; // Used for 3d viewing to calculate screen coordinates
135
136 Viewing view3d;   // Viewing settings for 3d
137
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 );
143
144 #define R 6378.140 // The radius of the earth by kilometers
145
146 /**
147   * count distance of two points (defined by longitude & latitude)
148   * on the surface of the earth.
149   */
150 qreal countDistance(Vector *p1, Vector *p2)
151 {
152     qreal dLon, dLat;   // delta of longitude & latitude
153     qreal a, c;
154
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;
159
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.
165
166     return R*c;   // Return distance in kilometers
167 }
168
169 /**
170   * Constructor of this class.
171   */
172 RouteDialog::RouteDialog(QWidget *parent) :
173     QDialog(parent), ui(new Ui::RouteDialog)
174 {
175     qDebug() << "__RouteDialog";
176     ui->setupUi(this);
177     this->setWindowTitle("Route");
178     left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates
179
180     // Button settings
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)");
185
186     // Clear labels
187     ui->labelInfoToUser->setText("");
188     ui->speedValueLabel->setText("");
189
190     // Check login
191     checkLogin();
192 }
193
194 /**
195   * Destructor of this class.
196   */
197 RouteDialog::~RouteDialog()
198 {
199     qDebug() << "__~RouteDialog";
200     if(ui)
201         delete ui;
202 }
203
204 /**
205   *
206   */
207 void RouteDialog::changeEvent(QEvent *e)
208 {
209     QDialog::changeEvent(e);
210     switch (e->type()) {
211     case QEvent::LanguageChange:
212         ui->retranslateUi(this);
213         break;
214     default:
215         break;
216     }
217 }
218
219 /**
220   *
221   */
222 int RouteDialog::getLeft()
223 {
224     return left;
225 }
226
227 /**
228   *
229   */
230 int RouteDialog::getRight()
231 {
232     return right;
233 }
234
235 /**
236   *
237   */
238 int RouteDialog::getTop()
239 {
240     return top;
241 }
242
243 /**
244   *
245   */
246 int RouteDialog::getBottom()
247 {
248     return bottom;
249 }
250
251 /**
252   *
253   */
254 void drawFlag( RouteDialog *rD, QPainter *p, int x, int y, QString startFinish)
255 {
256     /*QPolygon pg;
257
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))
263     {
264         // Upside
265         p->drawLine(x,y,x,y-15);
266         if (x <= (rD->getRight()-20))
267         {
268             // flag right
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);
272
273             // Draw start or finish
274             p->drawText(x+10, y, startFinish);
275         }
276         else
277         {
278             // Flag left
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);
282
283             // Draw start or finish
284             p->drawText(x+10, y, startFinish);
285         }    
286     }
287     else if (y <= (rD->getTop() + 25))
288     {
289         // downside
290         p->drawLine(x,y,x,y+15);
291         if (x <= (rD->getRight()-20))
292         {
293             // flag right
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);
297
298             // Draw start or finish
299             p->drawText(x+10, y+15, startFinish);
300         }
301         else
302         {
303             // Flag left
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);
307
308             // Draw start or finish
309             p->drawText(x+10, y+15, startFinish);
310         }
311     }
312     //p->drawPolygon();
313    // p->drawPolygon( pg,Qt::OddEvenFill);
314     //p->drawPolyline( &pg);
315     //p->drawPoints( pg);
316 }
317
318 /**
319   * Draws route to the route dialog.
320   * Type 0 is 2d viewing and type 1 is for 3d viewing
321   * @param QPaintEvent
322  */
323 /* */
324 void RouteDialog::paintEvent(QPaintEvent *)
325 {
326     // Check login
327     checkLogin();
328
329     int type = 0; //  0 for 2d, 1 for 3d
330     int startx, starty; // Starting point of the route
331     int i, maxi;
332     qreal x1, y1, x2, y2;
333     int x1Screen, y1Screen, x2Screen, y2Screen;
334     Vector v1, v2;
335     QPainter painter(this);
336     int startStop = 0;
337
338     painter.setRenderHint(QPainter::Antialiasing, true);
339     painter.setPen(QPen((Qt::white),2));
340     painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
341
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);*/
347
348     maxi = vertexList.size();
349
350     for (i=0; i<maxi-1; i++)
351     {
352        v1 = vertexList.at(i);
353        v2 = vertexList.at(i+1);
354
355        if (type == 0)
356        {    // 2d
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);
361
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);
366         }
367         else if (type == 1)
368         {   // 3d
369             transformseg( &view3d, &v1,&v2, &x1Screen, &y1Screen, &x2Screen, &y2Screen);
370         }
371
372         // Show with circle if starting point
373         if (i==0)
374         {
375             // Starting point
376             startx = x1Screen; starty = y1Screen;
377            // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
378            drawFlag( this, &painter,  x1Screen,  y1Screen, "Start" );
379         }
380         painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
381     }
382     // Show the endig point if different than the starting point
383     if (x2Screen != startx || y2Screen != starty)
384     {
385         //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
386         drawFlag( this, &painter, x2Screen, y2Screen, "Finish" );
387     }
388
389     {
390         qreal maxvx, maxvy; // max speed point coordinates
391         qreal maxv;         // max speed
392         Vector v;
393
394         maxv = 0.0;
395         for (i=0; i<maxi-1; i++)
396         {
397             v = vertexList.at(i);
398             if (v.getV() > maxv)
399             {
400                 maxv = v.getV();
401                 maxvx = v.getX();
402                 maxvy = v.getY();
403             }
404         }
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);
408
409         // Show max velocity point by yellow circle
410         painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
411         painter.drawEllipse( 650, 225, 10, 10);
412
413         QString jono;
414         //jono = QString("%1 km/h").arg(maxv);
415         jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
416         ui->speedValueLabel->setText(jono);
417     }
418 }
419
420 /**
421   *
422   */
423 bool RouteDialog::readRouteFromFile( QString &routeFile)
424  {
425     QString rFile = routeFile; //Not used
426     Vector temp;
427     QString rivi;
428     QFile file;
429
430     //QString fileName = QFileDialog::getOpenFileName(this,
431     //     tr("Read Route"), "./", tr("Route Files (*.txt)"));
432
433     //file.setFileName( fileName);
434     file.setFileName( "routetemp.xml");
435     if (!file.open(QIODevice::ReadOnly))
436     {
437         QMessageBox::about(0, "Error", "File not found");
438         return false;
439     }
440
441     vertexList.clear();
442
443     while(!file.atEnd())
444     {
445         int count;
446         bool allRead;
447         QString astr1, astr2, astr3, astr4;
448         QString str1, str2, str3, str4;
449         rivi = file.readLine();
450
451         allRead = false;
452         count = 0;
453         while( !allRead)
454         {
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=""
459
460             {
461                 double x, y, z, v;
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);
468                 /* */
469
470                 if (str1.length() > 0)
471                 {
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
481                     temp.setV( v);
482
483                     vertexList.append(temp);
484                     count++;
485                 }
486                 else
487                 {
488                     allRead = true;
489                 }
490             }
491         }
492         // Older version
493         /*
494         str1 = rivi.section(" ", 0, 0);
495         if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
496         {
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);
503
504             if (str1.length() > 0)
505             {
506                 double x, y, z, v;
507                 x = str2.toDouble();
508                 y = str1.toDouble();
509                 z = str3.toDouble();
510                 v = str4.toDouble();
511                 temp.setX( x); // Longitude
512                 temp.setY( y); // Latitude
513                 temp.setZ( z); // altitude
514                 temp.setV( v);
515
516                 vertexList.append(temp);
517             }
518         }
519         */
520     }
521
522     file.close();
523
524      /********  in 3d use only */
525      a = 400/2.;
526      b = 1 - a*(-1);
527      c = -300/2.;
528      d = 300 - c*(-1);
529      //angle = toradians(60);
530
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);
536      view3d.setEye();
537      /****** end of 3d *****/
538
539      /*
540      //Testing distance counting
541      Vector a1, a2;
542      qreal dist;
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);
550      */
551
552      return true;
553  }
554
555 /**
556   * Find out data range for x-, y- and z-coordinates
557   */
558 void dataMinMax( void)
559 {
560     int i, maxi;
561     qreal x,y,z;
562     Vector temp;
563
564     temp = vertexList.at(0);
565     objxmax = objxmin = temp.getX();
566     objymax = objymin = temp.getY();
567     objzmax = objzmin = temp.getZ();
568
569     maxi = vertexList.size();
570     for (i=1; i<maxi; i++)
571     {
572         temp = vertexList.at(i);
573         x = temp.getX();
574         y = temp.getY();
575         z = temp.getZ();
576
577         if (x < objxmin)
578         {
579                 objxmin = x;
580         }
581         else if (x > objxmax)
582         {
583                objxmax = x;
584         }
585
586         if (y < objymin)
587         {
588                 objymin = y;
589         }
590         else if (y > objymax)
591         {
592                objymax = y;
593         }
594
595         if (z < objzmin)
596         {
597                 objzmin = z;
598         }
599         else if (z > objzmax)
600         {
601                objzmax = z;
602         }
603     }
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);
608 }
609
610 /**
611   * Setting the point where the viewed object is. In the middle of datapoints.
612   */
613 void setAtPoint( Viewing *v)
614 {
615     qreal x, y, z;
616     dataMinMax();
617     //Vector test;
618
619     x = (objxmax+objxmin)/2.0;
620     y= (objymax+objymin)/2.0;
621     z= (objzmax+objzmin)/2.0;
622
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);
626
627     /* *
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);
631     * */
632 }
633
634 /**
635   * Setting the point where the object is viewed by eye.
636   */
637 void setFromPoint( Viewing *v)
638 {
639     qreal x, y, z;
640     Vector point;
641     point = v->getAtPoint();
642     //Vector test;
643     //fromPoint.setX( atPoint.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0));
644     //x = 3.0;
645     //x = point.getX() + (objxmax-objxmin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objymax-objymin)/2.0);
646     x = point.getX();
647     //y = point.getY();
648     y = point.getY() + 40; // + (objymax-objymin)/2.0 + WIDTH*maxof((objzmax-objzmin)/2.0,(objxmax-objxmin)/2.0);
649     z = point.getZ();
650
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);
654     /* *
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)
658     * */
659 }
660
661
662 #define NOEDGE     0x00
663 #define LEFTEDGE   0x01
664 #define RIGHTEDGE  0x02
665 #define BOTTOMEDGE 0x04
666 #define TOPEDGE    0x08
667
668 /**
669   * Returns a code specifying which edge in the viewing pyramid was crossed.
670   * There may be more than one.
671   */
672 int code( qreal x, qreal y, qreal z)
673 {
674     int c;
675
676     c = NOEDGE;
677     if (x<-z) c |= LEFTEDGE;
678     if (x>z) c |= RIGHTEDGE;
679     if (y<-z) c |= BOTTOMEDGE;
680     if (y>z) c |= TOPEDGE;
681
682     return c;
683 }
684
685 /**
686   * Converts clipped world coordinates to screen coordinates.
687   */
688 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
689 {
690    *xScreen = (int) (a*xWorld+b);
691    *yScreen = (int) (c*yWorld+d);
692 }
693
694 /**
695   * Clips the line segment in three-dimensional coordinates to the
696   * viewing pyramid.
697   */
698 void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
699 {
700     int c,c1,c2;
701     qreal x,y,z,t;
702
703     c1 = code(x1,y1,z1);
704     c2 = code(x2,y2,z2);
705
706     while (c1!= NOEDGE || c2 != NOEDGE)
707     {
708         if ((c1 & c2 ) != NOEDGE) return;
709         c = c1;
710         if (c == NOEDGE) c = c2;
711         if ((c&LEFTEDGE) == LEFTEDGE)
712         {
713                 // Crosses left edge
714                 t = (z1+x1)/((x1-x2)-(z2-z1));
715                 z = t*(z2-z1)+z1;
716                 x = -z;
717                 y = t*(y2-y1)+y1;
718         }
719         else if ((c&RIGHTEDGE) == RIGHTEDGE)
720         {
721                 // Crosses right edge
722                 t = (z1-x1)/((x2-x1)-(z2-z1));
723                 z = t*(z2-z1)+z1;
724                 x = z;
725                 y = t*(y2-y1)+y1;
726         }
727         else if ((c&BOTTOMEDGE) == BOTTOMEDGE)
728         {
729                 // Crosses bottom edge
730                 t = (z1+y1)/((y1-y2)-(z2-z1));
731                 z = t*(z2-z1)+z1;
732                 x = t*(x2-x1)+x1;
733                 y = -z;
734         }
735         else if ((c&TOPEDGE) == TOPEDGE)
736         {
737                 // Crosses top edge
738                 t = (z1-y1)/((y2-y1)-(z2-z1));
739                 z = t*(z2-z1)+z1;
740                 x = t*(x2-x1)+x1;
741                 y = z;
742         }
743
744         if (c == c1)
745         {
746             x1=x; y1=y; z1=z;
747             c1 = code(x,y,z);
748         }
749         else
750         {
751             x2=x; y2=y; z2=z;
752             c2 = code(x,y,z);
753         }
754     }
755
756     if (z1 != 0)
757     {
758         WORLDtoSCREEN(x1/z1,y1/z1,xscreen1, yscreen1);
759         WORLDtoSCREEN(x2/z2,y2/z2,xscreen2, yscreen2);
760     }
761     else
762     {
763         WORLDtoSCREEN(x1,y1,xscreen1, yscreen1);
764         WORLDtoSCREEN(x2,y2,xscreen2, yscreen2);
765     }
766     //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
767 }
768
769 /**
770   * Transform the segment connecting the two vectors into the viewing plane.
771   * clip3d() clips the line if needed.
772   */
773 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2)
774
775 {
776     qreal x1, y1, z1, x2, y2, z2;
777     Vector a1, a2, a3;
778
779     a1 = v->getA1();
780     a2 = v->getA2();
781     a3 = v->getA3();
782
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();
786
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();
790
791     clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
792 }
793
794 /**
795   * This slot function is called when ever new push button clicked.
796   */
797 void RouteDialog::on_newPushButton_clicked()
798 {
799     close();    // go back to previous dialog
800 }
801
802 /**
803   * This slot function is called when ever send push button clicked.
804   */
805 void RouteDialog::on_sendPushButton_clicked()
806 {
807     ui->sendPushButton->setEnabled(false);
808     emit sendroute();
809 }
810
811 /**
812   * This function is set info text to user.
813   */
814 void RouteDialog::setLabelInfoToUser(QString infoText)
815 {
816     this->ui->labelInfoToUser->setText(infoText);
817 }
818
819 /**
820   * This function enable send server button.
821   */
822 void RouteDialog::setSendServerButtonEnabled()
823 {
824     ui->sendPushButton->setEnabled(true);
825 }
826
827 /**
828   * This function check login and set send route to server button disabled/enabled.
829   */
830 void RouteDialog::checkLogin()
831 {
832     if (loginSaved())
833     {
834         ui->sendPushButton->setEnabled(true);
835         ui->labelInfoToUser->setText("");
836     }
837     else
838     {
839         ui->sendPushButton->setEnabled(false);
840         ui->labelInfoToUser->setText("You're not logged! Please register or log in.");
841     }
842 }