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