src/EditYeast.cpp

Fri, 22 Apr 2022 13:46:59 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 22 Apr 2022 13:46:59 +0200
changeset 152
58e4ce7dd217
parent 90
2396457a8167
child 293
6df94ca2bfde
permissions
-rw-r--r--

Fixed vanishing mash profiles from some recipes. All ingnoreChanges flags removed and replaced by blocking signals. Update prompts and yeast amounts depending on the yeast form. Save water profile names fixed.

/**
 * EditYeast.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 "EditYeast.h"
#include "../ui/ui_EditYeast.h"
#include "MainWindow.h"


EditYeast::EditYeast(int id, QWidget *parent) : QDialog(parent), ui(new Ui::EditYeast)
{
    QSqlQuery query;

    qDebug() << "EditYeast record:" << id;
    ui->setupUi(this);
    this->recno = id;

    WindowTitle();

    ui->typeEdit->addItem(tr("Lager"));
    ui->typeEdit->addItem(tr("Ale"));
    ui->typeEdit->addItem(tr("Wheat"));
    ui->typeEdit->addItem(tr("Wine"));
    ui->typeEdit->addItem(tr("Champagne"));
    ui->typeEdit->addItem(tr("Brett"));
    ui->typeEdit->addItem(tr("Kveik"));
    ui->typeEdit->addItem(tr("Hybrid"));

    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"));

    ui->flocEdit->addItem(tr("Low"));
    ui->flocEdit->addItem(tr("Medium"));
    ui->flocEdit->addItem(tr("High"));
    ui->flocEdit->addItem(tr("Very high"));

    if (id >= 0) {
	query.prepare("SELECT * FROM inventory_yeasts WHERE record = :recno");
	query.bindValue(":recno", id);
	query.exec();
	query.next();

	ui->nameEdit->setText(query.value(1).toString());
	ui->typeEdit->setCurrentIndex(query.value(2).toInt());
	ui->formEdit->setCurrentIndex(query.value(3).toInt());
	ui->laboratoryEdit->setText(query.value(4).toString());
	ui->productidEdit->setText(query.value(5).toString());
	ui->temploEdit->setValue(query.value(6).toDouble());
	ui->temphiEdit->setValue(query.value(7).toDouble());
	ui->flocEdit->setCurrentIndex(query.value(8).toInt());
	ui->attEdit->setValue(query.value(9).toDouble());
	ui->toleranceEdit->setValue(query.value(10).toDouble());
	ui->notesEdit->setPlainText(query.value(11).toString());
	ui->bestforEdit->setPlainText(query.value(12).toString());
	ui->reuseEdit->setValue(query.value(13).toInt());
	if (query.value(3).toInt() == 0)
	    ui->inventoryEdit->setValue(query.value(14).toDouble());		/* Liquid */
	else
	    ui->inventoryEdit->setValue(query.value(14).toDouble() * 1000);
	ui->costEdit->setValue(query.value(15).toDouble());
	SetForm(query.value(3).toInt());
	if (query.value(16).toString().length() == 10) {
            ui->prodEdit->setDate(query.value(16).toDate());
        } else {
            ui->prodEdit->clear();
        }
	if (query.value(17).toString().length() == 10) {
	    ui->thtEdit->setDate(query.value(17).toDate());
	} else {
	    ui->thtEdit->clear();
	}
	ui->cellsEdit->setValue(query.value(18).toDouble() / 1000000000);
	ui->sta1Edit->setChecked(query.value(19).toInt() ? true:false);
	ui->bacteriaEdit->setChecked(query.value(20).toInt() ? true:false);
	ui->harvesttopEdit->setChecked(query.value(21).toInt() ? true:false);
	ui->harvesttimeEdit->setValue(query.value(22).toInt());
	ui->pitchtempEdit->setValue(query.value(23).toDouble());
	ui->pofEdit->setChecked(query.value(24).toInt() ? true:false);
	ui->yeastbankEdit->setText(query.value(26).toString());
	ui->grhlloEdit->setValue(query.value(27).toInt());
	ui->sgloEdit->setValue(query.value(28).toDouble());
	ui->grhlhiEdit->setValue(query.value(29).toInt());
	ui->sghiEdit->setValue(query.value(30).toDouble());
    } else {
	/* Set some defaults */
	ui->typeEdit->setCurrentIndex(1);
	ui->formEdit->setCurrentIndex(1);
	SetForm(1);
	ui->temploEdit->setValue(18.0);
	ui->temphiEdit->setValue(22.0);
	ui->attEdit->setValue(77.0);
	ui->reuseEdit->setValue(10);
	ui->grhlloEdit->setValue(50);
	ui->sgloEdit->setValue(1.050);
	ui->grhlhiEdit->setValue(80);
	ui->sghiEdit->setValue(1.080);
	ui->prodEdit->clear();
	ui->thtEdit->clear();
    }
    connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditYeast::is_changed);
    connect(ui->typeEdit, &QComboBox::currentTextChanged, this, &EditYeast::is_changed);
    connect(ui->formEdit, &QComboBox::currentTextChanged, this, &EditYeast::is_changed);
    connect(ui->laboratoryEdit, &QLineEdit::textChanged, this, &EditYeast::is_changed);
    connect(ui->productidEdit, &QLineEdit::textChanged, this, &EditYeast::is_changed);
    connect(ui->temploEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::temp_lo_changed);
    connect(ui->temphiEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::temp_hi_changed);
    connect(ui->flocEdit, &QComboBox::currentTextChanged, this, &EditYeast::is_changed);
    connect(ui->attEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
    connect(ui->toleranceEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
    connect(ui->bestforEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
    connect(ui->reuseEdit, &QSpinBox::textChanged, this, &EditYeast::is_changed);
    connect(ui->inventoryEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
    connect(ui->costEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
    connect(ui->prodEdit, &QDateEdit::dateChanged, this, &EditYeast::is_changed);
    connect(ui->thtEdit, &QDateEdit::dateChanged, this, &EditYeast::is_changed);
    connect(ui->cellsEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
    connect(ui->sta1Edit, &QCheckBox::stateChanged, this, &EditYeast::is_changed);
    connect(ui->bacteriaEdit, &QCheckBox::stateChanged, this, &EditYeast::is_changed);
    connect(ui->harvesttopEdit, &QCheckBox::stateChanged, this, &EditYeast::is_changed);
    connect(ui->harvesttimeEdit, &QSpinBox::textChanged, this, &EditYeast::is_changed);
    connect(ui->pitchtempEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::is_changed);
    connect(ui->pofEdit, &QCheckBox::stateChanged, this, &EditYeast::is_changed);
    connect(ui->yeastbankEdit, &QLineEdit::textChanged, this, &EditYeast::is_changed);
    connect(ui->grhlloEdit, &QSpinBox::textChanged, this, &EditYeast::grhllo_changed);
    connect(ui->sgloEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::sglo_changed);
    connect(ui->grhlhiEdit, &QSpinBox::textChanged, this, &EditYeast::grhlhi_changed);
    connect(ui->sghiEdit, &QDoubleSpinBox::textChanged, this, &EditYeast::sghi_changed);

    ui->saveButton->setEnabled(false);
    ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && id >= 0) ? true:false);
}


EditYeast::~EditYeast()
{
    qDebug() << "EditYeast done";
    delete ui;
    emit entry_changed();
}


void EditYeast::SetForm(int form)
{
    if (form == 0) {
	/*
	 * Liquid yeast
	 */
	ui->costLabel->setText(tr("Price per pack:"));
	ui->inventoryLabel->setText(tr("Inventory Packs:"));
	ui->cellsLabel->setText(tr("Billion cells/pack:"));
	ui->inventoryEdit->setDecimals(0);
	ui->inventoryEdit->setSingleStep(1.0);
	ui->valueEdit->setValue(ui->inventoryEdit->value() * ui->costEdit->value());
    } else if (form == 1 || form == 6) {
	/*
	 * Dry or dried yeast
	 */
	ui->costLabel->setText(tr("Price per Kg:"));
	ui->inventoryLabel->setText(tr("Inventory gram:"));
	ui->cellsLabel->setText(tr("Billion cells/gram:"));
	ui->inventoryEdit->setDecimals(1);
	ui->inventoryEdit->setSingleStep(0.5);
	ui->valueEdit->setValue((ui->inventoryEdit->value() / 1000) * ui->costEdit->value());
    } else {
	/*
	 * All others
	 */
	ui->costLabel->setText(tr("Price per litre:"));
	ui->inventoryLabel->setText(tr("Inventory ml:"));
	ui->cellsLabel->setText(tr("Billion cells/ml:"));
	ui->inventoryEdit->setDecimals(1);
	ui->inventoryEdit->setSingleStep(0.5);
	ui->valueEdit->setValue((ui->inventoryEdit->value() / 1000) * ui->costEdit->value());
    }

    if (form == 1) {
	ui->grhlloEdit->setVisible(true);
	ui->grhlhiEdit->setVisible(true);
	ui->sgloEdit->setVisible(true);
	ui->sghiEdit->setVisible(true);
	ui->pitchloLabel->setVisible(true);
	ui->pitchhiLabel->setVisible(true);
	ui->at1->setVisible(true);
	ui->at2->setVisible(true);
    } else {
	ui->grhlloEdit->setVisible(false);
	ui->grhlhiEdit->setVisible(false);
	ui->sgloEdit->setVisible(false);
	ui->sghiEdit->setVisible(false);
	ui->pitchloLabel->setVisible(false);
	ui->pitchhiLabel->setVisible(false);
	ui->at1->setVisible(false);
	ui->at2->setVisible(false);
    }
}



/*
 * Window header, mark any change with '**'
 */
void EditYeast::WindowTitle()
{
    QString txt;

    if (this->recno < 0) {
	txt = QString(tr("BMSapp - Add new yeast"));
    } else {
	txt = QString(tr("BMSapp - Edit yeast %1").arg(this->recno));
    }

    if (this->textIsChanged) {
	txt.append((QString(" **")));
    }
    setWindowTitle(txt);
}


void EditYeast::on_saveButton_clicked()
{
    QSqlQuery query;

    /* If there are errors in the form, show a message and do "return;" */
    if (ui->nameEdit->text().length() < 2) {
	QMessageBox::warning(this, tr("Edit Yeast"), tr("Name empty or too short."));
	return;
    }
    if (ui->laboratoryEdit->text().length() < 2) {
        QMessageBox::warning(this, tr("Edit Yeast"), tr("Laboratory empty or too short."));
        return;
    }

    if (this->textIsChanged) {
    	if (this->recno == -1) {
    	    query.prepare("INSERT INTO inventory_yeasts SET name=:name, type=:type, form=:form, "
		"laboratory=:laboratory, product_id=:productid, min_temperature=:templo, max_temperature=:temphi, "
		"flocculation=:floc, attenuation=:att, tolerance=:tolerance, notes=:notes, best_for=:bestfor, "
		"max_reuse=:reuse, inventory=:inventory, cost=:cost, production_date=:prod, tht_date=:tht, "
		"cells=:cells, sta1=:sta1, bacteria=:bacteria, harvest_top=:harvesttop, harvest_time=:harvesttime, "
		"pitch_temperature=:pitchtemp, pofpos=:pof, short_desc=:yeastbank, gr_hl_lo=:grhllo, sg_lo=:sglo, "
		"gr_hl_hi=:grhlhi, sg_hi=:sghi, uuid = :uuid");
    	} else {
	    query.prepare("UPDATE inventory_yeasts SET name=:name, type=:type, form=:form, "
		"laboratory=:laboratory, product_id=:productid, min_temperature=:templo, max_temperature=:temphi, "
		"flocculation=:floc, attenuation=:att, tolerance=:tolerance, notes=:notes, best_for=:bestfor, "
		"max_reuse=:reuse, inventory=:inventory, cost=:cost, production_date=:prod, tht_date=:tht, "
		"cells=:cells, sta1=:sta1, bacteria=:bacteria, harvest_top=:harvesttop, harvest_time=:harvesttime, "
		"pitch_temperature=:pitchtemp, pofpos=:pof, short_desc=:yeastbank, gr_hl_lo=:grhllo, sg_lo=:sglo, "
		"gr_hl_hi=:grhlhi, sg_hi=:sghi WHERE record = :recno");
    	}
	query.bindValue(":name", ui->nameEdit->text());
	query.bindValue(":type", ui->typeEdit->currentIndex());
	query.bindValue(":form", ui->formEdit->currentIndex());
	query.bindValue(":laboratory", ui->laboratoryEdit->text());
	query.bindValue(":productid", ui->productidEdit->text());
	query.bindValue(":templo", QString("%1").arg(ui->temploEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":temphi", QString("%1").arg(ui->temphiEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":floc", ui->flocEdit->currentIndex());
	query.bindValue(":att", QString("%1").arg(ui->attEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":tolerance", QString("%1").arg(ui->toleranceEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":notes", ui->notesEdit->toPlainText());
	query.bindValue(":bestfor", ui->bestforEdit->toPlainText());
	query.bindValue(":reuse", ui->reuseEdit->value());
	if (ui->formEdit->currentIndex() == 0)
	    query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value(), 5, 'f', 4, '0'));
	else
	    query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value() / 1000, 5, 'f', 4, '0'));
	query.bindValue(":cost", QString("%1").arg(ui->costEdit->value(), 3, 'f', 2, '0'));
	/* Uses https://www.qtcentre.org/threads/17295-How-to-put-empty-value-in-QDateEdit */
	query.bindValue(":prod", ui->prodEdit->nullDate());
	query.bindValue(":tht", ui->thtEdit->nullDate());
	query.bindValue(":cells", QString("%1").arg(ui->cellsEdit->value() * 1000000000, 1, 'f', 0, '0'));
	query.bindValue(":sta1", ui->sta1Edit->isChecked() ? 1:0);
	query.bindValue(":bacteria", ui->bacteriaEdit->isChecked() ? 1:0);
	query.bindValue(":harvesttop", ui->harvesttopEdit->isChecked() ? 1:0);
	query.bindValue(":harvesttime", ui->harvesttimeEdit->value());
	query.bindValue(":pitchtemp", QString("%1").arg(ui->pitchtempEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":pof", ui->pofEdit->isChecked() ? 1:0);
	query.bindValue(":yeastbank", ui->yeastbankEdit->text());
	query.bindValue(":grhllo", QString("%1").arg(ui->grhlloEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":sglo", ui->sgloEdit->value());
	query.bindValue(":grhlhi", QString("%1").arg(ui->grhlhiEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":sghi", ui->sghiEdit->value());
	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()) {
	    qDebug() << "EditYeast" << 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() << "EditYeast Saved";
	}
    }

    ui->saveButton->setEnabled(false);
    this->textIsChanged = false;
    WindowTitle();
}


void EditYeast::on_deleteButton_clicked()
{
    QSqlQuery query;

    query.prepare("DELETE FROM inventory_yeasts WHERE record = :recno");
    query.bindValue(":recno", this->recno);
    query.exec();
    if (query.lastError().isValid()) {
	qDebug() << "EditYeast" << 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() << "EditYeast Deleted" << this->recno;
    }

    this->close();
    this->setResult(1);
}


void EditYeast::is_changed()
{
    SetForm(ui->formEdit->currentIndex());
    ui->saveButton->setEnabled(true);
    ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && this->recno >= 0) ? true:false);
    this->textIsChanged = true;
    WindowTitle();
}


void EditYeast::temp_lo_changed()
{
    if (ui->temploEdit->value() > (ui->temphiEdit->value() - 1))
	ui->temphiEdit->setValue(ui->temploEdit->value() + 1);
    is_changed();
}


void EditYeast::temp_hi_changed()
{
    if (ui->temphiEdit->value() < (ui->temploEdit->value() + 1))
	ui->temploEdit->setValue(ui->temphiEdit->value() - 1);
    is_changed();
}


void EditYeast::grhllo_changed()
{
    if (ui->grhlloEdit->value() > (ui->grhlhiEdit->value() - 1))
	ui->grhlhiEdit->setValue(ui->grhlloEdit->value() + 1);
    is_changed();
}


void EditYeast::sglo_changed()
{
    if (ui->sgloEdit->value() > (ui->sghiEdit->value() - 0.010))
	ui->sghiEdit->setValue(ui->sgloEdit->value() + 0.010);
    is_changed();
}


void EditYeast::grhlhi_changed()
{
    if (ui->grhlhiEdit->value() < (ui->grhlloEdit->value() + 1))
	ui->grhlloEdit->setValue(ui->grhlhiEdit->value() - 1);
    is_changed();
}


void EditYeast::sghi_changed()
{
    if (ui->sghiEdit->value() < (ui->sgloEdit->value() + 0.010))
	ui->sgloEdit->setValue(ui->sghiEdit->value() - 0.010);
    is_changed();
}


void EditYeast::on_quitButton_clicked()
{
    if (this->textIsChanged) {
	int rc = QMessageBox::warning(this, tr("Yeast changed"), tr("The yeast 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