# HG changeset patch # User Matti Hamalainen # Date 1525709953 -10800 # Node ID 54ab3f3e28c08954cd5af3918b16a3484c2d0d49 # Parent 2e0fcb3d0b95db7ff8f6c15828c46c25a474399a Split EditPerson and ViewTransactions to separate source files. diff -r 2e0fcb3d0b95 -r 54ab3f3e28c0 Makefile.gen --- a/Makefile.gen Mon May 07 18:37:08 2018 +0300 +++ b/Makefile.gen Mon May 07 19:19:13 2018 +0300 @@ -34,7 +34,15 @@ src/ui_editperson.h \ src/ui_aboutwindow.h -APP_OBJS=main.o sqlmodels.o printing.o resources.o moc_main.o runguard.o +APP_OBJS=\ + main.o \ + editperson.o \ + viewtransactions.o \ + sqlmodels.o \ + printing.o \ + resources.o \ + moc_main.o \ + runguard.o LOGO_SVG ?= kampuscafe4.svg LOGO_IMG ?= CafeKampus_logo_FINAL_RGB.png diff -r 2e0fcb3d0b95 -r 54ab3f3e28c0 src/editperson.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/editperson.cpp Mon May 07 19:19:13 2018 +0300 @@ -0,0 +1,251 @@ +// +// Syntilista - debt list/management database program +// Programmed and designed by Matti Hämäläinen +// (C) Copyright 2017-2018 Tecnic Software productions (TNSP) +// +// Distributed under 3-clause BSD style license, refer to +// included file "COPYING" for exact terms. +// +#include "main.h" +#include "ui_editperson.h" + + +EditPerson::EditPerson(QWidget *parent) : + QDialog(parent), + ui(new Ui::EditPerson) +{ + ui->setupUi(this); + + slSetCommonStyleSheet(this); + + setModal(true); + setAttribute(Qt::WA_DeleteOnClose); + show(); + activateWindow(); + raise(); + setFocus(); + + model_Transactions = new SLTransactionSQLModel(); + ui->tableview_Transactions->setModel(model_Transactions); + ui->tableview_Transactions->setItemDelegate(new QSqlRelationalDelegate(ui->tableview_Transactions)); + ui->tableview_Transactions->verticalHeader()->setVisible(false); + ui->tableview_Transactions->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); + + ui->edit_FirstName->setMaxLength(SQL_LEN_FIRST_NAME); + ui->edit_LastName->setMaxLength(SQL_LEN_LAST_NAME); + + connect( + ui->textedit_ExtraInfo, + SIGNAL(textChanged()), + this, + SLOT(on_textedit_ExtraInfo_textChanged())); + + validateForm(); +} + + +EditPerson::~EditPerson() +{ + delete ui; + delete model_Transactions; +} + + +void EditPerson::statusMsg(const QString &msg) +{ + // Pass the status message to main window + dynamic_cast(parent())->statusMsg(msg); +} + + +bool EditPerson::validateForm() +{ + selPerson.firstName = slCleanupStr(ui->edit_FirstName->text()); + selPerson.lastName = slCleanupStr(ui->edit_LastName->text()); + selPerson.extraInfo = ui->textedit_ExtraInfo->document()->toPlainText(); + bool extraInfoValid = selPerson.extraInfo.length() < SQL_LEN_EXTRA_INFO; + + ui->textedit_ExtraInfo->setStyleSheet(!extraInfoValid ? "background-color: red;" : NULL); + ui->edit_FirstName->setStyleSheet(selPerson.firstName == "" ? "background-color: red;" : NULL); + ui->edit_LastName->setStyleSheet(selPerson.lastName == "" ? "background-color: red;" : NULL); + + return selPerson.firstName != "" && selPerson.lastName != "" && extraInfoValid; +} + + +void EditPerson::on_button_Cancel_clicked() +{ + close(); +} + + +void EditPerson::on_button_OK_clicked() +{ + // + // Check form validation + // + if (!validateForm()) + { + slErrorMsg( + tr("Virhe!"), + tr("Vaaditut kentät (etunimi, sukunimi) eivät ole täytetty tai lisätietojen pituus on liian suuri.")); + + return; + } + + if (selPerson.id >= 0) + { + // + // We are in update/edit person mode, thus we check if the + // first/last name have changed and if there is someone with + // different ID and same names. + // + QSqlQuery person; + person.prepare(QStringLiteral("SELECT * FROM people WHERE id <> ? AND first_name=? AND last_name=?")); + person.addBindValue(selPerson.id); + person.addBindValue(selPerson.firstName); + person.addBindValue(selPerson.lastName); + person.exec(); + + slCheckAndReportSQLError("SELECT check for existing person by same name (UPDATE)", person.lastError()); + + if (person.next()) + { + // There exists another person with that name + slErrorMsg( + tr("Virhe!"), + tr("Ei pysty! Samalla nimellä '%1 %2' on olemassa jo henkilö!"). + arg(selPerson.firstName).arg(selPerson.lastName)); + return; + } + + // Allest klar, update the person data + dynamic_cast(parent())->model_People->updatePerson(selPerson); + dynamic_cast(parent())->setActivePerson(selPerson.id); + + statusMsg(tr("Päivitettiin henkilö '%1 %2' (#%3)."). + arg(selPerson.firstName).arg(selPerson.lastName).arg(selPerson.id)); + } + else + { + // + // We are in "add new person" mode, check if there exists + // someone with same first+last name. + // + QSqlQuery person; + person.prepare("SELECT * FROM people WHERE first_name=? AND last_name=?"); + person.addBindValue(selPerson.firstName); + person.addBindValue(selPerson.lastName); + person.exec(); + + slCheckAndReportSQLError("SELECT check for existing person by same name (ADD)", person.lastError()); + + if (person.next()) + { + // There exists a record with same name + slErrorMsg( + tr("Virhe!"), + tr("Ei pysty! Samalla nimellä '%1 %2' on olemassa jo henkilö!"). + arg(selPerson.firstName).arg(selPerson.lastName)); + + return; + } + + // Attempt to add a person + qint64 nid = dynamic_cast(parent())->model_People->addPerson(selPerson); + if (nid < 0) + { + slErrorMsg( + tr("Virhe!"), + tr("Tietokannan käsittelyssä tapahtui virhe (#%1)."). + arg(nid)); + } + else + { + dynamic_cast(parent())->updatePersonList(); + dynamic_cast(parent())->setActivePerson(nid); + dynamic_cast(parent())->focusDebtEdit(); + + statusMsg(tr("Lisättiin uusi henkilö '%1 %2'."). + arg(selPerson.firstName).arg(selPerson.lastName)); + } + } + + close(); +} + + +void EditPerson::on_edit_FirstName_textChanged(const QString &arg1) +{ + (void) arg1; + validateForm(); +} + + +void EditPerson::on_edit_LastName_textChanged(const QString &arg1) +{ + (void) arg1; + validateForm(); +} + + +void EditPerson::on_textedit_ExtraInfo_textChanged() +{ + validateForm(); +} + + +void EditPerson::clearForm() +{ + ui->edit_FirstName->clear(); + ui->edit_LastName->clear(); + ui->textedit_ExtraInfo->document()->clear(); + ui->edit_FirstName->setFocus(); +} + + +// +// Set the person to be edited +// +void EditPerson::setPerson(qint64 id) +{ + selPerson.id = id; + + if (id >= 0) + { + SLPersonInfo pinfo; + if (!slGetPersonInfo(id, pinfo)) + { + statusMsg(tr("Virhe! Ei henkilöä ID:llä #%1").arg(id)); + // Intentional fall-through below + } + else + { + ui->edit_FirstName->setText(pinfo.firstName); + ui->edit_LastName->setText(pinfo.lastName); + ui->textedit_ExtraInfo->document()->setPlainText(pinfo.extraInfo); + ui->label_AddedValue->setText(slDateTimeToStr(pinfo.added)); + + QSqlQuery query; + query.prepare(QStringLiteral("SELECT id,value,added FROM transactions WHERE person=? ORDER BY added DESC")); + query.addBindValue(pinfo.id); + query.exec(); + slCheckAndReportSQLError("SELECT transactions for tableview_Transactions", query.lastError()); + + model_Transactions->setQuery(query); + + model_Transactions->setHeaderData(0, Qt::Horizontal, tr("ID")); + model_Transactions->setHeaderData(1, Qt::Horizontal, tr("Summa")); + model_Transactions->setHeaderData(2, Qt::Horizontal, tr("Aika")); + + ui->tableview_Transactions->setModel(model_Transactions); + ui->tableview_Transactions->setColumnHidden(0, true); + + return; // Ugly + } + } + + // In case of id < 0 or errors .. + clearForm(); + ui->tableview_Transactions->setModel(NULL); +} diff -r 2e0fcb3d0b95 -r 54ab3f3e28c0 src/main.cpp --- a/src/main.cpp Mon May 07 18:37:08 2018 +0300 +++ b/src/main.cpp Mon May 07 19:19:13 2018 +0300 @@ -12,9 +12,7 @@ #include #include "main.h" #include "ui_mainwindow.h" -#include "ui_editperson.h" #include "ui_aboutwindow.h" -#include "ui_viewtransactions.h" #include "runguard.h" @@ -1287,250 +1285,6 @@ // -// Edit person dialog -// -EditPerson::EditPerson(QWidget *parent) : - QDialog(parent), - ui(new Ui::EditPerson) -{ - ui->setupUi(this); - - slSetCommonStyleSheet(this); - - setModal(true); - setAttribute(Qt::WA_DeleteOnClose); - show(); - activateWindow(); - raise(); - setFocus(); - - model_Transactions = new SLTransactionSQLModel(); - ui->tableview_Transactions->setModel(model_Transactions); - ui->tableview_Transactions->setItemDelegate(new QSqlRelationalDelegate(ui->tableview_Transactions)); - ui->tableview_Transactions->verticalHeader()->setVisible(false); - ui->tableview_Transactions->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); - - ui->edit_FirstName->setMaxLength(SQL_LEN_FIRST_NAME); - ui->edit_LastName->setMaxLength(SQL_LEN_LAST_NAME); - - connect( - ui->textedit_ExtraInfo, - SIGNAL(textChanged()), - this, - SLOT(on_textedit_ExtraInfo_textChanged())); - - validateForm(); -} - - -EditPerson::~EditPerson() -{ - delete ui; - delete model_Transactions; -} - - -void EditPerson::statusMsg(const QString &msg) -{ - // Pass the status message to main window - dynamic_cast(parent())->statusMsg(msg); -} - - -bool EditPerson::validateForm() -{ - selPerson.firstName = slCleanupStr(ui->edit_FirstName->text()); - selPerson.lastName = slCleanupStr(ui->edit_LastName->text()); - selPerson.extraInfo = ui->textedit_ExtraInfo->document()->toPlainText(); - bool extraInfoValid = selPerson.extraInfo.length() < SQL_LEN_EXTRA_INFO; - - ui->textedit_ExtraInfo->setStyleSheet(!extraInfoValid ? "background-color: red;" : NULL); - ui->edit_FirstName->setStyleSheet(selPerson.firstName == "" ? "background-color: red;" : NULL); - ui->edit_LastName->setStyleSheet(selPerson.lastName == "" ? "background-color: red;" : NULL); - - return selPerson.firstName != "" && selPerson.lastName != "" && extraInfoValid; -} - - -void EditPerson::on_button_Cancel_clicked() -{ - close(); -} - - -void EditPerson::on_button_OK_clicked() -{ - // - // Check form validation - // - if (!validateForm()) - { - slErrorMsg( - tr("Virhe!"), - tr("Vaaditut kentät (etunimi, sukunimi) eivät ole täytetty tai lisätietojen pituus on liian suuri.")); - - return; - } - - if (selPerson.id >= 0) - { - // - // We are in update/edit person mode, thus we check if the - // first/last name have changed and if there is someone with - // different ID and same names. - // - QSqlQuery person; - person.prepare(QStringLiteral("SELECT * FROM people WHERE id <> ? AND first_name=? AND last_name=?")); - person.addBindValue(selPerson.id); - person.addBindValue(selPerson.firstName); - person.addBindValue(selPerson.lastName); - person.exec(); - - slCheckAndReportSQLError("SELECT check for existing person by same name (UPDATE)", person.lastError()); - - if (person.next()) - { - // There exists another person with that name - slErrorMsg( - tr("Virhe!"), - tr("Ei pysty! Samalla nimellä '%1 %2' on olemassa jo henkilö!"). - arg(selPerson.firstName).arg(selPerson.lastName)); - return; - } - - // Allest klar, update the person data - dynamic_cast(parent())->model_People->updatePerson(selPerson); - dynamic_cast(parent())->setActivePerson(selPerson.id); - - statusMsg(tr("Päivitettiin henkilö '%1 %2' (#%3)."). - arg(selPerson.firstName).arg(selPerson.lastName).arg(selPerson.id)); - } - else - { - // - // We are in "add new person" mode, check if there exists - // someone with same first+last name. - // - QSqlQuery person; - person.prepare("SELECT * FROM people WHERE first_name=? AND last_name=?"); - person.addBindValue(selPerson.firstName); - person.addBindValue(selPerson.lastName); - person.exec(); - - slCheckAndReportSQLError("SELECT check for existing person by same name (ADD)", person.lastError()); - - if (person.next()) - { - // There exists a record with same name - slErrorMsg( - tr("Virhe!"), - tr("Ei pysty! Samalla nimellä '%1 %2' on olemassa jo henkilö!"). - arg(selPerson.firstName).arg(selPerson.lastName)); - - return; - } - - // Attempt to add a person - qint64 nid = dynamic_cast(parent())->model_People->addPerson(selPerson); - if (nid < 0) - { - slErrorMsg( - tr("Virhe!"), - tr("Tietokannan käsittelyssä tapahtui virhe (#%1)."). - arg(nid)); - } - else - { - dynamic_cast(parent())->updatePersonList(); - dynamic_cast(parent())->setActivePerson(nid); - dynamic_cast(parent())->focusDebtEdit(); - - statusMsg(tr("Lisättiin uusi henkilö '%1 %2'."). - arg(selPerson.firstName).arg(selPerson.lastName)); - } - } - - close(); -} - - -void EditPerson::on_edit_FirstName_textChanged(const QString &arg1) -{ - (void) arg1; - validateForm(); -} - - -void EditPerson::on_edit_LastName_textChanged(const QString &arg1) -{ - (void) arg1; - validateForm(); -} - - -void EditPerson::on_textedit_ExtraInfo_textChanged() -{ - validateForm(); -} - - -void EditPerson::clearForm() -{ - ui->edit_FirstName->clear(); - ui->edit_LastName->clear(); - ui->textedit_ExtraInfo->document()->clear(); - ui->edit_FirstName->setFocus(); -} - - -// -// Set the person to be edited -// -void EditPerson::setPerson(qint64 id) -{ - selPerson.id = id; - - if (id >= 0) - { - SLPersonInfo pinfo; - if (!slGetPersonInfo(id, pinfo)) - { - statusMsg(tr("Virhe! Ei henkilöä ID:llä #%1").arg(id)); - // Intentional fall-through below - } - else - { - ui->edit_FirstName->setText(pinfo.firstName); - ui->edit_LastName->setText(pinfo.lastName); - ui->textedit_ExtraInfo->document()->setPlainText(pinfo.extraInfo); - ui->label_AddedValue->setText(slDateTimeToStr(pinfo.added)); - - QSqlQuery query; - query.prepare(QStringLiteral("SELECT id,value,added FROM transactions WHERE person=? ORDER BY added DESC")); - query.addBindValue(pinfo.id); - query.exec(); - slCheckAndReportSQLError("SELECT transactions for tableview_Transactions", query.lastError()); - - model_Transactions->setQuery(query); - - model_Transactions->setHeaderData(0, Qt::Horizontal, tr("ID")); - model_Transactions->setHeaderData(1, Qt::Horizontal, tr("Summa")); - model_Transactions->setHeaderData(2, Qt::Horizontal, tr("Aika")); - - ui->tableview_Transactions->setModel(model_Transactions); - ui->tableview_Transactions->setColumnHidden(0, true); - - return; // Ugly - } - } - - // In case of id < 0 or errors .. - clearForm(); - ui->tableview_Transactions->setModel(NULL); -} - - -// // About window // AboutWindow::AboutWindow(QWidget *parent) : @@ -1614,63 +1368,3 @@ { close(); } - - -// -// Global transactions list viewer -// -ViewTransactions::ViewTransactions(QWidget *parent) : - QDialog(parent), - ui(new Ui::ViewTransactions) -{ - ui->setupUi(this); - - slSetCommonStyleSheet(this); - - setModal(true); - setAttribute(Qt::WA_DeleteOnClose); - show(); - activateWindow(); - raise(); - setFocus(); - - model_Transactions = new SLTransactionSQLModel(); - ui->tableview_Transactions->setModel(model_Transactions); - ui->tableview_Transactions->setItemDelegate(new QSqlRelationalDelegate(ui->tableview_Transactions)); - ui->tableview_Transactions->verticalHeader()->setVisible(false); - ui->tableview_Transactions->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); - - QSqlQuery query; - query.prepare(QStringLiteral( - "SELECT transactions.id,transactions.value,transactions.added," - "people.last_name,people.first_name FROM transactions " - "LEFT JOIN people ON transactions.person=people.id ORDER BY transactions.added DESC" - )); - - query.exec(); - slCheckAndReportSQLError("SELECT transactions for tableview_Transactions", query.lastError()); - - model_Transactions->setQuery(query); - - model_Transactions->setHeaderData(0, Qt::Horizontal, tr("ID")); - model_Transactions->setHeaderData(1, Qt::Horizontal, tr("Summa")); - model_Transactions->setHeaderData(2, Qt::Horizontal, tr("Aika")); - model_Transactions->setHeaderData(3, Qt::Horizontal, tr("Sukunimi")); - model_Transactions->setHeaderData(4, Qt::Horizontal, tr("Etunimi")); - - ui->tableview_Transactions->setModel(model_Transactions); - ui->tableview_Transactions->setColumnHidden(0, true); -} - - -ViewTransactions::~ViewTransactions() -{ - delete ui; - delete model_Transactions; -} - - -void ViewTransactions::on_button_Close_clicked() -{ - close(); -} diff -r 2e0fcb3d0b95 -r 54ab3f3e28c0 src/main.h --- a/src/main.h Mon May 07 18:37:08 2018 +0300 +++ b/src/main.h Mon May 07 19:19:13 2018 +0300 @@ -90,6 +90,8 @@ bool slCheckAndReportSQLError(const QString where, const QSqlError &err, bool report = false); void slGetPersonInfoRec(QSqlQuery &query, SLPersonInfo &info); +bool slGetPersonInfo(qint64 id, SLPersonInfo &info); +void slSetCommonStyleSheet(QWidget *widget); // diff -r 2e0fcb3d0b95 -r 54ab3f3e28c0 src/viewtransactions.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/viewtransactions.cpp Mon May 07 19:19:13 2018 +0300 @@ -0,0 +1,67 @@ +// +// Syntilista - debt list/management database program +// Programmed and designed by Matti Hämäläinen +// (C) Copyright 2017-2018 Tecnic Software productions (TNSP) +// +// Distributed under 3-clause BSD style license, refer to +// included file "COPYING" for exact terms. +// +#include "main.h" +#include "ui_viewtransactions.h" + + +ViewTransactions::ViewTransactions(QWidget *parent) : + QDialog(parent), + ui(new Ui::ViewTransactions) +{ + ui->setupUi(this); + + slSetCommonStyleSheet(this); + + setModal(true); + setAttribute(Qt::WA_DeleteOnClose); + show(); + activateWindow(); + raise(); + setFocus(); + + model_Transactions = new SLTransactionSQLModel(); + ui->tableview_Transactions->setModel(model_Transactions); + ui->tableview_Transactions->setItemDelegate(new QSqlRelationalDelegate(ui->tableview_Transactions)); + ui->tableview_Transactions->verticalHeader()->setVisible(false); + ui->tableview_Transactions->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); + + QSqlQuery query; + query.prepare(QStringLiteral( + "SELECT transactions.id,transactions.value,transactions.added," + "people.last_name,people.first_name FROM transactions " + "LEFT JOIN people ON transactions.person=people.id ORDER BY transactions.added DESC" + )); + + query.exec(); + slCheckAndReportSQLError("SELECT transactions for tableview_Transactions", query.lastError()); + + model_Transactions->setQuery(query); + + model_Transactions->setHeaderData(0, Qt::Horizontal, tr("ID")); + model_Transactions->setHeaderData(1, Qt::Horizontal, tr("Summa")); + model_Transactions->setHeaderData(2, Qt::Horizontal, tr("Aika")); + model_Transactions->setHeaderData(3, Qt::Horizontal, tr("Sukunimi")); + model_Transactions->setHeaderData(4, Qt::Horizontal, tr("Etunimi")); + + ui->tableview_Transactions->setModel(model_Transactions); + ui->tableview_Transactions->setColumnHidden(0, true); +} + + +ViewTransactions::~ViewTransactions() +{ + delete ui; + delete model_Transactions; +} + + +void ViewTransactions::on_button_Close_clicked() +{ + close(); +}