gestionale/argento/MainWindow.cpp

85 lines
3.3 KiB
C++

#include "MainWindow.h"
MainWindow* mw;
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) {
ui->setupUi(this);
lineCognome = this->findChild<QLineEdit*>("lineCognome");
lineNome = this->findChild<QLineEdit*>("lineNome");
tableSoci = this->findChild<QTableView*>("tableSoci");
}
void MainWindow::connectDatabase(void) {
/* retrieve settings for this application, group database */
QSettings settings("it.linux.golem", "argento");
settings.beginGroup("database");
/* open database and attach query */
db = QSqlDatabase::addDatabase("QMYSQL"); /* QMYSQL <-- database driver */
db.setHostName(settings.value("hostname").toString());
db.setDatabaseName(settings.value("database").toString());
db.setUserName(settings.value("username").toString());
db.setPassword(settings.value("password").toString());
query = QSqlQuery(db);
status(Ui::INFO, QString("Connecting to %1...").arg(settings.value("hostname").toString()));
if (! db.open())
status(Ui::ERROR, db.lastError().text());
else
status(Ui::INFO, QString("Connected to %1").arg(settings.value("hostname").toString()));
}
MainWindow::~MainWindow() {
delete ui;
}
void MainWindow::on_buttonCerca_clicked() {
if (!db.open()) connectDatabase();
query.prepare("SELECT id, nome, cognome FROM socio WHERE LOWER(nome) LIKE LOWER(:nome) AND LOWER(cognome) LIKE LOWER(:cognome)");
query.bindValue( ":nome", QString("%%1%").arg(lineNome->text()) );
query.bindValue( ":cognome", QString("%%1%").arg(lineCognome->text()) );
if (! query.exec()) { status(Ui::ERROR, fullQuery(query)); return; }
if ( query.size() == 0) { status(Ui::INFO, "no results found"); return; }
if (sociModel != nullptr) delete sociModel; // TODO -- does this look a cunning thing?
sociModel = new SociModel(nullptr, query);
status(Ui::INFO, QString("%1 results found").arg(query.size()));
tableSoci->setModel(sociModel);
/* the signal is automagically added by some obscure qt build system, provided the function is named in a standard way */
//QObject::connect(tableSoci, SIGNAL(clicked(const QModelIndex&)), this, SLOT(on_tableSoci_clicked(const QModelIndex&)));
tableSoci->show();
}
void MainWindow::on_buttonNuovo_clicked() {
if (! db.open()) connectDatabase();
query.prepare("INSERT INTO socio (nome, cognome) VALUES (NULLIF(:nome, ''), NULLIF(:cognome, ''))");
query.bindValue(":nome", lineNome->text());
query.bindValue(":cognome", lineCognome->text());
if (! query.exec()) { status(Ui::ERROR, fullQuery(query)); return; }
if (! query.lastInsertId().isValid()) { status(Ui::ERROR, fullQuery(query)); return; }
EditWindow*w = new EditWindow(query.lastInsertId().toInt(), this);
w->setAttribute(Qt::WA_DeleteOnClose);
w->show();
}
void MainWindow::on_tableSoci_clicked(const QModelIndex& index) {
if (index.isValid()) {
/* extracts id socio from first column of table view */
int id = index.model()->data(index.siblingAtColumn(0)).toInt();
if (id != 0) { /* if click is not on other fields than id... */
EditWindow* w = new EditWindow(id, this);
w->setAttribute(Qt::WA_DeleteOnClose); /* delete window at the end */
w->show();
}
}
}