--- a/src/EditRecipeTab4.cpp Fri Apr 15 20:20:22 2022 +0200 +++ b/src/EditRecipeTab4.cpp Sat Apr 16 13:05:47 2022 +0200 @@ -171,14 +171,84 @@ } +/* + * Manipulate the memory array and update the miscs table. + */ +void EditRecipe::brewing_salt_sub(QString salt, double val) +{ + QTableWidgetItem *item; + + if (val == 0) { + /* + * Remove this salt if it is in the table. + */ + for (int i = 0; i < recipe->miscs.size(); i++) { + if (salt.contains(recipe->miscs.at(i).m_name)) { + qDebug() << " brewing_salt_sub delete" << salt; + recipe->miscs.removeAt(i); + refreshMiscs(); + return; + } + } + qDebug() << " brewing_salt_sub delete error"; + return; + } + + /* + * First see if this salt is in the table. + * If it is, update the amount. + */ + for (int i = 0; i < recipe->miscs.size(); i++) { + if (salt.contains(recipe->miscs.at(i).m_name)) { + recipe->miscs[i].m_amount = val / 1000.0; + if (recipe->miscs.at(i).m_amount_is_weight) + item = new QTableWidgetItem(QString("%1 gr").arg(val, 3, 'f', 2, '0')); + else + item = new QTableWidgetItem(QString("%1 ml").arg(val, 3, 'f', 2, '0')); + item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter); + ui->miscsTable->setItem(i, 4, item); + return; + } + } + + /* + * We need a new entry. Search in the database is tricky because + * we are here with possible more names for the same salt. The + * name can be like 'Lactic Melkzuur'. So we select only the + * brewing salts and manually check their names. + */ + QSqlQuery query("SELECT * FROM inventory_miscs WHERE type = 4"); + while (query.next()) { + if (salt.contains(query.value(1).toString())) { + qDebug() << " found it, append"; + Miscs m; + m.m_name = query.value(1).toString(); + m.m_amount = val / 1000.0; + m.m_type = query.value(2).toInt(); + m.m_use_use = query.value(3).toInt(); + m.m_time = query.value(4).toDouble(); + m.m_amount_is_weight = query.value(5).toInt() ? true:false; + m.m_cost = query.value(10).toDouble(); + recipe->miscs.append(m); + refreshMiscs(); + return; + } + } + + qDebug() << "brewing_salt_sub, nothing done." << salt << val; +} + + +/* + * Edit brewing salt and recalculate. + */ void EditRecipe::set_brewing_salt(QString salt, double val) { if (this->ignoreChanges) return; qDebug() << "set_brewing_salt" << salt << val; - - + brewing_salt_sub(salt, val); calcWater(); is_changed(); }