/
serviceblock.cpp
226 lines (193 loc) · 9.32 KB
/
serviceblock.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include "serviceblock.h"
#include "ui_serviceblock.h"
ServiceBlock::ServiceBlock(int idC, QWidget *parent) :
QDialog(parent),
idCar(idC),
ui(new Ui::ServiceBlock)
{
ui->setupUi(this);
setCalendarColor(ui->deBegin->calendarWidget(),QColor(255,140,0));
setCalendarColor(ui->deEnd->calendarWidget(),QColor(255,140,0));
setCalendarColor(ui->deEvent->calendarWidget(),QColor(255,140,0));
setCalendarColor(ui->deGuarantee->calendarWidget(),QColor(255,140,0));
QSqlQueryModel * windTitle = new QSqlQueryModel(this);
windTitle->setQuery(QString("SELECT Brand, Model, LicensePlate FROM car WHERE idCar = %1").arg(idCar));
this->setWindowTitle( QString("Naprawy - ")
+ windTitle->data(windTitle->index(windTitle->rowCount()-1,0)).toString() + QString(" ")
+ windTitle->data(windTitle->index(windTitle->rowCount()-1,1)).toString() + QString(" - ")
+ windTitle->data(windTitle->index(windTitle->rowCount()-1,2)).toString()
);
connect(this,SIGNAL(saved()),this,SLOT(updateView()),Qt::DirectConnection);
connect(this,SIGNAL(deleted()),this,SLOT(updateView()));
// fill category combobox
QStringList categoryList({QString("Silnik"),QString("Zawieszenie"),QString("Elektronika"),
QString("Elektryka"),QString("Układ napędowy"),QString("Układ hamulcowy"),
QString("Układ kierowniczy"),QString("Nadwozie")});
ui->cbCategory->addItems(categoryList);
updateView();
isOpen = true;
}
ServiceBlock::~ServiceBlock()
{
isOpen = false;
delete ui;
}
bool ServiceBlock::isOpen;
void ServiceBlock::on_lvServices_clicked(const QModelIndex &index)
{
Q_UNUSED(index);
if(Database::isOpen()) {
serviceTable = new QSqlQueryModel(this);
serviceTable->setQuery(QString("SELECT * FROM service WHERE idCar = %1 AND idService = %2;").arg(idCar).arg(ui->lvServices->currentItem()->data(Qt::UserRole).toInt()));
loadData();
}
else QMessageBox::critical(this,tr("Błąd!"), tr("Utracono połączenie z bazą danych!"));
}
void ServiceBlock::setCalendarColor(QCalendarWidget *calendarWidget,QColor color)
{
QWidget *calendarNavBar = calendarWidget->findChild<QWidget *>("qt_calendar_navigationbar");
if (calendarNavBar) {
QPalette pal = calendarNavBar->palette();
pal.setColor(calendarNavBar->backgroundRole(), color);
calendarNavBar->setPalette(pal);
}
}
bool ServiceBlock::isDateCorrect()
{
if(ui->deBegin->date() > ui->deEnd->date()) {
QMessageBox::warning(this, tr("Uwaga!"), tr("Nie poprawny czas trwania naprawy."));
return false;
}
if(ui->deEvent->date() > ui->deBegin->date()) {
QMessageBox::warning(this, tr("Uwaga!"), tr("Data zdarzenia jest większa od początkowej daty naprawy."));
return false;
}
return true;
}
void ServiceBlock::loadData()
{
if(ui->lvServices->currentIndex().data(Qt::DisplayRole ).toString() == "Dodaj naprawę ...")
clearWidgets();
else {
ui->pbDelete->setVisible(true);
idService = serviceTable->index(0,0).data().toInt();
ui->leName->setText( serviceTable->index(0,1).data().toString() );
ui->cbCategory->setCurrentText( serviceTable->index(0,2).data().toString() );
ui->deEvent->setDate( serviceTable->index(0,3).data().toDate() );
ui->deGuarantee->setDate( serviceTable->index(0,4).data().toDate() );
ui->deBegin->setDate( serviceTable->index(0,5).data().toDate() );
ui->deEnd->setDate( serviceTable->index(0,6).data().toDate() );
ui->leCost->setText( serviceTable->index(0,7).data().toString() );
ui->teNotes->setText( serviceTable->index(0,8).data().toString() );
}
}
void ServiceBlock::clearWidgets()
{
ui->pbDelete->setVisible(false);
ui->leName->clear();
ui->cbCategory->setCurrentIndex(0);
ui->deEvent->setDate(QDate::currentDate());
ui->deGuarantee->setDate(QDate::currentDate());
ui->deBegin->setDate(QDate::currentDate());
ui->deEnd->setDate(QDate::currentDate());
ui->leCost->clear();
ui->teNotes->clear();
}
void ServiceBlock::updateView()
{
if(Database::isOpen()) {
lwiVector.clear();
ui->lvServices->clear();
serviceTable = new QSqlQueryModel(this);
serviceTable->setQuery(QString("SELECT * FROM service WHERE idCar = %1 ORDER BY EventDate DESC;").arg(idCar));
// fill repairs list
for(int i=0; i<serviceTable->rowCount(); ++i) {
lwiVector.push_back(new QListWidgetItem(serviceTable->data(serviceTable->index(i,1)).toString()));
lwiVector.back()->setData(Qt::UserRole, serviceTable->data(serviceTable->index(i,0)).toInt());
}
ui->lvServices->addItem(QString("Dodaj naprawę ..."));
for(auto elem : lwiVector) {
ui->lvServices->addItem(elem);
}
ui->lvServices->item(0)->setSelected(true);
ui->lvServices->setFocus();
ui->pbDelete->setVisible(false);
}
else QMessageBox::critical(this,tr("Błąd!"), tr("Utracono połączenie z bazą danych!"));
}
void ServiceBlock::on_pbSave_clicked()
{
if(Database::isOpen()) {
// insert new repair
if(!isDateCorrect())
return;
if(ui->lvServices->currentIndex().data(Qt::DisplayRole ).toString() == "Dodaj naprawę ...") {
QSqlQuery qry;
qry.prepare("INSERT INTO service(Name,ComponentCategory,EventDate,GuaranteeDate,"
"BeginDate,EndDate,Cost,Notes,idCar)"
"VALUES(:_Name,:_ComponentCategory,:_EventDate,:_GuaranteeDate,"
":_BeginDate,:_EndDate,:_Cost,:_Notes,:_idCar)");
if(ui->leName->text()!="") qry.bindValue(":_Name", ui->leName->text());
qry.bindValue(":_ComponentCategory", ui->cbCategory->currentText());
qry.bindValue(":_EventDate", ui->deEvent->date());
qry.bindValue(":_GuaranteeDate", ui->deGuarantee->date());
qry.bindValue(":_BeginDate", ui->deBegin->date());
qry.bindValue(":_EndDate", ui->deEnd->date());
qry.bindValue(":_Cost", ui->leCost->text() );
qry.bindValue(":_Notes", ui->teNotes->toPlainText());
qry.bindValue(":_idCar", idCar);
if(!qry.exec())
QMessageBox::warning(this,tr("Uwaga!"),"Dodawanie nie powiodło się.\nERROR: "+qry.lastError().text()+"");
else {
QSqlQueryModel lastInserted;
lastInserted.setQuery(QString("SELECT * FROM service WHERE idCar = %1 WHERE idCar = 1 AND idService = (SELECT MAX(idService) FROM service);").arg(idCar));
lwiVector.push_back(new QListWidgetItem(ui->leName->text()));
lwiVector.back()->setData(0, lastInserted.index(0,0).data().toInt());
clearWidgets();
ui->lvServices->item(0)->setSelected(true);
updateView();
QMessageBox::information(this,"Informacja","Dodano naprawę!");
}
}
// update repair
else {
QSqlQuery qry;
qry.prepare("UPDATE service SET Name=:_Name,ComponentCategory=:_ComponentCategory,"
"EventDate=:_EventDate,GuaranteeDate=:_GuaranteeDate,"
"BeginDate=:_BeginDate,EndDate=:_EndDate,Cost=:_Cost,Notes=:_Notes WHERE idService=:_idS");
if(ui->leName->text()!="") qry.bindValue(":_Name", ui->leName->text());
qry.bindValue(":_ComponentCategory", ui->cbCategory->currentText());
qry.bindValue(":_EventDate", ui->deEvent->date());
qry.bindValue(":_GuaranteeDate", ui->deGuarantee->date());
qry.bindValue(":_BeginDate", ui->deBegin->date());
qry.bindValue(":_EndDate", ui->deEnd->date());
qry.bindValue(":_Cost", ui->leCost->text() );
qry.bindValue(":_Notes", ui->teNotes->toPlainText());
qry.bindValue(":_idS", idService);
if( !qry.exec() )
QMessageBox::warning(this,tr("Uwaga!"),"Aktualizacja nie powiodła się.\nERROR: "+qry.lastError().text()+"");
else {
QMessageBox::information(this,tr("Informacja"),tr("Zaktualizowano!"));
clearWidgets();
emit saved();
}
}
}
else QMessageBox::critical(this,tr("Błąd!"), tr("Utracono połączenie z bazą danych!"));
}
void ServiceBlock::on_pbDelete_clicked()
{
if(Database::isOpen()) {
QSqlQuery qry;
qry.prepare("DELETE FROM service WHERE idService=:_idS");
qry.bindValue(":_idS", idService);
if( !qry.exec() )
QMessageBox::warning(this,tr("Uwaga!"),"Usuwanie nie powiodło się.\nERROR: "+qry.lastError().text()+"");
else {
QMessageBox::information(this,tr("Informacja"),tr("Usunięto naprawę!"));
clearWidgets();
emit deleted();
}
}
else QMessageBox::critical(this,tr("Błąd!"), tr("Utracono połączenie z bazą danych!"));
}