Files
Rennbahn/training.cpp

202 lines
6.1 KiB
C++

#include "training.h"
#include "ui_training.h"
#include <QObject>
#include <string>
#include <sstream>
#include <iostream>
#include <QShortcut>
#include "unistd.h"
using std::vector;
using std::string;
using std::cout;
using std::endl;
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()), this, SLOT(deaSlot()));
QObject::connect(Hardware, SIGNAL(Shell()), this, SLOT(shellSlot()));
//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()));
}
void Training::ResetShell(){
this->VecShell.clear();
this->ui->lWShellTime->clear();
this->ui->lBestZeitShell->setText("");
this->ui->lCurRoundTimeShell->setText("");
this->firstTimeShell = true;
}
void Training::ResetDea(){
this->VecDea.clear();
this->ui->lWDeaTime->clear();
this->ui->lBestZeitDea->setText("");
this->ui->lCurRoundDea->setText("");
this->firstTimeDea = true;
}
void Training::Reset(){
this->VecShell.clear();
this->VecDea.clear();
this->ui->lWDeaTime->clear();
this->ui->lWShellTime->clear();
this->ui->lBestZeitDea->setText("");
this->ui->lBestZeitShell->setText("");
this->ui->lCurRoundTimeShell->setText("");
this->ui->lCurRoundDea->setText("");
this->firstTimeShell = true;
this->firstTimeDea = true;
}
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 = false;
}
void Training::shellSlot(){
if(started && !paused && !this->finished){
if(firstTimeShell){
firstTimeShell = false;
counterShell.start();
}
else{
long zeit = counterShell.getTime();
VecShell.push_back(zeit);
QListWidgetItem *item = new QListWidgetItem;
if(getMinimum(VecShell) == zeit && VecShell.size() > 1){
item->setBackgroundColor(Qt::green);
}
if(zeit < this->minimumTime){
item->setBackgroundColor(Qt::red);
this->firstTimeShell = true;
}
item->setText(QString::number((double)zeit/1000));
ui->lWShellTime->addItem(item);
ui->lWShellTime->scrollToBottom();
if(getMinimum(VecShell) > 0){
ui->lBestZeitShell->setText(QString::number((double)getMinimum(VecShell)/1000));
}
ui->lCurRoundTimeShell->setText(QString::number((double)zeit/1000));
}
}
}
void Training::closeEvent(QCloseEvent *event){
}
void Training::deaSlot(){
if(started && !paused && !this->finished){
if(firstTimeDea){
firstTimeDea = false;
counterDea.start();
}
else{
/*
long zeit = counterDea.getTime();
VecDea.push_back(zeit);
QListWidgetItem *item = new QListWidgetItem;
if(getMinimum(VecDea) == zeit && VecDea.size() > 1){
item->setBackgroundColor(Qt::green);
}
item->setText(QString::number((double)zeit/1000));
ui->lWDeaTime->addItem(item);
ui->lWDeaTime->scrollToBottom();
ui->lBestZeitDea->setText(QString::number((double)getMinimum(VecDea)/1000));
ui->lCurRoundDea->setText(QString::number((double)zeit/1000));
*/
//
long zeit = counterDea.getTime();
VecDea.push_back(zeit);
QListWidgetItem *item = new QListWidgetItem;
if(getMinimum(VecDea) == zeit && VecDea.size() > 1){
item->setBackgroundColor(Qt::green);
}
if(zeit < this->minimumTime){
item->setBackgroundColor(Qt::red);
this->firstTimeDea = true;
}
item->setText(QString::number((double)zeit/1000));
ui->lWDeaTime->addItem(item);
ui->lWDeaTime->scrollToBottom();
if(getMinimum(VecDea) > 0){
ui->lBestZeitDea->setText(QString::number((double)getMinimum(VecDea)/1000));
}
ui->lCurRoundDea->setText(QString::number((double)zeit/1000));
}
}
}
long Training::getMinimum(std::vector<long> a){
long minimum = -1;
for(unsigned int i = 0; i < a.size(); i++){
if(minimum < 0){
if(a[i] >= this->minimumTime){
minimum = a[i];
}
}
else{
if(a[i] < minimum && a[i] >= this->minimumTime){
minimum = a[i];
}
}
}
return minimum;
}
Training::~Training()
{
this->Hardware->setStop();
usleep(1000);
delete this->Hardware;
delete ui;
}