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