85 lines
3.6 KiB
C++
85 lines
3.6 KiB
C++
#include "EditWindow.h"
|
|
|
|
EditWindow::EditWindow(int idSocio, QWidget* parent) : QMainWindow(parent), ui(new Ui::EditWindow) {
|
|
ui->setupUi(this);
|
|
|
|
/* connect query with common database */
|
|
query = QSqlQuery(db);
|
|
|
|
/* retrieve professione and fonte from database */
|
|
query.prepare("SELECT id, professione FROM professione ORDER BY professione");
|
|
query.exec();
|
|
while (query.next()) {
|
|
ui->comboProfessione->addItem(query.value(1).toString(), query.value(0).toInt());
|
|
}
|
|
|
|
query.prepare("SELECT id, fonte FROM fonte ORDER BY fonte");
|
|
query.exec();
|
|
while (query.next()) {
|
|
ui->comboFonte->addItem(query.value(1).toString(), query.value(0).toInt());
|
|
}
|
|
|
|
/* populate fields from database */
|
|
query.prepare("SELECT s.id, s.nome, s.cognome, \
|
|
s.dataNascita, s.comuneResidenza, s.email, s.professione, s.fonte, s.abilitaQuestionario, \
|
|
q.dataCompilazione \
|
|
FROM socio AS s LEFT OUTER JOIN questionario AS q ON s.id = q.socio \
|
|
WHERE s.id = :id");
|
|
query.bindValue( ":id", idSocio );
|
|
query.exec();
|
|
query.first();
|
|
|
|
/* show everything in the interface */
|
|
ui->lineID->setText(query.value( query.record().indexOf("id") ).toString());
|
|
ui->lineNome->setText(query.value( query.record().indexOf("nome") ).toString());
|
|
ui->lineCognome->setText(query.value( query.record().indexOf("cognome") ).toString());
|
|
ui->lineComuneResidenza->setText(query.value(query.record().indexOf("comuneResidenza")).toString());
|
|
ui->lineEmail->setText(query.value(query.record().indexOf("email")).toString());
|
|
ui->comboProfessione->setCurrentIndex(ui->comboProfessione->findData(query.value(query.record().indexOf("professione")).toInt()));
|
|
ui->comboFonte->setCurrentIndex(ui->comboFonte->findData(query.value(query.record().indexOf("fonte")).toInt()));
|
|
ui->dateDataNascita->setDate(QDate(1900, 1, 1)); ui->dateDataNascita->setDate(query.value(query.record().indexOf("dataNascita")).toDate());
|
|
ui->dateDataCompilazione->setDate(QDate(1900, 1, 1)); ui->dateDataCompilazione->setDate(query.value(query.record().indexOf("dataCompilazione")).toDate());
|
|
ui->checkAbilitaQuestionario->setChecked( query.value(query.record().indexOf("abilitaQuestionario")).toBool() );
|
|
}
|
|
|
|
void EditWindow::on_buttonSalva_clicked() {
|
|
|
|
query.prepare("UPDATE socio \
|
|
SET nome = NULLIF(:nome, ''), \
|
|
cognome = NULLIF(:cognome, ''), \
|
|
dataNascita = NULLIF(:dataNascita, '1900-01-01'), \
|
|
comuneResidenza = NULLIF(:comuneResidenza, ''), \
|
|
email = NULLIF(:email, ''), \
|
|
professione = NULLIF(:professione, 0), \
|
|
fonte = NULLIF(:fonte, 0), \
|
|
abilitaQuestionario = :abilitaQuestionario \
|
|
WHERE id = :id");
|
|
query.bindValue(":id", ui->lineID->text());
|
|
query.bindValue(":nome", ui->lineNome->text());
|
|
query.bindValue(":cognome", ui->lineCognome->text());
|
|
query.bindValue(":comuneResidenza", ui->lineComuneResidenza->text());
|
|
query.bindValue(":email", ui->lineEmail->text());
|
|
query.bindValue(":dataNascita", (ui->dateDataNascita->date()).toString("yyyy-MM-dd"));
|
|
query.bindValue(":professione", ui->comboProfessione->currentData().toInt());
|
|
query.bindValue(":fonte", ui->comboFonte->currentData().toInt());
|
|
query.bindValue(":abilitaQuestionario", ui->checkAbilitaQuestionario->isChecked());
|
|
|
|
if ( ! query.exec() ) {
|
|
qDebug() << fullQuery(query);
|
|
qDebug() << db.lastError().text();
|
|
Ui::status(Ui::ERROR, "query error");
|
|
return;
|
|
}
|
|
|
|
Ui::status(Ui::SUCCESS, "successfull update");
|
|
}
|
|
|
|
void EditWindow::on_buttonAnnulla_clicked() {
|
|
this->close();
|
|
}
|
|
|
|
EditWindow::~EditWindow()
|
|
{
|
|
delete ui;
|
|
}
|