#include "training.h" #include "ui_training.h" #include #include #include #include #include #include "unistd.h" #include #include "timemodel.h" #include "qvectorhelper.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(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())); } void Training::ResetShell(){ this->VecShell.clear(); // this->ui->lWShellTime->clear(); timeModelShell = new TimeModel(VecShell, 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, this); this->ui->lWDeaTime->setModel(timeModelDea); 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 = true; } void Training::shellSlot(int time, int sector){ cout << "Shell slot" << endl; // slot is called when a sector is finished // time is given in ms // counting of sector beginns at 1 if(started && !paused && !this->finished){ if(firstTimeShell){ firstTimeShell = false; } else{ switch(sector){ case 1: VecShell.push_back(QVector()); VecShell.last().push_back(time); timeModelShell = new TimeModel(VecShell, 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, this); this->ui->lWShellTime->setModel(timeModelShell); } else{ } } 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, 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 if(started && !paused && !this->finished){ if(firstTimeDea){ firstTimeDea = false; } else{ switch(sector){ case 1: VecDea.push_back(QVector()); VecDea.last().push_back(time); cout << "Dea Sektor 1" << endl; timeModelDea = new TimeModel(VecDea, 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, 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, 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() { this->Hardware->setStop(); usleep(1000); delete this->Hardware; delete ui; }