add FEATURE: edit cars and drivers

This commit is contained in:
2018-12-21 10:27:23 +01:00
parent c4feb773bd
commit 4b94b4dbb5
12 changed files with 362 additions and 26 deletions

View File

@@ -54,6 +54,8 @@ set(helloworld_SRCS
result.cpp
evaluation.cpp
racelistgenerator.cpp
datahelper.cpp
colorwidget.cpp
mainwindow.ui
windowrace.ui

13
colorwidget.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "colorwidget.h"
#include <iostream>
using std::cout;
using std::endl;
ColorWidget::ColorWidget(QWidget * parent) : QWidget(parent) {
}
void ColorWidget::mouseDoubleClickEvent(QMouseEvent * e) {
Q_UNUSED(e);
emit this->doubleClickedSignal();
}

19
colorwidget.h Normal file
View File

@@ -0,0 +1,19 @@
#ifndef COLORWIDGET_H
#define COLORWIDGET_H
#include <QMouseEvent>
#include <QWidget>
class ColorWidget : public QWidget {
Q_OBJECT
public:
ColorWidget(QWidget * parent);
// QWidget interface
protected:
void mouseDoubleClickEvent(QMouseEvent * event);
signals:
void doubleClickedSignal();
};
#endif // COLORWIDGET_H

View File

@@ -41,6 +41,21 @@ vector<vector<QString>> DataBase::getData2(std::string statement, int cols) {
return lines;
}
QStringList DataBase::getDataQStringList(std::string statement) {
QStringList qstrList;
try {
vector<vector<QString>> ret = this->getData2(statement, 1);
for (auto row : ret) {
qstrList << row.at(0);
}
} catch (std::exception & e) {
Q_UNUSED(e);
}
return qstrList;
}
vector<vector<QString>> DataBase::getData(std::string statement, int cols) {
char * buffer = new char[statement.length() + 1];

View File

@@ -1,24 +1,25 @@
#ifndef DATABASE_H
#define DATABASE_H
#include <QDir>
#include <QtSql/QSql>
#include <QtSql/QSqlDatabase>
#include <QDir>
#include <QtSql/QSqlQuery>
#include <vector>
#include <string>
#include <vector>
using std::vector;
class DataBase
{
public:
class DataBase {
public:
DataBase();
~DataBase();
vector< vector<QString> > getData(std::string statement, int cols);
vector< vector<QString> > getData2(std::string statement, int cols);
vector<vector<QString>> getData(std::string statement, int cols);
vector<vector<QString>> 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

13
datahelper.cpp Normal file
View File

@@ -0,0 +1,13 @@
#include "datahelper.h"
DataHelper::DataHelper() {
}
QStringList DataHelper::vectorListToQstringList(vector<vector<QString>> list,
unsigned long index) {
QStringList qstrList;
for (auto row : list) {
qstrList << row.at(index);
}
return qstrList;
}

17
datahelper.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef DATAHELPER_H
#define DATAHELPER_H
#include<qstringlist.h>
#include <vector>
#include <qstring.h>
using std::vector;
class DataHelper
{
public:
DataHelper();
static QStringList vectorListToQstringList(vector<vector<QString>> list, unsigned long index);
};
#endif // DATAHELPER_H

View File

@@ -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() {

View File

@@ -17,7 +17,7 @@
<item>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="QListView" name="lWEvaluation"/>
<widget class="QListView" name="lVEvaluation"/>
</item>
</layout>
</item>

View File

@@ -1,9 +1,12 @@
#include "windowssettings.h"
#include "datahelper.h"
#include "racelistgenerator.h"
#include "ui_windowssettings.h"
#include <QStringListModel>
#include <boost/lexical_cast.hpp>
#include <iostream>
#include <qcolordialog.h>
#include <qmessagebox.h>
#include <stdio.h>
#include <string>
#include <time.h>
@@ -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<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->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();

View File

@@ -3,6 +3,7 @@
#include "database.h"
#include <QMainWindow>
#include <qcolordialog.h>
#include <qstringlistmodel.h>
#include <string>
@@ -27,7 +28,18 @@ class WindowsSettings : public QMainWindow {
vector<vector<QString>> driversList;
QStringListModel * carModel;
QStringListModel * driversModel;
vector<vector<QString>> 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

View File

@@ -377,7 +377,7 @@
<item>
<widget class="QTabWidget" name="tabWidget">
<property name="currentIndex">
<number>1</number>
<number>0</number>
</property>
<widget class="QWidget" name="tab_3">
<attribute name="title">
@@ -395,7 +395,7 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_35">
<item>
<widget class="QLineEdit" name="lECarName"/>
<widget class="QLineEdit" name="lEEditCarName"/>
</item>
</layout>
</widget>
@@ -409,7 +409,7 @@
<item>
<layout class="QVBoxLayout" name="verticalLayout_32">
<item>
<widget class="QLineEdit" name="lECarEngine"/>
<widget class="QLineEdit" name="lEEditCarEngine"/>
</item>
</layout>
</item>
@@ -423,10 +423,10 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_34">
<item>
<widget class="QWidget" name="wCarColor" native="true"/>
<widget class="ColorWidget" name="wEditCarColor" native="true"/>
</item>
<item>
<widget class="QPushButton" name="pBChangeColor">
<widget class="QPushButton" name="pBEditCarChangeColor">
<property name="text">
<string>Farbe ändern</string>
</property>
@@ -436,7 +436,7 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="pBCarSave">
<widget class="QPushButton" name="pBEditCarSave">
<property name="text">
<string>Speichern</string>
</property>
@@ -451,7 +451,7 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_36">
<item>
<widget class="QListView" name="lWCarSavedCars"/>
<widget class="QListView" name="lVCarSavedCars"/>
</item>
</layout>
</widget>
@@ -503,10 +503,10 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_41">
<item>
<widget class="QWidget" name="widget_2" native="true"/>
<widget class="ColorWidget" name="wNewCarColor" native="true"/>
</item>
<item>
<widget class="QPushButton" name="pushButton">
<widget class="QPushButton" name="pBNewCarChangeColor">
<property name="text">
<string>Farbe bearbeiten</string>
</property>
@@ -516,7 +516,7 @@
</widget>
</item>
<item>
<widget class="QPushButton" name="pBNewCarAdd">
<widget class="QPushButton" name="pBNewCarSave">
<property name="text">
<string>Auto hinzufügen</string>
</property>
@@ -531,7 +531,7 @@
</property>
<layout class="QVBoxLayout" name="verticalLayout_42">
<item>
<widget class="QListView" name="listView"/>
<widget class="QListView" name="lvNewCar"/>
</item>
</layout>
</widget>
@@ -778,6 +778,14 @@
</widget>
<widget class="QStatusBar" name="statusbar"/>
</widget>
<customwidgets>
<customwidget>
<class>ColorWidget</class>
<extends>QWidget</extends>
<header location="global">colorwidget.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections/>
</ui>