201 const auto& formattedJson = QString("%1").arg(f_json); |
201 const auto& formattedJson = QString("%1").arg(f_json); |
202 QJsonDocument fermentables = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); |
202 QJsonDocument fermentables = QJsonDocument::fromJson(formattedJson.toUtf8(), &parseError); |
203 if (parseError.error != QJsonParseError::NoError) { |
203 if (parseError.error != QJsonParseError::NoError) { |
204 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; |
204 qDebug() << "Parse error: " << parseError.errorString() << "at" << parseError.offset ; |
205 } else if (fermentables.isArray()) { |
205 } else if (fermentables.isArray()) { |
|
206 double percentcheck = 0; |
206 for (int i = 0; i < fermentables.array().size(); i++) { |
207 for (int i = 0; i < fermentables.array().size(); i++) { |
207 QJsonObject obj = fermentables.array().at(i).toObject(); |
208 QJsonObject obj = fermentables.array().at(i).toObject(); |
208 //qDebug() << i << obj; |
209 //qDebug() << i << obj; |
209 Fermentables f; |
210 Fermentables f; |
210 f.f_name = obj["f_name"].toString(); |
211 f.f_name = obj["f_name"].toString(); |
229 f.f_percentage = obj["f_percentage"].toDouble(); |
230 f.f_percentage = obj["f_percentage"].toDouble(); |
230 f.f_di_ph = obj["f_di_ph"].toDouble(); |
231 f.f_di_ph = obj["f_di_ph"].toDouble(); |
231 f.f_acid_to_ph_57 = obj["f_acid_to_ph_57"].toDouble(); |
232 f.f_acid_to_ph_57 = obj["f_acid_to_ph_57"].toDouble(); |
232 if (f.f_adjust_to_total_100) |
233 if (f.f_adjust_to_total_100) |
233 recipe->fermentables_use100 = true; |
234 recipe->fermentables_use100 = true; |
|
235 percentcheck += f.f_percentage; |
234 recipe->fermentables.append(f); |
236 recipe->fermentables.append(f); |
235 } |
237 } |
236 qDebug() << "fermentables" << recipe->fermentables.size(); |
238 qDebug() << "fermentables" << recipe->fermentables.size() << percentcheck; |
|
239 if (percentcheck == 0) { |
|
240 /* Missing percentages, fix it. */ |
|
241 double total = 0; |
|
242 for (int i = 0; i < recipe->fermentables.size(); i++) { |
|
243 if (recipe->fermentables.at(i).f_added < 4) |
|
244 total += recipe->fermentables.at(i).f_amount; |
|
245 } |
|
246 for (int i = 0; i < recipe->fermentables.size(); i++) { |
|
247 if (recipe->fermentables.at(i).f_added < 4) |
|
248 recipe->fermentables[i].f_percentage = round((recipe->fermentables.at(i).f_amount / total) * 10000.0) / 100.0; |
|
249 } |
|
250 qDebug() << " fixed missing percentages"; |
|
251 is_changed(); |
|
252 } |
237 } |
253 } |
238 } else { |
254 } else { |
239 qDebug() << "empty fermentables"; |
255 qDebug() << "empty fermentables"; |
240 } |
256 } |
241 |
257 |