src/EditRecipe.cpp

changeset 118
c57771e40ca8
parent 117
84eda8ff3b60
child 119
68853aee2ef9
--- a/src/EditRecipe.cpp	Thu Apr 07 21:27:53 2022 +0200
+++ b/src/EditRecipe.cpp	Fri Apr 08 17:34:27 2022 +0200
@@ -108,7 +108,7 @@
 	recipe->sparge_acid_perc = query.value(41).toDouble();
 	recipe->sparge_acid_amount = query.value(42).toDouble();
 	recipe->mash_ph = query.value(43).toDouble();
-	recipe->mash_name = query.value(44).toDouble();
+	recipe->mash_name = query.value(44).toString();
 	recipe->calc_acid = query.value(45).toInt() ? true:false;
 
 	recipe->w1_name = query.value(46).toString();
@@ -749,6 +749,7 @@
 
     double og = Utils::estimate_sg(sugarsf + addedS, ui->batch_sizeEdit->value());
     qDebug() << "  OG" << ui->est_ogEdit->value() << og;
+    recipe->est_og = og;
     ui->est_ogEdit->setValue(og);
     ui->est_og2Edit->setValue(og);
     ui->est_og3Edit->setValue(og);
@@ -770,7 +771,8 @@
 	double cw = colort / ui->batch_sizeEdit->value() * 8.34436;
 	color = Utils::kw_to_ebc(ui->color_methodEdit->currentIndex(), cw);
     }
-    qDebug() << "  color" << ui->est_colorEdit->value() << color;
+    qDebug() << "  color" << ui->est_colorEdit->value() << color << recipe->est_color;
+    recipe->est_color = color;
     ui->est_colorEdit->setValue(color);
     ui->est_colorEdit->setStyleSheet(Utils::ebc_to_style(color));
     ui->est_color2Edit->setValue(color);
@@ -810,6 +812,7 @@
     else
 	fg = Utils::estimate_fg(psugar, pcara, 0, 0, 0, svg, og);
     qDebug() << "  FG" << ui->est_fgEdit->value() << fg;
+    recipe->est_fg = fg;
     ui->est_fgEdit->setValue(fg);
     ui->est_fg3Edit->setValue(fg);
     ui->est_fgShow->setValue(fg);
@@ -819,6 +822,7 @@
     ui->est_abvEdit->setValue(abv);
     ui->est_abv2Edit->setValue(abv);
     ui->est_abvShow->setValue(abv);
+    recipe->est_abv = abv;
 
     /*
      * Calculate kilocalories/liter. Formula from brouwhulp.
@@ -912,7 +916,9 @@
 		"st_ibu_min=:st_ibu_min, st_ibu_max=:st_ibu_max, st_color_min=:st_color_min, st_color_max=:st_color_max, "
 		"st_carb_min=:st_carb_min, st_carb_max=:st_carb_max, st_abv_min=:st_abv_min, st_abv_max=:st_abv_max, "
 		"name=:name, notes=:notes, type=:type, batch_size=:batch_size, boil_size=:boil_size, "
-		"boil_time=:boil_time, efficiency=:efficiency, sparge_temp=:sparge_temp, sparge_ph=:sparge_ph, "
+		"boil_time=:boil_time, efficiency=:efficiency, est_og=:est_og, est_fg=:est_fg, est_abv=:est_abv, "
+		"est_color=:est_color, color_method=:color_method, est_ibu=:est_ibu, ibu_method=:ibu_method, "
+		"est_carb=:est_carb, sparge_temp=:sparge_temp, sparge_ph=:sparge_ph, "
 		"sparge_volume=:sparge_volume, sparge_source=:sparge_source, sparge_acid_type=:sparge_acid_type, "
 		"sparge_acid_perc=:sparge_acid_perc, sparge_acid_amount=:sparge_acid_amount, mash_ph=:mash_ph, "
 		"mash_name=:mash_name, calc_acid=:calc_acid, "
@@ -937,7 +943,9 @@
                 "st_ibu_min=:st_ibu_min, st_ibu_max=:st_ibu_max, st_color_min=:st_color_min, st_color_max=:st_color_max, "
                 "st_carb_min=:st_carb_min, st_carb_max=:st_carb_max, st_abv_min=:st_abv_min, st_abv_max=:st_abv_max, "
                 "name=:name, notes=:notes, type=:type, batch_size=:batch_size, boil_size=:boil_size, "
-		"boil_time=:boil_time, efficiency=:efficiency, sparge_temp=:sparge_temp, sparge_ph=:sparge_ph, "
+		"boil_time=:boil_time, efficiency=:efficiency, est_og=:est_og, est_fg=:est_fg, est_abv=:est_abv, "
+                "est_color=:est_color, color_method=:color_method, est_ibu=:est_ibu, ibu_method=:ibu_method, "
+                "est_carb=:est_carb, sparge_temp=:sparge_temp, sparge_ph=:sparge_ph, "
 		"sparge_volume=:sparge_volume, sparge_source=:sparge_source, sparge_acid_type=:sparge_acid_type, "
                 "sparge_acid_perc=:sparge_acid_perc, sparge_acid_amount=:sparge_acid_amount, mash_ph=:mash_ph, "
                 "mash_name=:mash_name, calc_acid=:calc_acid, "
@@ -1072,7 +1080,7 @@
 	    }
 	    QJsonDocument doc;
 	    doc.setArray(array);
-	    qDebug() << doc.toJson(QJsonDocument::Compact);
+//	    qDebug() << doc.toJson(QJsonDocument::Compact);
 	    query.bindValue(":json_fermentables", doc.toJson(QJsonDocument::Compact));
 	}
 
@@ -1432,9 +1440,127 @@
     is_changed();
 }
 
+void EditRecipe::ferment_to100_changed(bool val)
+{
+    qDebug() << "ferment_to100_changed()" << recipe->fermentables_row << val;
+}
+
+void EditRecipe::ferment_select_changed(int val)
+{
+    QSqlQuery query;
+    bool instock = instockEdit->isChecked();
+    QString w;
+    QTableWidgetItem *item;
+
+    if (val < 1)
+	return;
+
+    qDebug() << "ferment_select_changed()" << recipe->fermentables_row << val << instock;
+
+    /*
+     * Search the fermentable pointed by the index and instock flag.
+     */
+    QString sql = "SELECT name,origin,supplier,cost,type,yield,color,coarse_fine_diff,moisture,diastatic_power,protein,dissolved_protein,max_in_batch,"
+	  	  "graintype,recommend_mash,add_after_boil,di_ph,acid_to_ph_57 FROM inventory_fermentables ";
+    if (instock)
+	sql.append("WHERE inventory > 0 ");
+    sql.append("ORDER BY supplier,name");
+    query.prepare(sql);
+    query.exec();
+    query.first();
+    for (int i = 0; i < (val - 1); i++) {
+	query.next();
+    }
+    qDebug() << "found" << query.value(2).toString() << query.value(0).toString();
+
+    /*
+     * Replace the fermentable record contents
+     */
+    this->ignoreChanges = true;
+    recipe->fermentables[recipe->fermentables_row].f_name = query.value(0).toString();
+    recipe->fermentables[recipe->fermentables_row].f_origin = query.value(1).toString();
+    recipe->fermentables[recipe->fermentables_row].f_supplier = query.value(2).toString();
+    recipe->fermentables[recipe->fermentables_row].f_cost = query.value(3).toDouble();
+    recipe->fermentables[recipe->fermentables_row].f_type = query.value(4).toInt();
+    recipe->fermentables[recipe->fermentables_row].f_yield = query.value(5).toDouble();
+    recipe->fermentables[recipe->fermentables_row].f_color = query.value(6).toDouble();
+    recipe->fermentables[recipe->fermentables_row].f_coarse_fine_diff = query.value(7).toDouble();
+    recipe->fermentables[recipe->fermentables_row].f_moisture = query.value(8).toDouble();
+    recipe->fermentables[recipe->fermentables_row].f_diastatic_power = query.value(9).toDouble();
+    recipe->fermentables[recipe->fermentables_row].f_protein = query.value(10).toDouble();
+    recipe->fermentables[recipe->fermentables_row].f_dissolved_protein = query.value(11).toDouble();
+    recipe->fermentables[recipe->fermentables_row].f_max_in_batch = query.value(12).toDouble();
+    recipe->fermentables[recipe->fermentables_row].f_graintype = query.value(13).toInt();
+    recipe->fermentables[recipe->fermentables_row].f_recommend_mash = query.value(14).toInt() ? true:false;
+    recipe->fermentables[recipe->fermentables_row].f_add_after_boil = query.value(15).toInt() ? true:false;
+    recipe->fermentables[recipe->fermentables_row].f_di_ph = query.value(16).toDouble();
+    recipe->fermentables[recipe->fermentables_row].f_acid_to_ph_57 = query.value(17).toDouble();
+
+    /*
+     * Update the visible fields
+     */
+    nameEdit->setText(recipe->fermentables.at(recipe->fermentables_row).f_name);
+    supplierEdit->setText(recipe->fermentables.at(recipe->fermentables_row).f_supplier);
+    maxEdit->setValue(recipe->fermentables.at(recipe->fermentables_row).f_max_in_batch);
+
+    ui->fermentablesTable->setItem(recipe->fermentables_row, 0, new QTableWidgetItem(recipe->fermentables.at(recipe->fermentables_row).f_supplier));
+    ui->fermentablesTable->setItem(recipe->fermentables_row, 1, new QTableWidgetItem(recipe->fermentables.at(recipe->fermentables_row).f_name));
+
+    w = QString("%1").arg(recipe->fermentables.at(recipe->fermentables_row).f_color, 1, 'f', 0, '0');
+    item = new QTableWidgetItem(w);
+    item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
+    ui->fermentablesTable->setItem(recipe->fermentables_row, 2, item);
+
+    item = new QTableWidgetItem(f_types[recipe->fermentables.at(recipe->fermentables_row).f_type]);
+    item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+    ui->fermentablesTable->setItem(recipe->fermentables_row, 3, item);
+
+    item = new QTableWidgetItem(f_graintypes[recipe->fermentables.at(recipe->fermentables_row).f_graintype]);
+    item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+    ui->fermentablesTable->setItem(recipe->fermentables_row, 4, item);
+
+    item = new QTableWidgetItem(QString("%1%").arg(recipe->fermentables.at(recipe->fermentables_row).f_yield, 2, 'f', 1, '0'));
+    item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
+    ui->fermentablesTable->setItem(recipe->fermentables_row, 6, item);
+
+    this->ignoreChanges = false;
+    calcFermentables();
+    is_changed();
+}
+
+void EditRecipe::ferment_instock_changed(bool val)
+{
+    QSqlQuery query;
+
+    qDebug() << "ferment_instock_changed()" << recipe->fermentables_row << val;
+
+    this->selectEdit->setCurrentIndex(-1);
+    this->selectEdit->clear();
+    QString sql = "SELECT supplier,name,color,inventory FROM inventory_fermentables ";
+    if (val)
+	sql.append("WHERE inventory > 0 ");
+    sql.append("ORDER BY supplier,name");
+    query.prepare(sql);
+    query.exec();
+    query.first();
+    this->selectEdit->addItem("");	// Start with empty value
+    for (int i = 0; i < query.size(); i++) {
+        this->selectEdit->addItem(query.value(0).toString()+" - "+query.value(1).toString()+" ("+query.value(2).toString()+" EBC) "+
+			QString("%1 kg").arg(query.value(3).toDouble(), 4, 'f', 3, '0'));
+        query.next();
+    }
+}
+
+void EditRecipe::ferment_added_changed(int val)
+{
+    qDebug() << "ferment_added_changed()" << recipe->fermentables_row << val;
+}
+
 
 void EditRecipe::on_editFermentRow_clicked()
 {
+    QSqlQuery query;
+
     QPushButton *pb = qobject_cast<QPushButton *>(QObject::sender());
     recipe->fermentables_row = pb->objectName().toInt();
     qDebug() << "Edit fermentable row" << recipe->fermentables_row;
@@ -1487,7 +1613,7 @@
     QLabel *instockLabel = new QLabel(dialog);
     instockLabel->setObjectName(QString::fromUtf8("instockLabel"));
     instockLabel->setText(tr("In stock:"));
-    instockLabel->setGeometry(QRect(420, 70, 121, 20));
+    instockLabel->setGeometry(QRect(525, 70, 121, 20));
     instockLabel->setAlignment(Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter);
     QLabel *maxLabel = new QLabel(dialog);
     maxLabel->setObjectName(QString::fromUtf8("maxLabel"));
@@ -1497,7 +1623,8 @@
 
     selectEdit = new QComboBox(dialog);
     selectEdit->setObjectName(QString::fromUtf8("selectEdit"));
-    selectEdit->setGeometry(QRect(160, 70, 251, 23));
+    selectEdit->setGeometry(QRect(160, 70, 371, 23));
+
     nameEdit = new QLineEdit(dialog);
     nameEdit->setObjectName(QString::fromUtf8("nameEdit"));
     nameEdit->setText(recipe->fermentables.at(recipe->fermentables_row).f_name);
@@ -1518,7 +1645,6 @@
     amountEdit->setMaximum(100000.0);
     amountEdit->setSingleStep(0.0010);
     amountEdit->setValue(recipe->fermentables.at(recipe->fermentables_row).f_amount);
-    connect(amountEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::ferment_amount_changed);
 
     pctEdit = new QDoubleSpinBox(dialog);
     pctEdit->setObjectName(QString::fromUtf8("pctEdit"));
@@ -1537,7 +1663,6 @@
     pctEdit->setMaximum(100.0);
     pctEdit->setSingleStep(0.1);
     pctEdit->setValue(recipe->fermentables.at(recipe->fermentables_row).f_percentage);
-    connect(pctEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::ferment_pct_changed);
 
     addedEdit = new QComboBox(dialog);
     addedEdit->setObjectName(QString::fromUtf8("addedEdit"));
@@ -1557,7 +1682,8 @@
 
     instockEdit = new QCheckBox(dialog);
     instockEdit->setObjectName(QString::fromUtf8("instockEdit"));
-    instockEdit->setGeometry(QRect(550, 70, 85, 21));
+    instockEdit->setGeometry(QRect(655, 70, 85, 21));
+    instockEdit->setChecked(true);
 
     maxEdit = new QDoubleSpinBox(dialog);
     maxEdit->setObjectName(QString::fromUtf8("maxEdit"));
@@ -1568,18 +1694,44 @@
     maxEdit->setDecimals(1);
     maxEdit->setValue(recipe->fermentables.at(recipe->fermentables_row).f_max_in_batch);
 
+    ferment_instock_changed(true);
+
+    connect(selectEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditRecipe::ferment_select_changed);
+    connect(amountEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::ferment_amount_changed);
+    connect(pctEdit, QOverload<double>::of(&QDoubleSpinBox::valueChanged), this, &EditRecipe::ferment_pct_changed);
+    connect(addedEdit, QOverload<int>::of(&QComboBox::currentIndexChanged), this, &EditRecipe::ferment_added_changed);
+    connect(to100Edit, &QCheckBox::stateChanged, this, &EditRecipe::ferment_to100_changed);
+    connect(instockEdit, &QCheckBox::stateChanged, this, &EditRecipe::ferment_instock_changed);
     connect(buttonBox, SIGNAL(rejected()), dialog, SLOT(reject()));
     connect(buttonBox, SIGNAL(accepted()), dialog, SLOT(accept()));
+
+    dialog->setModal(true);
     dialog->exec();
     if (dialog->result() == QDialog::Rejected) {
-	qDebug() << "rejected";
-	// restore fermentbackup
-	// recalculate percentages
-    } else {
-	qDebug() << "accepted";
+	qDebug() << "reject and rollback";
+	recipe->fermentables[recipe->fermentables_row] = backup;
+	/*
+	 * Recalculate the percentages
+	 */
+	double total = 0;
+	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;
+	for (int i = 0; i < recipe->fermentables.size(); i++) {
+	    if (recipe->fermentables.at(i).f_added < 4) {
+		recipe->fermentables[i].f_percentage = recipe->fermentables.at(i).f_amount / total * 100;
+	    }
+	}
     }
 
-    // disconnect
+    disconnect(selectEdit, nullptr, nullptr, nullptr);
+    disconnect(amountEdit, nullptr, nullptr, nullptr);
+    disconnect(pctEdit, nullptr, nullptr, nullptr);
+    disconnect(addedEdit, nullptr, nullptr, nullptr);
+    disconnect(to100Edit, nullptr, nullptr, nullptr);
+    disconnect(instockEdit, nullptr, nullptr, nullptr);
+    disconnect(buttonBox, nullptr, nullptr, nullptr);
+
     emit refreshAll();
 }
 

mercurial