Files
Rennbahn/training.cpp

375 lines
14 KiB
C++

#include "training.h"
#include "qvectorhelper.h"
#include "timemodel.h"
#include "ui_training.h"
#include "unistd.h"
#include <QCloseEvent>
#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";
vector<vector<QString>> mintime = this->db->getData(statement, 1);
if (mintime.size() > 0) {
if (mintime.at(0).size() > 0) {
this->minimumTime = mintime.at(0).at(0).toInt();
}
}
else {
cout << "missing database :(" << endl;
}
this->finished = false;
firstTimeDea = true;
firstTimeShell = true;
started = true;
paused = false;
this->Hardware = std::make_shared<HardwareSetup>(this->db);
Hardware->start();
QObject::connect(Hardware.get(), SIGNAL(Dea(int, int)), this,
SLOT(deaSlot(int, int)));
QObject::connect(Hardware.get(), 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()));
shortcut = std::make_shared<QShortcut>(QKeySequence("Ctrl+Q"), this);
shellReset = std::make_shared<QShortcut>(QKeySequence("Ctrl+s"), this);
deaReset = std::make_shared<QShortcut>(QKeySequence("Ctrl+d"), this);
QObject::connect(shortcut.get(), SIGNAL(activated()), this, SLOT(close()));
QObject::connect(shellReset.get(), SIGNAL(activated()), this,
SLOT(ResetShell()));
QObject::connect(deaReset.get(), 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, mindestRundenDauer from "
"renndauer order by id "
"DESC limit 1";
vector<vector<QString>> res;
try {
res = db->getData(statement, 4);
this->minSecTime.append(res.at(0).at(0).toInt());
this->minSecTime.append(res.at(0).at(1).toInt());
this->minSecTime.append(res.at(0).at(2).toInt());
this->minSecTime.append(res.at(0).at(3).toInt());
QVectorHelper::minSec1 = this->minSecTime.at(0);
QVectorHelper::minSec2 = this->minSecTime.at(1);
QVectorHelper::minSec3 = this->minSecTime.at(2);
QVectorHelper::minTrackTime = this->minSecTime.at(3);
} catch (const std::exception & e) {
Q_UNUSED(e);
cout << "missing database :(" << endl;
}
}
void Training::ResetShell() {
cout << "reset shell" << endl;
this->VecShell.clear();
// this->ui->lWShellTime->clear();
timeModelShell = std::make_unique<TimeModel>(VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell.get());
this->ui->lBestZeitShell->setText("");
this->ui->lCurRoundTimeShell->setText("");
this->firstTimeShell = true;
this->ui->lBridgeShellTop->setText("");
this->ui->lStraightShellTop->setText("");
this->ui->lCurvesShellTop->setText("");
this->ui->lDeltaTopTimeShell->setText("");
this->ui->lShellLaps->setText("0");
}
void Training::ResetDea() {
cout << "reset dea" << endl;
this->VecDea.clear();
// this->ui->lWDeaTime->clear();
timeModelDea = std::make_unique<TimeModel>(VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea.get());
this->ui->lBestZeitDea->setText("");
this->ui->lCurRoundDea->setText("");
this->firstTimeDea = true;
this->ui->lBridgeDeaTop->setText("");
this->ui->lStraightDeaTop->setText("");
this->ui->lCurvesDeaTop->setText("");
this->ui->lDeltaTopTimeDea->setText("");
this->ui->lDeaLaps->setText("0");
}
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->ui->lBridgeShellTop->setText("");
this->ui->lStraightShellTop->setText("");
this->ui->lCurvesShellTop->setText("");
this->ui->lDeltaTopTimeShell->setText("");
this->ui->lBridgeDeaTop->setText("");
this->ui->lStraightDeaTop->setText("");
this->ui->lCurvesDeaTop->setText("");
this->ui->lDeltaTopTimeDea->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 =
std::make_unique<TimeModel>(VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell.get());
this->ui->lBridgeShellTop->setText(QString::number(
static_cast<double>(QVectorHelper::getMinSec1(VecShell)) /
1000));
break;
case 2:
if (VecShell.size() > 0) {
if (VecShell.last().size() == 1) {
// cout << time << sector <<
// endl;
VecShell.last().push_back(time);
timeModelShell = std::make_unique<TimeModel>(
VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell.get());
}
}
this->ui->lStraightShellTop->setText(QString::number(
static_cast<double>(QVectorHelper::getMinSec2(VecShell)) /
1000));
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 = std::make_unique<TimeModel>(
VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell.get());
}
// best time on widget
if (QVectorHelper::getCurTime(VecShell.last()) <=
QVectorHelper::getMin(VecShell)) {
ui->lBestZeitShell->setText(QString::number(
static_cast<double>(
QVectorHelper::getMin(VecShell)) /
1000));
}
ui->lCurRoundTimeShell->setText(QString::number(
static_cast<double>(
QVectorHelper::getCurTime(VecShell.last())) /
1000));
}
this->ui->lCurvesShellTop->setText(QString::number(
static_cast<double>(QVectorHelper::getMinSec3(VecShell)) /
1000));
theoreticalMinShell = 9999;
theoreticalMinShell = QVectorHelper::getMinSec1(VecShell) +
QVectorHelper::getMinSec2(VecShell) +
QVectorHelper::getMinSec3(VecShell);
deltaShell =
QVectorHelper::getMin(VecShell) - theoreticalMinShell;
this->ui->lDeltaTopTimeShell->setText(
QString::number(static_cast<double>(deltaShell) / 1000));
// count valid laps
int validLaps = 0;
for (auto vec : VecShell) {
if (vec.size() == 4) {
validLaps += 1;
}
}
this->ui->lShellLaps->setText(QString::number(validLaps));
break;
}
ui->lWShellTime->scrollToBottom();
ui->lWShellTime->horizontalHeader()->setSectionResizeMode(
QHeaderView::Stretch);
}
}
}
void Training::closeEvent(QCloseEvent * event) {
Hardware->setStop();
event->accept();
}
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 =
std::make_unique<TimeModel>(VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea.get());
this->ui->lBridgeDeaTop->setText(QString::number(
static_cast<double>(QVectorHelper::getMinSec1(VecDea)) /
1000));
break;
case 2:
if (VecDea.size() > 0) {
if (VecDea.last().size() == 1) {
// cout << time << sector <<
// endl;
VecDea[VecDea.size() - 1].push_back(time);
timeModelDea = std::make_unique<TimeModel>(
VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea.get());
}
else {
// VecDea[VecDea.size()-1].append(9999);
}
this->ui->lStraightDeaTop->setText(QString::number(
static_cast<double>(QVectorHelper::getMinSec2(VecDea)) /
1000));
}
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 = std::make_unique<TimeModel>(
VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea.get());
}
// 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(
static_cast<double>(QVectorHelper::getMin(VecDea)) /
1000));
}
ui->lCurRoundDea->setText(QString::number(
static_cast<double>(
QVectorHelper::getCurTime(VecDea.last())) /
1000));
this->ui->lCurvesDeaTop->setText(QString::number(
static_cast<double>(QVectorHelper::getMinSec3(VecDea)) /
1000));
theoreticalMinDea = 9999;
theoreticalMinDea = QVectorHelper::getMinSec1(VecDea) +
QVectorHelper::getMinSec2(VecDea) +
QVectorHelper::getMinSec3(VecDea);
deltaDea =
QVectorHelper::getMin(VecDea) - theoreticalMinDea;
this->ui->lDeltaTopTimeDea->setText(
QString::number(static_cast<double>(deltaDea) / 1000));
}
int validLaps = 0;
for (auto vec : VecDea) {
if (vec.size() == 4) {
validLaps += 1;
}
}
this->ui->lDeaLaps->setText(QString::number(validLaps));
break;
}
ui->lWDeaTime->scrollToBottom();
ui->lWDeaTime->horizontalHeader()->setSectionResizeMode(
QHeaderView::Stretch);
}
}
}
Training::~Training() {
cout << "destr Training" << endl;
this->Hardware->setStop();
usleep(1000000);
delete ui;
}