Files
Rennbahn/windowssettings.cpp

599 lines
22 KiB
C++

#include "windowssettings.h"
#include "datahelper.h"
#include "racelistgenerator.h"
#include "ui_windowssettings.h"
#include <QStringListModel>
#include <boost/lexical_cast.hpp>
#include <filesystem>
#include <iostream>
#include <qcolordialog.h>
#include <qmessagebox.h>
#include <stdio.h>
#include <string>
#include <time.h>
#include <vector>
namespace fs = std::filesystem;
using std::cout;
using std::endl;
using std::string;
using std::vector;
WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent)
: QMainWindow(parent), ui(new Ui::WindowsSettings) {
ui->setupUi(this);
QObject::connect(ui->cBInterface, SIGNAL(activated(int)), this,
SLOT(changeInterface()));
QObject::connect(ui->pBSpeichernStrecke, SIGNAL(clicked()), this,
SLOT(StreckeSpeichernSlot()));
QObject::connect(ui->pBAbbrechenStrecke, SIGNAL(clicked()), this,
SLOT(AbbrechenSlot()));
QObject::connect(this->ui->pbAbbrechenDauer, SIGNAL(clicked()), this,
SLOT(AbbrechenSlot()));
QObject::connect(this->ui->pbSaveDauer, SIGNAL(clicked()), this,
SLOT(SaveDauerSlot()));
QObject::connect(this->ui->pbSaveAndExitDauer, SIGNAL(clicked()), this,
SLOT(SaveDauerAndExitSlot()));
QObject::connect(this->ui->lEMinRundenzeit, SIGNAL(textChanged(QString)),
this, SLOT(repaintMinCurLapTime()));
QObject::connect(this->ui->pBcreateListClose, SIGNAL(clicked()), this,
SLOT(createRaceListAndClose()));
QObject::connect(this->ui->pBCancel, SIGNAL(clicked()), this,
SLOT(closeWindow()));
QObject::connect(this->ui->pBNewDriverSave, SIGNAL(clicked()), this,
SLOT(saveNewDriver()));
QObject::connect(this->ui->lVCarSavedCars,
SIGNAL(doubleClicked(const QModelIndex &)), this,
SLOT(listClickEditCar(const QModelIndex &)));
// change color of car
QObject::connect(this->ui->wEditCarColor, SIGNAL(doubleClickedSignal()),
this, SLOT(openEditColor()));
QObject::connect(this->ui->pBEditCarChangeColor, SIGNAL(clicked()), this,
SLOT(openEditColor()));
QObject::connect(this->ui->pBEditCarSave, SIGNAL(clicked()), this,
SLOT(saveEditCar()));
// set color of new car
QObject::connect(this->ui->wNewCarColor, SIGNAL(doubleClickedSignal()),
this, SLOT(openNewColor()));
QObject::connect(this->ui->pBNewCarChangeColor, SIGNAL(clicked()), this,
SLOT(openNewColor()));
QObject::connect(this->ui->pBNewCarSave, SIGNAL(clicked()), this,
SLOT(saveNewCar()));
QObject::connect(this->ui->pBCarClose, SIGNAL(clicked()), this,
SLOT(closeWindow()));
QObject::connect(this->ui->pBNewDriverClose, SIGNAL(clicked()), this,
SLOT(closeWindow()));
QObject::connect(this->ui->pBDriverEditClose, SIGNAL(clicked()), this,
SLOT(closeWindow()));
this->ui->wNewCarColor->setAutoFillBackground(true);
// update minimal lap time on changeing minimal sector time
// QObject::connect(this->ui->lEMinTimeSec1,
// SIGNAL(textChanged(QString)),
// this, SLOT(repaintMinLapTime()));
// QObject::connect(this->ui->lEMinTimeSec2,
// SIGNAL(textChanged(QString)),
// this, SLOT(repaintMinLapTime()));
// QObject::connect(this->ui->lEMinTimeSec3,
// SIGNAL(textChanged(QString)),
// this, SLOT(repaintMinLapTime()));
this->db = db;
// Shell prepare
string statement =
"select geraden, kurven_aussen, "
"kurven_innen, steilkurve_innen, steilkurve_aussen, seit "
"from Strecke where id_bahn like 1 order by seit DESC limit 1";
vector<vector<QString>> res;
try {
res = db->getData(statement, 6);
ui->lEShellGeraden->setText(res.at(0).at(0));
ui->lEShellKurvenAussen->setText(res.at(0).at(1));
ui->lEShellKurvenInnen->setText(res.at(0).at(2));
ui->lEShellSteilkurveInnen->setText(res.at(0).at(3));
ui->lEShellSteilkurveaussen->setText(res.at(0).at(4));
// Dea prepare
statement =
"select geraden, kurven_aussen, "
"kurven_innen, steilkurve_innen, steilkurve_aussen, seit "
"from Strecke where id_bahn like 2 order by seit DESC limit 1";
res = db->getData(statement, 6);
ui->lEDeaGeraden->setText(res.at(0).at(0));
ui->lEDeaKurvenAussen->setText(res.at(0).at(1));
ui->lEDeaKurvenInnen->setText(res.at(0).at(2));
ui->lEDeaSteilkurveInnen->setText(res.at(0).at(3));
ui->lEDeaSteilkurveAussen->setText(res.at(0).at(4));
// duration prepare
statement =
"select dauer, mindestrundendauer, minsec1, minsec2, minsec3 "
"from renndauer";
res = db->getData(statement, 5);
this->ui->leRenndauer->setText(res.at(0).at(0));
this->ui->lEMinTimeSec1->setText(res.at(0).at(2));
this->ui->lEMinTimeSec2->setText(res.at(0).at(3));
this->ui->lEMinTimeSec3->setText(res.at(0).at(4));
// changed -> not reasonable
// int minLapTime = res[0][2].toInt() + res[0][3].toInt() +
// res[0][4].toInt();
this->ui->lEMinRundenzeit->setText(res.at(0).at(1));
statement =
"SELECT id, minimumroundtime FROM rennen order by id DESC limit 1";
res = this->db->getData(statement, 2);
this->rennId = res.at(0).at(0).toInt();
this->ui->lEMinRundenzeitAktRennen->setText(res.at(0).at(1));
// setup Rennliste
// setup drivers
this->ui->lVDrivers->setSelectionMode(
QAbstractItemView::ExtendedSelection);
/*
statement = "select name, id from fahrer";
driversList = this->db->getData2(statement, 2);
driversModel = std::make_shared<QStringListModel>();
QStringList qDriversList;
std::for_each(
driversList.begin(), driversList.end(),
[&qDriversList](vector<QString> i) { qDriversList << i.at(0); });
driversModel->setStringList(qDriversList);
this->ui->lVDrivers->setModel(driversModel.get());
*/
// setup cars
this->ui->lVCars->setSelectionMode(
QAbstractItemView::ExtendedSelection);
/*
QStringList carNames;
std::stringstream ss;
try {
statement = "select id from autos";
cout << "hier gehts noch" << endl;
this->carIds = this->db->getData2(statement, 1);
for (vector<QString> carId : carIds) {
ss << "select AutoKonfiguration.name from AutoKonfiguration "
"where id_auto like "
<< carId.at(0).toStdString()
<< " order by seit DESC limit 1";
carNames << this->db->getData2(ss.str(), 1).at(0).at(0);
cout
<< this->db->getData2(ss.str(), 1).at(0).at(0).toStdString()
<< endl;
ss.str(std::string());
}
} catch (std::exception & e) {
Q_UNUSED(e);
cout << "cars could not be read from database" << endl;
}
carModel = std::make_shared<QStringListModel>();
carModel->setStringList(carNames);
this->ui->lVCars->setModel(carModel.get());
*/
} catch (std::exception & e) {
Q_UNUSED(e);
cout << "missing database :(" << endl;
}
// fill listView of drivers
this->repaintDrivers();
// this->ui->lVDriverEditSavedDrivers
// ->EditingState
// fill listView of cars
this->repaintCars();
// setup Hardware interface
QStringList cbInterfaceList = QStringList();
cbInterfaceList.append("/dev/ttyAMC0");
std::string path = "/dev/pts/";
for (const auto & entry : fs::directory_iterator(path)) {
std::string pathToAppend = entry.path();
cbInterfaceList.append(QString::fromStdString(pathToAppend));
}
this->ui->cBInterface->addItems(cbInterfaceList);
// preset combobox from database
QString dataFromDatabase =
this->db->getData("select interface from interface where id like 1", 1)
.at(0)
.at(0);
int id = this->ui->cBInterface->findText(dataFromDatabase);
if (id > -1) {
this->ui->cBInterface->setCurrentIndex(id);
}
}
void WindowsSettings::saveEditCar() {
// slot to save an edited car
// id car
string carID = this->curCarId.toStdString();
string motor = this->ui->lEEditCarEngine->text().toStdString();
string name = this->ui->lEEditCarName->text().toStdString();
string since = this->currentDateTime();
string color = this->curCarColor.toStdString();
std::stringstream ss;
ss << "insert into AutoKonfiguration (Motor, name, seit, id_auto, farbe) "
"values ('"
<< motor << "', '" << name << "', '" << since << "', " << carID << ", '"
<< color << "')";
// cout << ss.str() << endl;
try {
this->db->setData(ss.str());
} catch (std::exception & e) {
Q_UNUSED(e);
cout << "couldn't push data to database :(" << endl;
}
}
void WindowsSettings::openEditColor() {
// cout << "hello from slot" << endl;
this->colorDialogEditCar = std::make_shared<QColorDialog>();
this->colorDialogEditCar->show();
this->colorDialogEditCar->setCurrentColor(QColor(this->curCarColor));
this->colorDialogEditCar->open(this, SLOT(changeColorEdit()));
}
void WindowsSettings::openNewColor() {
// cout << "hello from slot" << endl;
this->colorDialogNewCar = std::make_shared<QColorDialog>();
this->colorDialogNewCar->show();
this->colorDialogNewCar->open(this, SLOT(changeColorNew()));
}
void WindowsSettings::saveNewCar() {
// check if name of car already exists
vector<QString> carNames;
std::stringstream ss;
for (auto id : this->carIds) {
// clear ss
ss.str(string());
ss << "select id, Motor, name, seit, id_auto, farbe from "
"AutoKonfiguration where id_auto like "
<< id.at(0).toStdString() << " order by id DESC limit 1";
carNames.push_back(this->db->getData2(ss.str(), 6).at(0).at(2));
}
for (auto name : carNames) {
if (name == this->ui->leNewCarName->text() ||
this->ui->leNewCarName->text() == QString("")) {
// car already exists of name is empty
QMessageBox msgBox;
msgBox.setText("Name ist nicht zulässig");
msgBox.setInformativeText("");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
return;
}
}
// check if motor is set
if (this->ui->lENewCarEngine->text() == QString("")) {
QMessageBox msgBox;
msgBox.setText("Der Motor fehlt.");
msgBox.setInformativeText("");
msgBox.setStandardButtons(QMessageBox::Ok);
msgBox.setDefaultButton(QMessageBox::Ok);
return;
}
// add new car
ss.str(string());
ss << "insert into autos default values";
this->db->setData(ss.str());
// get added id
try {
ss.str(string());
ss << "select id from autos order by id DESC limit 1";
string id = this->db->getData(ss.str(), 1).at(0).at(0).toStdString();
string name = this->ui->leNewCarName->text().toStdString();
string engine = this->ui->lENewCarEngine->text().toStdString();
string since = this->currentDateTime();
string color = this->curCarColor.toStdString();
ss.str(string());
ss << "insert into AutoKonfiguration (id_auto, motor, seit, name, "
"farbe) values ("
<< id << ", '" << engine << "', '" << since << "', '" << name
<< "', '" << color << "')";
this->db->setData(ss.str());
repaintCars();
} catch (std::exception & e) {
Q_UNUSED(e);
}
}
void WindowsSettings::listClickEditCar(const QModelIndex & index) {
this->curCarId =
this->carIds.at(boost::lexical_cast<unsigned long>(index.row())).at(0);
std::stringstream ss;
ss << "select id, Motor, name, seit, id_auto, farbe from "
"AutoKonfiguration where id_auto like "
<< this->curCarId.toStdString() << " order by id DESC limit 1";
vector<vector<QString>> data;
try {
data = this->db->getData2(ss.str(), 6);
this->ui->lEEditCarName->setText(data.at(0).at(2));
this->ui->lEEditCarEngine->setText(data.at(0).at(1));
this->curCarColor = data.at(0).at(5);
this->ui->wEditCarColor->setAutoFillBackground(true);
QPalette pal = QPalette(QColor(this->curCarColor));
this->ui->wEditCarColor->setPalette(pal);
} catch (std::exception & e) {
Q_UNUSED(e);
}
}
void WindowsSettings::repaintDrivers() {
string statement = "select id, name from fahrer";
// this->driversLV;
this->driversList = this->db->getData2(statement, 2);
// driversList = this->driversLV;
// QStringListModel *
this->driversModel = std::make_shared<QStringListModel>();
QStringList driverList =
DataHelper::vectorListToQstringList(this->driversList, 1);
this->driversModel->setStringList(driverList);
this->ui->lVDriverEditSavedDrivers->setModel(this->driversModel.get());
this->ui->lVNewDriverSavedDrivers->setModel(this->driversModel.get());
this->ui->lVDrivers->setModel(this->driversModel.get());
}
void WindowsSettings::repaintCars() {
QStringList carNames;
std::stringstream ss;
// get ids of cars data
string statement = "select id from autos";
try {
this->carIds = this->db->getData2(statement, 1);
for (auto id : this->carIds) {
// clear ss
ss.str(string());
ss << "select id, Motor, name, seit, id_auto, farbe from "
"AutoKonfiguration where id_auto like "
<< id.at(0).toStdString() << " order by id DESC limit 1";
carNames << this->db->getData2(ss.str(), 6).at(0).at(2);
}
this->carModel = std::make_shared<QStringListModel>();
this->carModel->setStringList(carNames);
this->ui->lVCarSavedCars->setModel(this->carModel.get());
this->ui->lvNewCar->setModel(this->carModel.get());
this->ui->lVCars->setModel(this->carModel.get());
} catch (std::exception & e) {
Q_UNUSED(e);
cout << "Error on repainting cars" << endl;
}
}
void WindowsSettings::changeColorEdit() {
QPalette pal(this->colorDialogEditCar->selectedColor());
this->ui->wEditCarColor->setPalette(pal);
this->ui->wEditCarColor->show();
this->curCarColor = this->colorDialogEditCar->selectedColor().name();
}
void WindowsSettings::changeColorNew() {
cout << "new color " << endl;
QPalette pal(this->colorDialogNewCar->selectedColor());
cout << this->colorDialogNewCar->selectedColor().value() << endl;
this->ui->wNewCarColor->setPalette(pal);
this->ui->wNewCarColor->show();
this->curCarColor = this->colorDialogNewCar->selectedColor().name();
}
void WindowsSettings::closeWindow() {
this->close();
}
void WindowsSettings::saveNewDriver() {
std::stringstream ss;
string name = this->ui->lENewDriverName->text().toStdString();
if (name.size() >= 3) {
ss << "insert into fahrer (name) values ('" << name << "')";
cout << ss.str() << endl;
this->db->setData(ss.str());
this->repaintDrivers();
}
else {
cout << "Name is to short" << endl;
}
}
void WindowsSettings::repaintMinLapTime() {
int minlapTime = 0;
minlapTime += this->ui->lEMinTimeSec1->text().toInt();
minlapTime += this->ui->lEMinTimeSec2->text().toInt();
minlapTime += this->ui->lEMinTimeSec3->text().toInt();
this->ui->lEMinRundenzeit->setText(QString::number(minlapTime));
this->ui->lEMinRundenzeitAktRennen->setText(QString::number(minlapTime));
}
void WindowsSettings::repaintMinCurLapTime() {
this->ui->lEMinRundenzeitAktRennen->setText(
this->ui->lEMinRundenzeit->text());
}
void WindowsSettings::AbbrechenSlot() {
this->close();
}
void WindowsSettings::SaveDauerSlot() {
string statement =
"update renndauer set "
"dauer=" +
this->ui->leRenndauer->text().toStdString() + ", " +
"mindestrundendauer=" +
this->ui->lEMinRundenzeit->text().toStdString() + ", " +
"minsec1=" + this->ui->lEMinTimeSec1->text().toStdString() + ", " +
"minsec2=" + this->ui->lEMinTimeSec2->text().toStdString() + ", " +
"minsec3=" + this->ui->lEMinTimeSec3->text().toStdString() + " " +
" where id like 1";
cout << statement << endl;
this->db->setData(statement);
statement = "update rennen set minimumroundtime=" +
this->ui->lEMinRundenzeitAktRennen->text().toStdString() +
" where id like " + QString::number(this->rennId).toStdString();
this->db->setData(statement);
}
void WindowsSettings::SaveDauerAndExitSlot() {
this->SaveDauerSlot();
this->AbbrechenSlot();
}
void WindowsSettings::StreckeSpeichernSlot() {
QString shellGeraden, shellKurvenAussen, shellKurvenInnen, shellSteilAussen,
shellSteilInnen;
QString deaGeraden, deaKurvenAussen, deaKurvenInnen, deaSteilAussen,
deaSteilInnen;
shellGeraden = ui->lEShellGeraden->text();
shellKurvenAussen = ui->lEShellKurvenAussen->text();
shellKurvenInnen = ui->lEShellKurvenInnen->text();
shellSteilAussen = ui->lEShellSteilkurveaussen->text();
shellSteilInnen = ui->lEShellSteilkurveInnen->text();
QString statement = QString(
"insert into strecke (geraden, kurven_aussen, kurven_innen, "
"steilkurve_innen, steilkurve_aussen, seit, id_bahn)"
" values (" +
shellGeraden + ", " + shellKurvenAussen + ", " + shellKurvenInnen +
", " + shellSteilInnen + ", " + shellSteilAussen + ", '" +
QString::fromStdString(currentDateTime()) + "', 1)");
// cout << statement.toStdString() << endl;
db->setData(statement.toStdString());
deaGeraden = ui->lEDeaGeraden->text();
deaKurvenAussen = ui->lEDeaKurvenAussen->text();
deaKurvenInnen = ui->lEDeaKurvenInnen->text();
deaSteilAussen = ui->lEDeaSteilkurveAussen->text();
deaSteilInnen = ui->lEDeaSteilkurveInnen->text();
statement =
QString("insert into strecke (geraden, kurven_aussen, kurven_innen, "
"steilkurve_innen, steilkurve_aussen, seit, id_bahn)"
" values (" +
deaGeraden + ", " + deaKurvenAussen + ", " + deaKurvenInnen +
", " + deaSteilInnen + ", " + deaSteilAussen + ", '" +
QString::fromStdString(currentDateTime()) + "', 2)");
// cout << statement.toStdString() << endl;
db->setData(statement.toStdString());
}
WindowsSettings::~WindowsSettings() {
delete ui;
}
// Get current date/time, format is YYYY-MM-DD.HH:mm:ss
string WindowsSettings::currentDateTime() {
time_t now = time(nullptr);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
// Visit http://en.cppreference.com/w/cpp/chrono/c/strftime
// for more information about date/time format
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
return buf;
}
void WindowsSettings::changeInterface() {
std::string curInterface =
this->ui->cBInterface->currentText().toStdString();
std::string statement = "update interface set interface = '" +
curInterface + "' where id like 1";
cout << statement << endl;
this->db->setData(statement);
}
void WindowsSettings::createRaceListAndClose() {
vector<int> selectedCarIds;
foreach (const QModelIndex & index,
this->ui->lVCars->selectionModel()->selectedIndexes()) {
selectedCarIds.push_back(
this->carIds.at(static_cast<unsigned long>(index.row()))
.at(0)
.toInt());
}
vector<int> selectedDriverIds;
foreach (const QModelIndex & index,
this->ui->lVDrivers->selectionModel()->selectedIndexes()) {
selectedDriverIds.push_back(
this->driversList.at(static_cast<unsigned long>(index.row()))
.at(0)
.toInt());
}
RaceListGenerator raceListGeneator(selectedDriverIds, selectedCarIds);
vector<vector<int>> generatedList;
try {
// structure: shellcar, shelldriver, deacar, deadriver
generatedList = raceListGeneator.getList();
string statement = "select mindestrundendauer from renndauer";
string minRoundTime =
this->db->getData2(statement, 1).at(0).at(0).toStdString();
// calc current time
time_t now = time(nullptr);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%Y-%m-%d.%X", &tstruct);
// cout << buf << endl;
string curTime(buf);
std::stringstream ss;
ss << "insert into Rennen (date, minimumRoundtime) values ('" << curTime
<< "', " << minRoundTime << ")";
this->db->setData(ss.str());
// get id of current race
statement = "select id from Rennen order by id DESC limit 1";
string raceId =
this->db->getData2(statement, 1).at(0).at(0).toStdString();
for (auto zeile : generatedList) {
ss.str(std::string());
ss << "insert into aktRennen (autoShellId, fahrerShellId, "
"autoDeaId, fahrerDeaId, id_rennen) values ("
<< zeile.at(0) << ", " << zeile.at(1) << ", " << zeile.at(2)
<< ", " << zeile.at(3) << ", " << raceId << ")";
this->db->setData(ss.str());
}
} catch (std::exception & e) {
Q_UNUSED(e);
}
this->close();
// test
// std::for_each(selectedCarIds.begin(), selectedCarIds.end(),
// [](int id) { cout << id << " " << endl; });
// test
// std::for_each(selectedDriverIds.begin(), selectedDriverIds.end(),
// [](int id) { cout << id << " " << endl; });
}