src/EditYeastPack.cpp

changeset 480
94b3def5d778
child 481
8a25dbe682eb
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/EditYeastPack.cpp	Mon Jan 30 17:05:13 2023 +0100
@@ -0,0 +1,308 @@
+/**
+ * EditYeastPack.cpp is part of bmsapp.
+ *
+ * bmsapp is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * bmsapp is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ */
+#include "EditYeastPack.h"
+#include "../ui/ui_EditYeastPack.h"
+#include "MainWindow.h"
+#include "global.h"
+
+
+EditYeastPack::EditYeastPack(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditYeastPack)
+{
+    QSqlQuery query;
+
+    qDebug() << "EditYeastPack record:" << id;
+    ui->setupUi(this);
+    this->recno = id;
+
+    WindowTitle();
+
+    ui->formEdit->addItem(tr("Liquid"));
+    ui->formEdit->addItem(tr("Dry"));
+    ui->formEdit->addItem(tr("Slant"));
+    ui->formEdit->addItem(tr("Culture"));
+    ui->formEdit->addItem(tr("Frozen"));
+    ui->formEdit->addItem(tr("Bottle"));
+    ui->formEdit->addItem(tr("Dried"));
+
+    if (id >= 0) {
+	query.prepare("SELECT * FROM inventory_yeastpack WHERE record = :recno");
+	query.bindValue(":recno", id);
+	query.exec();
+	query.next();
+
+	ui->laboratoryEdit->setText(query.value("laboratory").toString());
+	ui->packageEdit->setText(query.value("package").toString());
+	ui->formEdit->setCurrentIndex(query.value("form").toInt());
+	ui->notesEdit->setPlainText(query.value("notes").toString());
+	ui->cellsEdit->setValue(query.value("cells").toDouble() / 1000000000);
+	ui->viabilityEdit->setValue(query.value("viability").toDouble());
+	ui->maxEdit->setValue(query.value("max").toInt());
+	ui->sizeEdit->setValue(query.value("size").toDouble() * 1000);
+	ui->usedEdit->setValue(query.value("used").toInt());
+
+    } else {
+	/* Set some defaults */
+	ui->formEdit->setCurrentIndex(YEAST_FORMS_DRY);
+	ui->viabilityEdit->setValue(0.99);
+	ui->maxEdit->setValue(100);
+	ui->usedEdit->setValue(0);
+    }
+
+    PackSet();
+    Viability();
+    connect(ui->laboratoryEdit, &QLineEdit::textChanged, this, &EditYeastPack::is_changed);
+    connect(ui->formEdit, &QComboBox::currentTextChanged, this, &EditYeastPack::form_changed);
+    connect(ui->packageEdit, &QLineEdit::textChanged, this, &EditYeastPack::is_changed);
+    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
+    connect(ui->cellsEdit, &QDoubleSpinBox::textChanged, this, &EditYeastPack::is_changed);
+    connect(ui->viabilityEdit, &QDoubleSpinBox::textChanged, this, &EditYeastPack::viability_changed);
+    connect(ui->maxEdit, &QSpinBox::textChanged, this, &EditYeastPack::viability_changed);
+    connect(ui->sizeEdit, &QDoubleSpinBox::textChanged, this, &EditYeastPack::is_changed);
+
+    ui->saveButton->setEnabled(false);
+}
+
+
+EditYeastPack::~EditYeastPack()
+{
+    delete ui;
+    emit entry_changed();
+}
+
+
+/*
+ * Window header, mark any change with '**'
+ */
+void EditYeastPack::WindowTitle()
+{
+    QString txt;
+
+    if (this->recno < 0) {
+	txt = QString(tr("BMSapp - Add new yeast package"));
+    } else {
+	txt = QString(tr("BMSapp - Edit yeast package %1").arg(this->recno));
+    }
+
+    if (this->textIsChanged) {
+	txt.append((QString(" **")));
+    }
+    setWindowTitle(txt);
+}
+
+
+void EditYeastPack::on_saveButton_clicked()
+{
+    QSqlQuery query;
+
+    /* If there are errors in the form, show a message and do "return;" */
+    if (ui->laboratoryEdit->text().length() < 2) {
+	QMessageBox::warning(this, tr("Edit Yeast Package"), tr("Laboratory name empty or too short."));
+	return;
+    }
+    if (ui->packageEdit->text().length() < 2) {
+        QMessageBox::warning(this, tr("Edit Yeast Package"), tr("Package name empty or too short."));
+        return;
+    }
+
+    if (this->textIsChanged) {
+    	if (this->recno == -1) {
+    	    query.prepare("INSERT INTO inventory_yeastpack SET laboratory=:laboratory, form=:form, "
+		"package=:package, notes=:notes, cells=:cells, viability=:viability, max=:max, "
+		"size=:size, used='0', uuid = :uuid");
+    	} else {
+	    query.prepare("UPDATE inventory_yeastpack SET laboratory=:laboratory, form=:form, "
+		"package=:package, notes=:notes, cells=:cells, viability=:viability, max=:max, "
+                "size=:size WHERE record = :recno");
+    	}
+	query.bindValue(":laboratory", ui->laboratoryEdit->text());
+	query.bindValue(":form", ui->formEdit->currentIndex());
+	query.bindValue(":package", ui->packageEdit->text());
+	query.bindValue(":notes", ui->notesEdit->toPlainText());
+	query.bindValue(":cells", QString("%1").arg(ui->cellsEdit->value() * 1000000000, 1, 'f', 0, '0'));
+	query.bindValue(":viability", QString("%1").arg(ui->viabilityEdit->value(), 5, 'f', 4, '0'));
+	query.bindValue(":max", ui->maxEdit->value());
+	query.bindValue(":size", QString("%1").arg(ui->sizeEdit->value() / 1000, 5, 'f', 4, '0'));
+	if (this->recno == -1) {
+	    query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
+	} else {
+	    query.bindValue(":recno", this->recno);
+	}
+	query.exec();
+	if (query.lastError().isValid()) {
+	    qWarning() << "EditYeastPack" << query.lastError();
+	    QMessageBox::warning(this, tr("Database error"),
+                        tr("MySQL error: %1\n%2\n%3")
+                        .arg(query.lastError().nativeErrorCode())
+                        .arg(query.lastError().driverText())
+                        .arg(query.lastError().databaseText()));
+	} else {
+	    qDebug() << "EditYeastPack Saved";
+	}
+    }
+
+    ui->saveButton->setEnabled(false);
+    this->textIsChanged = false;
+    WindowTitle();
+}
+
+
+void EditYeastPack::on_cloneButton_clicked()
+{
+    QSqlQuery query;
+    query.prepare("INSERT INTO inventory_yeastpack SET laboratory=:laboratory, form=:form, "
+                "package=:package, notes=:notes, cells=:cells, viability=:viability, max=:max, "
+                "size=:size, used=:used, uuid=:uuid");
+    query.bindValue(":laboratory", ui->laboratoryEdit->text());
+    query.bindValue(":form", ui->formEdit->currentIndex());
+    query.bindValue(":package", ui->packageEdit->text() + " [copy]");
+    query.bindValue(":notes", ui->notesEdit->toPlainText());
+    query.bindValue(":cells", QString("%1").arg(ui->cellsEdit->value() * 1000000000, 1, 'f', 0, '0'));
+    query.bindValue(":viability", QString("%1").arg(ui->viabilityEdit->value(), 5, 'f', 4, '0'));
+    query.bindValue(":max", ui->maxEdit->value());
+    query.bindValue(":size", QString("%1").arg(ui->sizeEdit->value() / 1000, 5, 'f', 4, '0'));
+    query.bindValue(":used", ui->usedEdit->value());
+    query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36));
+    query.exec();
+    if (query.lastError().isValid()) {
+	qWarning() << "EditYeastPack" << query.lastError();
+	QMessageBox::warning(this, tr("Database error"),
+                        tr("MySQL error: %1\n%2\n%3")
+                        .arg(query.lastError().nativeErrorCode())
+                        .arg(query.lastError().driverText())
+                        .arg(query.lastError().databaseText()));
+    } else {
+	qDebug() << "EditYeastPack Clone saved";
+    }
+}
+
+
+void EditYeastPack::on_deleteButton_clicked()
+{
+    QSqlQuery query;
+
+    int rc = QMessageBox::warning(this, tr("Delete yeast pack"), tr("Delete %1").arg(ui->packageEdit->text()),
+                    QMessageBox::Yes | QMessageBox::No, QMessageBox::No);
+    if (rc == QMessageBox::No)
+        return;
+
+    query.prepare("DELETE FROM inventory_yeastpack WHERE record = :recno");
+    query.bindValue(":recno", this->recno);
+    query.exec();
+    if (query.lastError().isValid()) {
+	qWarning() << "EditYeastPack" << query.lastError();
+	QMessageBox::warning(this, tr("Database error"),
+                        tr("MySQL error: %1\n%2\n%3")
+                        .arg(query.lastError().nativeErrorCode())
+                        .arg(query.lastError().driverText())
+                        .arg(query.lastError().databaseText()));
+    } else {
+	qDebug() << "EditYeastPack Deleted" << this->recno;
+    }
+
+    this->close();
+    this->setResult(1);
+}
+
+
+void EditYeastPack::Viability()
+{
+    double vpm = ui->viabilityEdit->value();
+    double max = ui->maxEdit->value();
+    double degrade = 1 - ((1 - vpm) / 30.41);	/* viability degradation per day. */
+    double base = max;
+
+    for (int i = 0; i < 182; i++) {
+	base = base * degrade;
+    }
+    if (base > max)
+	base = max;
+
+    ui->resultEdit->setValue(base);
+    qDebug() << "Viability()" << vpm << max << degrade << base;
+}
+
+
+void EditYeastPack::PackSet()
+{
+    if (ui->formEdit->currentIndex() == YEAST_FORMS_DRY || ui->formEdit->currentIndex() == YEAST_FORMS_DRIED) {
+	ui->sizeEdit->setSuffix(tr(" gr"));
+    } else {
+	ui->sizeEdit->setSuffix(tr(" ml"));
+    }
+
+    /*
+     * TODO: The delete button may be enabled for records with used > 0 if
+     *       there are more records with the same laboratory and form.
+     *       There should always be at least one record, the rest is allowed to
+     *       be deleted. So, we need to count.
+     */
+
+    if (ui->usedEdit->value() > 0) {
+	ui->laboratoryEdit->setReadOnly(true);
+	ui->formEdit->setDisabled(true);
+	ui->deleteButton->setEnabled(false);
+	qDebug() << "PackSet blocked";
+    } else {
+	ui->laboratoryEdit->setReadOnly(false);
+	ui->formEdit->setDisabled(false);
+	ui->deleteButton->setEnabled(true);
+    }
+}
+
+
+void EditYeastPack::is_changed()
+{
+    ui->saveButton->setEnabled(true);
+    this->textIsChanged = true;
+    WindowTitle();
+}
+
+
+void EditYeastPack::form_changed()
+{
+    PackSet();
+    is_changed();
+}
+
+
+void EditYeastPack::viability_changed()
+{
+    Viability();
+    is_changed();
+}
+
+
+void EditYeastPack::on_quitButton_clicked()
+{
+    if (this->textIsChanged) {
+	int rc = QMessageBox::warning(this, tr("Yeast package changed"), tr("The yeast package has been modified. Save changes?"),
+                                QMessageBox::Save | QMessageBox::Discard | QMessageBox::Cancel, QMessageBox::Save);
+        switch (rc) {
+            case QMessageBox::Save:
+                        on_saveButton_clicked();
+                        break;  /* Saved and then Quit */
+            case QMessageBox::Discard:
+                        break;  /* Quit without Save */
+            case QMessageBox::Cancel:
+                        return; /* Return to the editor page */
+        }
+    }
+
+    this->close();
+    this->setResult(1);
+}
+

mercurial