diff --git a/SociModel.cpp b/SociModel.cpp new file mode 100644 index 0000000..eb8106f --- /dev/null +++ b/SociModel.cpp @@ -0,0 +1,21 @@ +#include "SociModel.h" + +SociModel::SociModel(QObject* parent, QSqlQuery& query) : QAbstractTableModel(parent), query(query) { + ; +} + +int SociModel::rowCount(const QModelIndex&) const { + return query.size(); +} + +int SociModel::columnCount(const QModelIndex&) const { + return query.record().count(); +} + +QVariant SociModel::data(const QModelIndex& index, int role) const { + if (role == Qt::DisplayRole) { + query.seek(index.row()); + return query.value(index.column()).toString(); + } + return QVariant(); +} \ No newline at end of file diff --git a/SociModel.h b/SociModel.h new file mode 100644 index 0000000..e6e7743 --- /dev/null +++ b/SociModel.h @@ -0,0 +1,21 @@ +#ifndef SOCIMODEL_H +#define SOCIMODEL_H + +#include +#include +#include +#include + +class SociModel : public QAbstractTableModel { + Q_OBJECT + private: + QSqlQuery& query; + + public: + SociModel(QObject* parent, QSqlQuery& query); + int rowCount(const QModelIndex& parent = QModelIndex()) const; + int columnCount(const QModelIndex& parent = QModelIndex()) const; + QVariant data(const QModelIndex& index, int role = Qt::DisplayRole) const; +}; + +#endif \ No newline at end of file diff --git a/editwindow.cpp b/editwindow.cpp index 95bf5fa..5321647 100644 --- a/editwindow.cpp +++ b/editwindow.cpp @@ -1,11 +1,9 @@ #include "editwindow.h" -EditWindow::EditWindow(QWidget *parent) : - QMainWindow(parent), - ui(new Ui::EditWindow) -{ +EditWindow::EditWindow(int idSocio, QWidget* parent) : QMainWindow(parent), ui(new Ui::EditWindow) { ui->setupUi(this); + /* retrieve all elements of the GUI */ lineID = this->findChild("lineID"); lineNome = this->findChild("lineNome"); lineCognome = this->findChild("lineCognome"); @@ -17,10 +15,7 @@ EditWindow::EditWindow(QWidget *parent) : comboFonte = this->findChild("comboFonte"); checkAbilitaQuestionario = this->findChild("checkAbilitaQuestionario"); - populateUi(); -} - -bool EditWindow::populateUi(void) { + /* retrieve professione and fonte from database */ query.prepare("SELECT id, professione FROM professione ORDER BY professione"); query.exec(); while (query.next()) { @@ -33,18 +28,24 @@ bool EditWindow::populateUi(void) { comboFonte->addItem(query.value(1).toString(), query.value(0).toInt()); } - return true; + /* 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(); + + showFound(); // TODO } +/* void EditWindow::on_buttonCerca_clicked() { QLineEdit* lineCognome = this->findChild("lineCognome"); - 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.cognome LIKE :cognome"); - query.bindValue( ":cognome", QString("%%1%").arg(lineCognome->text()) ); + if ( ! query.exec() ) { setStatus(Ui::ERROR, "query error"); qDebug() << query.lastQuery(); return; } if ( query.size() == 0) { setStatus(Ui::INFO, "no results found"); return; } @@ -53,6 +54,7 @@ void EditWindow::on_buttonCerca_clicked() { query.first(); showFound(); } +*/ void EditWindow::showFound() { lineID->setText(query.value( query.record().indexOf("id") ).toString()); diff --git a/editwindow.h b/editwindow.h index b0e7c32..de61b0d 100644 --- a/editwindow.h +++ b/editwindow.h @@ -23,18 +23,16 @@ namespace Ui { }; } -class EditWindow : public QMainWindow -{ +class EditWindow : public QMainWindow { Q_OBJECT public: - explicit EditWindow(QWidget *parent = nullptr); + explicit EditWindow(int idSocio, QWidget *parent); ~EditWindow(); void setStatus(Ui::StatusType type, QString message); private slots: - void on_buttonCerca_clicked(); void on_buttonNuovo_clicked(); void on_buttonModifica_clicked(); void on_buttonSuccessivo_clicked(); diff --git a/gestionale.pro b/gestionale.pro index 2e976a4..010aa81 100644 --- a/gestionale.pro +++ b/gestionale.pro @@ -26,16 +26,23 @@ CONFIG += c++11 SOURCES += \ db.cpp \ + editwindow.cpp \ fullquery.cpp \ main.cpp \ - editwindow.cpp + mainwindow.cpp \ + SociModel.cpp + HEADERS += \ db.h \ + editwindow.h \ fullquery.h \ - editwindow.h + mainwindow.h \ + SociModel.h + FORMS += \ + mainwindow.ui \ editwindow.ui # Default rules for deployment. diff --git a/main.cpp b/main.cpp index dab208f..d80cb08 100644 --- a/main.cpp +++ b/main.cpp @@ -5,6 +5,7 @@ #include "db.h" #include "editwindow.h" +#include "mainwindow.h" int main(int argc, char *argv[]) { @@ -25,7 +26,7 @@ int main(int argc, char *argv[]) msgBox.exec(); } - EditWindow w; + MainWindow w; w.show(); return a.exec(); diff --git a/mainwindow.cpp b/mainwindow.cpp new file mode 100644 index 0000000..d5ef489 --- /dev/null +++ b/mainwindow.cpp @@ -0,0 +1,46 @@ +#include "mainwindow.h" + +MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow) { + ui->setupUi(this); + + lineCognome = this->findChild("lineCognome"); + lineNome = this->findChild("lineCognome"); + tableSoci = this->findChild("tableSoci"); +} + +MainWindow::~MainWindow() { + delete ui; +} + +void MainWindow::on_buttonCerca_clicked() { + + query.prepare("SELECT id, nome, cognome FROM socio WHERE cognome LIKE :cognome"); + query.bindValue( ":cognome", QString("%%1%").arg(lineCognome->text()) ); + + if ( ! query.exec() ) { qDebug() << query.lastQuery(); return; } + //if ( query.size() == 0) { setStatus(Ui::INFO, "no results found"); return; } + + if (sociModel != nullptr) delete sociModel; // TODO -- does this look a cunning thing? + sociModel = new SociModel(nullptr, query); + + //setStatus(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() { /* TODO -- please implement me thanks */;} + +void MainWindow::on_tableSoci_clicked(const QModelIndex& index) { + if (index.isValid()) { + int id = index.data().toInt(); /* retrieve id socio */ + if (id != 0) { /* if click is not on other fields than id... */ + EditWindow* w = new EditWindow(id, this); + w->show(); + // TODO -- free() this window at the end... maybe you can use a self Signal/Slot? + } + + } +} \ No newline at end of file diff --git a/mainwindow.h b/mainwindow.h new file mode 100644 index 0000000..1eb49a8 --- /dev/null +++ b/mainwindow.h @@ -0,0 +1,44 @@ +#ifndef MAINWINDOW_H +#define MAINWINDOW_H + +#include +#include +#include + +#include "editwindow.h" +#include "mainwindow.h" +#include "SociModel.h" +#include "ui_mainwindow.h" + +namespace Ui { + class MainWindow; +} + +class MainWindow : public QMainWindow { + Q_OBJECT + +public: + explicit MainWindow(QWidget *parent = nullptr); + ~MainWindow(); + + //void setStatus(Ui::StatusType type, QString message); + +private slots: + void on_buttonCerca_clicked(); + void on_buttonNuovo_clicked(); + void on_tableSoci_clicked(const QModelIndex& index); + +private: + SociModel* sociModel = nullptr; + + QLineEdit* lineNome = nullptr; + QLineEdit* lineCognome = nullptr; + QTableView* tableSoci = nullptr; +// QDateEdit* dateDataNascita = nullptr; + + QSqlQuery query; + + Ui::MainWindow *ui; +}; + +#endif \ No newline at end of file diff --git a/mainwindow.ui b/mainwindow.ui new file mode 100644 index 0000000..d0e2100 --- /dev/null +++ b/mainwindow.ui @@ -0,0 +1,126 @@ + + + MainWindow + + + + 0 + 0 + 635 + 362 + + + + Main Window + + + + + + 430 + 20 + 91 + 31 + + + + Cerca + + + + + + 10 + 20 + 201 + 32 + + + + + + + 530 + 20 + 91 + 31 + + + + Nuovo + + + + + + 10 + 0 + 38 + 18 + + + + Nome + + + + + + 220 + 0 + 60 + 18 + + + + Cognome + + + + + + 220 + 20 + 201 + 32 + + + + + + + 10 + 60 + 611 + 241 + + + + + + + + 0 + 0 + 635 + 30 + + + + + File + + + + + + + + + Exit + + + + + +