adddialog.cpp
2.54 KB
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
#include "adddialog.h"
#include "ui_adddialog.h"
#include "mainwindow.h"
#include <iostream>
#include <QMessageBox>
AddDialog::AddDialog(QWidget *parent) :
QDialog(parent),
ui(new Ui::AddDialog)
{
this->parent = parent;
ui->setupUi(this);
ui->nameLineEdit->setMaxLength(64);
ui->phoneLineEdit->setMaxLength(9);
ui->commentTextEdit->setMaxLength(140);
connect(ui->buttonBox,SIGNAL(rejected()),parent,SLOT(dismissAddDialog()));
connect(this,SIGNAL(requestClose()),parent,SLOT(dismissAddDialog()));
connect(ui->buttonBox,SIGNAL(accepted()),this,SLOT(add()));
showAssignatures();
}
void AddDialog::showAssignatures()
{
tableViewModel.setQuery("SELECT nom AS \"Assignatura\" FROM assignatures ORDER by nom;");
ui->assignaturesTableView->setModel(&tableViewModel);
ui->assignaturesTableView->resizeColumnsToContents();
}
void AddDialog::add()
{
QSqlQuery reserva;
QSqlQuery assignatura;
QModelIndexList indexes = ui->assignaturesTableView->selectionModel()->selectedRows();
if(indexes.count())
{
if(!MainWindow::checkPhone(ui->phoneLineEdit->text()))
{
QMessageBox::critical(this,"Error",trUtf8("Número de telèfon invàlid"));
return;
}
for(int i = 0; i < indexes.count(); i++)
{
int number = MainWindow::getNum(tableViewModel.data(indexes.at(i)).toString());
reserva.prepare("INSERT into reserves VALUES(:name,:num,:phone,NOW(),:comm);");
reserva.bindValue(":name",ui->nameLineEdit->text());
reserva.bindValue(":num",number);
reserva.bindValue(":phone",ui->phoneLineEdit->text().toInt());
reserva.bindValue(":comm",ui->commentTextEdit->toPlainText());
reserva.exec();
assignatura.prepare("UPDATE assignatures SET agafats = '1' WHERE num = :n;");
assignatura.bindValue(":n",number);
assignatura.exec();
}
((MainWindow*)parent)->triggerUpdate();
if(reserva.isActive() && assignatura.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();
}
else
{
QMessageBox::critical(this,"Error",trUtf8("No s'ha seleccionat cap assignatura"));
return;
}
emit requestClose();
}
AddDialog::~AddDialog()
{
delete ui;
}