# HG changeset patch # User Michiel Broek # Date 1649359673 -7200 # Node ID 84eda8ff3b60df99993c4ee8ce954eff9df463c1 # Parent 6f415bf6340aeb65444cfefa69664f406175cb96 The editrow variable replace by recipe->fermentables_current. The fermentable amount editor ignores fermentables added during bottling or kegging for percentage calculation. Added the fermentable percentage editor. diff -r 6f415bf6340a -r 84eda8ff3b60 src/EditRecipe.cpp --- a/src/EditRecipe.cpp Thu Apr 07 20:20:42 2022 +0200 +++ b/src/EditRecipe.cpp Thu Apr 07 21:27:53 2022 +0200 @@ -28,7 +28,7 @@ qDebug() << "EditRecipe record:" << id; recipe = new Recipe; ui->setupUi(this); - recipe->fermentables_current = recipe->hops_current = recipe->misc_current = recipe->yeasts_current = recipe->mashs_current = -1; + recipe->fermentables_row = recipe->hops_row = recipe->misc_row = recipe->yeasts_row = recipe->mashs_row = -1; recipe->fermentables_use100 = false; this->recno = id; @@ -1352,28 +1352,30 @@ if (recipe->fermentables_use100) return; - qDebug() << "ferment_amount_changed()" << editrow << val; + qDebug() << "ferment_amount_changed()" << recipe->fermentables_row << val; this->ignoreChanges = true; - recipe->fermentables[editrow].f_amount = val; + recipe->fermentables[recipe->fermentables_row].f_amount = val; item = new QTableWidgetItem(QString("%1 Kg").arg(val, 4, 'f', 3, '0')); item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); - ui->fermentablesTable->setItem(editrow, 7, item); + ui->fermentablesTable->setItem(recipe->fermentables_row, 7, item); for (int i = 0; i < recipe->fermentables.size(); i++) - total += recipe->fermentables.at(i).f_amount; - + if (recipe->fermentables.at(i).f_added < 4) // Only before bottle/kegging + total += recipe->fermentables.at(i).f_amount; /* * Recalculate the percentages */ for (int i = 0; i < recipe->fermentables.size(); i++) { - perc = recipe->fermentables.at(i).f_amount / total * 100; - recipe->fermentables[i].f_percentage = perc; - item = new QTableWidgetItem(QString("%1%").arg(perc, 2, 'f', 1, '0')); - item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); - ui->fermentablesTable->setItem(i, 8, item); - if (i == editrow) - this->pctEdit->setValue(perc); + if (recipe->fermentables.at(i).f_added < 4) { + perc = recipe->fermentables.at(i).f_amount / total * 100; + recipe->fermentables[i].f_percentage = perc; + item = new QTableWidgetItem(QString("%1%").arg(perc, 2, 'f', 1, '0')); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->fermentablesTable->setItem(i, 8, item); + if (i == recipe->fermentables_row) + this->pctEdit->setValue(perc); + } } this->ignoreChanges = false; is_changed(); @@ -1381,19 +1383,62 @@ void EditRecipe::ferment_pct_changed(double val) { + QTableWidgetItem *item; + double total = 0, row100 = -1; + if (! recipe->fermentables_use100) return; - qDebug() << "ferment_pct_changed()" << val; + qDebug() << "ferment_pct_changed()" << recipe->fermentables_row << val; + /* + * Since we have arrived here, adjust_to_100 is active and + * this is not the entry to be adjusted to 100. + */ + for (int i = 0; i < recipe->fermentables.size(); i++) { + if (recipe->fermentables.at(i).f_added < 4) // Only before bottle/kegging + total += recipe->fermentables.at(i).f_amount; + if (recipe->fermentables.at(i).f_adjust_to_total_100) + row100 = i; + } + double oldperc = recipe->fermentables.at(recipe->fermentables_row).f_percentage; + double diffp = val - oldperc; + double diffw = (diffp / 100) * total; + qDebug() << "row100" << row100 << "total" << total << "diff kg" << diffw << "diff %" << diffp; + + this->ignoreChanges = true; + recipe->fermentables[recipe->fermentables_row].f_percentage += diffp; + recipe->fermentables[recipe->fermentables_row].f_amount += diffw; + recipe->fermentables[row100].f_percentage -= diffp; + recipe->fermentables[row100].f_amount -= diffw; + + item = new QTableWidgetItem(QString("%1 Kg").arg(recipe->fermentables[recipe->fermentables_row].f_amount, 4, 'f', 3, '0')); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->fermentablesTable->setItem(recipe->fermentables_row, 7, item); + this->amountEdit->setValue(recipe->fermentables[recipe->fermentables_row].f_amount); + + item = new QTableWidgetItem(QString("%1%").arg(recipe->fermentables[recipe->fermentables_row].f_percentage, 2, 'f', 1, '0')); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->fermentablesTable->setItem(recipe->fermentables_row, 8, item); + + item = new QTableWidgetItem(QString("%1 Kg").arg(recipe->fermentables[row100].f_amount, 4, 'f', 3, '0')); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->fermentablesTable->setItem(row100, 7, item); + + item = new QTableWidgetItem(QString("%1%").arg(recipe->fermentables[row100].f_percentage, 2, 'f', 1, '0')); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->fermentablesTable->setItem(row100, 8, item); + + this->ignoreChanges = false; + is_changed(); } void EditRecipe::on_editFermentRow_clicked() { QPushButton *pb = qobject_cast(QObject::sender()); - editrow = pb->objectName().toInt(); - qDebug() << "Edit fermentable row" << editrow; - Fermentables backup = recipe->fermentables.at(editrow); + recipe->fermentables_row = pb->objectName().toInt(); + qDebug() << "Edit fermentable row" << recipe->fermentables_row; + Fermentables backup = recipe->fermentables.at(recipe->fermentables_row); QDialog* dialog = new QDialog(this); dialog->resize(738, 287); @@ -1455,12 +1500,12 @@ selectEdit->setGeometry(QRect(160, 70, 251, 23)); nameEdit = new QLineEdit(dialog); nameEdit->setObjectName(QString::fromUtf8("nameEdit")); - nameEdit->setText(recipe->fermentables.at(editrow).f_name); + nameEdit->setText(recipe->fermentables.at(recipe->fermentables_row).f_name); nameEdit->setGeometry(QRect(160, 10, 511, 23)); nameEdit->setReadOnly(true); supplierEdit = new QLineEdit(dialog); supplierEdit->setObjectName(QString::fromUtf8("supplierEdit")); - supplierEdit->setText(recipe->fermentables.at(editrow).f_supplier); + supplierEdit->setText(recipe->fermentables.at(recipe->fermentables_row).f_supplier); supplierEdit->setGeometry(QRect(160, 40, 511, 23)); supplierEdit->setReadOnly(true); amountEdit = new QDoubleSpinBox(dialog); @@ -1472,7 +1517,7 @@ amountEdit->setReadOnly(recipe->fermentables_use100); amountEdit->setMaximum(100000.0); amountEdit->setSingleStep(0.0010); - amountEdit->setValue(recipe->fermentables.at(editrow).f_amount); + amountEdit->setValue(recipe->fermentables.at(recipe->fermentables_row).f_amount); connect(amountEdit, QOverload::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::ferment_amount_changed); pctEdit = new QDoubleSpinBox(dialog); @@ -1482,7 +1527,7 @@ pctEdit->setAccelerated(true); pctEdit->setDecimals(1); if (recipe->fermentables_use100) { - if (recipe->fermentables.at(editrow).f_adjust_to_total_100) + if (recipe->fermentables.at(recipe->fermentables_row).f_adjust_to_total_100) pctEdit->setReadOnly(true); else pctEdit->setReadOnly(false); @@ -1491,7 +1536,7 @@ } pctEdit->setMaximum(100.0); pctEdit->setSingleStep(0.1); - pctEdit->setValue(recipe->fermentables.at(editrow).f_percentage); + pctEdit->setValue(recipe->fermentables.at(recipe->fermentables_row).f_percentage); connect(pctEdit, QOverload::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::ferment_pct_changed); addedEdit = new QComboBox(dialog); @@ -1503,12 +1548,12 @@ addedEdit->addItem(tr("Lagering")); addedEdit->addItem(tr("Bottle")); addedEdit->addItem(tr("Kegs")); - addedEdit->setCurrentIndex(recipe->fermentables.at(editrow).f_added); + addedEdit->setCurrentIndex(recipe->fermentables.at(recipe->fermentables_row).f_added); to100Edit = new QCheckBox(dialog); to100Edit->setObjectName(QString::fromUtf8("to100Edit")); to100Edit->setGeometry(QRect(160, 160, 85, 21)); - to100Edit->setChecked(recipe->fermentables.at(editrow).f_adjust_to_total_100); + to100Edit->setChecked(recipe->fermentables.at(recipe->fermentables_row).f_adjust_to_total_100); instockEdit = new QCheckBox(dialog); instockEdit->setObjectName(QString::fromUtf8("instockEdit")); @@ -1521,7 +1566,7 @@ maxEdit->setReadOnly(true); maxEdit->setButtonSymbols(QAbstractSpinBox::NoButtons); maxEdit->setDecimals(1); - maxEdit->setValue(recipe->fermentables.at(editrow).f_max_in_batch); + maxEdit->setValue(recipe->fermentables.at(recipe->fermentables_row).f_max_in_batch); connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject())); connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept())); diff -r 6f415bf6340a -r 84eda8ff3b60 src/EditRecipe.h --- a/src/EditRecipe.h Thu Apr 07 20:20:42 2022 +0200 +++ b/src/EditRecipe.h Thu Apr 07 21:27:53 2022 +0200 @@ -227,12 +227,12 @@ * that belong with the loaded recipe data and are present to * make things easier. */ - int fermentables_current; ///< Current record, -1 is invalid. + int fermentables_row; ///< Current row, -1 is invalid. bool fermentables_use100; ///< Use percentages instead of amount - int hops_current; - int misc_current; - int yeasts_current; - int mashs_current; + int hops_row; + int misc_row; + int yeasts_row; + int mashs_row; }; @@ -285,7 +285,7 @@ QString bar_red = "QProgressBar::chunk {background: #FF0000;}"; QString bar_orange = "QProgressBar::chunk {background: #EB7331;}"; QString bar_green = "QProgressBar::chunk {background: #008C00;}"; - int recno, editrow; + int recno; bool textIsChanged = false; bool ignoreChanges = false; Recipe *recipe;