Changes in measuredialog.cpp, .h and carmainwindow.cpp, .h.
authorJanne Änäkkälä <Janne_anakkala@hotmail.com>
Mon, 1 Mar 2010 13:06:31 +0000 (15:06 +0200)
committerJanne Änäkkälä <Janne_anakkala@hotmail.com>
Mon, 1 Mar 2010 13:06:31 +0000 (15:06 +0200)
Added timer in measuredialog which updates labels when interval time expires. Added slot function speedAchieved() in carmainwindow which opens result dialog when speed has been achieved.

Client/UI.pro
Client/carmainwindow.cpp
Client/carmainwindow.h
Client/measuredialog.cpp
Client/measuredialog.h
Client/measuredialog.ui

index c9c3c77..6c55c45 100644 (file)
@@ -9,11 +9,15 @@ SOURCES += main.cpp \
     carmainwindow.cpp \
     resultdialog.cpp \
     stringlistmodel.cpp \
-    measuredialog.cpp
+    measuredialog.cpp \
+    accelerometer.cpp \
+    calculate.cpp
 HEADERS += carmainwindow.h \
     resultdialog.h \
     stringlistmodel.h \
-    measuredialog.h
+    measuredialog.h \
+    accelerometer.h \
+    calculate.h
 FORMS += carmainwindow.ui \
     resultdialog.ui \
     measuredialog.ui
index ace503b..60b80e6 100644 (file)
@@ -63,7 +63,17 @@ void CarMainWindow::on_listView_clicked(QModelIndex index)
   */
 void CarMainWindow::on_autoStartButton_clicked()
 {
-   result->show();
+    if(measure)
+    {
+        delete measure;
+        measure = NULL;
+        measure = new MeasureDialog();
+    }
+
+    connect(measure, SIGNAL(speedAchieved()), this, SLOT(openResultView()));
+
+    // Show measure dialog.
+    measure->show();
 }
 
 /**
@@ -93,3 +103,12 @@ void CarMainWindow::initSpeedListView()
     QAbstractItemModel *model = new StringListModel(numbers);
     ui->listView->setModel(model);
 }
+
+/**
+  *This slot function is called when speed is achieved in measure dialog. Opens result dialog.
+  */
+void CarMainWindow::openResultView()
+{
+    // Show result dialog.
+    result->show();
+}
index 054b075..1e1e83d 100644 (file)
@@ -41,6 +41,7 @@ private slots:
     void on_autoStartButton_clicked();
     void on_listView_clicked(QModelIndex index);
     void updateUnitCompoBox(QString unit);
+    void openResultView();
 };
 
 #endif // CARMAINWINDOW_H
index 11944f2..892b9b9 100644 (file)
@@ -1,13 +1,32 @@
 #include "measuredialog.h"
 #include "ui_measuredialog.h"
 
+
+/**
+  *Constructor of this class.
+  *@param QWidget pointer to parent object. By default the value is NULL.
+  */
 MeasureDialog::MeasureDialog(QWidget *parent) :
     QDialog(parent),
     ui(new Ui::MeasureDialog)
 {
     ui->setupUi(this);
+    ui->labelInfo->setText("Accelerate to 100 km/h");
+    time = 0;
+    speed = 0;
+    timer = new QTimer();
+
+    accelerometer = new Accelerometer();
+    //accelerometer->start();
+
+    timer->setInterval(1000);
+    timer->start();
+    connect(this->timer, SIGNAL(timeout()), this, SLOT(after_timeout()));
 }
 
+/**
+  *Destructor of this class.  Should be used to release all allocated resources.
+  */
 MeasureDialog::~MeasureDialog()
 {
     delete ui;
@@ -24,3 +43,47 @@ void MeasureDialog::changeEvent(QEvent *e)
         break;
     }
 }
+
+/**
+  *This slot function is called when timer gives timeout signal.
+  */
+void MeasureDialog::after_timeout()
+{
+    QString timeString, speedString;
+    time++;
+    speed = accelerometer->getCurrentSpeed();
+
+    // If speed is over 100 km/h emits speedAchieved() signal and close this dialog.
+    if(speed>=100.0)
+    {
+        timer->stop();
+        //accelerometer->stop();
+        time = 0;
+        speed = 0;
+        emit this->speedAchieved();
+        this->close();
+
+    }
+
+    // Updates speed and time.
+    else
+    {
+        timeString.setNum(time);
+        speedString.setNum(speed);
+        ui->labelSpeed->setText(speedString);
+        ui->labelTime->setText(timeString);
+        timer->start();
+    }
+}
+
+/**
+  *This slot function is called when Abort button is clicked.
+  */
+void MeasureDialog::on_pushButtonAbort_clicked()
+{
+    timer->stop();
+    //accelerometer->stop();
+    time = 0;
+    speed = 0;
+    this->close();
+}
index 24a3773..b33450a 100644 (file)
@@ -1,7 +1,9 @@
 #ifndef MEASUREDIALOG_H
 #define MEASUREDIALOG_H
 
+#include "accelerometer.h"
 #include <QDialog>
+#include <QTimer>
 
 namespace Ui {
     class MeasureDialog;
@@ -18,6 +20,20 @@ protected:
 
 private:
     Ui::MeasureDialog *ui;
+
+    QTimer *timer;
+    Accelerometer *accelerometer;
+
+
+    int time;
+    qreal speed;
+
+    signals:
+    void speedAchieved();
+
+private slots:
+    void on_pushButtonAbort_clicked();
+    void after_timeout();
 };
 
 #endif // MEASUREDIALOG_H
index cda5b05..5f7954d 100644 (file)
@@ -13,7 +13,7 @@
   <property name="windowTitle">
    <string>Dialog</string>
   </property>
-  <widget class="QLabel" name="label_7">
+  <widget class="QLabel" name="labelInfo">
    <property name="geometry">
     <rect>
      <x>130</x>
     <item>
      <layout class="QVBoxLayout" name="verticalLayout">
       <item>
-       <widget class="QLabel" name="label">
+       <widget class="QLabel" name="labelTimeHeader">
         <property name="text">
          <string>Time:</string>
         </property>
        </widget>
       </item>
       <item>
-       <widget class="QLabel" name="label_2">
+       <widget class="QLabel" name="labelSpeedHeader">
         <property name="text">
          <string>Speed:</string>
         </property>
        </widget>
       </item>
-      <item>
-       <widget class="QLabel" name="label_3">
-        <property name="text">
-         <string>Distance:</string>
-        </property>
-       </widget>
-      </item>
      </layout>
     </item>
     <item>
      <layout class="QVBoxLayout" name="verticalLayout_2">
       <item>
-       <widget class="QLabel" name="label_6">
+       <widget class="QLabel" name="labelTime">
         <property name="text">
-         <string>14 s</string>
+         <string>0</string>
         </property>
        </widget>
       </item>
       <item>
-       <widget class="QLabel" name="label_4">
+       <widget class="QLabel" name="labelSpeed">
         <property name="text">
          <string>86 km/h</string>
         </property>
        </widget>
       </item>
-      <item>
-       <widget class="QLabel" name="label_5">
-        <property name="text">
-         <string>120 m</string>
-        </property>
-       </widget>
-      </item>
      </layout>
     </item>
    </layout>