src/EditRecipe.cpp

changeset 125
2e79e0975e58
parent 124
ba26b19572ab
child 126
3c013ef88a00
--- a/src/EditRecipe.cpp	Sat Apr 09 13:34:45 2022 +0200
+++ b/src/EditRecipe.cpp	Sat Apr 09 21:50:19 2022 +0200
@@ -488,6 +488,8 @@
     connect(ui->addFermentable, SIGNAL(clicked()), this, SLOT(on_addFermentRow_clicked()));
 
     // All signals from tab "Hops"
+    connect(ui->hop_tasteShow, &QProgressBar::valueChanged, this, &EditRecipe::on_Flavour_valueChanged);
+    connect(ui->hop_aromaShow, &QProgressBar::valueChanged, this, &EditRecipe::on_Aroma_valueChanged);
 //    connect(ui->hopsTable, SIGNAL(cellChanged(int, int)), this, SLOT(cell_Changed(int, int)));
 
     // All signals from tab "Miscs"
@@ -643,8 +645,186 @@
 }
 
 
+bool EditRecipe::hop_sort_test(const Hops &D1, const Hops &D2)
+{
+    return (D1.h_useat <= D2.h_useat ) && (D1.h_time >= D2.h_time) && (D1.h_amount >= D2.h_amount);
+}
+
+
 void EditRecipe::refreshHops()
 {
+    QString w;
+    QWidget* pWidget;
+    QHBoxLayout* pLayout;
+    QTableWidgetItem *item;
+
+    qDebug() << "refreshHops" << recipe->hops.size();
+    // std::sort(recipe->hops.begin(), recipe->hops.end(), hop_sort_test);
+
+    /*
+     * During filling the table turn off the cellChanged signal because every cell that is filled
+     * triggers the cellChanged signal. The QTableWidget has no better signal to use.
+     */
+    this->ignoreChanges = true;
+
+    const QStringList labels({tr("Origin"), tr("Hop"), tr("Type"), tr("Form"), tr("Alpha"), tr("Use at"), tr("Time"),
+                              tr("IBU"), tr("Amount"), tr("Delete"), tr("Edit") });
+
+    ui->hopsTable->setColumnCount(11);
+    ui->hopsTable->setColumnWidth(0, 150);     /* Origin	*/
+    ui->hopsTable->setColumnWidth(1, 225);     /* Hop		*/
+    ui->hopsTable->setColumnWidth(2,  75);     /* Type		*/
+    ui->hopsTable->setColumnWidth(3,  75);     /* Form          */
+    ui->hopsTable->setColumnWidth(4,  75);     /* Alpha%	*/
+    ui->hopsTable->setColumnWidth(5,  75);     /* Added         */
+    ui->hopsTable->setColumnWidth(6,  75);     /* Time		*/
+    ui->hopsTable->setColumnWidth(7,  60);     /* IBU		*/
+    ui->hopsTable->setColumnWidth(8,  75);     /* Amount	*/
+    ui->hopsTable->setColumnWidth(9,  80);     /* Delete        */
+    ui->hopsTable->setColumnWidth(10, 80);     /* Edit          */
+    ui->hopsTable->setHorizontalHeaderLabels(labels);
+    ui->hopsTable->verticalHeader()->hide();
+    ui->hopsTable->setRowCount(recipe->hops.size());
+
+    for (int i = 0; i < recipe->hops.size(); i++) {
+
+	ui->hopsTable->setItem(i, 0, new QTableWidgetItem(recipe->hops.at(i).h_origin));
+	ui->hopsTable->setItem(i, 1, new QTableWidgetItem(recipe->hops.at(i).h_name));
+
+	item = new QTableWidgetItem(h_types[recipe->hops.at(i).h_type]);
+        item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+        ui->hopsTable->setItem(i, 2, item);
+
+	item = new QTableWidgetItem(h_forms[recipe->hops.at(i).h_form]);
+        item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+        ui->hopsTable->setItem(i, 3, item);
+
+	item = new QTableWidgetItem(QString("%1%").arg(recipe->hops.at(i).h_alpha, 2, 'f', 1, '0'));
+        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
+        ui->hopsTable->setItem(i, 4, item);
+
+	item = new QTableWidgetItem(h_useat[recipe->hops.at(i).h_useat]);
+        item->setTextAlignment(Qt::AlignCenter|Qt::AlignVCenter);
+        ui->hopsTable->setItem(i, 5, item);
+
+	if (recipe->hops.at(i).h_useat == 2 || recipe->hops.at(i).h_useat == 4) {	// Boil or whirlpool
+	    item = new QTableWidgetItem(QString("%1 min.").arg(recipe->hops.at(i).h_time, 1, 'f', 0, '0'));
+	} else if (recipe->hops.at(i).h_useat == 5) {					// Dry-hop
+	    item = new QTableWidgetItem(QString("%1 days.").arg(recipe->hops.at(i).h_time / 1440, 1, 'f', 0, '0'));
+	} else {
+	    item = new QTableWidgetItem(QString(""));
+	}
+	item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
+        ui->hopsTable->setItem(i, 6, item);
+
+	double ibu = Utils::toIBU(recipe->hops.at(i).h_useat, recipe->hops.at(i).h_form, recipe->preboil_sg, recipe->batch_size, recipe->hops.at(i).h_amount,
+	                   recipe->hops.at(i).h_time, recipe->hops.at(i).h_alpha, recipe->ibu_method, 0, recipe->hops.at(i).h_time, 0);
+	item = new QTableWidgetItem(QString("%1").arg(ibu, 2, 'f', 1, '0'));
+        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
+        ui->hopsTable->setItem(i, 7, item);
+
+	if (recipe->hops.at(i).h_amount < 1.0) {
+	    item = new QTableWidgetItem(QString("%1 gr").arg(recipe->hops.at(i).h_amount * 1000.0, 2, 'f', 1, '0'));
+	} else {
+	    item = new QTableWidgetItem(QString("%1 kg").arg(recipe->hops.at(i).h_amount, 4, 'f', 3, '0'));
+	}
+        item->setTextAlignment(Qt::AlignRight|Qt::AlignVCenter);
+        ui->hopsTable->setItem(i, 8, item);
+
+	/* Add the Delete row button */
+        pWidget = new QWidget();
+        QPushButton* btn_dele = new QPushButton();
+        btn_dele->setObjectName(QString("%1").arg(i));  /* Send row with the button */
+        btn_dele->setText(tr("Delete"));
+        connect(btn_dele, SIGNAL(clicked()), this, SLOT(on_deleteHopRow_clicked()));
+        pLayout = new QHBoxLayout(pWidget);
+        pLayout->addWidget(btn_dele);
+        pLayout->setContentsMargins(5, 0, 5, 0);
+        pWidget->setLayout(pLayout);
+        ui->hopsTable->setCellWidget(i, 9, pWidget);
+
+        pWidget = new QWidget();
+        QPushButton* btn_edit = new QPushButton();
+        btn_edit->setObjectName(QString("%1").arg(i));  /* Send row with the button */
+        btn_edit->setText(tr("Edit"));
+        connect(btn_edit, SIGNAL(clicked()), this, SLOT(on_editHopRow_clicked()));
+        pLayout = new QHBoxLayout(pWidget);
+        pLayout->addWidget(btn_edit);
+        pLayout->setContentsMargins(5, 0, 5, 0);
+        pWidget->setLayout(pLayout);
+        ui->hopsTable->setCellWidget(i, 10, pWidget);
+    }
+    this->ignoreChanges = false;
+}
+
+
+void EditRecipe::on_Flavour_valueChanged(int value)
+{
+    if (value < 20) {
+        ui->hop_tasteShow->setStyleSheet(bar_20);
+	ui->hop_tasteShow->setFormat(tr("Very low"));
+    } else if (value < 40) {
+	ui->hop_tasteShow->setStyleSheet(bar_40);
+	ui->hop_tasteShow->setFormat(tr("Low"));
+    } else if (value < 60) {
+        ui->hop_tasteShow->setStyleSheet(bar_60);
+        ui->hop_tasteShow->setFormat(tr("Moderate"));
+    } else if (value < 80) {
+        ui->hop_tasteShow->setStyleSheet(bar_80);
+        ui->hop_tasteShow->setFormat(tr("High"));
+    } else {
+        ui->hop_tasteShow->setStyleSheet(bar_100);
+	ui->hop_tasteShow->setFormat(tr("Very high"));
+    }
+}
+
+
+void EditRecipe::on_Aroma_valueChanged(int value)
+{
+    if (value < 20) {
+        ui->hop_aromaShow->setStyleSheet(bar_20);
+        ui->hop_aromaShow->setFormat(tr("Very low"));
+    } else if (value < 40) {
+        ui->hop_aromaShow->setStyleSheet(bar_40);
+        ui->hop_aromaShow->setFormat(tr("Low"));
+    } else if (value < 60) {
+        ui->hop_aromaShow->setStyleSheet(bar_60);
+        ui->hop_aromaShow->setFormat(tr("Moderate"));
+    } else if (value < 80) {
+        ui->hop_aromaShow->setStyleSheet(bar_80);
+        ui->hop_aromaShow->setFormat(tr("High"));
+    } else {
+        ui->hop_aromaShow->setStyleSheet(bar_100);
+        ui->hop_aromaShow->setFormat(tr("Very high"));
+    }
+}
+
+
+void EditRecipe::calcIBUs()
+{
+    double hop_flavour = 0, hop_aroma = 0, ibus = 0;
+
+    for (int i = 0; i < recipe->hops.size(); i++) {
+
+	ibus += Utils::toIBU(recipe->hops.at(i).h_useat, recipe->hops.at(i).h_form, recipe->preboil_sg, recipe->batch_size, recipe->hops.at(i).h_amount,
+                           recipe->hops.at(i).h_time, recipe->hops.at(i).h_alpha, recipe->ibu_method, 0, recipe->hops.at(i).h_time, 0);
+	hop_flavour += Utils::hopFlavourContribution(recipe->hops.at(i).h_time, recipe->batch_size, recipe->hops.at(i).h_useat, recipe->hops.at(i).h_amount);
+        hop_aroma += Utils::hopAromaContribution(recipe->hops.at(i).h_time, recipe->batch_size, recipe->hops.at(i).h_useat, recipe->hops.at(i).h_amount);
+    }
+
+    hop_flavour = round(hop_flavour * 1000.0 / 5.0) / 10;
+    hop_aroma = round(hop_aroma * 1000.0 / 6.0) / 10;
+    if (hop_flavour > 100)
+        hop_flavour = 100;
+    if (hop_aroma > 100)
+        hop_aroma = 100;
+    qDebug() << "ibu" << recipe->est_ibu << ibus << "flavour" << hop_flavour << "aroma" << hop_aroma;
+
+    recipe->est_ibu = ibus;
+    ui->est_ibuEdit->setValue(recipe->est_ibu);
+    ui->est_ibu2Edit->setValue(recipe->est_ibu);
+    ui->hop_tasteShow->setValue(hop_flavour);
+    ui->hop_aromaShow->setValue(hop_aroma);
 }
 
 
@@ -666,8 +846,12 @@
 void EditRecipe::refreshAll()
 {
     refreshFermentables();
-
-    calcFermentables();
+    calcFermentables();		/* Must be before Hops */
+    refreshHops();
+    calcIBUs();
+    refreshMiscs();
+    refreshYeasts();
+    refreshMashs();
 }
 
 
@@ -789,8 +973,8 @@
     ui->est_og3Edit->setValue(og);
     ui->est_ogShow->setValue(og);
 
-    double preboil_sg = Utils::estimate_sg(sugarsm, ui->boil_sizeEdit->value());
-    qDebug() << "  preboil SG" << preboil_sg;
+    recipe->preboil_sg = Utils::estimate_sg(sugarsm, ui->boil_sizeEdit->value());
+    qDebug() << "  preboil SG" << recipe->preboil_sg;
 
     /*
      * Color of the wort

mercurial