Files
Rennbahn/training.cpp
2017-12-25 08:12:10 +01:00

267 lines
9.5 KiB
C++

#include "training.h"
#include "qvectorhelper.h"
#include "timemodel.h"
#include "ui_training.h"
#include "unistd.h"
#include <QListWidgetItem>
#include <QObject>
#include <QShortcut>
#include <iostream>
#include <sstream>
#include <string>
using std::cout;
using std::endl;
using std::string;
using std::vector;
Training::Training(QWidget * parent, DataBase * db)
: QMainWindow(parent), ui(new Ui::Training) {
ui->setupUi(this);
this->db = db;
string statement =
"select mindestrundendauer from renndauer where id like 1";
this->minimumTime = this->db->getData(statement, 1)[0][0].toInt();
this->finished = false;
firstTimeDea = true;
firstTimeShell = true;
started = true;
paused = false;
this->Hardware = new HardwareSetup;
Hardware->start();
QObject::connect(Hardware, SIGNAL(Dea(int, int)), this,
SLOT(deaSlot(int, int)));
QObject::connect(Hardware, SIGNAL(Shell(int, int)), this,
SLOT(shellSlot(int, int)));
// QObject::connect(this->ui->pBNextRace, SIGNAL(clicked()), this,
// SLOT(prepareNextRace())); QObject::connect(this->ui->pBStop,
// SIGNAL(clicked()), this, SLOT(stopClicked()));
QShortcut * shortcut = new QShortcut(QKeySequence("Ctrl+Q"), this);
QShortcut * shellReset = new QShortcut(QKeySequence("Ctrl+s"), this);
QShortcut * deaReset = new QShortcut(QKeySequence("Ctrl+d"), this);
QObject::connect(shortcut, SIGNAL(activated()), this, SLOT(close()));
QObject::connect(shellReset, SIGNAL(activated()), this, SLOT(ResetShell()));
QObject::connect(deaReset, SIGNAL(activated()), this, SLOT(ResetDea()));
QObject::connect(this->ui->pBReset, SIGNAL(clicked()), this, SLOT(Reset()));
QObject::connect(this->ui->pBResetDea, SIGNAL(clicked()), this,
SLOT(ResetDea()));
QObject::connect(this->ui->pBResetShell, SIGNAL(clicked()), this,
SLOT(ResetShell()));
// fill minSecTimes vector
statement = "select minsec1, minsec2, minsec3 from renndauer order by id "
"DESC limit 1";
vector<vector<QString>> res;
res = db->getData(statement, 3);
this->minSecTime.append(res[0][0].toInt());
this->minSecTime.append(res[0][1].toInt());
this->minSecTime.append(res[0][2].toInt());
}
void Training::ResetShell() {
this->VecShell.clear();
// this->ui->lWShellTime->clear();
timeModelShell = new TimeModel(VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell);
this->ui->lBestZeitShell->setText("");
this->ui->lCurRoundTimeShell->setText("");
this->firstTimeShell = true;
}
void Training::ResetDea() {
this->VecDea.clear();
// this->ui->lWDeaTime->clear();
timeModelDea = new TimeModel(VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea);
this->ui->lBestZeitDea->setText("");
this->ui->lCurRoundDea->setText("");
this->firstTimeDea = true;
}
void Training::Reset() {
this->ResetDea();
this->ResetShell();
}
void Training::prepareNextRace() {
this->firstTimeShell = true;
this->firstTimeDea = true;
// this->ui->lWShellTime->clear();
// this->ui->lWDeaTime->clear();
this->ui->lBestZeitDea->setText("");
this->ui->lBestZeitShell->setText("");
this->ui->lCurRoundTimeShell->setText("");
this->ui->lCurRoundDea->setText("");
this->finished = false;
this->paused = false;
this->started = true;
}
void Training::shellSlot(int time, int sector) {
// slot is called when a sector is finished
// time is given in ms
// counting of sector beginns at 1
cout << "received Shelltime: " << time << sector << endl;
if (started && !paused && !this->finished) {
if (firstTimeShell) {
firstTimeShell = false;
}
else {
switch (sector) {
case 1:
VecShell.push_back(QVector<int>());
VecShell.last().push_back(time);
timeModelShell = new TimeModel(VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell);
break;
case 2:
if (VecShell.size() > 0) {
if (VecShell.last().size() == 1) {
// cout << time << sector <<
// endl;
VecShell.last().push_back(time);
timeModelShell =
new TimeModel(VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell);
}
}
break;
case 3:
if (VecShell.size() > 0) {
// cout << "Time sec 3: " << time << endl;
if (VecShell.last().size() == 2) {
// cout << time << sector <<
// endl;
VecShell.last().push_back(time);
// add sum to vector
VecShell.last().push_back(
QVectorHelper::getCurTime(VecShell.last()));
timeModelShell =
new TimeModel(VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell);
}
// best time on widget
if (QVectorHelper::getCurTime(VecShell.last()) <=
QVectorHelper::getMin(VecShell)) {
ui->lBestZeitShell->setText(QString::number(
(double)QVectorHelper::getMin(VecShell) / 1000));
}
ui->lCurRoundTimeShell->setText(QString::number(
(double)QVectorHelper::getCurTime(VecShell.last()) /
1000));
}
break;
}
ui->lWShellTime->scrollToBottom();
ui->lWShellTime->horizontalHeader()->setSectionResizeMode(
QHeaderView::Stretch);
}
}
}
void Training::closeEvent(QCloseEvent * event) {
Hardware->setStop();
// delete this->Hardware;
}
void Training::deaSlot(int time, int sector) {
// slot is called when a sector is finished
// time is given in ms
// counting of sector beginns at 1
cout << "received Deatime: " << time << sector << endl;
if (started && !paused && !this->finished) {
if (firstTimeDea) {
firstTimeDea = false;
}
else {
switch (sector) {
case 1:
VecDea.push_back(QVector<int>());
VecDea.last().push_back(time);
// cout << "Dea Sektor 1" << endl;
timeModelDea = new TimeModel(VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea);
break;
case 2:
if (VecDea.size() > 0) {
if (VecDea.last().size() == 1) {
// cout << time << sector <<
// endl;
VecDea[VecDea.size() - 1].push_back(time);
timeModelDea = new TimeModel(VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea);
}
else {
// VecDea[VecDea.size()-1].append(9999);
}
}
break;
case 3:
if (VecDea.size() > 0) {
// cout << "Time sec 3: " << time << endl;
if (VecDea.last().size() == 2) {
// cout << time << sector <<
// endl;
VecDea.last().push_back(time);
// add sum to vector
VecDea.last().push_back(
QVectorHelper::getCurTime(VecDea.last()));
timeModelDea = new TimeModel(VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea);
}
// best time on widget
// cout << "cur time: " <<
// QVectorHelper::getCurTime(VecDea.last())
// << endl; cout << "min time: " <<
// QVectorHelper::getMin(VecDea) <<
// endl;
if (QVectorHelper::getCurTime(VecDea.last()) <=
QVectorHelper::getMin(VecDea)) {
ui->lBestZeitDea->setText(QString::number(
(double)QVectorHelper::getMin(VecDea) / 1000));
}
ui->lCurRoundDea->setText(QString::number(
(double)QVectorHelper::getCurTime(VecDea.last()) /
1000));
}
break;
}
ui->lWDeaTime->scrollToBottom();
ui->lWDeaTime->horizontalHeader()->setSectionResizeMode(
QHeaderView::Stretch);
}
}
}
Training::~Training() {
cout << "destr Training" << endl;
this->Hardware->setStop();
usleep(1000000);
delete this->Hardware;
delete ui;
}