Files
Rennbahn/training.cpp

368 lines
14 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";
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 = 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, 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() {
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;
this->ui->lBridgeShellTop->setText("");
this->ui->lStraightShellTop->setText("");
this->ui->lCurvesShellTop->setText("");
this->ui->lDeltaTopTimeShell->setText("");
this->ui->lShellLaps->setText("0");
}
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;
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 = new TimeModel(VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell);
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 =
new TimeModel(VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell);
}
}
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 =
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(
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() == 3) {
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();
Q_UNUSED(event);
}
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);
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 = new TimeModel(VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea);
}
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 = 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(
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() == 3) {
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 this->Hardware;
delete ui;
}