diff --git a/CMakeLists.txt b/CMakeLists.txt index 132c504..a1fa3d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -54,6 +54,8 @@ set(helloworld_SRCS result.cpp evaluation.cpp racelistgenerator.cpp + datahelper.cpp + colorwidget.cpp mainwindow.ui windowrace.ui diff --git a/colorwidget.cpp b/colorwidget.cpp new file mode 100644 index 0000000..6c830f1 --- /dev/null +++ b/colorwidget.cpp @@ -0,0 +1,13 @@ +#include "colorwidget.h" +#include + +using std::cout; +using std::endl; + +ColorWidget::ColorWidget(QWidget * parent) : QWidget(parent) { +} + +void ColorWidget::mouseDoubleClickEvent(QMouseEvent * e) { + Q_UNUSED(e); + emit this->doubleClickedSignal(); +} diff --git a/colorwidget.h b/colorwidget.h new file mode 100644 index 0000000..eaa41ae --- /dev/null +++ b/colorwidget.h @@ -0,0 +1,19 @@ +#ifndef COLORWIDGET_H +#define COLORWIDGET_H + +#include +#include + +class ColorWidget : public QWidget { + Q_OBJECT + public: + ColorWidget(QWidget * parent); + + // QWidget interface + protected: + void mouseDoubleClickEvent(QMouseEvent * event); + signals: + void doubleClickedSignal(); +}; + +#endif // COLORWIDGET_H diff --git a/database.cpp b/database.cpp index 4608486..c094b3e 100644 --- a/database.cpp +++ b/database.cpp @@ -41,6 +41,21 @@ vector> DataBase::getData2(std::string statement, int cols) { return lines; } +QStringList DataBase::getDataQStringList(std::string statement) { + QStringList qstrList; + try { + vector> ret = this->getData2(statement, 1); + + for (auto row : ret) { + qstrList << row.at(0); + } + + } catch (std::exception & e) { + Q_UNUSED(e); + } + return qstrList; +} + vector> DataBase::getData(std::string statement, int cols) { char * buffer = new char[statement.length() + 1]; diff --git a/database.h b/database.h index 1c20af0..95339e1 100644 --- a/database.h +++ b/database.h @@ -1,24 +1,25 @@ #ifndef DATABASE_H #define DATABASE_H +#include #include #include -#include #include -#include #include +#include using std::vector; -class DataBase -{ -public: +class DataBase { + public: DataBase(); ~DataBase(); - vector< vector > getData(std::string statement, int cols); - vector< vector > getData2(std::string statement, int cols); + vector> getData(std::string statement, int cols); + vector> getData2(std::string statement, int cols); void setData(std::string statement); -private: - QSqlDatabase *db; + QStringList getDataQStringList(std::string statement); + + private: + QSqlDatabase * db; }; #endif // DATABASE_H diff --git a/datahelper.cpp b/datahelper.cpp new file mode 100644 index 0000000..65ecfb8 --- /dev/null +++ b/datahelper.cpp @@ -0,0 +1,13 @@ +#include "datahelper.h" + +DataHelper::DataHelper() { +} + +QStringList DataHelper::vectorListToQstringList(vector> list, + unsigned long index) { + QStringList qstrList; + for (auto row : list) { + qstrList << row.at(index); + } + return qstrList; +} diff --git a/datahelper.h b/datahelper.h new file mode 100644 index 0000000..2c442f5 --- /dev/null +++ b/datahelper.h @@ -0,0 +1,17 @@ +#ifndef DATAHELPER_H +#define DATAHELPER_H + +#include +#include +#include + +using std::vector; + +class DataHelper +{ +public: + DataHelper(); + static QStringList vectorListToQstringList(vector> list, unsigned long index); +}; + +#endif // DATAHELPER_H diff --git a/evaluation.cpp b/evaluation.cpp index bdc39f7..f6b517f 100644 --- a/evaluation.cpp +++ b/evaluation.cpp @@ -18,6 +18,9 @@ Evaluation::Evaluation(DataBase * db, QWidget * parent) // connectsion QObject::connect(this->ui->pBClose, SIGNAL(clicked()), this, SLOT(close())); + QObject::connect(this->ui->lVEvaluation, + SIGNAL(doubleClicked(const QModelIndex &)), this, + SLOT(listClick(const QModelIndex &))); this->result = this->db->getData2("select date, id from Rennen", 2); QStringList listOfDates; @@ -28,10 +31,7 @@ Evaluation::Evaluation(DataBase * db, QWidget * parent) QStringListModel * model = new QStringListModel(); model->setStringList(listOfDates); - this->ui->lWEvaluation->setModel(model); - QObject::connect(this->ui->lWEvaluation, - SIGNAL(doubleClicked(const QModelIndex &)), this, - SLOT(listClick(const QModelIndex &))); + this->ui->lVEvaluation->setModel(model); // this->interfaceResult = new Result(this->db); // this->interfaceResult->show(); @@ -45,7 +45,6 @@ void Evaluation::listClick(const QModelIndex & ind) { this->interfaceResult = new Result(this->db, id); this->interfaceResult->show(); - } Evaluation::~Evaluation() { diff --git a/evaluation.ui b/evaluation.ui index 861ad43..a1ad2fb 100644 --- a/evaluation.ui +++ b/evaluation.ui @@ -17,7 +17,7 @@ - + diff --git a/windowssettings.cpp b/windowssettings.cpp index 3fcf8be..a91c139 100644 --- a/windowssettings.cpp +++ b/windowssettings.cpp @@ -1,9 +1,12 @@ #include "windowssettings.h" +#include "datahelper.h" #include "racelistgenerator.h" #include "ui_windowssettings.h" #include #include #include +#include +#include #include #include #include @@ -37,6 +40,33 @@ WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent) 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)), @@ -146,12 +176,213 @@ WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent) 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(); diff --git a/windowssettings.h b/windowssettings.h index 5fdd45f..5e3ef51 100644 --- a/windowssettings.h +++ b/windowssettings.h @@ -3,6 +3,7 @@ #include "database.h" #include +#include #include #include @@ -27,7 +28,18 @@ class WindowsSettings : public QMainWindow { vector> driversList; QStringListModel * carModel; QStringListModel * driversModel; + vector> driversLV; + QColorDialog * colorDialogEditCar; + QColorDialog * colorDialogNewCar; + QString curCarColor; + QString curCarId; + + void repaintDrivers(); + void repaintCars(); + public slots: + void changeColorEdit(); + void changeColorNew(); void SaveDauerSlot(); void AbbrechenSlot(); void StreckeSpeichernSlot(); @@ -36,6 +48,12 @@ class WindowsSettings : public QMainWindow { void repaintMinCurLapTime(); void createRaceListAndClose(); void closeWindow(); + void saveNewDriver(); + void saveEditCar(); + void listClickEditCar(const QModelIndex & index); + void openEditColor(); + void openNewColor(); + void saveNewCar(); }; #endif // WINDOWSSETTINGS_H diff --git a/windowssettings.ui b/windowssettings.ui index 35da7c0..71cbb2e 100644 --- a/windowssettings.ui +++ b/windowssettings.ui @@ -377,7 +377,7 @@ - 1 + 0 @@ -395,7 +395,7 @@ - + @@ -409,7 +409,7 @@ - + @@ -423,10 +423,10 @@ - + - + Farbe ändern @@ -436,7 +436,7 @@ - + Speichern @@ -451,7 +451,7 @@ - + @@ -503,10 +503,10 @@ - + - + Farbe bearbeiten @@ -516,7 +516,7 @@ - + Auto hinzufügen @@ -531,7 +531,7 @@ - + @@ -778,6 +778,14 @@ + + + ColorWidget + QWidget +
colorwidget.h
+ 1 +
+