|
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
|
#include "editdialog.h"
#include "ui_editdialog.h"
#include "mainwindow.h"
#include <QMessageBox>
EditDialog::EditDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::EditDialog)
{
ui->setupUi(this);
ui->nameLineEdit->setMaxLength(64);
ui->phoneLineEdit->setMaxLength(9);
ui->commentTextEdit->setMaxLength(140);
connect(ui->buttonBox,SIGNAL(rejected()),parent,SLOT(dismissEditDialog()));
connect(this,SIGNAL(requestClose()),parent,SLOT(dismissEditDialog()));
connect(ui->buttonBox,SIGNAL(accepted()),this,SLOT(edit()));
EditDialog::parent = parent;
showValues();
}
void EditDialog::showValues()
{
QSqlRecord previousReserva = ((MainWindow*)parent)->getSelectedReserva();
previousAssignatura = previousReserva.value(1).toString();
ui->nameLineEdit->setText(previousReserva.value(0).toString());
ui->phoneLineEdit->setText(previousReserva.value(2).toString());
ui->commentTextEdit->setText(previousReserva.value(4).toString());
populateAssignatures(previousAssignatura);
}
void EditDialog::populateAssignatures(QString selected)
{
tableViewModel.setQuery("SELECT nom FROM assignatures WHERE agafats = 0 ORDER by nom;");
ui->assignaturaComboBox->addItem(selected);
for(int i = 0; i < tableViewModel.rowCount(); i++)
{
ui->assignaturaComboBox->addItem(tableViewModel.record(i).value("nom").toString());
}
}
void EditDialog::edit()
{
QSqlQuery reserva;
QSqlQuery newAssignatura;
QSqlQuery oldAssignatura;
if(!MainWindow::checkPhone(ui->phoneLineEdit->text()))
{
QMessageBox::critical(this,"Error",trUtf8("Número de telèfon invàlid"));
return;
}
int newNumber = MainWindow::getNum(ui->assignaturaComboBox->currentText());
int oldNumber = MainWindow::getNum(previousAssignatura);
reserva.prepare("UPDATE reserves SET nom_i_cognoms = :n, apunts = :na, telefon = :t, comentari = :c WHERE apunts = :a;");
reserva.bindValue(":n",ui->nameLineEdit->text());
reserva.bindValue(":na",newNumber);
reserva.bindValue(":t",ui->phoneLineEdit->text().toInt());
reserva.bindValue(":c",ui->commentTextEdit->toPlainText());
reserva.bindValue(":a",oldNumber);
reserva.exec();
newAssignatura.prepare("UPDATE assignatures SET agafats = '1' WHERE num = :na;");
oldAssignatura.prepare("UPDATE assignatures SET agafats = '0' WHERE num = :a;");
newAssignatura.bindValue(":na",newNumber);
oldAssignatura.bindValue(":n",oldNumber);
newAssignatura.exec();
oldAssignatura.exec();
((MainWindow*)parent)->triggerUpdate();
if(reserva.isActive() && newAssignatura.isActive() && oldAssignatura.isActive())
{
((MainWindow*)parent)->setStatusMessage(trUtf8("Canvis desats: s'han afegit els préstecs"));
emit requestClose();
return;
}
((MainWindow*)parent)->setStatusMessage(trUtf8("Error: no s'ha pogut escriure a la base de dades"));
emit requestClose();
}
EditDialog::~EditDialog()
{
delete ui;
}
|