src/EditFermentable.cpp

Fri, 29 Jul 2022 13:12:26 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Fri, 29 Jul 2022 13:12:26 +0200
changeset 373
b02aca4e926c
parent 293
6df94ca2bfde
child 385
09af9f46518f
permissions
-rw-r--r--

First load of changes for hops. In EditHop load the dropdown buttons from the global table. Use named query fields. Added database utilisation and bu_factor fields for hop extracts. Added edit fields for these new fields. Added post boil SG, utilisation and bu_factor parameters to the toIBU function. Added hops form parameter to the hopFlavourContribution and hopAromaContribution display bars. In the hops inventory list dispay volumes instead of weight for hop extracts. Modified the TinsethIBU function to use utilisation and bu_factor parameters. Add calculations for co2 and iso hop extracts, this is work in progress. The toIBU function makes use of the preSG and postSG values to use the correct SG to caall the TinsethIBU function. This results in a bit lower IBU values mostly affecting the late additions. Added use hop at bottling for iso hop extracts like Tetra hops using the formula from BarthHaas.

/**
 * EditFermentable.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 "EditFermentable.h"
#include "../ui/ui_EditFermentable.h"
#include "MainWindow.h"
#include "Utils.h"


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

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

    WindowTitle();

    ui->typeEdit->addItem(tr("Grain"));
    ui->typeEdit->addItem(tr("Sugar"));
    ui->typeEdit->addItem(tr("Extract"));
    ui->typeEdit->addItem(tr("Dry extract"));
    ui->typeEdit->addItem(tr("Adjunct"));

    ui->graintypeEdit->addItem(tr("Base"));
    ui->graintypeEdit->addItem(tr("Roast"));
    ui->graintypeEdit->addItem(tr("Crystal"));
    ui->graintypeEdit->addItem(tr("Kilned"));
    ui->graintypeEdit->addItem(tr("Sour Malt"));
    ui->graintypeEdit->addItem(tr("Special"));
    ui->graintypeEdit->addItem(tr("No malt"));

    ui->addedEdit->addItem(tr("Mash"));
    ui->addedEdit->addItem(tr("Boil"));
    ui->addedEdit->addItem(tr("Fermentation"));
    ui->addedEdit->addItem(tr("Lagering"));
    ui->addedEdit->addItem(tr("Bottle"));
    ui->addedEdit->addItem(tr("Kegs"));

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

	QSqlRecord rec = query.record();
    	for (int i = 0; i < rec.count(); i++)
            qDebug() << i << rec.fieldName(i) << query.value(i);

	ui->nameEdit->setText(query.value("name").toString());
	ui->notesEdit->setPlainText(query.value("notes").toString());
	ui->originEdit->setText(query.value("origin").toString());
	ui->supplierEdit->setText(query.value("supplier").toString());
	ui->typeEdit->setCurrentIndex(query.value("type").toInt());
	ui->graintypeEdit->setCurrentIndex(query.value("graintype").toInt());
	ui->maxinbatchEdit->setValue(query.value("max_in_batch").toDouble());
	ui->mashEdit->setChecked(query.value("recommend_mash").toInt() ? true:false);
	ui->addafterEdit->setChecked(query.value("add_after_boil").toInt() ? true:false);
	ui->addedEdit->setCurrentIndex(query.value("added").toInt());
	ui->alwaysEdit->setChecked(query.value("always_on_stock").toInt() ? true:false);
	ui->inventoryEdit->setValue(query.value("inventory").toDouble());
	ui->costEdit->setValue(query.value("cost").toDouble());
	ui->valueEdit->setValue(query.value("inventory").toDouble() * query.value("cost").toDouble());
	ui->yieldEdit->setValue(query.value("yield").toDouble());
	ui->colorEdit->setValue(query.value("color").toDouble());
	ui->moistureEdit->setValue(query.value("moisture").toDouble());
	ui->coarseEdit->setValue(query.value("coarse_fine_diff").toDouble());
	ui->proteinEdit->setValue(query.value("protein").toDouble());
	ui->dissolvedEdit->setValue(query.value("dissolved_protein").toDouble());
	ui->lintnerEdit->setValue(query.value("diastatic_power").toDouble());
	ui->wkEdit->setValue(Utils::lintner_to_kolbach(query.value("diastatic_power").toDouble()));
	ui->diphEdit->setValue(query.value("di_ph").toDouble());
	ui->acidphEdit->setValue(query.value("acid_to_ph_57").toDouble());
	if (query.value("production_date").toString().length() == 10) {
            ui->prodEdit->setDate(query.value("production_date").toDate());
        } else {
            ui->prodEdit->clear();
        }
	if (query.value("tht_date").toString().length() == 10) {
	    ui->thtEdit->setDate(query.value("tht_date").toDate());
	} else {
	    ui->thtEdit->clear();
	}
    } else {
	/* Set some defaults */
	ui->typeEdit->setCurrentIndex(0);
	ui->graintypeEdit->setCurrentIndex(0);
	ui->maxinbatchEdit->setValue(100);
	ui->mashEdit->setChecked(true);
	ui->addedEdit->setCurrentIndex(0);
	ui->yieldEdit->setValue(80);
	ui->colorEdit->setValue(3);
	ui->coarseEdit->setValue(3);
	ui->moistureEdit->setValue(4);
	ui->prodEdit->clear();
        ui->thtEdit->clear();
    }
    connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditFermentable::is_changed);
    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
    connect(ui->originEdit, &QLineEdit::textChanged, this, &EditFermentable::is_changed);
    connect(ui->supplierEdit, &QLineEdit::textChanged, this, &EditFermentable::is_changed);
    connect(ui->typeEdit, &QComboBox::currentTextChanged, this, &EditFermentable::is_changed);
    connect(ui->graintypeEdit, &QComboBox::currentTextChanged, this, &EditFermentable::is_changed);
    connect(ui->maxinbatchEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed);
    connect(ui->mashEdit, &QCheckBox::stateChanged, this, &EditFermentable::is_changed);
    connect(ui->addafterEdit, &QCheckBox::stateChanged, this, &EditFermentable::is_changed);
    connect(ui->addedEdit, &QComboBox::currentTextChanged, this, &EditFermentable::is_changed);
    connect(ui->alwaysEdit, &QCheckBox::stateChanged, this, &EditFermentable::is_changed);
    connect(ui->inventoryEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed);
    connect(ui->costEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed);
    connect(ui->yieldEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed);
    connect(ui->colorEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed);
    connect(ui->moistureEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed);
    connect(ui->coarseEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed);
    connect(ui->proteinEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed);
    connect(ui->dissolvedEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed);
    connect(ui->wkEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditFermentable::wk_changed);
    connect(ui->lintnerEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditFermentable::lintner_changed);
    connect(ui->diphEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed);
    connect(ui->acidphEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_changed);
    connect(ui->prodEdit, &QDateEdit::dateChanged, this, &EditFermentable::is_changed);
    connect(ui->prodButton1, SIGNAL(clicked()), this, SLOT(prod_date_today()));
    connect(ui->prodButton2, SIGNAL(clicked()), this, SLOT(prod_date_clear()));
    connect(ui->thtEdit, &QDateEdit::dateChanged, this, &EditFermentable::is_changed);
    connect(ui->thtButton1, SIGNAL(clicked()), this, SLOT(tht_date_today()));
    connect(ui->thtButton2, SIGNAL(clicked()), this, SLOT(tht_date_clear()));

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


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


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

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

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


void EditFermentable::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 Fermentable"), tr("Name empty or too short."));
	return;
    }
    if (ui->originEdit->text().length() < 2) {
        QMessageBox::warning(this, tr("Edit Fermentable"), tr("Origin empty or too short."));
        return;
    }
    if (ui->supplierEdit->text().length() < 2) {
        QMessageBox::warning(this, tr("Edit Fermentable"), tr("Supplier empty or too short."));
        return;
    }

    if (this->textIsChanged) {
    	if (this->recno == -1) {
    	    query.prepare("INSERT INTO inventory_fermentables SET name=:name, type=:type, yield=:yield, color=:color, "
	        "add_after_boil=:addafter, origin=:origin, supplier=:supplier, notes=:notes, coarse_fine_diff=:coarse, "
		"moisture=:moisture, diastatic_power=:diastatic, protein=:protein, dissolved_protein=:dissolved, "
		"max_in_batch=:maxinbatch, recommend_mash=:mash, added=:added, always_on_stock=:always, di_ph=:diph, "
		"acid_to_ph_57=:acidph, graintype=:graintype, inventory=:inventory, cost=:cost, production_date=:prod, "
		"tht_date=:tht, uuid = :uuid");
    	} else {
	    query.prepare("UPDATE inventory_fermentables SET name=:name, type=:type, yield=:yield, color=:color, "
	        "add_after_boil=:addafter, origin=:origin, supplier=:supplier, notes=:notes, coarse_fine_diff=:coarse, "
                "moisture=:moisture, diastatic_power=:diastatic, protein=:protein, dissolved_protein=:dissolved, "
                "max_in_batch=:maxinbatch, recommend_mash=:mash, added=:added, always_on_stock=:always, di_ph=:diph, "
                "acid_to_ph_57=:acidph, graintype=:graintype, inventory=:inventory, cost=:cost, production_date=:prod, "
                "tht_date=:tht WHERE record = :recno");
    	}
	query.bindValue(":name", ui->nameEdit->text());
	query.bindValue(":type", ui->typeEdit->currentIndex());
	query.bindValue(":yield", QString("%1").arg(ui->yieldEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":color", QString("%1").arg(ui->colorEdit->value(), 1, 'f', 0, '0'));
	query.bindValue(":addafter", ui->addafterEdit->isChecked() ? 1:0);
	query.bindValue(":origin", ui->originEdit->text());
	query.bindValue(":supplier", ui->supplierEdit->text());
	query.bindValue(":notes", ui->notesEdit->toPlainText());
	query.bindValue(":coarse", QString("%1").arg(ui->coarseEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":moisture", QString("%1").arg(ui->moistureEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":diastatic", Utils::kolbach_to_lintner(ui->wkEdit->value()));
	query.bindValue(":protein", QString("%1").arg(ui->proteinEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":dissolved", QString("%1").arg(ui->dissolvedEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":maxinbatch", QString("%1").arg(ui->maxinbatchEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":mash", ui->mashEdit->isChecked() ? 1:0);
	query.bindValue(":added", ui->addedEdit->currentIndex());
	query.bindValue(":always", ui->alwaysEdit->isChecked() ? 1:0);
	query.bindValue(":diph", QString("%1").arg(ui->diphEdit->value(), 3, 'f', 2, '0'));
	query.bindValue(":acidph", QString("%1").arg(ui->acidphEdit->value(), 6, 'f', 5, '0'));
	query.bindValue(":graintype", ui->graintypeEdit->currentIndex());
	query.bindValue(":inventory", QString("%1").arg(ui->inventoryEdit->value(), 4, 'f', 3, '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());
	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() << "EditFermentable" << 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() << "EditFermentable Saved";
	}
    }

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


void EditFermentable::on_deleteButton_clicked()
{
    QSqlQuery query;

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

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


void EditFermentable::is_changed()
{
    ui->valueEdit->setValue(ui->inventoryEdit->value() * ui->costEdit->value());
    ui->saveButton->setEnabled(true);
    ui->deleteButton->setEnabled((ui->inventoryEdit->value() == 0 && this->recno >= 0) ? true:false);
    this->textIsChanged = true;
    WindowTitle();
}


void EditFermentable::wk_changed(double val)
{
    const QSignalBlocker blocker1(ui->lintnerEdit);
    if (val == 0)
	ui->lintnerEdit->setValue(0);
    else
    	ui->lintnerEdit->setValue(Utils::kolbach_to_lintner(val));
    is_changed();
}


void EditFermentable::lintner_changed(double val)
{
    const QSignalBlocker blocker1(ui->wkEdit);
    ui->wkEdit->setValue(Utils::lintner_to_kolbach(val));
    is_changed();
}


void EditFermentable::on_quitButton_clicked()
{
    if (this->textIsChanged) {
	int rc = QMessageBox::warning(this, tr("Fermentable changed"), tr("This fermentable 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);
}


void EditFermentable::prod_date_clear()
{
    ui->prodEdit->setDate(QDate());
    is_changed();
}


void EditFermentable::prod_date_today()
{
    ui->prodEdit->setDate(QDate::currentDate());
    is_changed();
}


void EditFermentable::tht_date_clear()
{
    ui->thtEdit->setDate(QDate());
    is_changed();
}


void EditFermentable::tht_date_today()
{
    ui->thtEdit->setDate(QDate::currentDate());
    is_changed();
}

mercurial