Wed, 18 Oct 2023 16:00:06 +0200
Added application icon
/** * 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 "MainWindow.h" #include "EditEquipment.h" #include "../ui/ui_EditEquipment.h" #include "global.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(); for (int i = 0; i < 4; i++) ui->tun_materialEdit->addItem(QCoreApplication::translate("TunMaterial", g_tun_materials[i])); for (int i = 0; i < 5; i++) ui->chiller_typeEdit->addItem(QCoreApplication::translate("ChillerType", g_chiller_types[i])); 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("name").toString()); ui->boil_sizeEdit->setValue(query.value("boil_size").toDouble()); ui->batch_sizeEdit->setValue(query.value("batch_size").toDouble()); ui->tun_volumeEdit->setValue(query.value("tun_volume").toDouble()); ui->tun_weightEdit->setValue(query.value("tun_weight").toDouble()); ui->tun_specific_heatEdit->setValue(query.value("tun_specific_heat").toDouble()); ui->tun_materialEdit->setCurrentIndex(query.value("tun_material").toInt()); ui->tun_heightEdit->setValue(query.value("tun_height").toDouble() * 100.0); ui->top_up_waterEdit->setValue(query.value("top_up_water").toDouble()); ui->trub_lossEdit->setValue(query.value("trub_loss").toDouble()); ui->evap_rateEdit->setValue(query.value("evap_rate").toDouble()); ui->boil_timeEdit->setValue(query.value("boil_time").toDouble()); ui->top_up_kettleEdit->setValue(query.value("top_up_kettle").toDouble()); ui->notesEdit->setPlainText(query.value("notes").toString()); ui->lauter_deadspaceEdit->setValue(query.value("lauter_deadspace").toDouble()); ui->kettle_volumeEdit->setValue(query.value("kettle_volume").toDouble()); ui->kettle_heightEdit->setValue(query.value("kettle_height").toDouble() * 100.0); ui->mash_volumeEdit->setValue(query.value("mash_volume").toDouble()); ui->mash_maxEdit->setValue(query.value("mash_max").toDouble()); ui->efficiencyEdit->setValue(query.value("efficiency").toDouble()); ui->chiller_typeEdit->setCurrentIndex(query.value("chiller_type").toInt()); ui->chiller_to79Edit->setValue(query.value("chiller_to79").toDouble()); ui->chiller_lossEdit->setValue(query.value("chiller_loss").toDouble()); ui->chiller_volumeEdit->setValue(query.value("chiller_volume").toDouble()); ui->chiller_lpmEdit->setValue(query.value("chiller_lpm").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("name").toString()); query2.exec(); inuse = query2.size(); 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->trub_lossEdit->setValue(0.5); ui->evap_rateEdit->setValue(1.8); ui->boil_timeEdit->setValue(90); ui->top_up_kettleEdit->setValue(0); 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); ui->chiller_typeEdit->setCurrentIndex(1); ui->chiller_to79Edit->setValue(10); ui->chiller_lossEdit->setValue(0); ui->chiller_volumeEdit->setValue(0.5); ui->chiller_lpmEdit->setValue(0); inuse = 0; } /* Block select no chiller used */ qobject_cast<QStandardItemModel*>(ui->chiller_typeEdit->model())->item(CHILLER_TYPE_NONE)->setEnabled(false); chiller_setup(ui->chiller_typeEdit->currentIndex()); 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->trub_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->top_up_kettleEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); connect(ui->notesEdit, SIGNAL(textChanged()), this, SLOT(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); connect(ui->chiller_typeEdit, &QComboBox::currentTextChanged, this, &EditEquipment::chiller_changed); connect(ui->chiller_to79Edit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); connect(ui->chiller_volumeEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); connect(ui->chiller_lpmEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); connect(ui->chiller_lossEdit, &QDoubleSpinBox::textChanged, this, &EditEquipment::is_changed); calcBatchVolume(); ui->saveButton->setEnabled(false); ui->deleteButton->setEnabled((inuse == 0 && id >= 0) ? true:false); } EditEquipment::~EditEquipment() { 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; QString sql = ""; /* 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) { sql = "INSERT INTO"; } else { sql = "UPDATE"; } sql.append(" 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_loss=:trub_loss, evap_rate=:evap_rate, " "boil_time=:boil_time, top_up_kettle=:top_up_kettle, " "notes=:notes, lauter_deadspace=:lauter_deadspace, kettle_volume=:kettle_volume, " "kettle_height=:kettle_height, mash_volume=:mash_volume, mash_max=:mash_max, " "efficiency=:efficiency, chiller_type=:chiller_type, chiller_to79=:chiller_to79, " "chiller_volume=:chiller_volume, chiller_lpm=:chiller_lpm, chiller_loss=:chiller_loss"); if (this->recno == -1) { sql.append(", uuid=:uuid"); } else { sql.append(" WHERE record = :recno"); } query.prepare(sql); 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(":trub_loss", QString("%1").arg(ui->trub_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(":top_up_kettle", QString("%1").arg(ui->top_up_kettleEdit->value(), 2, 'f', 1, '0')); query.bindValue(":notes", ui->notesEdit->toPlainText()); 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')); query.bindValue(":chiller_type", ui->chiller_typeEdit->currentIndex()); query.bindValue(":chiller_to79", QString("%1").arg(ui->chiller_to79Edit->value(), 1, 'f', 0, '0')); query.bindValue(":chiller_volume", QString("%1").arg(ui->chiller_volumeEdit->value(), 3, 'f', 2, '0')); query.bindValue(":chiller_lpm", QString("%1").arg(ui->chiller_lpmEdit->value(), 3, 'f', 2, '0')); query.bindValue(":chiller_loss", QString("%1").arg(ui->chiller_lossEdit->value(), 3, 'f', 2, '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() << "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_cloneButton_clicked() { QSqlQuery query; 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_loss=:trub_loss, evap_rate=:evap_rate, " "boil_time=:boil_time, top_up_kettle=:top_up_kettle, " "notes=:notes, lauter_deadspace=:lauter_deadspace, kettle_volume=:kettle_volume, " "kettle_height=:kettle_height, mash_volume=:mash_volume, mash_max=:mash_max, " "efficiency=:efficiency, chiller_type=:chiller_type, chiller_to79=:chiller_to79, " "chiller_volume=:chiller_volume, chiller_lpm=:chiller_lpm, chiller_loss=:chiller_loss, uuid=:uuid"); query.bindValue(":name", ui->nameEdit->text() + " [copy]"); 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(":trub_loss", QString("%1").arg(ui->trub_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(":top_up_kettle", QString("%1").arg(ui->top_up_kettleEdit->value(), 2, 'f', 1, '0')); query.bindValue(":notes", ui->notesEdit->toPlainText()); 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')); query.bindValue(":uuid", QUuid::createUuid().toString().mid(1, 36)); query.bindValue(":chiller_type", ui->chiller_typeEdit->currentIndex()); query.bindValue(":chiller_to79", QString("%1").arg(ui->chiller_to79Edit->value(), 1, 'f', 0, '0')); query.bindValue(":chiller_volume", QString("%1").arg(ui->chiller_volumeEdit->value(), 3, 'f', 2, '0')); query.bindValue(":chiller_lpm", QString("%1").arg(ui->chiller_lpmEdit->value(), 3, 'f', 2, '0')); query.bindValue(":chiller_loss", QString("%1").arg(ui->chiller_lossEdit->value(), 3, 'f', 2, '0')); query.exec(); if (query.lastError().isValid()) { qWarning() << "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"; } } void EditEquipment::on_deleteButton_clicked() { QSqlQuery query; int rc = QMessageBox::warning(this, tr("Delete equipment"), tr("Delete %1").arg(ui->nameEdit->text()), QMessageBox::Yes | QMessageBox::No, QMessageBox::No); if (rc == QMessageBox::No) return; query.prepare("DELETE FROM inventory_equipments WHERE record = :recno"); query.bindValue(":recno", this->recno); query.exec(); if (query.lastError().isValid()) { qWarning() << "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->trub_lossEdit->value() - ui->chiller_lossEdit->value()) * 100) / 100.0; batch = round(batch * 100) / 100.0; if (ui->batch_sizeEdit->value() != batch) qDebug() << "Batch volue updated to" << batch; 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::chiller_setup(int val) { if (val == CHILLER_TYPE_IMMERSION) { ui->chiller_volumeEdit->setReadOnly(false); ui->chiller_volumeEdit->setButtonSymbols(QAbstractSpinBox::UpDownArrows); ui->chiller_volumeEdit->setFixedSize(101, 24); if (ui->chiller_volumeEdit->value() == 0) ui->chiller_volumeEdit->setValue(0.5); } else { ui->chiller_volumeEdit->setReadOnly(true); ui->chiller_volumeEdit->setButtonSymbols(QAbstractSpinBox::NoButtons); ui->chiller_volumeEdit->setFixedSize(86, 24); ui->chiller_volumeEdit->setValue(0); } if (val == CHILLER_TYPE_COUNTERFLOW) { ui->chiller_to79Edit->setReadOnly(true); ui->chiller_to79Edit->setButtonSymbols(QAbstractSpinBox::NoButtons); ui->chiller_to79Edit->setFixedSize(86, 24); ui->chiller_to79Edit->setValue(0); ui->chiller_lpmEdit->setReadOnly(false); ui->chiller_lpmEdit->setButtonSymbols(QAbstractSpinBox::UpDownArrows); ui->chiller_lpmEdit->setFixedSize(101, 24); if (ui->chiller_lpmEdit->value() == 0) ui->chiller_lpmEdit->setValue(2.5); ui->chiller_lossEdit->setReadOnly(false); ui->chiller_lossEdit->setButtonSymbols(QAbstractSpinBox::UpDownArrows); ui->chiller_lossEdit->setFixedSize(101, 24); if (ui->chiller_lossEdit->value() == 0) ui->chiller_lossEdit->setValue(0.4); } else { ui->chiller_to79Edit->setReadOnly(false); ui->chiller_to79Edit->setButtonSymbols(QAbstractSpinBox::UpDownArrows); ui->chiller_to79Edit->setFixedSize(101, 24); int time = 5; if (val == CHILLER_TYPE_AUBAINMARIE) time = 20; if (val == CHILLER_TYPE_NOCHILL) time = 240; if (ui->chiller_to79Edit->value() == 0) ui->chiller_to79Edit->setValue(time); ui->chiller_lpmEdit->setReadOnly(true); ui->chiller_lpmEdit->setButtonSymbols(QAbstractSpinBox::NoButtons); ui->chiller_lpmEdit->setFixedSize(86, 24); ui->chiller_lpmEdit->setValue(0); ui->chiller_lossEdit->setReadOnly(true); ui->chiller_lossEdit->setButtonSymbols(QAbstractSpinBox::NoButtons); ui->chiller_lossEdit->setFixedSize(86, 24); ui->chiller_lossEdit->setValue(0); } } void EditEquipment::chiller_changed() { const QSignalBlocker blocker1(ui->chiller_to79Edit); const QSignalBlocker blocker2(ui->chiller_volumeEdit); const QSignalBlocker blocker3(ui->chiller_lpmEdit); const QSignalBlocker blocker4(ui->chiller_lossEdit); chiller_setup(ui->chiller_typeEdit->currentIndex()); 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); }