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