changed more raw pointers to smart pointers

This commit is contained in:
2018-12-28 22:48:47 +01:00
parent fcec2ad4b4
commit a811514b04
8 changed files with 64 additions and 52 deletions

View File

@@ -7,15 +7,13 @@
using std::vector; using std::vector;
DataBase::DataBase() { DataBase::DataBase() {
this->db = new QSqlDatabase(); this->db = std::make_shared<QSqlDatabase>();
*this->db = QSqlDatabase::addDatabase("QSQLITE"); *this->db = QSqlDatabase::addDatabase("QSQLITE");
this->db->setDatabaseName("Renndatenbank.sqlite"); this->db->setDatabaseName("Renndatenbank.sqlite");
// std::cout << "Konstruktor Database" << std::endl; // std::cout << "Konstruktor Database" << std::endl;
} }
DataBase::~DataBase() { DataBase::~DataBase() {
// std::cout << "Destruktor aus Datenbank" << std::endl; // std::cout << "Destruktor aus Datenbank" << std::endl;
delete this->db;
// delete this;
} }
vector<vector<QString>> DataBase::getData2(std::string statement, int cols) { vector<vector<QString>> DataBase::getData2(std::string statement, int cols) {
@@ -58,14 +56,15 @@ QStringList DataBase::getDataQStringList(std::string statement) {
vector<vector<QString>> DataBase::getData(std::string statement, int cols) { vector<vector<QString>> DataBase::getData(std::string statement, int cols) {
char * buffer = new char[statement.length() + 1]; // char * buffer = new char[statement.length() + 1];
strcpy(buffer, statement.c_str()); std::shared_ptr<char> buffer(new char[statement.length() + 1]);
strcpy(buffer.get(), statement.c_str());
vector<QString> data; vector<QString> data;
vector<vector<QString>> lines; vector<vector<QString>> lines;
bool ok = this->db->open(); bool ok = this->db->open();
QString qstr; QString qstr;
if (ok) { if (ok) {
QSqlQuery query(buffer); QSqlQuery query(buffer.get());
while (query.next()) { while (query.next()) {
@@ -78,16 +77,16 @@ vector<vector<QString>> DataBase::getData(std::string statement, int cols) {
} }
} }
this->db->close(); this->db->close();
delete[] buffer; // delete[] buffer;
return lines; return lines;
} }
void DataBase::setData(std::string statement) { void DataBase::setData(std::string statement) {
char * buffer = new char[statement.length() + 1]; std::shared_ptr<char> buffer(new char[statement.length() + 1]);
strcpy(buffer, statement.c_str()); strcpy(buffer.get(), statement.c_str());
bool ok = this->db->open(); bool ok = this->db->open();
if (ok) { if (ok) {
QSqlQuery query(buffer); QSqlQuery query(buffer.get());
} }
this->db->close(); this->db->close();
} }

View File

@@ -4,6 +4,7 @@
#include <QtSql/QSql> #include <QtSql/QSql>
#include <QtSql/QSqlDatabase> #include <QtSql/QSqlDatabase>
#include <QtSql/QSqlQuery> #include <QtSql/QSqlQuery>
#include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
@@ -19,7 +20,7 @@ class DataBase {
QStringList getDataQStringList(std::string statement); QStringList getDataQStringList(std::string statement);
private: private:
QSqlDatabase * db; std::shared_ptr<QSqlDatabase> db;
}; };
#endif // DATABASE_H #endif // DATABASE_H

View File

@@ -63,8 +63,7 @@ void MainWindow::WindowTraining() {
} }
void MainWindow::NewWindowSettings() { void MainWindow::NewWindowSettings() {
this->interfaceSettings = this->interfaceSettings = std::make_shared<WindowsSettings>(this->db, this);
std::make_shared<WindowsSettings>(this->db.get(), this);
this->interfaceSettings->show(); this->interfaceSettings->show();
} }

View File

@@ -93,8 +93,8 @@ void Training::ResetShell() {
this->VecShell.clear(); this->VecShell.clear();
// this->ui->lWShellTime->clear(); // this->ui->lWShellTime->clear();
timeModelShell = new TimeModel(VecShell, minSecTime, this); timeModelShell = std::make_shared<TimeModel>(VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell); this->ui->lWShellTime->setModel(timeModelShell.get());
this->ui->lBestZeitShell->setText(""); this->ui->lBestZeitShell->setText("");
this->ui->lCurRoundTimeShell->setText(""); this->ui->lCurRoundTimeShell->setText("");
@@ -111,8 +111,8 @@ void Training::ResetDea() {
this->VecDea.clear(); this->VecDea.clear();
// this->ui->lWDeaTime->clear(); // this->ui->lWDeaTime->clear();
timeModelDea = new TimeModel(VecDea, minSecTime, this); timeModelDea = std::make_shared<TimeModel>(VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea); this->ui->lWDeaTime->setModel(timeModelDea.get());
this->ui->lBestZeitDea->setText(""); this->ui->lBestZeitDea->setText("");
@@ -174,8 +174,9 @@ void Training::shellSlot(int time, int sector) {
VecShell.push_back(QVector<int>()); VecShell.push_back(QVector<int>());
VecShell.last().push_back(time); VecShell.last().push_back(time);
timeModelShell = new TimeModel(VecShell, minSecTime, this); timeModelShell =
this->ui->lWShellTime->setModel(timeModelShell); std::make_shared<TimeModel>(VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell.get());
this->ui->lBridgeShellTop->setText(QString::number( this->ui->lBridgeShellTop->setText(QString::number(
static_cast<double>(QVectorHelper::getMinSec1(VecShell)) / static_cast<double>(QVectorHelper::getMinSec1(VecShell)) /
@@ -188,9 +189,9 @@ void Training::shellSlot(int time, int sector) {
// cout << time << sector << // cout << time << sector <<
// endl; // endl;
VecShell.last().push_back(time); VecShell.last().push_back(time);
timeModelShell = timeModelShell = std::make_shared<TimeModel>(
new TimeModel(VecShell, minSecTime, this); VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell); this->ui->lWShellTime->setModel(timeModelShell.get());
} }
} }
@@ -210,9 +211,9 @@ void Training::shellSlot(int time, int sector) {
VecShell.last().push_back( VecShell.last().push_back(
QVectorHelper::getCurTime(VecShell.last())); QVectorHelper::getCurTime(VecShell.last()));
timeModelShell = timeModelShell = std::make_shared<TimeModel>(
new TimeModel(VecShell, minSecTime, this); VecShell, minSecTime, this);
this->ui->lWShellTime->setModel(timeModelShell); this->ui->lWShellTime->setModel(timeModelShell.get());
} }
// best time on widget // best time on widget
@@ -279,8 +280,9 @@ void Training::deaSlot(int time, int sector) {
VecDea.push_back(QVector<int>()); VecDea.push_back(QVector<int>());
VecDea.last().push_back(time); VecDea.last().push_back(time);
// cout << "Dea Sektor 1" << endl; // cout << "Dea Sektor 1" << endl;
timeModelDea = new TimeModel(VecDea, minSecTime, this); timeModelDea =
this->ui->lWDeaTime->setModel(timeModelDea); std::make_shared<TimeModel>(VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea.get());
this->ui->lBridgeDeaTop->setText(QString::number( this->ui->lBridgeDeaTop->setText(QString::number(
static_cast<double>(QVectorHelper::getMinSec1(VecDea)) / static_cast<double>(QVectorHelper::getMinSec1(VecDea)) /
1000)); 1000));
@@ -291,8 +293,9 @@ void Training::deaSlot(int time, int sector) {
// cout << time << sector << // cout << time << sector <<
// endl; // endl;
VecDea[VecDea.size() - 1].push_back(time); VecDea[VecDea.size() - 1].push_back(time);
timeModelDea = new TimeModel(VecDea, minSecTime, this); timeModelDea = std::make_shared<TimeModel>(
this->ui->lWDeaTime->setModel(timeModelDea); VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea.get());
} }
else { else {
// VecDea[VecDea.size()-1].append(9999); // VecDea[VecDea.size()-1].append(9999);
@@ -314,8 +317,9 @@ void Training::deaSlot(int time, int sector) {
VecDea.last().push_back( VecDea.last().push_back(
QVectorHelper::getCurTime(VecDea.last())); QVectorHelper::getCurTime(VecDea.last()));
timeModelDea = new TimeModel(VecDea, minSecTime, this); timeModelDea = std::make_shared<TimeModel>(
this->ui->lWDeaTime->setModel(timeModelDea); VecDea, minSecTime, this);
this->ui->lWDeaTime->setModel(timeModelDea.get());
} }
// best time on widget // best time on widget
// cout << "cur time: " << // cout << "cur time: " <<

View File

@@ -50,8 +50,8 @@ class Training : public QMainWindow {
int deltaDea; int deltaDea;
// timeModel // timeModel
TimeModel * timeModelDea; std::shared_ptr<TimeModel> timeModelDea;
TimeModel * timeModelShell; std::shared_ptr<TimeModel> timeModelShell;
QVector<int> minSecTime; QVector<int> minSecTime;
// shortcuts // shortcuts

View File

@@ -17,7 +17,7 @@ using std::endl;
using std::string; using std::string;
using std::vector; using std::vector;
WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent) WindowsSettings::WindowsSettings(std::shared_ptr<DataBase> db, QWidget * parent)
: QMainWindow(parent), ui(new Ui::WindowsSettings) { : QMainWindow(parent), ui(new Ui::WindowsSettings) {
ui->setupUi(this); ui->setupUi(this);
@@ -66,6 +66,15 @@ WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent)
QObject::connect(this->ui->pBNewCarSave, SIGNAL(clicked()), this, QObject::connect(this->ui->pBNewCarSave, SIGNAL(clicked()), this,
SLOT(saveNewCar())); 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); this->ui->wNewCarColor->setAutoFillBackground(true);
// update minimal lap time on changeing minimal sector time // update minimal lap time on changeing minimal sector time
// QObject::connect(this->ui->lEMinTimeSec1, // QObject::connect(this->ui->lEMinTimeSec1,
@@ -135,13 +144,13 @@ WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent)
QAbstractItemView::ExtendedSelection); QAbstractItemView::ExtendedSelection);
statement = "select name, id from fahrer"; statement = "select name, id from fahrer";
driversList = this->db->getData2(statement, 2); driversList = this->db->getData2(statement, 2);
driversModel = new QStringListModel(); driversModel = std::make_shared<QStringListModel>();
QStringList qDriversList; QStringList qDriversList;
std::for_each( std::for_each(
driversList.begin(), driversList.end(), driversList.begin(), driversList.end(),
[&qDriversList](vector<QString> i) { qDriversList << i.at(0); }); [&qDriversList](vector<QString> i) { qDriversList << i.at(0); });
driversModel->setStringList(qDriversList); driversModel->setStringList(qDriversList);
this->ui->lVDrivers->setModel(driversModel); this->ui->lVDrivers->setModel(driversModel.get());
// setup cars // setup cars
this->ui->lVCars->setSelectionMode( this->ui->lVCars->setSelectionMode(
@@ -167,10 +176,10 @@ WindowsSettings::WindowsSettings(DataBase * db, QWidget * parent)
cout << "cars could not be read from database" << endl; cout << "cars could not be read from database" << endl;
} }
carModel = new QStringListModel(); carModel = std::make_shared<QStringListModel>();
carModel->setStringList(carNames); carModel->setStringList(carNames);
this->ui->lVCars->setModel(carModel); this->ui->lVCars->setModel(carModel.get());
} catch (std::exception & e) { } catch (std::exception & e) {
Q_UNUSED(e); Q_UNUSED(e);
@@ -212,7 +221,7 @@ void WindowsSettings::saveEditCar() {
void WindowsSettings::openEditColor() { void WindowsSettings::openEditColor() {
// cout << "hello from slot" << endl; // cout << "hello from slot" << endl;
this->colorDialogEditCar = new QColorDialog(); this->colorDialogEditCar = std::make_shared<QColorDialog>();
this->colorDialogEditCar->show(); this->colorDialogEditCar->show();
this->colorDialogEditCar->setCurrentColor(QColor(this->curCarColor)); this->colorDialogEditCar->setCurrentColor(QColor(this->curCarColor));
this->colorDialogEditCar->open(this, SLOT(changeColorEdit())); this->colorDialogEditCar->open(this, SLOT(changeColorEdit()));
@@ -220,7 +229,7 @@ void WindowsSettings::openEditColor() {
void WindowsSettings::openNewColor() { void WindowsSettings::openNewColor() {
// cout << "hello from slot" << endl; // cout << "hello from slot" << endl;
this->colorDialogNewCar = new QColorDialog(); this->colorDialogNewCar = std::make_shared<QColorDialog>();
this->colorDialogNewCar->show(); this->colorDialogNewCar->show();
this->colorDialogNewCar->open(this, SLOT(changeColorNew())); this->colorDialogNewCar->open(this, SLOT(changeColorNew()));
@@ -338,10 +347,10 @@ void WindowsSettings::repaintCars() {
<< id.at(0).toStdString() << " order by id DESC limit 1"; << id.at(0).toStdString() << " order by id DESC limit 1";
carNames << this->db->getData2(ss.str(), 6).at(0).at(2); carNames << this->db->getData2(ss.str(), 6).at(0).at(2);
} }
this->carModel = new QStringListModel(); this->carModel = std::make_shared<QStringListModel>();
this->carModel->setStringList(carNames); this->carModel->setStringList(carNames);
this->ui->lVCarSavedCars->setModel(this->carModel); this->ui->lVCarSavedCars->setModel(this->carModel.get());
this->ui->lvNewCar->setModel(this->carModel); this->ui->lvNewCar->setModel(this->carModel.get());
} catch (std::exception & e) { } catch (std::exception & e) {
Q_UNUSED(e); Q_UNUSED(e);
@@ -399,7 +408,6 @@ void WindowsSettings::repaintMinCurLapTime() {
} }
void WindowsSettings::AbbrechenSlot() { void WindowsSettings::AbbrechenSlot() {
this->close(); this->close();
delete this;
} }
void WindowsSettings::SaveDauerSlot() { void WindowsSettings::SaveDauerSlot() {
string statement = string statement =

View File

@@ -17,21 +17,22 @@ class WindowsSettings : public QMainWindow {
Q_OBJECT Q_OBJECT
public: public:
explicit WindowsSettings(DataBase * db, QWidget * parent = 0); explicit WindowsSettings(std::shared_ptr<DataBase> db,
QWidget * parent = nullptr);
~WindowsSettings(); ~WindowsSettings();
private: private:
Ui::WindowsSettings * ui; Ui::WindowsSettings * ui;
string currentDateTime(); string currentDateTime();
DataBase * db; std::shared_ptr<DataBase> db;
int rennId; int rennId;
vector<vector<QString>> carIds; vector<vector<QString>> carIds;
vector<vector<QString>> driversList; vector<vector<QString>> driversList;
QStringListModel * carModel; std::shared_ptr<QStringListModel> carModel;
QStringListModel * driversModel; std::shared_ptr<QStringListModel> driversModel;
vector<vector<QString>> driversLV; vector<vector<QString>> driversLV;
QColorDialog * colorDialogEditCar; std::shared_ptr<QColorDialog> colorDialogEditCar;
QColorDialog * colorDialogNewCar; std::shared_ptr<QColorDialog> colorDialogNewCar;
QString curCarColor; QString curCarColor;
QString curCarId; QString curCarId;

View File

@@ -21,7 +21,7 @@
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Rennliste Erstellen&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string> <string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Rennliste Erstellen&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property> </property>
<property name="currentIndex"> <property name="currentIndex">
<number>2</number> <number>3</number>
</property> </property>
<widget class="QWidget" name="Dauer"> <widget class="QWidget" name="Dauer">
<attribute name="title"> <attribute name="title">
@@ -559,7 +559,7 @@
<item> <item>
<widget class="QTabWidget" name="tabWidget_2"> <widget class="QTabWidget" name="tabWidget_2">
<property name="currentIndex"> <property name="currentIndex">
<number>0</number> <number>1</number>
</property> </property>
<widget class="QWidget" name="tab_6"> <widget class="QWidget" name="tab_6">
<attribute name="title"> <attribute name="title">