From a811514b04df71946b8100af467336627d4fff48 Mon Sep 17 00:00:00 2001 From: Johannes Paehr Date: Fri, 28 Dec 2018 22:48:47 +0100 Subject: [PATCH] changed more raw pointers to smart pointers --- database.cpp | 19 +++++++++---------- database.h | 3 ++- mainwindow.cpp | 3 +-- training.cpp | 40 ++++++++++++++++++++++------------------ training.h | 4 ++-- windowssettings.cpp | 30 +++++++++++++++++++----------- windowssettings.h | 13 +++++++------ windowssettings.ui | 4 ++-- 8 files changed, 64 insertions(+), 52 deletions(-) diff --git a/database.cpp b/database.cpp index c094b3e..3666686 100644 --- a/database.cpp +++ b/database.cpp @@ -7,15 +7,13 @@ using std::vector; DataBase::DataBase() { - this->db = new QSqlDatabase(); + this->db = std::make_shared(); *this->db = QSqlDatabase::addDatabase("QSQLITE"); this->db->setDatabaseName("Renndatenbank.sqlite"); // std::cout << "Konstruktor Database" << std::endl; } DataBase::~DataBase() { // std::cout << "Destruktor aus Datenbank" << std::endl; - delete this->db; - // delete this; } vector> DataBase::getData2(std::string statement, int cols) { @@ -58,14 +56,15 @@ QStringList DataBase::getDataQStringList(std::string statement) { vector> DataBase::getData(std::string statement, int cols) { - char * buffer = new char[statement.length() + 1]; - strcpy(buffer, statement.c_str()); + // char * buffer = new char[statement.length() + 1]; + std::shared_ptr buffer(new char[statement.length() + 1]); + strcpy(buffer.get(), statement.c_str()); vector data; vector> lines; bool ok = this->db->open(); QString qstr; if (ok) { - QSqlQuery query(buffer); + QSqlQuery query(buffer.get()); while (query.next()) { @@ -78,16 +77,16 @@ vector> DataBase::getData(std::string statement, int cols) { } } this->db->close(); - delete[] buffer; + // delete[] buffer; return lines; } void DataBase::setData(std::string statement) { - char * buffer = new char[statement.length() + 1]; - strcpy(buffer, statement.c_str()); + std::shared_ptr buffer(new char[statement.length() + 1]); + strcpy(buffer.get(), statement.c_str()); bool ok = this->db->open(); if (ok) { - QSqlQuery query(buffer); + QSqlQuery query(buffer.get()); } this->db->close(); } diff --git a/database.h b/database.h index 95339e1..2a135c5 100644 --- a/database.h +++ b/database.h @@ -4,6 +4,7 @@ #include #include #include +#include #include #include @@ -19,7 +20,7 @@ class DataBase { QStringList getDataQStringList(std::string statement); private: - QSqlDatabase * db; + std::shared_ptr db; }; #endif // DATABASE_H diff --git a/mainwindow.cpp b/mainwindow.cpp index cc1bd53..72814a5 100644 --- a/mainwindow.cpp +++ b/mainwindow.cpp @@ -63,8 +63,7 @@ void MainWindow::WindowTraining() { } void MainWindow::NewWindowSettings() { - this->interfaceSettings = - std::make_shared(this->db.get(), this); + this->interfaceSettings = std::make_shared(this->db, this); this->interfaceSettings->show(); } diff --git a/training.cpp b/training.cpp index 5f2d062..9cebafc 100644 --- a/training.cpp +++ b/training.cpp @@ -93,8 +93,8 @@ void Training::ResetShell() { this->VecShell.clear(); // this->ui->lWShellTime->clear(); - timeModelShell = new TimeModel(VecShell, minSecTime, this); - this->ui->lWShellTime->setModel(timeModelShell); + timeModelShell = std::make_shared(VecShell, minSecTime, this); + this->ui->lWShellTime->setModel(timeModelShell.get()); this->ui->lBestZeitShell->setText("∞"); this->ui->lCurRoundTimeShell->setText("∞"); @@ -111,8 +111,8 @@ void Training::ResetDea() { this->VecDea.clear(); // this->ui->lWDeaTime->clear(); - timeModelDea = new TimeModel(VecDea, minSecTime, this); - this->ui->lWDeaTime->setModel(timeModelDea); + timeModelDea = std::make_shared(VecDea, minSecTime, this); + this->ui->lWDeaTime->setModel(timeModelDea.get()); this->ui->lBestZeitDea->setText("∞"); @@ -174,8 +174,9 @@ void Training::shellSlot(int time, int sector) { VecShell.push_back(QVector()); VecShell.last().push_back(time); - timeModelShell = new TimeModel(VecShell, minSecTime, this); - this->ui->lWShellTime->setModel(timeModelShell); + timeModelShell = + std::make_shared(VecShell, minSecTime, this); + this->ui->lWShellTime->setModel(timeModelShell.get()); this->ui->lBridgeShellTop->setText(QString::number( static_cast(QVectorHelper::getMinSec1(VecShell)) / @@ -188,9 +189,9 @@ void Training::shellSlot(int time, int sector) { // cout << time << sector << // endl; VecShell.last().push_back(time); - timeModelShell = - new TimeModel(VecShell, minSecTime, this); - this->ui->lWShellTime->setModel(timeModelShell); + timeModelShell = std::make_shared( + VecShell, minSecTime, this); + this->ui->lWShellTime->setModel(timeModelShell.get()); } } @@ -210,9 +211,9 @@ void Training::shellSlot(int time, int sector) { VecShell.last().push_back( QVectorHelper::getCurTime(VecShell.last())); - timeModelShell = - new TimeModel(VecShell, minSecTime, this); - this->ui->lWShellTime->setModel(timeModelShell); + timeModelShell = std::make_shared( + VecShell, minSecTime, this); + this->ui->lWShellTime->setModel(timeModelShell.get()); } // best time on widget @@ -279,8 +280,9 @@ void Training::deaSlot(int time, int sector) { VecDea.push_back(QVector()); VecDea.last().push_back(time); // cout << "Dea Sektor 1" << endl; - timeModelDea = new TimeModel(VecDea, minSecTime, this); - this->ui->lWDeaTime->setModel(timeModelDea); + timeModelDea = + std::make_shared(VecDea, minSecTime, this); + this->ui->lWDeaTime->setModel(timeModelDea.get()); this->ui->lBridgeDeaTop->setText(QString::number( static_cast(QVectorHelper::getMinSec1(VecDea)) / 1000)); @@ -291,8 +293,9 @@ void Training::deaSlot(int time, int sector) { // cout << time << sector << // endl; VecDea[VecDea.size() - 1].push_back(time); - timeModelDea = new TimeModel(VecDea, minSecTime, this); - this->ui->lWDeaTime->setModel(timeModelDea); + timeModelDea = std::make_shared( + VecDea, minSecTime, this); + this->ui->lWDeaTime->setModel(timeModelDea.get()); } else { // VecDea[VecDea.size()-1].append(9999); @@ -314,8 +317,9 @@ void Training::deaSlot(int time, int sector) { VecDea.last().push_back( QVectorHelper::getCurTime(VecDea.last())); - timeModelDea = new TimeModel(VecDea, minSecTime, this); - this->ui->lWDeaTime->setModel(timeModelDea); + timeModelDea = std::make_shared( + VecDea, minSecTime, this); + this->ui->lWDeaTime->setModel(timeModelDea.get()); } // best time on widget // cout << "cur time: " << diff --git a/training.h b/training.h index a85a817..180682d 100644 --- a/training.h +++ b/training.h @@ -50,8 +50,8 @@ class Training : public QMainWindow { int deltaDea; // timeModel - TimeModel * timeModelDea; - TimeModel * timeModelShell; + std::shared_ptr timeModelDea; + std::shared_ptr timeModelShell; QVector minSecTime; // shortcuts diff --git a/windowssettings.cpp b/windowssettings.cpp index a91c139..e8d278c 100644 --- a/windowssettings.cpp +++ b/windowssettings.cpp @@ -17,7 +17,7 @@ using std::endl; using std::string; using std::vector; -WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent) +WindowsSettings::WindowsSettings(std::shared_ptr db, QWidget * parent) : QMainWindow(parent), ui(new Ui::WindowsSettings) { ui->setupUi(this); @@ -66,6 +66,15 @@ WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent) 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, @@ -135,13 +144,13 @@ WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent) QAbstractItemView::ExtendedSelection); statement = "select name, id from fahrer"; driversList = this->db->getData2(statement, 2); - driversModel = new QStringListModel(); + driversModel = std::make_shared(); QStringList qDriversList; std::for_each( driversList.begin(), driversList.end(), [&qDriversList](vector i) { qDriversList << i.at(0); }); driversModel->setStringList(qDriversList); - this->ui->lVDrivers->setModel(driversModel); + this->ui->lVDrivers->setModel(driversModel.get()); // setup cars this->ui->lVCars->setSelectionMode( @@ -167,10 +176,10 @@ WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent) cout << "cars could not be read from database" << endl; } - carModel = new QStringListModel(); + carModel = std::make_shared(); carModel->setStringList(carNames); - this->ui->lVCars->setModel(carModel); + this->ui->lVCars->setModel(carModel.get()); } catch (std::exception & e) { Q_UNUSED(e); @@ -212,7 +221,7 @@ void WindowsSettings::saveEditCar() { void WindowsSettings::openEditColor() { // cout << "hello from slot" << endl; - this->colorDialogEditCar = new QColorDialog(); + this->colorDialogEditCar = std::make_shared(); this->colorDialogEditCar->show(); this->colorDialogEditCar->setCurrentColor(QColor(this->curCarColor)); this->colorDialogEditCar->open(this, SLOT(changeColorEdit())); @@ -220,7 +229,7 @@ void WindowsSettings::openEditColor() { void WindowsSettings::openNewColor() { // cout << "hello from slot" << endl; - this->colorDialogNewCar = new QColorDialog(); + this->colorDialogNewCar = std::make_shared(); this->colorDialogNewCar->show(); this->colorDialogNewCar->open(this, SLOT(changeColorNew())); @@ -338,10 +347,10 @@ void WindowsSettings::repaintCars() { << 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 = std::make_shared(); this->carModel->setStringList(carNames); - this->ui->lVCarSavedCars->setModel(this->carModel); - this->ui->lvNewCar->setModel(this->carModel); + this->ui->lVCarSavedCars->setModel(this->carModel.get()); + this->ui->lvNewCar->setModel(this->carModel.get()); } catch (std::exception & e) { Q_UNUSED(e); @@ -399,7 +408,6 @@ void WindowsSettings::repaintMinCurLapTime() { } void WindowsSettings::AbbrechenSlot() { this->close(); - delete this; } void WindowsSettings::SaveDauerSlot() { string statement = diff --git a/windowssettings.h b/windowssettings.h index 91eae67..8b731ea 100644 --- a/windowssettings.h +++ b/windowssettings.h @@ -17,21 +17,22 @@ class WindowsSettings : public QMainWindow { Q_OBJECT public: - explicit WindowsSettings(DataBase * db, QWidget * parent = 0); + explicit WindowsSettings(std::shared_ptr db, + QWidget * parent = nullptr); ~WindowsSettings(); private: Ui::WindowsSettings * ui; string currentDateTime(); - DataBase * db; + std::shared_ptr db; int rennId; vector> carIds; vector> driversList; - QStringListModel * carModel; - QStringListModel * driversModel; + std::shared_ptr carModel; + std::shared_ptr driversModel; vector> driversLV; - QColorDialog * colorDialogEditCar; - QColorDialog * colorDialogNewCar; + std::shared_ptr colorDialogEditCar; + std::shared_ptr colorDialogNewCar; QString curCarColor; QString curCarId; diff --git a/windowssettings.ui b/windowssettings.ui index 71cbb2e..722b2ea 100644 --- a/windowssettings.ui +++ b/windowssettings.ui @@ -21,7 +21,7 @@ <html><head/><body><p>Rennliste Erstellen</p></body></html> - 2 + 3 @@ -559,7 +559,7 @@ - 0 + 1