#include "windowssettings.h" #include "datahelper.h" #include "racelistgenerator.h" #include "ui_windowssettings.h" #include #include #include #include #include #include #include #include #include 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->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())); 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> 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 = new QStringListModel(); QStringList qDriversList; std::for_each( driversList.begin(), driversList.end(), [&qDriversList](vector i) { qDriversList << i.at(0); }); driversModel->setStringList(qDriversList); this->ui->lVDrivers->setModel(driversModel); // setup cars this->ui->lVCars->setSelectionMode( QAbstractItemView::ExtendedSelection); QStringList carNames; std::stringstream ss; try { statement = "select id from autos"; this->carIds = this->db->getData2(statement, 1); for (vector 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); ss.str(std::string()); } } catch (std::exception & e) { Q_UNUSED(e); cout << "cars could not be read from database" << endl; } carModel = new QStringListModel(); carModel->setStringList(carNames); this->ui->lVCars->setModel(carModel); } 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(); // this->ui->wEditCarColor->grab() } 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 = new 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 = new QColorDialog(); this->colorDialogNewCar->show(); this->colorDialogNewCar->open(this, SLOT(changeColorNew())); } void WindowsSettings::saveNewCar() { // check if name of car already exists vector 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(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> 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->db->getData2(statement, 2); QStringListModel * driverModel = new QStringListModel(); QStringList driverList = DataHelper::vectorListToQstringList(this->driversLV, 1); driverModel->setStringList(driverList); this->ui->lVDriverEditSavedDrivers->setModel(driverModel); this->ui->lVNewDriverSavedDrivers->setModel(driverModel); } 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 = new QStringListModel(); this->carModel->setStringList(carNames); this->ui->lVCarSavedCars->setModel(this->carModel); this->ui->lvNewCar->setModel(this->carModel); } 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(); delete this; } 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; std::cout << "Destruktor einstellungen" << std::endl; } // 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::createRaceListAndClose() { vector selectedCarIds; foreach (const QModelIndex & index, this->ui->lVCars->selectionModel()->selectedIndexes()) { selectedCarIds.push_back( this->carIds.at(static_cast(index.row())) .at(0) .toInt()); } vector selectedDriverIds; foreach (const QModelIndex & index, this->ui->lVDrivers->selectionModel()->selectedIndexes()) { selectedDriverIds.push_back( this->driversList.at(static_cast(index.row())) .at(1) .toInt()); } RaceListGenerator raceListGeneator(selectedDriverIds, selectedCarIds); vector> 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; }); }