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.
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
*/
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();
}
/**
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();
+}
void on_autoStartButton_clicked();
void on_listView_clicked(QModelIndex index);
void updateUnitCompoBox(QString unit);
+ void openResultView();
};
#endif // CARMAINWINDOW_H
#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;
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();
+}
#ifndef MEASUREDIALOG_H
#define MEASUREDIALOG_H
+#include "accelerometer.h"
#include <QDialog>
+#include <QTimer>
namespace Ui {
class MeasureDialog;
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
<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>