Bugfix: Route dialog draws start and finish texts out of the screen.
[speedfreak] / Client / routedialog.cpp
index 42f5faa..e51d8d0 100644 (file)
@@ -1,37 +1,44 @@
 /*
  * RouteDialog class
  *
- * @author     Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
- * @copyright  (c) 2010 Speed Freak team
- * @license    http://opensource.org/licenses/gpl-license.php GNU Public License
+ * @author      Olavi Pulkkinen <olavi.pulkkinen@fudeco.com>
+ * @author      Toni Jussila   <toni.jussila@fudeco.com>
+ * @copyright   (c) 2010 Speed Freak team
+ * @license     http://opensource.org/licenses/gpl-license.php GNU Public License
  */
 
 #include "routedialog.h"
 #include "ui_routedialog.h"
+#include "usersettings.h"
 #include <cmath>
 #include <QPainter>
 #include <QList>
 #include <QMessageBox>
 #include <QFile>
 #include <QFileDialog>
+#include <QPolygon>
+#include <QDebug>
 
-/*
+/**
   * Vector class.
   * In starting Qt 4.6 there is QVector3D.
   * Later (updating Qt version) this class can be removed.
   */
 class Vector
 {
-    qreal x, y, z;
+    qreal x, y, z;      // Location
+    qreal v;            // Velocity
 public:
     Vector() { x=0.; y=0. ; z=0.; };
     Vector( qreal initX, qreal initY, qreal initZ) { x = initX, y = initY; z = initZ; };
     void setX( qreal newX) { x = newX; };
     void setY( qreal newY) { y = newY; };
     void setZ( qreal newZ) { z = newZ; };
+    void setV( qreal newV) { v = newV; };
     qreal getX() { return x; };
     qreal getY() { return y; };
     qreal getZ() { return z; };
+    qreal getV() { return v; };
     qreal length() { return sqrt(x*x+y*y+z*z); };
     Vector operator+(Vector v)
     {
@@ -135,7 +142,8 @@ void setFromPoint( Viewing *v);
 void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscreen1, int *xscreen2, int *yscreen2 );
 
 #define R 6378.140 // The radius of the earth by kilometers
-/*
+
+/**
   * count distance of two points (defined by longitude & latitude)
   * on the surface of the earth.
   */
@@ -158,19 +166,44 @@ qreal countDistance(Vector *p1, Vector *p2)
     return R*c;   // Return distance in kilometers
 }
 
+/**
+  * Constructor of this class.
+  */
 RouteDialog::RouteDialog(QWidget *parent) :
-    QDialog(parent),
-    ui(new Ui::RouteDialog)
+    QDialog(parent), ui(new Ui::RouteDialog)
 {
+    qDebug() << "__RouteDialog";
     ui->setupUi(this);
-    left = 5; top = 5; right = 395; bottom = 195; // Limits in screen coordinates
+    this->setWindowTitle("Route");
+    left = 5; top = 5; right = 495; bottom = 295; // Limits in screen coordinates
+
+    // Button settings
+    ui->sendPushButton->setAutoFillBackground(true);
+    ui->sendPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
+    ui->newPushButton->setAutoFillBackground(true);
+    ui->newPushButton->setStyleSheet("background-color: rgb(0, 0, 0); color: rgb(255, 255, 255)");
+
+    // Clear labels
+    ui->labelInfoToUser->setText("");
+    ui->speedValueLabel->setText("");
+
+    // Check login
+    checkLogin();
 }
 
+/**
+  * Destructor of this class.
+  */
 RouteDialog::~RouteDialog()
 {
-    delete ui;
+    qDebug() << "__~RouteDialog";
+    if(ui)
+        delete ui;
 }
 
+/**
+  *
+  */
 void RouteDialog::changeEvent(QEvent *e)
 {
     QDialog::changeEvent(e);
@@ -183,6 +216,104 @@ void RouteDialog::changeEvent(QEvent *e)
     }
 }
 
+/**
+  *
+  */
+int RouteDialog::getLeft()
+{
+    return left;
+}
+
+/**
+  *
+  */
+int RouteDialog::getRight()
+{
+    return right;
+}
+
+/**
+  *
+  */
+int RouteDialog::getTop()
+{
+    return top;
+}
+
+/**
+  *
+  */
+int RouteDialog::getBottom()
+{
+    return bottom;
+}
+
+/**
+  *
+  */
+void drawFlag( RouteDialog *rD, QPainter *p, int x, int y, QString startFinish)
+{
+    /*QPolygon pg;
+
+    pg.setPoint(0,x, y-25);
+    pg.setPoint(1,x+10,y-20);
+    pg.setPoint(2,x, y-15);
+    pg.setPoint(3,x,y-20);*/
+    if (y> (rD->getTop() + 25))
+    {
+        // Upside
+        p->drawLine(x,y,x,y-15);
+        if (x <= (rD->getRight()-20))
+        {
+            // flag right
+            p->drawLine( x,    y-25, x+10, y-20);
+            p->drawLine( x+10, y-20, x,    y-15);
+            p->drawLine( x,    y-15, x,    y-25);
+
+            // Draw start or finish
+            p->drawText(x+10, y, startFinish);
+        }
+        else
+        {
+            // Flag left
+            p->drawLine( x,    y-25, x-10, y-20);
+            p->drawLine( x-10, y-20, x,    y-15);
+            p->drawLine( x,    y-15, x,    y-25);
+
+            // Draw start or finish
+            p->drawText(x+10, y, startFinish);
+        }    
+    }
+    else if (y <= (rD->getTop() + 25))
+    {
+        // downside
+        p->drawLine(x,y,x,y+15);
+        if (x <= (rD->getRight()-20))
+        {
+            // flag right
+            p->drawLine( x,    y+25, x+10, y+20);
+            p->drawLine( x+10, y+20, x,    y+15);
+            p->drawLine( x,    y+15, x,    y+25);
+
+            // Draw start or finish
+            p->drawText(x+10, y+15, startFinish);
+        }
+        else
+        {
+            // Flag left
+            p->drawLine( x,    y+25, x-10, y+20);
+            p->drawLine( x-10, y+20, x,    y+15);
+            p->drawLine( x,    y+15, x,    y+25);
+
+            // Draw start or finish
+            p->drawText(x+10, y+15, startFinish);
+        }
+    }
+    //p->drawPolygon();
+   // p->drawPolygon( pg,Qt::OddEvenFill);
+    //p->drawPolyline( &pg);
+    //p->drawPoints( pg);
+}
 
 /**
   * Draws route to the route dialog.
@@ -192,6 +323,9 @@ void RouteDialog::changeEvent(QEvent *e)
 /* */
 void RouteDialog::paintEvent(QPaintEvent *)
 {
+    // Check login
+    checkLogin();
+
     int type = 0; //  0 for 2d, 1 for 3d
     int startx, starty; // Starting point of the route
     int i, maxi;
@@ -199,17 +333,18 @@ void RouteDialog::paintEvent(QPaintEvent *)
     int x1Screen, y1Screen, x2Screen, y2Screen;
     Vector v1, v2;
     QPainter painter(this);
+    int startStop = 0;
 
     painter.setRenderHint(QPainter::Antialiasing, true);
-    painter.setPen(QPen((Qt::black),2));
+    painter.setPen(QPen((Qt::white),2));
     painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
 
     // Draw route window frame
     /*painter.drawLine(left,top,right,top);
     painter.drawLine(right,top,right,bottom);
     painter.drawLine(left,top,left,bottom);
-    painter.drawLine(left,bottom,right,bottom);
-    */
+    painter.drawLine(left,bottom,right,bottom);*/
+
     maxi = vertexList.size();
 
     for (i=0; i<maxi-1; i++)
@@ -239,35 +374,64 @@ void RouteDialog::paintEvent(QPaintEvent *)
         {
             // Starting point
             startx = x1Screen; starty = y1Screen;
-            painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
+           // painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
+           drawFlag( this, &painter,  x1Screen,  y1Screen, "Start" );
         }
         painter.drawLine( x1Screen, y1Screen, x2Screen, y2Screen);
     }
     // Show the endig point if different than the starting point
     if (x2Screen != startx || y2Screen != starty)
     {
-        painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
+        //painter.drawEllipse( x2Screen-5, y2Screen-5, 10, 10);
+        drawFlag( this, &painter, x2Screen, y2Screen, "Finish" );
     }
-}
 
-void RouteDialog::on_closePushButton_clicked()
-{
-    close();
+    {
+        qreal maxvx, maxvy; // max speed point coordinates
+        qreal maxv;         // max speed
+        Vector v;
+
+        maxv = 0.0;
+        for (i=0; i<maxi-1; i++)
+        {
+            v = vertexList.at(i);
+            if (v.getV() > maxv)
+            {
+                maxv = v.getV();
+                maxvx = v.getX();
+                maxvy = v.getY();
+            }
+        }
+        // Translate world coordinates to screen coordinates
+        x1Screen = left + (maxvx-xmin)/(xmax-xmin)*(right-left);
+        y1Screen = top + (ymax-maxvy)/(ymax-ymin)*(bottom-top);
+
+        // Show max velocity point by yellow circle
+        painter.drawEllipse( x1Screen-5, y1Screen-5, 10, 10);
+        painter.drawEllipse( 650, 225, 10, 10);
+
+        QString jono;
+        //jono = QString("%1 km/h").arg(maxv);
+        jono.sprintf("%.1f km/h", maxv); // Show only 1 decimal
+        ui->speedValueLabel->setText(jono);
+    }
 }
 
-/*
-  * Read route coordinates (latitude, longitude, altitude) from file.
+/**
+  *
   */
-bool RouteDialog::readRouteFromFile( QString &filename)
-{
+bool RouteDialog::readRouteFromFile( QString &routeFile)
+ {
+    QString rFile = routeFile; //Not used
     Vector temp;
     QString rivi;
     QFile file;
 
-    QString fileName = QFileDialog::getOpenFileName(this,
-         tr("Read Route"), "./", tr("Route Files (*.txt)"));
+    //QString fileName = QFileDialog::getOpenFileName(this,
+    //     tr("Read Route"), "./", tr("Route Files (*.txt)"));
 
-    file.setFileName( fileName);
+    //file.setFileName( fileName);
+    file.setFileName( "routetemp.xml");
     if (!file.open(QIODevice::ReadOnly))
     {
         QMessageBox::about(0, "Error", "File not found");
@@ -275,33 +439,84 @@ bool RouteDialog::readRouteFromFile( QString &filename)
     }
 
     vertexList.clear();
+
     while(!file.atEnd())
     {
-        QString str1, str2, str3;
+        int count;
+        bool allRead;
+        QString astr1, astr2, astr3, astr4;
+        QString str1, str2, str3, str4;
         rivi = file.readLine();
 
+        allRead = false;
+        count = 0;
+        while( !allRead)
+        {
+            astr1 = rivi.section(" ", count*4+1, count*4+1); // latitude=""
+            astr2 = rivi.section(" ", count*4+2, count*4+2); // longitude=""
+            astr3 = rivi.section(" ", count*4+3, count*4+3); // altitude=""
+            astr4 = rivi.section(" ", count*4+4, count*4+4); // speed=""
+
+            {
+                double x, y, z, v;
+                str1 = astr1.section('"',1,1);
+                str2 = astr2.section('"',1,1);
+                str3 = astr3.section('"',1,1);
+                str4 = astr4.section('"',1,1);
+            //QString str = QString("%1 %2 %3 %4").arg(str1).arg(str2).arg(str3).arg(str4);
+            //QMessageBox::about(0, "LUKEE", str);
+                /* */
+
+                if (str1.length() > 0)
+                {
+                    x = str2.toDouble();// latitude y-value
+                    y = str1.toDouble();// longitude x-value
+                    z = str3.toDouble();// altitude z-value
+                    v = str4.toDouble();// speed km/h
+               // QString str = QString("%1 %2 %3 %4").arg(x).arg(y).arg(z).arg(v);
+               // QMessageBox::about(0, "LUKEE", str);
+                    temp.setX( x); // Longitude
+                    temp.setY( y); // Latitude
+                    temp.setZ( z); // altitude
+                    temp.setV( v);
+
+                    vertexList.append(temp);
+                    count++;
+                }
+                else
+                {
+                    allRead = true;
+                }
+            }
+        }
+        // Older version
+        /*
         str1 = rivi.section(" ", 0, 0);
         if (str1.compare("Start:") != 0 && str1.compare("Stop:") != 0)
         {
-            str1 = rivi.section(" ",  2, 2); // latitude y-value
-            str2 = rivi.section(" ",  4, 4); // longitude x-value
-            str3 = rivi.section(" ",  6, 6); // altitude z-value
+            str1 = rivi.section(" ", 2, 2); // latitude y-value
+            str2 = rivi.section(" ", 4, 4); // longitude x-value
+            str3 = rivi.section(" ", 6, 6); // altitude z-value
+            str4 = rivi.section(" ", 8, 8); // speed km/h
             //QString str = QString("la: %1 lo: %2 al: %3").arg(str1).arg(str2).arg(str3);
             //QMessageBox::about(0, "LUKEE", str);
 
             if (str1.length() > 0)
             {
-                double x, y, z;
+                double x, y, z, v;
                 x = str2.toDouble();
                 y = str1.toDouble();
                 z = str3.toDouble();
+                v = str4.toDouble();
                 temp.setX( x); // Longitude
                 temp.setY( y); // Latitude
                 temp.setZ( z); // altitude
+                temp.setV( v);
 
                 vertexList.append(temp);
             }
         }
+        */
     }
 
     file.close();
@@ -335,9 +550,9 @@ bool RouteDialog::readRouteFromFile( QString &filename)
      */
 
      return true;
-}
+ }
 
-/*
+/**
   * Find out data range for x-, y- and z-coordinates
   */
 void dataMinMax( void)
@@ -392,7 +607,7 @@ void dataMinMax( void)
     //QMessageBox::about(0,"Tark", jono);
 }
 
-/*
+/**
   * Setting the point where the viewed object is. In the middle of datapoints.
   */
 void setAtPoint( Viewing *v)
@@ -416,7 +631,7 @@ void setAtPoint( Viewing *v)
     * */
 }
 
-/*
+/**
   * Setting the point where the object is viewed by eye.
   */
 void setFromPoint( Viewing *v)
@@ -449,7 +664,8 @@ void setFromPoint( Viewing *v)
 #define RIGHTEDGE  0x02
 #define BOTTOMEDGE 0x04
 #define TOPEDGE    0x08
-/*
+
+/**
   * Returns a code specifying which edge in the viewing pyramid was crossed.
   * There may be more than one.
   */
@@ -466,7 +682,7 @@ int code( qreal x, qreal y, qreal z)
     return c;
 }
 
-/*
+/**
   * Converts clipped world coordinates to screen coordinates.
   */
 void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
@@ -475,7 +691,7 @@ void WORLDtoSCREEN( qreal xWorld, qreal yWorld, int *xScreen, int *yScreen)
    *yScreen = (int) (c*yWorld+d);
 }
 
-/*
+/**
   * Clips the line segment in three-dimensional coordinates to the
   * viewing pyramid.
   */
@@ -550,7 +766,7 @@ void clip3d( qreal x1, qreal y1, qreal z1, qreal x2, qreal y2, qreal z2, int *xs
     //Now ready to draw line( xscreen1, yscreen1, xscreen2, yscreen2);
 }
 
-/*
+/**
   * Transform the segment connecting the two vectors into the viewing plane.
   * clip3d() clips the line if needed.
   */
@@ -574,3 +790,53 @@ void transformseg( Viewing *v, Vector *v1, Vector *v2, int *xscreen1, int *yscre
 
     clip3d(x1,y1,z1,x2,y2,z2, xscreen1, yscreen1, xscreen2, yscreen2 );
 }
+
+/**
+  * This slot function is called when ever new push button clicked.
+  */
+void RouteDialog::on_newPushButton_clicked()
+{
+    close();    // go back to previous dialog
+}
+
+/**
+  * This slot function is called when ever send push button clicked.
+  */
+void RouteDialog::on_sendPushButton_clicked()
+{
+    ui->sendPushButton->setEnabled(false);
+    emit sendroute();
+}
+
+/**
+  * This function is set info text to user.
+  */
+void RouteDialog::setLabelInfoToUser(QString infoText)
+{
+    this->ui->labelInfoToUser->setText(infoText);
+}
+
+/**
+  * This function enable send server button.
+  */
+void RouteDialog::setSendServerButtonEnabled()
+{
+    ui->sendPushButton->setEnabled(true);
+}
+
+/**
+  * This function check login and set send route to server button disabled/enabled.
+  */
+void RouteDialog::checkLogin()
+{
+    if (loginSaved())
+    {
+        ui->sendPushButton->setEnabled(true);
+        ui->labelInfoToUser->setText("");
+    }
+    else
+    {
+        ui->sendPushButton->setEnabled(false);
+        ui->labelInfoToUser->setText("You're not logged! Please register or log in.");
+    }
+}