Files
Rennbahn/windowrace.cpp

395 lines
12 KiB
C++

#include "windowrace.h"
#include "ui_windowrace.h"
#include <QObject>
#include <string>
#include <sstream>
#include <iostream>
#include "unistd.h"
using std::vector;
using std::string;
using std::cout;
using std::endl;
WindowRace::WindowRace(DataBase *db, QWidget *parent) :
QMainWindow(parent),
ui(new Ui::WindowRace)
{
ui->setupUi(this);
this->ui->pBNextRace->setEnabled(false);
this->finished = false;
this->db = db;
vector< vector< QString > > res;
res = db->getData("select dauer from renndauer", 1);
this->fahrzeit = res[0][0].toInt();
string statement = "select minimumroundtime from rennen order by id DESC limit 1";
res = db->getData(statement, 1);
this->minimumTime = res[0][0].toInt();
firstTimeDea = true;
firstTimeShell = true;
started = false;
countdownValue = this->fahrzeit;
countdown = new Countdown;
startAmpelThread = new Ampel;
ampelCounter = 0;
ui->pBStart->setFocus();
paused = false;
Hardware = new HardwareSetup;
Hardware->start();
QObject::connect(Hardware, SIGNAL(Dea()), this, SLOT(deaSlot()));
QObject::connect(Hardware, SIGNAL(Shell()), this, SLOT(shellSlot()));
QObject::connect(ui->pBStart, SIGNAL(clicked()), this, SLOT(go()));
QObject::connect(countdown, SIGNAL(CountdownUpdate()), this, SLOT(countdownUpdate()));
QObject::connect(startAmpelThread, SIGNAL(ampelUpdate()), this, SLOT(ampelSlot()));
QObject::connect(startAmpelThread, SIGNAL(ampelUpdate()), this, SLOT(laufcheck()));
QObject::connect(ui->pBBreak, SIGNAL(clicked()), this, SLOT(breakCounter()));
QObject::connect(this->ui->pBNextRace, SIGNAL(clicked()), this, SLOT(prepareNextRace()));
QObject::connect(this->ui->pBStop, SIGNAL(clicked()), this, SLOT(stopClicked()));
ui->lCountdown->setText(timeWrapper(countdownValue));
//Ampelsetup
ui->WAmpel15->hide();
ui->WAmpel25->hide();
ui->hW15->setVisible(true);
ui->hW25->setVisible(true);
ui->WAmpel14->hide();
ui->WAmpel24->hide();
ui->hW14->setVisible(true);
ui->hW24->setVisible(true);
ui->WAmpel13->hide();
ui->WAmpel23->hide();
ui->hW13->setVisible(true);
ui->hW23->setVisible(true);
ui->WAmpel12->hide();
ui->WAmpel22->hide();
ui->hW12->setVisible(true);
ui->hw22->setVisible(true);
ui->WAmpel11->hide();
ui->WAmpel21->hide();
ui->hW11->setVisible(true);
ui->hW21->setVisible(true);
vector< vector<QString> > fahrer, autos, rennid, tableData;
statement = "select id_rennen from aktrennen group by id_rennen order by id_rennen DESC limit 1";
rennid = this->db->getData(statement, 1);
this->renn_id = rennid[0][0].toInt();
statement = "select fahrershellid, autoshellid, fahrerdeaid, autodeaid from aktrennen where id_rennen like "+rennid[0][0].toStdString()+" limit 1";
tableData = db->getData2(statement, 4);
statement = "select name from fahrer where id like " + tableData[0][0].toStdString();
fahrer = this->db->getData2(statement, 1);
statement = "select name from AutoKonfiguration where id_auto like " + tableData[0][1].toStdString()+" order by seit DESC";
autos = this->db->getData2(statement, 1);
this->ui->gbShell->setTitle("Shell, "+fahrer[0][0]+", "+autos[0][0]);
statement = "select name from fahrer where id like " + tableData[0][2].toStdString();
fahrer = this->db->getData2(statement, 1);
statement = "select name from AutoKonfiguration where id_auto like " + tableData[0][3].toStdString()+" order by seit DESC";
autos = this->db->getData2(statement, 1);
this->ui->gbDea->setTitle("Dea, "+fahrer[0][0]+", "+autos[0][0]);
this->shellDriverId = tableData[0][0].toInt();
this->shellCarId = tableData[0][1].toInt();
this->deaDriverId = tableData[0][2].toInt();
this->deaCarId = tableData[0][3].toInt();
}
void WindowRace::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;
this->countdownValue = this->fahrzeit;
this->ampelCounter = 0;
this->startAmpelThread->terminate();
this->countdown->terminate();
this->ui->pBNextRace->setEnabled(false);
this->ui->lCountdown->setText(timeWrapper(countdownValue));
//Ampel Setup
ui->WAmpel15->hide();
ui->WAmpel25->hide();
ui->hW15->setVisible(true);
ui->hW25->setVisible(true);
ui->WAmpel14->hide();
ui->WAmpel24->hide();
ui->hW14->setVisible(true);
ui->hW24->setVisible(true);
ui->WAmpel13->hide();
ui->WAmpel23->hide();
ui->hW13->setVisible(true);
ui->hW23->setVisible(true);
ui->WAmpel12->hide();
ui->WAmpel22->hide();
ui->hW12->setVisible(true);
ui->hw22->setVisible(true);
ui->WAmpel11->hide();
ui->WAmpel21->hide();
ui->hW11->setVisible(true);
ui->hW21->setVisible(true);
}
void WindowRace::setDriverAndCar(vector<QString> vec){
this->ui->gbShell->setTitle("Shell, "+vec[0]+", "+vec[1]);
this->ui->gbDea->setTitle("Dea, "+vec[2]+", "+vec[3]);
}
void WindowRace::stopClicked(){
this->prepareNextRace();
}
void WindowRace::setWindowRennliste(WindowRennliste *ptrInstance){
this->wRennliste = ptrInstance;
QObject::connect(this->ui->pBNextRace, SIGNAL(clicked()), this->wRennliste, SLOT(changeSelection()));
}
void WindowRace::setDriverAndCarId(vector<QString> vec){
this->shellDriverId = vec[0].toInt();
this->shellCarId = vec[1].toInt();
this->deaDriverId = vec[2].toInt();
this->deaCarId = vec[3].toInt();
}
void WindowRace::closeEvent(QCloseEvent *event){
QMessageBox msgBox;
msgBox.setText("Wirklich schliessen?");
msgBox.setInformativeText("");
msgBox.setStandardButtons(QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel);
msgBox.setDefaultButton(QMessageBox::Save);
int ret = msgBox.exec();
this->wRennliste->schliessen();
event->accept();
}
void WindowRace::breakCounter(){
if(!paused){
paused = true;
ui->pBBreak->setText("Weiter");
}
else{
ui->pBBreak->setText("Pause");
paused = false;
}
}
void WindowRace::laufcheck(){
//std::cout << "thread lauft noch" << std::endl;
}
QString WindowRace::timeWrapper(long zahl){
long min = zahl / 60;
int sec = zahl % 60;
QString sec_q;
if(sec < 10){
sec_q = "0"+QString::number(sec);
}
else{
sec_q = QString::number(sec);
}
QString min_q = QString::number(min);
return min_q+":"+sec_q;
}
void WindowRace::countdownUpdate(){
if(!paused){
countdownValue -= 1;
if(countdownValue <= 15){
ui->lCountdown->setStyleSheet("QLabel { color: red; }");
}
if(countdownValue <= -1){
this->finished = true;
this->ui->pBNextRace->setEnabled(true);
}
else{
ui->lCountdown->setText(timeWrapper(countdownValue));
}
}
}
WindowRace::~WindowRace()
{
this->startAmpelThread->setStop();
this->countdown->setStop();
this->Hardware->setStop();
usleep(1000010); //eine Sekunde
delete this->countdown;
delete this->Hardware;
delete this->startAmpelThread;
delete ui;
}
void WindowRace::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);
}
item->setText(QString::number((double)zeit/1000));
ui->lWShellTime->addItem(item);
QString statement = "insert into Zeiten (id_rennen, id_fahrer, id_auto, id_bahn, zeit) values ("+QString::number(this->renn_id)+
", "+QString::number(this->shellDriverId)+", "+QString::number(this->shellCarId)+", 1, "+QString::number(zeit)+")";
//cout << statement.toStdString() << endl;
this->db->setData(statement.toStdString());
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 WindowRace::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);
}
if(zeit < this->minimumTime){
item->setBackgroundColor(Qt::red);
}
item->setText(QString::number((double)zeit/1000));
ui->lWDeaTime->addItem(item);
QString statement = "insert into Zeiten (id_rennen, id_fahrer, id_auto, id_bahn, zeit) values ("+QString::number(this->renn_id)+
", "+QString::number(this->deaDriverId)+", "+QString::number(this->deaCarId)+", 2, "+QString::number(zeit)+")";
//cout << statement.toStdString() << endl;
this->db->setData(statement.toStdString());
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 WindowRace::getMinimum(std::vector<long> a){
long minimum = a[0]; //could be wrong
for(unsigned int i = 0; i < a.size(); i++){
if(a[i] < minimum && a[i] >= this->minimumTime){
minimum = a[i];
}
}
if(minimum >= this->minimumTime){
return minimum;
}
else{
return -1;
}
}
void WindowRace::ampelSlot(){
if(ampelCounter == 5){
ui->WAmpel15->hide();
ui->WAmpel25->hide();
ui->hW15->setVisible(true);
ui->hW25->setVisible(true);
ui->WAmpel14->hide();
ui->WAmpel24->hide();
ui->hW14->setVisible(true);
ui->hW24->setVisible(true);
ui->WAmpel13->hide();
ui->WAmpel23->hide();
ui->hW13->setVisible(true);
ui->hW23->setVisible(true);
ui->WAmpel12->hide();
ui->WAmpel22->hide();
ui->hW12->setVisible(true);
ui->hw22->setVisible(true);
ui->WAmpel11->hide();
ui->WAmpel21->hide();
ui->hW11->setVisible(true);
ui->hW21->setVisible(true);
startAmpelThread->terminate();
started = true;
countdown->start();
}
if(ampelCounter == 4){
ampelCounter = 5;
ui->WAmpel15->setVisible(true);
ui->WAmpel25->setVisible(true);
ui->hW15->hide();
ui->hW25->hide();
}
if(ampelCounter == 3){
ampelCounter = 4;
ui->WAmpel14->setVisible(true);
ui->WAmpel24->setVisible(true);
ui->hW14->hide();
ui->hW24->hide();
}
if(ampelCounter == 2){
ampelCounter = 3;
ui->WAmpel13->setVisible(true);
ui->WAmpel23->setVisible(true);
ui->hW13->hide();
ui->hW23->hide();
}
if(ampelCounter == 1){
ampelCounter = 2;
ui->WAmpel12->setVisible(true);
ui->WAmpel22->setVisible(true);
ui->hW12->hide();
ui->hw22->hide();
}
if(ampelCounter == 0){
ampelCounter = 1;
ui->WAmpel11->setVisible(true);
ui->WAmpel21->setVisible(true);
ui->hW11->hide();
ui->hW21->hide();
}
}
void WindowRace::go(){
startAmpelThread->start();
}