--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/src/EditFermentable.cpp Sat Feb 19 22:17:09 2022 +0100 @@ -0,0 +1,296 @@ +/** + * 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 "bmsapp.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(); + + ui->nameEdit->setText(query.value(1).toString()); + ui->notesEdit->setPlainText(query.value(8).toString()); + ui->originEdit->setText(query.value(6).toString()); + ui->supplierEdit->setText(query.value(7).toString()); + ui->typeEdit->setCurrentIndex(query.value(2).toInt()); + ui->graintypeEdit->setCurrentIndex(query.value(20).toInt()); + ui->maxinbatchEdit->setValue(query.value(14).toDouble()); + ui->mashEdit->setChecked(query.value(16).toInt() ? true:false); + ui->addafterEdit->setChecked(query.value(5).toInt() ? true:false); + ui->addedEdit->setCurrentIndex(query.value(17).toInt()); + ui->alwaysEdit->setChecked(query.value(5).toInt() ? true:false); + ui->inventoryEdit->setValue(query.value(21).toDouble()); + ui->costEdit->setValue(query.value(22).toDouble()); + ui->valueEdit->setValue(query.value(21).toDouble() * query.value(22).toDouble()); + ui->yieldEdit->setValue(query.value(3).toDouble()); + ui->colorEdit->setValue(query.value(4).toDouble()); + ui->moistureEdit->setValue(query.value(10).toDouble()); + ui->coarseEdit->setValue(query.value(9).toDouble()); + ui->proteinEdit->setValue(query.value(12).toDouble()); + ui->dissolvedEdit->setValue(query.value(13).toDouble()); + ui->diastaticEdit->setValue(Utils::lintner_to_kolbach(query.value(11).toDouble())); + ui->diphEdit->setValue(query.value(18).toDouble()); + ui->acidphEdit->setValue(query.value(19).toDouble()); + if (query.value(23).toString().length() == 10) { + ui->prodEdit->setDate(query.value(23).toDate()); + } else { + ui->prodEdit->clear(); + } + if (query.value(24).toString().length() == 10) { + ui->thtEdit->setDate(query.value(24).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); + } + 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->diastaticEdit, &QDoubleSpinBox::textChanged, this, &EditFermentable::is_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->thtEdit, &QDateEdit::dateChanged, this, &EditFermentable::is_changed); + + 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->diastaticEdit->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::on_quitButton_clicked() +{ + if (this->textIsChanged) { + int rc = QMessageBox::warning(this, tr("Fermentable changed"), tr("The fermentable has been modified\n 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); +}