Added settingsdialog functionality
[speedfreak] / Client / resultdialog.cpp
index 3313f73..c650ce2 100644 (file)
@@ -8,16 +8,27 @@
 
 #include "resultdialog.h"
 #include "ui_resultdialog.h"
+#include "math.h"
 #include <QPainter>
+#include <QPicture>
 
 const int DIAGRAM_WIDTH = 400;
 const int DIAGRAM_HEIGHT = 300;
 
-const int DIAGRAMGAP100KMH = 30;
+const int DIAGRAMGAP10KMH = 300;
+const int DIAGRAMGAP20KMH = 150;
+const int DIAGRAMGAP30KMH = 100;
 const int DIAGRAMGAP40KMH = 75;
-const int DIAGRAMGAP60KMH = 50;
 const int DIAGRAMGAP50KMH = 60;
+const int DIAGRAMGAP60KMH = 50;
+const double DIAGRAMGAP70KMH = 42.86;
 const double DIAGRAMGAP80KMH = 37.5;
+const double DIAGRAMGAP90KMH = 33.33;
+const int DIAGRAMGAP100KMH = 30;
+
+const int DIAGRAMGAP5S = 80;
+const int DIAGRAMGAP10S = 40;
+const int DIAGRAMGAP20S = 20;
 
 const QPoint diagramStemStart(70, 330);
 const QPoint diagramStemEnd(70, 30);
@@ -43,9 +54,25 @@ ResultDialog::ResultDialog(QWidget *parent) :
     ui(new Ui::ResultDialog)
 {
     ui->setupUi(this);
+    timeAxelLength = 10;
     speedList << "0" << "10" << "20" << "30" << "40" << "50" << "60" << "70" << "80" << "90" << "100" ;
     timeList << "0" << "1" << "2" << "3" << "4" << "5" << "6" << "7" << "8" << "9" << "10" << "11"
             << "12" << "13" << "14" << "15" << "16" << "17" << "18" << "19" << "20";
+    for (int i = 0; i < 11; i++)
+    {
+        timeArray[i] = 0;
+    }
+    /*
+    // TODO check that logging is ok.
+    if (isloggingNotOk)
+    {
+        ui->pushButtonSend->setEnabled(false);
+    }
+    else
+    {
+        ui->pushButtonSend->setEnabled(true);
+    }
+    */
 }
 
 /**
@@ -74,27 +101,57 @@ void ResultDialog::changeEvent(QEvent *e)
  */
 void ResultDialog::paintEvent(QPaintEvent *)
 {
-        QPainter painter(this);
+    QString resultString;
+    resultString.append("Time was ");
+
+    if (this->diagramGapStem == DIAGRAMGAP100KMH)
+    {
+        resultString.append(QString::number(timeArray[10]));
+        this->setWindowTitle("Result for accelerating 100 km/h");
+    }
+
+    else if (this->diagramGapStem == DIAGRAMGAP80KMH)
+    {
+        resultString.append(QString::number(timeArray[8]));
+        this->setWindowTitle("Result for accelerating 80 km/h");
+    }
+
+    else
+    {
+        resultString.append(QString::number(timeArray[4]));
+        this->setWindowTitle("Result for accelerating 40 km/h");
+    }
+
+    QPainter painter(this);
 
     painter.setRenderHint(QPainter::Antialiasing, true);
-    painter.setPen(QPen((Qt::red),2));
+    painter.setPen(QPen((Qt::gray),2));
     QFont font;
+    QFont fontForResult;
     font.setPixelSize(12);
     painter.setFont(font);
     painter.setBrush(QBrush((Qt::yellow), Qt::SolidPattern));
+
+    fontForResult.setPixelSize(50);
+    painter.setFont(fontForResult);
+    painter.drawText(diagramStemStart.x() + 50, diagramStemStart.y() - 150, resultString);
+    painter.setFont(font);
+
+    painter.setPen(QPen((Qt::darkCyan),2));
     painter.drawLine(diagramStemStart, diagramStemEnd);
     painter.drawLine(diagramHorizontalStart, diagramHorizontalEnd);
 
     int currentX = 0;
     int currentY = diagramStemStart.y();
 
-    painter.setPen(QPen((Qt::blue),1));
-    // Draws diagram's X-axel
+    painter.setPen(QPen((Qt::darkCyan),1));
+
     int i = 0;
+    // Draws diagram's X-axel
     while (currentX <= DIAGRAM_WIDTH)
     {
         painter.drawLine(currentX + diagramStemStart.x(), currentY, currentX + diagramStemStart.x(), currentY - 300);
-        painter.drawText(currentX + diagramStemStart.x(), currentY + 20, timeList[i]);
+        painter.drawText(currentX + diagramStemStart.x() - 3, currentY + 20, timeList[i]);
         currentX += this->diagramGapHorizontal;
         i++;
     }
@@ -107,37 +164,90 @@ void ResultDialog::paintEvent(QPaintEvent *)
     while (currentY >= -(DIAGRAM_HEIGHT))
     {
         painter.drawLine(currentX, diagramStemStart.y() + currentY, currentX+400, diagramStemStart.y() + currentY);
-        painter.drawText(currentX - 25, diagramStemStart.y() + currentY, speedList[i]);
+        painter.drawText(currentX - 25, diagramStemStart.y() + currentY + 3, speedList[i]);
         currentY -= this->diagramGapStem;
         i++;
     }
 
-    painter.setPen(QPen((Qt::white),2));
+    painter.setPen(QPen((Qt::red),2));
+
+    int pointsToShow = 0;
+    bool pointsUnderDiagramWidth = true;
+
+    for (i = 0; i < 11 ; i++)
+    {
+        if (points[i].x() > diagramHorizontalEnd.x())
+        {
+            pointsToShow = i;
+            pointsUnderDiagramWidth = false;
+            i = 10;
+        }
+    }
 
     // Draws result line to the diagram
     if (this->diagramGapStem == DIAGRAMGAP100KMH)
     {
-        painter.drawPolyline(points, 11);
+        if (pointsUnderDiagramWidth)
+        {
+            painter.drawPolyline(points, 11);
+        }
+
+        else
+        {
+            painter.drawPolyline(points, pointsToShow);
+        }
     }
 
     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
     {
-        painter.drawPolyline(points, 9);
+        if (pointsUnderDiagramWidth)
+        {
+            painter.drawPolyline(points, 9);
+        }
+
+        else
+        {
+            painter.drawPolyline(points, pointsToShow);
+        }
     }
 
     else if (this->diagramGapStem == DIAGRAMGAP60KMH)
     {
-        painter.drawPolyline(points, 7);
+        if (pointsUnderDiagramWidth)
+        {
+            painter.drawPolyline(points, 7);
+        }
+
+        else
+        {
+            painter.drawPolyline(points, pointsToShow);
+        }
     }
 
     else if (this->diagramGapStem == DIAGRAMGAP50KMH)
     {
-        painter.drawPolyline(points, 6);
+        if (pointsUnderDiagramWidth)
+        {
+            painter.drawPolyline(points, 6);
+        }
+
+        else
+        {
+            painter.drawPolyline(points, pointsToShow);
+        }
     }
 
     else
     {
-        painter.drawPolyline(points, 5);
+        if (pointsUnderDiagramWidth)
+        {
+            painter.drawPolyline(points, 5);
+        }
+
+        else
+        {
+            painter.drawPolyline(points, pointsToShow);
+        }
     }
 }
 
@@ -157,31 +267,31 @@ QPoint ResultDialog::changeMeasuresToDiagramPoint(int aSpeed, qreal aTime)
     if (this->diagramGapStem == DIAGRAMGAP100KMH)
     {
         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/100;
-        timeAsPixels = DIAGRAM_WIDTH*aTime/10;
+        timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
     }
 
     else if (this->diagramGapStem == DIAGRAMGAP80KMH)
     {
         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/80;
-        timeAsPixels = DIAGRAM_WIDTH*aTime/10;
+        timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
     }
 
     else if (this->diagramGapStem == DIAGRAMGAP60KMH)
     {
         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/60;
-        timeAsPixels = DIAGRAM_WIDTH*aTime/10;
+        timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
     }
 
     else if (this->diagramGapStem == DIAGRAMGAP50KMH)
     {
         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/50;
-        timeAsPixels = DIAGRAM_WIDTH*aTime/10;
+        timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
     }
 
     else
     {
         speedAsPixels = DIAGRAM_HEIGHT*aSpeed/40;
-        timeAsPixels = DIAGRAM_WIDTH*aTime/10;
+        timeAsPixels = DIAGRAM_WIDTH*aTime/timeAxelLength;
     }
     point.setY(diagramStemStart.y()-speedAsPixels);
     point.setX(diagramStemStart.x()+timeAsPixels);
@@ -193,6 +303,7 @@ QPoint ResultDialog::changeMeasuresToDiagramPoint(int aSpeed, qreal aTime)
   * Saves the given measures to array.
   * @param pMeasures has information about acceleration.
   */
+/*
 void ResultDialog::saveMeasuresToArray(Measures *pMeasures)
 {
     timeArray[0] = 0;
@@ -207,6 +318,8 @@ void ResultDialog::saveMeasuresToArray(Measures *pMeasures)
     timeArray[9] = pMeasures->getTime90kmh();
     timeArray[10] = pMeasures->getTime100kmh();
 
+    setTimeAxelLength();
+
     for (int i = 0; i < 11; i++)
     {
         points[i] = changeMeasuresToDiagramPoint(speedArray[i], timeArray[i]);
@@ -214,17 +327,7 @@ void ResultDialog::saveMeasuresToArray(Measures *pMeasures)
 
     setTimesIntoLabels();
     this->repaint();
-
-    for (int i = 0; i < 11; i++)
-    {
-        timeArray[i] = 0;
-    }
-}
-
-void ResultDialog::on_pushButtonSend_clicked()
-{
-    emit sendresult();
-}
+}*/
 
 /**
   * Saves the given diagram gap to the member variable.
@@ -311,6 +414,16 @@ void ResultDialog::setTimesIntoLabels()
         ui->labelResult100kmh->hide();
     }
 
+    else if (this->diagramGapStem == DIAGRAMGAP80KMH)
+    {
+        ui->labelResult50kmh->show();
+        ui->labelResult60kmh->show();
+        ui->labelResult70kmh->show();
+        ui->labelResult80kmh->show();
+        ui->labelResult90kmh->hide();
+        ui->labelResult100kmh->hide();
+    }
+
     else
     {
         ui->labelResult50kmh->show();
@@ -322,7 +435,234 @@ void ResultDialog::setTimesIntoLabels()
     }
 }
 
+/**
+  * Sets right timeAxelLength value depending the time which
+  * has spent to reach target speed.
+  */
+void ResultDialog::setTimeAxelLength()
+{
+    if (this->diagramGapStem == DIAGRAMGAP40KMH)
+    {
+        if (timeArray[4] <= 5)
+        {
+            timeAxelLength = 5;
+        }
+
+        else if (timeArray[4] <= 10)
+        {
+            timeAxelLength = 10;
+        }
+
+        else if (timeArray[4] <= 15)
+        {
+            timeAxelLength = 15;
+        }
+
+        else
+        {
+            timeAxelLength = 20;
+        }
+    }
+
+    else if (this->diagramGapStem == DIAGRAMGAP80KMH)
+    {
+        if (timeArray[8] <= 5)
+        {
+            timeAxelLength = 5;
+        }
+
+        else if (timeArray[8] <= 10)
+        {
+            timeAxelLength = 10;
+        }
+
+        else if (timeArray[8] <= 15)
+        {
+            timeAxelLength = 15;
+        }
+
+        else
+        {
+            timeAxelLength = 20;
+        }
+    }
+
+    else
+    {
+        if (timeArray[10] <= 5)
+        {
+            timeAxelLength = 5;
+        }
+
+        else if (timeArray[10] <= 10)
+        {
+            timeAxelLength = 10;
+        }
+
+        else if (timeArray[10] <= 15)
+        {
+            timeAxelLength = 15;
+        }
+
+        else
+        {
+            timeAxelLength = 20;
+        }
+    }
+}
+
+/**
+  * This slot function close result dialog when new run -button has been clicked.
+  */
+void ResultDialog::on_pushButtonNew_clicked()
+{
+    for (int i = 0; i < 11; i++)
+    {
+        timeArray[i] = 0;
+    }
+    this->close();
+}
+
+/**
+  * This slot function emits sendresult signal for sending results to server when
+  * send results -button has been clicked.
+  */
 void ResultDialog::on_pushButtonSend_clicked()
 {
-    emit sendresult();
+    if (this->diagramGapStem == DIAGRAMGAP100KMH)
+    {
+        emit sendresult(timeArray[10]);
+    }
+
+    else if (this->diagramGapStem == DIAGRAMGAP80KMH)
+    {
+        emit sendresult(timeArray[8]);
+    }
+
+    else
+    {
+        emit sendresult(timeArray[4]);
+    }
+}
+
+/**
+  * This public function sets diagram's stem gap
+  * @param pValue is the speed value which determines diagram gap's value
+  */
+void ResultDialog::setEnd(int pValue)
+{
+    switch (pValue)
+    {
+    case 10:
+        this->diagramGapStem = DIAGRAMGAP10KMH;
+        break;
+
+    case 20:
+        this->diagramGapStem = DIAGRAMGAP20KMH;
+        break;
+
+    case 30:
+        this->diagramGapStem = DIAGRAMGAP30KMH;
+        break;
+
+    case 40:
+        this->diagramGapStem = DIAGRAMGAP40KMH;
+        break;
+
+    case 50:
+        this->diagramGapStem = DIAGRAMGAP50KMH;
+        break;
+
+    case 60:
+        this->diagramGapStem = DIAGRAMGAP60KMH;
+        break;
+
+    case 70:
+        this->diagramGapStem = DIAGRAMGAP70KMH;
+        break;
+
+    case 80:
+        this->diagramGapStem = DIAGRAMGAP80KMH;
+        break;
+
+    case 90:
+        this->diagramGapStem = DIAGRAMGAP90KMH;
+        break;
+
+    case 100:
+        this->diagramGapStem = DIAGRAMGAP100KMH;
+        break;
+
+    default:
+        this->diagramGapStem = DIAGRAMGAP100KMH;
+        break;
+    }
+
+}
+
+/**
+  * This public function stores times in timeArray
+  * @param pSpeed is the speed value at the time so we know where store time
+  * @param pTime is the result which needs to be store in timeArray
+  */
+void ResultDialog::setValue(int pSpeed, double pTime)
+{
+    timeArray[0] = 0;
+    if (floor(pTime) <= 5)
+    {
+        this->diagramGapHorizontal = DIAGRAMGAP5S;
+    }
+
+    else if (floor(pTime) <= 10)
+    {
+       this->diagramGapHorizontal = DIAGRAMGAP10S;
+    }
+
+    else
+    {
+        this->diagramGapHorizontal = DIAGRAMGAP20S;
+    }
+
+    switch (pSpeed)
+    {
+    case 10:
+        timeArray[1] = pTime;
+        break;
+    case 20:
+        timeArray[2] = pTime;
+        break;
+    case 30:
+        timeArray[3] = pTime;
+        break;
+    case 40:
+        timeArray[4] = pTime;
+        break;
+    case 50:
+        timeArray[5] = pTime;
+        break;
+    case 60:
+        timeArray[6] = pTime;
+        break;
+    case 70:
+        timeArray[7] = pTime;
+        break;
+    case 80:
+        timeArray[8] = pTime;
+        break;
+    case 90:
+        timeArray[9] = pTime;
+        break;
+    case 100:
+        timeArray[10] = pTime;
+        break;
+    }
+    setTimeAxelLength();
+
+    for (int i = 0; i < 11; i++)
+    {
+        points[i] = changeMeasuresToDiagramPoint(speedArray[i], timeArray[i]);
+    }
+
+    setTimesIntoLabels();
+    this->repaint();
 }