src/EditEquipment.cpp

Thu, 21 Apr 2022 17:22:01 +0200

author
Michiel Broek <mbroek@mbse.eu>
date
Thu, 21 Apr 2022 17:22:01 +0200
changeset 151
b5b2483f3a3f
parent 90
2396457a8167
child 385
09af9f46518f
permissions
-rw-r--r--

New recipe, calculate the boil_size. Lot's of ignoreChanges removeals and where needed QSignalBlocker is used. Bottle priming calculation added. In fermentables editor block and release to100 settings only in mash to fermentation steps, bottle and kegging are ignored. Update the IBU slider after hop changes. Set the mash name when another mash profile is selected. Don't backup initial infuse amount if there was no mash table. A small cosmetic layout change on the mash tab.

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


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

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

    WindowTitle();
    
    ui->tun_materialEdit->addItem(tr("Stainless Steel"));
    ui->tun_materialEdit->addItem(tr("Aluminium"));
    ui->tun_materialEdit->addItem(tr("Plastics"));
    ui->tun_materialEdit->addItem(tr("Copper"));

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

	ui->nameEdit->setText(query.value(1).toString());
	ui->boil_sizeEdit->setValue(query.value(2).toDouble());
	ui->batch_sizeEdit->setValue(query.value(3).toDouble());
	ui->tun_volumeEdit->setValue(query.value(4).toDouble());
	ui->tun_weightEdit->setValue(query.value(5).toDouble());
	ui->tun_specific_heatEdit->setValue(query.value(6).toDouble());
	ui->tun_materialEdit->setCurrentIndex(query.value(7).toInt());
	ui->tun_heightEdit->setValue(query.value(8).toDouble() * 100.0);
	ui->top_up_waterEdit->setValue(query.value(9).toDouble());
	ui->chiller_lossEdit->setValue(query.value(10).toDouble());
	ui->evap_rateEdit->setValue(query.value(11).toDouble());
	ui->boil_timeEdit->setValue(query.value(12).toDouble());
	ui->calcboilEdit->setChecked(query.value(13).toInt() ? true:false);
	ui->top_up_kettleEdit->setValue(query.value(14).toDouble());
	ui->hopfactorEdit->setValue(query.value(15).toDouble());
	ui->notesEdit->setPlainText(query.value(16).toString());
	ui->lauter_volumeEdit->setValue(query.value(17).toDouble());
	ui->lauter_heightEdit->setValue(query.value(18).toDouble() * 100.0);
	ui->lauter_deadspaceEdit->setValue(query.value(19).toDouble());
	ui->kettle_volumeEdit->setValue(query.value(20).toDouble());
	ui->kettle_heightEdit->setValue(query.value(21).toDouble() * 100.0);
	ui->mash_volumeEdit->setValue(query.value(22).toDouble());
	ui->mash_maxEdit->setValue(query.value(23).toDouble());
	ui->efficiencyEdit->setValue(query.value(24).toDouble());
	/*
	 * Now we have loaded this record, check if this equipment is
	 * being used by a product. If so, make the name field read-only.
	 */
	query2.prepare("SELECT eq_name FROM products WHERE eq_name=:name");
	query2.bindValue(":name", query.value(1).toString());
	query2.exec();
	inuse = query2.size();
	qDebug() << "in use" << inuse;
	ui->nameEdit->setReadOnly(inuse > 0);
    } else {
	/* Set some defaults */
	ui->boil_sizeEdit->setValue(18);
	ui->batch_sizeEdit->setValue(15.3);
	ui->tun_volumeEdit->setValue(20);
	ui->tun_weightEdit->setValue(2);
	ui->tun_specific_heatEdit->setValue(0.11);
	ui->tun_materialEdit->setCurrentIndex(0);
	ui->tun_heightEdit->setValue(20);
	ui->top_up_waterEdit->setValue(0);
	ui->chiller_lossEdit->setValue(0.5);
	ui->evap_rateEdit->setValue(1.8);
	ui->boil_timeEdit->setValue(90);
	ui->calcboilEdit->setChecked(true);
	ui->top_up_kettleEdit->setValue(0);
	ui->hopfactorEdit->setValue(100);
	ui->lauter_volumeEdit->setValue(20);
	ui->lauter_heightEdit->setValue(20);
	ui->lauter_deadspaceEdit->setValue(0.5);
	ui->kettle_volumeEdit->setValue(20);
	ui->kettle_heightEdit->setValue(20);
	ui->mash_volumeEdit->setValue(18);
	ui->mash_maxEdit->setValue(6);
	ui->efficiencyEdit->setValue(75);
	inuse = 0;
    }
    connect(ui->nameEdit, &QLineEdit::textChanged, this, &EditEquipment::is_changed);
    connect(ui->boil_sizeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->batch_sizeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->tun_volumeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->tun_weightEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->tun_materialEdit, &QComboBox::currentTextChanged, this, &EditEquipment::material_changed);   
    connect(ui->tun_heightEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->top_up_waterEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->chiller_lossEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->evap_rateEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->boil_timeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->calcboilEdit, &QCheckBox::stateChanged, this, &EditEquipment::is_changed);
    connect(ui->top_up_kettleEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->hopfactorEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(is_changed()));
    connect(ui->lauter_volumeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->lauter_heightEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->lauter_deadspaceEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->kettle_volumeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->kettle_heightEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->mash_volumeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->mash_maxEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);
    connect(ui->efficiencyEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed);

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


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


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

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

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


void EditEquipment::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 Equipment"), tr("Name empty or too short."));
	return;
    }

    if (this->textIsChanged) {
    	if (this->recno == -1) {
    	    query.prepare("INSERT INTO inventory_equipments SET name=:name, boil_size=:boil_size, "
		"batch_size=:batch_size, tun_volume=:tun_volume, tun_weight=:tun_weight, "
		"tun_specific_heat=:tun_specific_heat, tun_material=:tun_material, tun_height=:tun_height, "
		"top_up_water=:top_up_water, trub_chiller_loss=:chiller_loss, evap_rate=:evap_rate, "
		"boil_time=:boil_time, calc_boil_volume=:calcboil, top_up_kettle=:top_up_kettle, "
		"hop_utilization=:hopfactor, notes=:notes, lauter_volume=:lauter_volume, "
		"lauter_height=:lauter_height, lauter_deadspace=:lauter_deadspace, kettle_volume=:kettle_volume, "
		"kettle_height=:kettle_height, mash_volume=:mash_volume, mash_max=:mash_max, "
		"efficiency=:efficiency, uuid=:uuid");
    	} else {
	    query.prepare("UPDATE inventory_equipments SET name=:name, boil_size=:boil_size, "
		"batch_size=:batch_size, tun_volume=:tun_volume, tun_weight=:tun_weight, "
                "tun_specific_heat=:tun_specific_heat, tun_material=:tun_material, tun_height=:tun_height, "
                "top_up_water=:top_up_water, trub_chiller_loss=:chiller_loss, evap_rate=:evap_rate, "
                "boil_time=:boil_time, calc_boil_volume=:calcboil, top_up_kettle=:top_up_kettle, "
                "hop_utilization=:hopfactor, notes=:notes, lauter_volume=:lauter_volume, "
                "lauter_height=:lauter_height, lauter_deadspace=:lauter_deadspace, kettle_volume=:kettle_volume, "
		"kettle_height=:kettle_height, mash_volume=:mash_volume, mash_max=:mash_max, "
                "efficiency=:efficiency WHERE record=:recno");
    	}
	query.bindValue(":name", ui->nameEdit->text());
	query.bindValue(":boil_size", QString("%1").arg(ui->boil_sizeEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":batch_size", QString("%1").arg(ui->batch_sizeEdit->value(), 3, 'f', 2, '0'));
	query.bindValue(":tun_volume", QString("%1").arg(ui->tun_volumeEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":tun_weight", QString("%1").arg(ui->tun_weightEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":tun_specific_heat", QString("%1").arg(ui->tun_specific_heatEdit->value(), 4, 'f', 3, '0'));
	query.bindValue(":tun_material", ui->tun_materialEdit->currentIndex());
	query.bindValue(":tun_height", QString("%1").arg(ui->tun_heightEdit->value() / 100, 4, 'f', 3, '0'));
	query.bindValue(":top_up_water", QString("%1").arg(ui->top_up_waterEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":chiller_loss", QString("%1").arg(ui->chiller_lossEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":evap_rate", QString("%1").arg(ui->evap_rateEdit->value(), 3, 'f', 2, '0'));
	query.bindValue(":boil_time", QString("%1").arg(ui->boil_timeEdit->value(), 1, 'f', 0, '0'));
	query.bindValue(":calcboil", ui->calcboilEdit->isChecked() ? 1:0);
	query.bindValue(":top_up_kettle", QString("%1").arg(ui->top_up_kettleEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":hopfactor", QString("%1").arg(ui->hopfactorEdit->value(), 1, 'f', 0, '0'));
	query.bindValue(":notes", ui->notesEdit->toPlainText());
	query.bindValue(":lauter_volume", QString("%1").arg(ui->lauter_volumeEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":lauter_height", QString("%1").arg(ui->lauter_heightEdit->value() / 100, 4, 'f', 3, '0'));
	query.bindValue(":lauter_deadspace", QString("%1").arg(ui->lauter_deadspaceEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":kettle_volume", QString("%1").arg(ui->kettle_volumeEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":kettle_height", QString("%1").arg(ui->kettle_heightEdit->value() / 100, 4, 'f', 3, '0'));
	query.bindValue(":mash_volume", QString("%1").arg(ui->mash_volumeEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":mash_max", QString("%1").arg(ui->mash_maxEdit->value(), 2, 'f', 1, '0'));
	query.bindValue(":efficiency", QString("%1").arg(ui->efficiencyEdit->value(), 2, 'f', 1, '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()) {
	    qDebug() << "EditEquipment" << 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() << "EditEquipment Saved";
	}
    }

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


void EditEquipment::on_deleteButton_clicked()
{
    QSqlQuery query;

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

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


void EditEquipment::calcBatchVolume()
{
    double batch = ui->boil_sizeEdit->value() - (ui->evap_rateEdit->value() * ui->boil_timeEdit->value() / 60) + ui->top_up_kettleEdit->value();
    double fermenter = round(((batch / 1.04) + ui->top_up_waterEdit->value() - ui->chiller_lossEdit->value()) * 100) / 100.0;
    batch = round(batch * 100) / 100.0;
    ui->batch_sizeEdit->setValue(batch);
    ui->vol_fermenterEdit->setValue(fermenter);
}


void EditEquipment::is_changed()
{
    calcBatchVolume();
    ui->saveButton->setEnabled(true);
    ui->deleteButton->setEnabled((inuse == 0 && this->recno >= 0) ? true:false);
    this->textIsChanged = true;
    WindowTitle();
}


void EditEquipment::material_changed()
{
    switch (ui->tun_materialEdit->currentIndex()) {
	case 0:	ui->tun_specific_heatEdit->setValue(0.11);	break;
	case 1:	ui->tun_specific_heatEdit->setValue(0.22);	break;
	case 2:	ui->tun_specific_heatEdit->setValue(0.46);	break;
	case 3:	ui->tun_specific_heatEdit->setValue(0.092);	break;
    }
    is_changed();
}


void EditEquipment::on_quitButton_clicked()
{
    if (this->textIsChanged) {
	int rc = QMessageBox::warning(this, tr("Equipment changed"), tr("This equipment 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