#include "editdialog.h" #include "ui_editdialog.h" #include "mainwindow.h" #include 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; }