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